Graph Black Boxes (Inputs/Outputs/Runtime) Flashcards
Max flow algorithm inputs and outputs
Inputs
(this set of inputs is known as a flow network)
- G(V,E): a directed graph
- s, t: two vertices in V, s is a source and t is a sink
- c: capacities of each edge. c(u,v)>0 for all (u,v) in E.
- *Outputs**
- f: a function for flow such that f(u,v) gives how much is sent through edge (u,v)
- Sum f(*,t) is maximized. That is the flow from any vertex to t.
- C = Sum f(,t) (sometimes called f) is the size of the max flow.
Max flow algorithm assumptions
- cycles are okay.
- anti-parallel edges are NOT okay. To remove an anti-parallel edge (edge that is parallel but going in opposite direction), you can add an extra vertex to the anti-parallel edge.
- the flow for each edge is non-negative and does not exceed capacity.
- For each vertex that is not s or t, the total flow into that vertex equals the total flow out.
- The total flow into s is equal to the total flow into t. Both are equal to C.
Build a Residual Network inputs and outputs
- *Inputs:**
- G(V,E): a directed graph
- c: capacities of each edge. c(u,v)>0 for all (u,v) in E.
- f: a function for flow such that f(u,v) gives how much is sent through edge (u,v)
- *Outputs:**
- G^f = (V,E^f): the residual graph, with edges that point in the opposite direction as the edges in the original graph, between any two vertices
- c^f: the capacities show how much flow can be taken away from that edge in the original graph (i.e. flow in the other direction)
Build a Residual Network runtime
O(n + m) but we assumes G is a connected graph and so m >= n-1 and so O(m)
Ford-Fulkerson assumptions / implementation
- Uses the general max flow algorithm. See that algorithm for inputs and outputs.
- Assumes all capacities are positive integers
Ford-Fulkerson runtime
- Rounds O(C): Flow increases by at least 1 per round, and there are C rounds (because C is the size of the max flow).
- Per round: O(m)
- Total: O(mC)
Edmonds-Karp difference from Ford-Fulkerson
- Uses general max flow algorithm, like ford-fulkerson. However there is a modification - in step 3, instead of finding any path, we look for the shortest path in the residual graph G^f using BFS.
- Ford-Fulkerson runtime is O(mC) where C is the max-flow (i.e. Sum f(s,) == Sum f(,t)).
- Edmonds-Karp runtime is O(nm^2)
Edmonds-Karp runtime
O(nm^2)
Breadth First Search (BFS) Inputs, Outputs, and Runtime
- *Inputs:**
- Graph: G=(V,E), directed or undirected, starting vertex s in V.
- *Outputs:**
- dist[u]: distance from s to u if s can reach u, infinity otherwise.
- prev[z]: the parent index of vertex z.
Runtime:
O(n+m)
When should you use BFS?
Use for unweighted single source shortest path problems.
The BFS function outputs a distance-value for each edge. What does this represent?
d[u] gives the distance from u to the start vertex. This is the number of edges. It is NOT the sum of weights (in the case of a weighted graph). BFS does NOT consider weights.
Dijkstra’s Algorithm Inputs, Outputs, and Runtime
- *Input:**
- Graph: G=(V,E), directed or undirected
- s: starting vertex s in V.
- w: positive edge weights.
- *Outputs:**
- dist[u]: distance from s to u if s can be reached from u. Infinity otherwise.
- prev[z]: the parent index of vertex z.
- *Runtime:**
- O( (n+m) logn)
- *remember the logn - this takes longer than BFS and DFS because weights are involved.
Depth First Search (DFS) Inputs, Outputs, and Runtime
- *Inputs:**
- Graph: G=(V,E), directed or undirected
- *Outputs:**
- prev[z]: the parent index of vertex z in the DFS visitation
- pre[z]: the pre-order number in the DFS visitation
- post[z]: the post-order number in the DFS visitation
- ccnum[z]: the connected components number of the vertex z. Another card covers when this is the SCC number.
Runtime:
O(n+m)
When is the ccnum[z] the strongly connected component number for z?
**Only relevant for directed graphs.
1) you have to run DFS on a reverse of the directed graph.
2) Then, run DFS, starting with the highest post order number from 1.
In this case, the ccnum is also the topological sort order in reverse.
Strongly Connected Components (SCC) algorithm inputs, outputs
- *Inputs:**
- Graph: G=(V,E), directed
- *Outputs:**
- G^SCC = (V^SCC, E^SCC): where G^SCC is a meta graph (a DAG) with each SCC in G forming a vertex in V^SCC and each edge between SCCs in E^SCC
- ccnum[*]: comes from DFS, which is used the algorithm. Gives the SCC each vertex is a part of.
- V^SCC looks like 1, 2, 3, … which are ccnums for each SCC
- E^SCC: looks like (1,2), (2,3), … which are the edges between V^SCC
Strongly Connected Components (SCC) runtime
O(n+m)
How do you find the SCC of a vertex in a directed graph after running the SCC algorithm on it?
Use the ccnum value for a given vertex that the SCC algorithm returns. I.e. use ccnum[v] for v in the original graph’s vertices to find the SCC of v.