Ch 9: Graphs I Flashcards
what is a graph?
a data structure for representing connections among items, and consists of vertices connected by edges
what is a vertex(node)?
represents an item in a graph
what is an edge?
represents a connection between two vertices in a graph
what does it mean for two vertices to be adjacent?
they are connected by a single edge
what is a path?
a sequence of edges leading from a source(starting) vertex to a destination(ending) vertex
what is path length?
the number of edges in the path
what is distance?
the number of edges on the path between two vertices
what does the shortest path algorithm do?
minimizes the sum of edges to the destination, not shortest time
what is an adjacency list?
- graph representation
- each vertex has a list of adjacent vertices, each list item represents an edge
what is the advantage of an adjacency list?
has a size of O(V+E) (v- vertices, e- edges) because each vertex appears once and each edge appears twice
what is the disadvantage of an adjacency list?
determining whether the two vertices are adjacent is O(V) because one vertex’s adjacent list must be traversed looking for the other vertex
what is a sparse graph?
when each vertex has far fewer edges than the maximum possible
in most applications, when a vertex is only adjacent to a fraction of the other vertices, what does it result in?
a sparse graph
what is an adjacency matrix?
- graph representation
- each vertex is assigned to a matrix row and column, and a matrix element is 1 if the corresponding two vertices have an edge or is 0 otherwise
in an adjacency matrix that is implemented as a 2-d array, what is the run time of accessing elements?
O(1)
in an adjacency matrix that is implemented as a 2-d array, what is the run time of determining whether two vertices are adjacent or not?
O(1)
what is the key drawback of an adjacency matrix?
size is O(V^2)
what is graph traversal?
an algorithm that commonly visits every vertex in a graph in some order
what is breadth-first search (BFS)?
a traversal that visits the starting index then all vertices of distance 1 from the vertex, then distance 2, then so on without revisiting a vertex
Is BFS unique? Why?
no because there can be multiple pathways
explain the BFS traversal algorithm
- enqueues starting vertex into a queue
- while the queue is not empty, dequeue the vertex
- check that dequeued vertex
- enqueue its adjacent vertices that have yet to be discovered
repeat
what does it mean for a vertex to be discovered?
algorithm visited the vector
what is a frontier?
vertices in the queue, being vertices thus far discovered but not yet visited
what is depth-first search (DFS)?
a traversal that visits a starting vertex then visits every vertex along each path starting from that vertex to the path’s end before backtracking
explain the DFS traversal algorithm
- push starting vertex to stack
- while the stack is not empty, pop the vertex from the top
- if vertex has not been visited visit vertex and push adjacent vertices
repeat