Graph Algorithms Flashcards
What kinds of graphs are there?
Directed or undirected, weighted or unweighted, connected or unconnected.
What are the graph black boxes?
- BFS
- DFS (+Explore)
- Dijkstra’s
- Topological Sort
- Kruskal
- Prim
- Strongly Connected Components
- 2SAT
What are the max flow black boxes?
- Build residual network
- Ford-Fulkerson
- Edmonds-Karp
What are the steps to solve any graph problem?
- Determine the black box
- Munge the input and output, almost never modify the black box
- State the steps of the algorithm (no pseudocode)
- Prove the correctness
- Analyze the runtime
What is BFS?
Gives single source shortest path from s to all vertices.
Input:
- G = (V, E), directed or undirected
- s start vertex, s in V
Output:
- dist[u] shortest distance from s to u if there’s a path, inf otherwise
- prev[v] previous node along path to v
Runtime: O(n+m)
Dist is the number of edges from s to u, not the edge weights.
What is DFS?
Explores all vertices. Calls Explore on all unexplored vertices.
Input:
- G = (V, E), directed or undirected
Output:
- prev[z] previous node along visitation path to z
- pre[z] pre number of vertex z along visitation path
- post[z] post number of vertex z along visitation path
- ccnum[z] connected component number of z
Runtime: O(n+m)
How do directed, undirected and connected, strongly connected relate?
- An undirected graph cannot have strongly connected components (or it’s meaningless), only connected components
- A directed graph can have both connected and strongly connected components, but usually strongly connected components are more meaningful
What is Explore?
Explores all nodes reachable from a given start vertex.
Input:
- G = (V, E), directed or undirected
- s start vertex, s in V
Output:
- visited[u] is set to true for all nodes reachable from s
- Any other data structure that DFS provides (pre, post)
Runtime: O(m) if part of DFS, O(n+m) if run by itself
If you are allowed to modify DFS, it’s likely in here.
What is topological sort?
Provides a possible ordering of vertices in a DAG such that, for each edge, the source vertex always comes before the end vertex.
Input:
- G = (V, E), DAG (doesn’t need to be a DAG when used in SCC)
Output:
- topo[i]: the vertex number of the i’th vertex in topological order from left to right, source to sink (in descending post order)
Runtime: O(n+m)
Uses DFS on the DAG and sorts by post order.
What is the SCC algorithm?
Computes the strongly connected components in a directed graph and creates a metagraph.
Input:
- G = (V, E), directed, but not necessarily a DAG
Output:
- G^SCC = (V^SCC, E^SCC) metagraph (DAG), with each SCC forming a vertex
- ccnum[.] comes from DFS underneath (use it to figure out which SCC a vertex in the original graph is in)
- V^SCC will look like 1, 2, 3, 4 which are CC nums
- E^SCC will look like (1, 2), (2, 3), etc.
Runtime: O(n+m)
Runs with two DFSs
- Reverse the edges and run DFS to find a source SCC which is the highest post num. This is a sink SCC in the original graph.
- Sort V by highest to lowest post-order number in the original graph
- Run DFS again using sorted V, now highest post-order num will be source, lowest will be sink
- Gather up vertices, create new metanodes, connect them using original graph
What is the 2SAT algorithm?
TODO
Runtime: O(n+m)
What is Kruskal’s algorithm?
TODO
An algorithm for getting an MST. Greedily searches for the next lowest weight edge which connects two unconnected vertices.
Runtime: O(m log n) or O(m log m)
What is Prim’s algorithm?
TODO
An algorithm for getting an MST. Greedily searches for the next lowest weight edge that connects an explored vertex to an unexplored vertex.
Runtime: O(m log n) or O(m log m)
What is Ford-Fulkerson?
An algorithm for solving max flow problems. A container for an explore algorithm which can be either DFS (default) or BFS (Edmonds-Karp).
What is Edmonds-Karp?
An algorithm for solving max flow problems. A specialization of Ford-Fulkerson that uses BFS.
What is 2SAT?
TODO
An algorithm for solving a series of clauses of two boolean variables using graph theory. Uses SCC under the hood. Is in P, while kSAT (k=3+) is in NP.
Procedure:
- Iteratively simplify the inputs such that there are no clauses with single variables
- Construct a graph mapping dependencies
- Run SCC algorithm
What are the seven ways of solving graph problems (and one way recommended not to)?
- Change an edge weight to make it get picked at a certain time (first, last, never)
- Reverse the graph and run algorithm to determine pathing to a vertex instead of from it
- (Directed) Get SCCs to get more info about graph
- (Directed) Get SCCs to determine pathing, connectivity, cycles, etc.
- (Directed, acyclic) Get a DAG, topo sort it, go down the ordering and look for info
- Encode as 2SAT and solve
- Run an algorithm from multiple sources/reversed (but not all)
Do NOT Explore from every vertex—that’s brute force
Unless otherwise specified, what can you assume about MST problems?
The graph is undirected, connected
What are the MST properties?
- Cut property: A cut is a set of edges that separate a graph into two components. The cut property states that, for any cut, if any edge in the cut has a weight smaller than all other edges in the cut, then it must be in the MST.
- Cycle property: The cycle property states that, for any cycle in a graph, the highest weighted edge must not be in the MST because it would never make sense to include it.
- n-1 property: An MST always has n-1 edges.
- Minimum-cost edge property: If the minimum cost edge is unique, then this edge must be in the MST.
What assumptions does Ford-Fulkerson make?
Assumes integer capacities.
What assumptions does Edmonds-Karp make?
None