Graph Algorithms (Crowdsourced)
Graph Algorithms (Crowdsourced)
Prim’s Algorithm
Prim's algorithm is the second and final algorithm used to find the MSTs as
discussed in class.
Prim's Algorithm in 2 minutes
Input:
Connected, Undirected Graph G = (V, E) with edge weights w_e
Output:
An MST defined by the prev[] array
Graphs that can use Prim's:
Connected
Undirected
Weighted
Runtime:
O(m log n) if graph is connected
O((m + n) log n) if graph is not connected
2SAT
The 2-SAT problem is to determine whether there exists an assignment to variables
of a given Boolean formula in 2-CNF (conjunctive normal form) such that the
formula evaluates to true. The algorithm for solving 2-SAT uses graph theory by
constructing an implication graph and then checking for the existence of a path that
satisfies the conditions.
Input:
A Boolean formula in 2-CNF is represented as a set of clauses where each
clause is a disjunction of exactly two literals.
Output:
A Boolean value indicates whether the given 2-CNF formula is satisfiable. If it
is satisfiable, the algorithm may also provide a satisfying assignment of
variables.
Graphs that can use 2-SAT:
Directed graphs
o The implication graph is inherently directed since each implication (¬x
→ y) has a direction.
Runtime:
O(m + n) - m is the number of clauses in the 2-CNF formula, n is the number
of literal or variables.
o This runtime stems from the linear runtime of SCC finding algorithms
and the construction of the implication graph.
FF (Ford-Fulkerson)
A greedy algorithm to find max flow on networks. The algorithm continually sends
flow along paths from the source (starting node) to the sink (end node), provided
there is available capacity on all edges involved. This flow continues until no further
augmenting paths with available capacity are detected.
Ford-Fulkerson in 5 minutes
Ford-Fulkerson Algorithm Web Animation
Input:
G = (V, E)
Flow capacity c
Source node s
Sink node t
Output:
Max flow
We have access to:
Can trivially create the final residual network with G
Max flow of G
Example use:
We run FF on the flow network to get the maximum flow.
We use this to construct the residual graph.
Graphs that can use FF:
Directed graphs with capacity of edges
Runtime:
O(C * m)
o C is the maximum flow in the network