Exam 2 - Graphs Flashcards
What algorithm would I use to topologically sort a DAG?
DFS
What algorithm would I use to find all of the connected components in a graph?
DFS
Can DFS be used on directed graphs or undirected graphs?
It can be used on both
Can DFS be used on DAGs
Yes
What is the runtime of DFS?
O(n+m)
What are the inputs of DFS?
G = (V,E)
What are the outputs of DFS?
ccnum[] the array of connected component numbers
prev[] the previous array which can be used for backtracking to find a path
pre[] the preorder numbers for each vertex
post[] the postorder numbers for each vertex, which can be used for topological sorting
What algorithm can I use to find which nodes are reachable from S?
Explore subroutine
What are the inputs of the Explore subroutine?
G = (V,E)
Starting vertex v from V
What is the output of the Explore subroutine?
visited[]
array of vertices reached before a given vertex
true for all vertices reachable from v
What is the runtime of the Explore subroutine?
O(m+n)
Can you use BFS on a graph with edge weights?
No
What kind of graphs can BFS be used on?
Unweighted, both directed and undirected
What are the inputs of BFS?
G = (V,E)
Start vertex v from V
What does BFS do?
Performs a breadth-first search of the graph from a starting vertex, finding the number of edges from v to any other vertex
What are the outputs of BFS?
dist[] for all vertices u reachable from v, dist[u] is the length of the number of edges in the shortest path from v to u – infinite if unreachable
prev[] vertex preceding u in the shortest path from v to reachable vertex u
What is the runtime of BFS?
O(m+n)
What graph trait can Dijkstra’s algorithm not handle?
Negative edge weights
What algorithm would I use to find the shortest distance from a source vertex to all other vertices in a graph with nonnegative edge weights?
Dijkstra’s algorithm
What are the inputs to Dijkstra’s algorithm?
G = (V,E)
Start vertex v in V
w_e for e in E, non negative weights
What are the outputs of Dijkstra’s algorithm?
dist[] shortest distance between vertex v and reachable vertex u, or infinity if not reachable
prev[] vertex preceding u in the shortest path from v to reachable vertex u
What is the runtime of Dijkstra’s algorithm?
O((m+n) log n)
What algorithm would I use if I wanted to find the shortest distance from a source vertex to all other vertices in a graph that may contain negative edge weights?
Bellman-Ford
What are the inputs to the Bellman-Ford algorithm?
G = (V,E)
Start vertex s in V
w_e for e in E
What is the output of the Bellman-Ford algorithm?
Shortest path from vertex s to all other vertices