Undirected Graphs Flashcards
What is the input format used for all API’s?
- Num vertex
- Num edge
- E pairs of values between 0 and v -1
What are the two basic requirements for representation?
- Space to accomodate
- Time-efficient
What are the 3 data structures we can use for graphs, and what are their caviates?
- Adjacency matrix V x V Boolean with A{vw} = true if adjacent edge. Space cost V^2
- Array of edges using Edge class. adj() implementation would involve examining all edges - time constraint
- Adjacency list - Vertex-indexed array of lists of the vertices adjacent to each vertex, using bags.
What is the worst case time to mark vertices connected to given source?
proportional to sum of degrees
How do we search an undirected graph using DFS and what do we use to explore multiple edges?
- First unmarked vertex in current vertex’s adjacency list. Call recursive and mark vertex.
- First vertex will be first in newly marked vertex’s adjacency list, so skip.
- Continue forwards until vertex has all edges marked. Once reached, return to previous until unmarked edge present.
- Once everything checked, dfs() checks vertices on list then the other remaining lists, however all have been marked.
Use stack
What is the difference between BFS and DFS?
Taking next vertex from data structure.
* Least recent for BFS
* Most recent for DFS
What is the time complexit in the worst case for BFS?
V + E
What is the product of the path finding for BFS?
An array edgeTo[], which is a parent-link representation of a tree rooted at s which defines shortest paths from s to every vertex connected to s
How does the path-finding in BFS work and what do we use to explore multiple edges?
- Put source vertex on queue
- Take next vertex v from queue and mark it
- Put onto the queue all unmarked vertices that are adjacent to v
- Continue 2, 3 until queue empty
We use a queue
What is a single source path?
Given graph and source vertex, is there a path from s to a given target vertex v? If yes, find path.
What is the purpose of the edgeTo[]
array in finding paths?
Integer values that give way to find path back to s for each vertex connected to s.
Remember edge v-w
that takes us to each vertex w
for the first time, setting edgeTo[w] = v