Content
- Basic Definitions
- Graph Connectivity and Graph Traversal
- Testing Bipartiteness
- Connectivity in Directed Graphs
- DAGs and Topological Ordering
Algorithm II
3. Graphs
WU Xiaokun 吴晓堃
xkun.wu [at] gmail
Computer deals with discrete mathematics.
Notation. G = (V, E)
V = \{ 1, 2, 3, 4, 5, 6, 7, 8
\}
E = \{ 1–2, 1–3, 2–3, 2–4, 2–5, 3–5, 3–7, 3–8,
4–5, 5–6, 7–8 \}
m = 11, n = 8
graph | node | edge |
---|---|---|
communication | telephone, computer | fiber optic cable |
circuit | gate, register, processor | wire |
mechanical | joint | rod, beam, spring |
financial | stock, currency | transactions |
transportation | street intersection, airport | highway, airway route |
internet | network hub | connection |
game | board position | legal move |
social relationship | person, actor | friendship, movie cast |
neural network | neuron | synapse |
protein network | protein | protein-protein interaction |
molecule | atom | bond |
Adjacency matrix. n-by-n matrix with A_{uv} = 1 if (u, v) is an edge.
Adjacency lists. Node-indexed array of lists.
Degree n_v of a node v: the number of incident edges it has.
Sum of the degrees. \sum_{v \in V} n_v = 2m.
Pf. Each edge e = (v,
w) contributes exactly twice to this sum.
Theorem. Adjacency matrix representation of a graph requires O(n^2) space; Adjacency list representation requires only O(m + n) space.
Def. A path in an undirected graph G = (V, E) is a sequence of nodes v_1, v_2, \ldots, v_k with the property that each consecutive pair v_{i–1}, v_i is joined by a different edge in E.
Def. A path is simple if all nodes are distinct.
Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between them.
Def. A cycle is a path v_1, v_2, \ldots, v_k in which v_1 = v_k and k \ge 2.
Def. A cycle is simple if all nodes are distinct (except for v_1 and v_k).
Def. An undirected graph is a tree if it is connected and contains no cycle.
Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third:
Rooted tree. Given a tree T, choose a root node r and orient each edge away from r.
Importance. Models hierarchical structure.
s-t connectivity problem. Given two nodes s and t, is there a path between them?
s-t shortest path problem. Given two nodes s and t, what is the length of a shortest path between them?
Applications.
BFS intuition. Start at root s and “flood” the graph.
BFS algorithm.
Theorem. For each i \ge 1, L_i consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer.
Note: non-tree edges all either connected nodes in the same layer, or connected nodes in adjacent layers.
Property. Let T be
a breadth-first search tree, let x and
y be nodes in T belonging to layers L_i and L_j
respectively, and let (x, y) be an edge
of G. Then i and j
differ by at most 1.
Pf.
BFS corresponds exactly to queue structure.
Cycle? Array Discovered
of length n
Discovered[v]= true
as soon as our search
first sees v.Discovered
[s] =
true
; Discovered
[v] = false
for all other v;
L[0] = \{s\}; layer counter i = 0; current tree T = \{\};
While L[i] is not empty:
Discovered
[v] = false
:
Discovered
[v] = true
;Theorem. The above implementation of the BFS
algorithm runs in time O(m + n) (i.e.,
linear in the input size), if the graph is given by the adjacency list
representation.
Pf.
Discovered
.DFS intuition: explore a maze.
Depth-first search tree: non-tree edges can only connect ancestors to descendants.
DFS corresponds exactly to stack structure.
Cycle? Array Discovered
of length n
Discovered[v]= true
as soon as our search
first sees v.Connected component. Find all nodes reachable from s.
Theorem. Upon termination, R is the connected component containing s.
Def. An undirected graph G
= (V, E) is bipartite if the nodes can be
colored blue or white
such that every edge has one
white
and one blue
end.
Applications.
blue
, women =
white
.blue
, jobs =
white
.If the underlying graph is bipartite, many graph problems become:
Before attempting to design an algorithm, we need to understand structure of bipartite graphs.
Clearly a triangle is not bipartite.
Lemma. If a graph G
is bipartite, it cannot contain an odd-length cycle.
Pf. Not possible to 2-color the odd-length cycle, let
alone G.
Lemma. Let G be a connected graph, and let L_0, \ldots, L_k be the layers produced by BFS starting at node s. Exactly one of the following holds:
(Proofs in the following slides.)
Lemma. Let G be a connected graph, and let L_0, \ldots, L_k be the layers produced by BFS starting at node s. Exactly one of the following holds:
Pf. (i)
white
= nodes on odd levels,
blue
= nodes on even levels.Lemma. Let G be a connected graph, and let L_0, \ldots, L_k be the layers produced by BFS starting at node s. Exactly one of the following holds:
Pf. (ii)
Corollary. A graph G is bipartite iff it contains no odd-length cycle.
Notation. G = (V, E).
Ex. Web graph: hyperlink points from one web page to another.
Ex. Road network
Directed reachability. Given a node s, find all nodes reachable from s.
Directed s \leadsto t shortest path problem. Given two nodes s and t, what is the length of a shortest path between them?
Graph search. BFS extends naturally to directed graphs.
Web crawler. Start from web page s: find all web pages linked from s, either directly or indirectly.
Def. Nodes u and v are mutually reachable if there is both a path from u to v and also a path from v to u.
Def. A graph is strongly connected if every pair of nodes is mutually reachable.
Lemma. Let s be any
node. G is strongly connected
iff every node is reachable from s, and
s is reachable from every node.
Pf. \Rightarrow
Follows from definition.
Pf. \Leftarrow
Theorem. Can determine if G is strongly connected in O(m + n) time.
Pf.
Def. A strong component is a maximal subset of mutually reachable nodes.
Theorem. [Tarjan 1972] Can find all strong components in O(m + n) time.
Def. A DAG is a directed graph that contains no directed cycles.
Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v_1, v_2, \ldots, v_n so that for every edge (v_i, v_j) we have i < j.
Precedence constraints. Edge (v_i, v_j) means task v_i must occur before v_j.
Applications.
Lemma. If G has a
topological order, then G is a
DAG.
Pf. [by contradiction]
Lemma. If G is a
DAG, then G has a node with no entering
edges.
Pf. [by contradiction]
Lemma. If G is a
DAG, then G has a topological
ordering.
Pf. [by induction on n]
Theorem. Algorithm finds a topological order in
O(m + n) time.
Pf.