Graph Theory Study Notes
Strongly Connected Components
Some Properties
- In a directed graph, each vertex belongs to exactly one strongly connected component.
- Strongly connected components are transitive.
- A maximal strongly connected component means a strongly connected component that cannot be enlarged any further.
Some Details of Tarjan's Algorithm
- When a node's DFN equals its LOW, a strongly connected component has been found, so pop the stack.
Some Example Problems
P2002: Message Spread — Solution
Finding Cut Vertices
Similar to the Tarjan algorithm above, as long as \(dfn[u]\leq low[u]\), this vertex can be considered a cut vertex. Intuition: if the downstream vertices cannot reach the upstream vertices with smaller dfn except by paths through vertex u, then this is a cut vertex. But we also need to consider the case where vertex \(u\) is the root, because if vertex \(u\) is the root, then it counts as a cut vertex as long as it has more than two subtrees, since connectivity between the subtrees has to go through the root as a hub.
Code for the template problem:
// P3388.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 20010, MAX_M = 100010;
int head[MAX_N], current, n, m, tmpx, tmpy, low[MAX_N], dfn[MAX_N], tot;
bool ans[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_M << 1];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void tarjan(int u, int fa)
{
low[u] = dfn[u] = ++tot;
int child = 0;
for (int i = head[u]; i != -1; i = edges[i].nxt)
{
if (!dfn[edges[i].to])
{
tarjan(edges[i].to, fa), low[u] = min(low[u], low[edges[i].to]);
if (low[edges[i].to] >= dfn[u] && u != fa)
ans[u] = true;
if (u == fa)
child++;
}
low[u] = min(low[u], dfn[edges[i].to]);
}
if (child >= 2 && u == fa)
ans[u] = true;
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
int cnt = 0;
for (int i = 1; i <= m; i++)
scanf("%d%d", &tmpx, &tmpy), addpath(tmpx, tmpy), addpath(tmpy, tmpx);
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, i);
for (int i = 1; i <= n; i++)
if (ans[i])
cnt++;
printf("%d\n", cnt);
for (int i = 1; i <= n; i++)
if (ans[i])
printf("%d ", i);
return 0;
}
Biconnected Components
Vertex-Biconnected Components
Definition: in an undirected graph, for a pair of vertices \(u,v\), if they stay connected after deleting any one vertex other than \(u,v\) from the graph, then this pair is called a vertex-biconnected component.
Edge-Biconnected Components
Definition: in an undirected graph, for a pair of vertices \(u,v\), if \(u,v\) stay connected after deleting any one edge from the graph, then it is called an edge-biconnected component.
Bipartite Graphs
https://www.renfei.org/blog/bipartite-matching.html
Properties
In a bipartite graph, choosing the fewest vertices such that these vertices are incident to all the edges (covering all the edges) is called a minimum vertex cover.
Minimum vertex cover of a bipartite graph = maximum matching of the bipartite graph
Maximum independent set of a bipartite graph = total number of vertices - maximum total matching of the bipartite graph
Example problems: [USACO2006NOV]Asteriod, [Usaco2005 jan]Muddy Fields
Hungarian Algorithm
Template link: P3386 [Template] Bipartite Matching
bool dfs(int u, int tm)
{
for (int i = head[u]; i != -1; i = edges[i].nxt)
{
int to = edges[i].to;
if (dfn[to] != tm)
{
dfn[to] = tm;
if ((!match[to]) || dfs(match[to], tm))
{
match[to] = u;
return true;
}
}
}
return false;
}
Personally, I think this is a rather ingenious way to write it: it combines “keep matching” and “negotiate upward” perfectly, which is really excellent.
2-SAT
This model can be used to find the solution set of variables under multiple constraints. You can derive feasible relations from certain contradiction relations and add them as directed edges, then just color with Tarjan.
Example problem: HDU3062: Party — Solution
%%%