Exam2 Algo-Combined Flashcards
RSA/Graph Theory
Undirected Graph
Depth First Search
Purpose, Input, Output, Runtime
Purpose: Perform a depth first search on an undirected graph G.
Input: Undirected Graph G = (V, E)
Output: 4 arrays: prev, pre, post each of length n
- Prev[z] The parent index of vertex Z in the DFS visition
- pre[z] the pre number of vertex z in the DFS visitation
- post[z] the post number of the vertex z in the DFS visitation
- (Optional) ccnum[z] the connected component number, if stored after SCC algorithm run on graph.
Runtime: O(n + m)
AKA:O(|V| + (|E|)
Directed Graph
Depth First Search
Input, Output, Runtime
Purpose: Perform a depth first search on a directed graph G
Input: Directed Graph G=(V, E)
Output: 3 arrays, prev, pre, post, each of length n
- prev[z]: The parent index of vertex Z in the DFS visitation
- pre[z]: the pre order number of vertex Z
- post[z]: the post order number of vertex Z.
Runtime: O(n + m)
AKA: O(|V| + |E|)
Topographial Sort Directed
Purpose, input, output, runtime.
Purpose: Sort the verticies of an input graph in topological order. (Following left to right on resulting list means all preconditions will be done in order)
Input: directed acyclic graph G = (V, E)
Output: array topo length n
- topo[i]: The vertex number of the i’th vertex in topological order from left to right (in descending post order)
Runtime: O(n + m) no need to do sort after DFS, we know post order numbers and their ranges.
AKA: O(|V| + |E|)
Sink: Node with smallest post order
Source: Node with highest post order.
Find Directed Strongly Connected Components
Purpose, input, output, runtime
Purpose: Find the strongly conencted components in a directed graph G.
Input: Directed Graph G=(V,E)
Output: Array ccnum of length n, array topo length t, where t is the number of SCCs, a directed acyclic metagraph Ga = Va, Ea
- ccnum[z]: The connected component numer of vertex z for z in V.
- topo[i] the SCC number for the ith SCC in topological order from left to right.
- Ga = the SCC acyclic metagraph.
Runtime: O(n + m)
AKA: O(|V| + |E|)
BFS_Explore
Purpose, Input, Output, Runtime, Note
Purpose: Explore a graph G (directed or undirected) from a given vertex s using the breath-frist strategy.
Input: a graph G = (V, E) (directed or undirected) and a “source” vertex s in V
Output: an array dist such that dist[u] = distantce (in terms of #edges crossed from vertex s to vertex u
Runtime: O(n + m). AKA: O(V + E)
note: dist[u] = +inf if the vertex u is not reachable from s, and BFS doesn’t account for edge weights, it can only find the shortest path from s to u in terms of edge count.
dfs_explore
Purpose, input, output, runtime
Purpose: Explore a graph G (directed or undirected) from a given vertex v
Input: G=(V, E), v in Vertexes
Output: (For reachable nodes, -1 for the rest)
- prev[z]
- pre[z]
- post[z]
Runtime: O(m) AKA O(E), only iterating over edges.
Reverse Directed Graph
(Considered housekeeping not need to be black boxed)
Purpose, Input, output, runtime
Purpose: Reverse a given directed graph G
input: A driected Graph G=(V,E)
output: A reversed graph Gr = (Vr, Er)
runtime: O(n + m) AKA O(|V| + |E|)
KruskalMST
Purpose, Input, Output, Runtime
+Note
Purpose: Finds the minimum spanning tree over an input undirected connected graph. (Sorts edges and selects edges not traversing graph)
Input: an undirected graph G = (V,E) and a weight function w such that w(u,v) = weight of edge.
Output: a set of edges X, which is the minimum spannign tree of G.
Runtime: O(m log n) AKA: O(E log (v))
Note:If the graph is not connected, we get a minimum spanning forest.
Prim MST
Purpose, input, output, runtime, note
Purpose: Finds the minimum spanning tree G over an input undirected connected graph.
Input: An undirected graph G=(V,E) and a weight function w such that w(u,v) = weight of edge.
Ouptut: an array prev where prev[n] is the parent vertex number of vertex v. prev[root] = null by def.
runtime: O(m log(n)) AKA: O(E log (V))
Note: PRIM expects a connected graph. This is similar to dikjstra’s only instead of using the weight of the full path in the priority tree, it uses the weight of the solitary edge to build up on the frontier.
Dijkstra’s Single Source Shortest Path
Purpose, input, output, runtime, note
Purpose: Finds the shortest path to all nodes from a source s in a weighted graph G (directed or undirected) with non-negative weights.
Input: A graph G=(V,E) directed or undirected, a source vertex and edge weights.
Output: dist[v] shortest distance from s to v, and prev[v] = prev of v in the shortest path from s to v.
Runtime: O((n + m) log(n))
Note: if the graph is connected, M dominates n+m and our time is O(mlog(n))
Directed Acyclic Graph Single Shorted Path
Purpose, Input, output, runtime, note
Purpose: Find the shortest path and its length from the given source vertex s to all vertices v in V, in a directed acyclic weighted graph G=(V,E)
“Same as depth first search, topological sorting”
Input: a DAG G=(V,E), a source vertex s in V, and weights
Output: dist[v] the shortest distance from s to v and prev[v] = the prev of v in the shortest path from s to v.
Runtime: O(n + m)
Note: If it is not possible to reach a vertex u from s, then dist[u] = inf, and negative edges are allowed in this algorithm.
BellmanFord Single Source Shortest Path
Purpose, Input, Output, Runtime, Note
Purpose: Find the shortest path to all nodes from a source s in a weight graph. With postiive or negative weights. (CAPABLE OF FINDING SHORTEST PATHS WITH NEGATIVE WEIGHTS)
Input: A graph G=(V,E), a source vertex s in v, and edge weights
Output: Dist[v] shortest distance from s to v, prev[v] previous vertex in shortest path from s to v.
Runtime: O(nm)
Note: Normally runs in V-1 cycles, however runnin gone more cycle can help detect negative weighted cycles. Also only cycles reachable from s can be detected.
FloydWarshall All Point Shortest Path
Purpose, input, output, runtime, note
Purpose: Finds the shortest path for all pairs, with positive or negative edge weights.
Input: A graph G(V,E) directed or undirected and edge weights.
Output: dist[s,t]: the shortest distance from s to t. (dist is an nxn array
Runtime O(n3)
Can detect any negative cycles by checking distance from node to itself, if it is less than zero, there is a negative weight cycle.
Ford-Fulkerson (max flow/min cut)
Purpose, Input, output, runtime
Purpose: Finds the maximum st-flow in a flow network
Input: Flow Network G=(V,E) with capacities and source sink s & t
Output: Flow fe for e in Edges
Runtime: O(mC) where C is the size of the maximium flow.
Edmonds-Karp (max flow/min-cut)
purpose, input, output, runtime, note
Purpose: Finds the maximium st-flow in a flow network (a directed graph G=(V,E) with capacities
Input: Flow network G=(V,E) with capacties, a source, and a sink
Ouput: Flow fe for edges
Runtime: O(m2n)
Note: Same as F-F, but uses a BFS to find augmenting paths to give a runtime without the C.
What is a Valid Flow?
What is a Maximum Flow?
A Valid flow in a flow network is a flow in g that satisfies all capacity constraints for the edges. (E.g. fe <= C(E)
A Maximum flow is a flow of maximum value.
Let G = (V,E) be a flow network with source s, sink t and integer capacities. Suppose that we are given a maximum flow in G.
Suppose that the capacity of a single edge (u, v) ∈ E is increased by 1. Give an O(V + E) time algorithm to update the maximum flow.
Compose the residual graph on the original flow. Add a positive 1 capacity on the edge that has been increased. Using BFS, search for an augmenting path; if the path exists, we can update the flow, otherwise, the flow is unchanged. We only need to do this once, as the augmenting path, if it exists, increases the flow by 1, which is the maximum increase possible.
Let G = (V,E) be a flow network with source s, sink t and integer capacities. Suppose that we are given a maximum flow in G.
Suppose that the capacity of a single edge (u, v) ∈ E is decreased by 1. Give an O(V + E) time algorithm to update the maximum flow.
Again, compose the residual graph on the original flow. If the decreased edge was not at capacity (that is, it still has positive residual capacity), then we can decrease the edge capacity by one without affecting the maximum flow. If not, then we add one to the negative capacity on the edge, and look for an augmenting path in reverse (going from t to s instead of from s to t) which includes the decreased edge.