Graphs 2
1. Strongly Connected Components
In a directed graph, an SCC is a maximal set of vertices where every vertex can reach every other. Upon compressing all SCC's into their respective nodes, the resulting graph is a DAG.
-
Turning directed graphs into DAGs enable other techniques we have seen before such as DP/topo sort.
-
Algorithms to perform the compression:
- Kosaraju: two DFS passes (on graph and reversed graph)
- Tarjan: one DFS pass with low-link values
-
For SCC's and 2-SAT, you can check out the implementation in KACTL
Complexity
O(n + m)
Practice:
2. 2-SAT
A 2-SAT instance in CNF can be stated as a bunch of OR clauses with 2 variables AND'd together. For example a possible instance of 2-SAT is:
Determine whether there is an assignment of truth values to , , and such that the following statement is true:
We can transform 2-SAT by creating an implication graph and determining the SCC's of this graph. For each variable in the 2-SAT instance we create nodes - one for itself and one for its complement. In the example above, the graph would have nodes: .
Then we write each OR clause as an implication and its contrapositive. For example is equivalent to which is equivalent to . We draw these two edges as directed edges in the original graph for each of the clauses.
Now, one thing that would clearly prove a 2-SAT instance is unsatisfiable is if for some variable , both and via some chain of implications. Confirming that this doesn't happen turns out to be sufficient to show that the 2-SAT instance is satisfiable. A rough proof idea of this is that if a variable implies via a chain, we set to be false and to be true. This assignment will never cause a contradiction to occur due to the nature of how the implication graph is constructed (details to be covered in class).
Problems:
- Giant Pizza (CSES)
- Corporate Retreat (Rocky Mountain Regional 2025)
- Coprime Solitaire (AC)
- Babysitting (CF)
3. Union-Find (Disjoint Set Union, DSU)
DSU maintains a partition of nodes into connected components under merges.
- Union by rank or size gives a complexity of .
- Use path compression as well for near-constant time. (amortized ~
O(α(n))) per operation
Practice:
4. Minimum Spanning Trees (MST)
Given a connected weighted undirected graph, an MST is a tree that connects all nodes with minimum sum of all edge weights.
Cut property: For any cut, one of the lightest edges crossing it must be in the MST. Moreover taking any of the lightest edges will result in a MST. This is the main property used to reason about MST algorithms and problems.
Other properties:
Cycle property: For any cycle in the graph, if an edge is the unique maximum, it will not be included in any MST.
Edge Removal: The two trees created by removing an edge from an MST are MST's of their respective subgraphs.
Two standard algorithms:
Kruskal: Process edges in increasing order by weight and include it in the MST if it connects two different components. Use a DSU to maintain the connected components.
Prim:: Start with a single node and grow the MST one edge/vertex at a time. Repeatedly pick the smallest edge connected a vertex in the current tree to one outside it. Use a heap over the cut edges to efficiently query the minimum at each step.
Can you use the cut property to prove the correctness of both algorithms?
Complexity:
- Kruskal:
O(m log m)(sorting dominates) - Prim:
O(m log n)with heap
Practice: