The Decomposition of Graphs
DPV Chapter 3
Jim Royer
EECS
February 6, 2019
Royer (EECS) Graph Decomposition February 6, 2019 1 / 32
The Decomposition of Graphs DPV Chapter 3 Jim Royer EECS February - - PowerPoint PPT Presentation
The Decomposition of Graphs DPV Chapter 3 Jim Royer EECS February 6, 2019 Royer (EECS) Graph Decomposition February 6, 2019 1 / 32 Graph basics, 1 Definition An undirected graph consists of a set of vertices V and a set of edges E between
DPV Chapter 3
EECS
Royer (EECS) Graph Decomposition February 6, 2019 1 / 32
See: http://en.wikipedia.org/wiki/Seven_bridges_of_konigsberg
For a Google-map view, click here—they seem to have lost a few bridges. Royer (EECS) Graph Decomposition February 6, 2019 2 / 32
main parse init cleanup printf execute make_string compare
Royer (EECS) Graph Decomposition February 6, 2019 3 / 32
Diagram from http://en.wikipedia.org/wiki/Graph_(mathematics) Royer (EECS) Graph Decomposition February 6, 2019 4 / 32
Diagram from http://en.wikipedia.org/wiki/Directed_graph Royer (EECS) Graph Decomposition February 6, 2019 5 / 32
Royer (EECS) Graph Decomposition February 6, 2019 6 / 32
Royer (EECS) Graph Decomposition February 6, 2019 7 / 32
// Input: a graph G = (V, E) and v ∈ V // Output: for all vertices u, reachable from v: visited[u] is set to true visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v) 3, 5, and 6 are adjacent to 4 3 is adjacent to 4, but neither 1 nor 2 is adjacent to 4.
Royer (EECS) Graph Decomposition February 6, 2019 8 / 32
Suppose initially visited[u] = false for all u ∈ V. Then explore visits exactly all the vertices reachable from v. Proof:
visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v) Claim 1: If u is visited, then u is reachable from v. (Why?) Claim 1′: If u is not reachable from u, then v is unvisited. (Why?)
Royer (EECS) Graph Decomposition February 6, 2019 9 / 32
Suppose initially visited[u] = false for each u ∈ V. Then explore visits exactly all the vertices reachable from v. Proof (continued):
visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v) Claim 2: If u is reachable from v, then u is eventually visited. By way of contradiction, suppose there is an unvisited, reachable u. v = u. (Why?) Take a path from v to u. [Draw the picture!] Let y be the last visited vertex in the path. [Draw the picture!] Let z be the next vertex after y on the path. [Draw the picture!] But by the algorithm, z must be visited, a contradiction. Therefore, Claim 2 and the lemma follow.
Royer (EECS) Graph Decomposition February 6, 2019 10 / 32
// G = (V, E) for each v ∈ V do visited[v] ← false for each v ∈ V do if not visited[v] then explore(G, v)
visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v)
1 2 3 5 6 4
Royer (EECS) Graph Decomposition February 6, 2019 11 / 32
Depth-First Exploration of the Entire Graph
procedure dfs(G) // G = (V, E) for each v ∈ V do visited[v] ← false for each v ∈ V do if not visited[v] then explore(G, v) procedure explore(G, v) visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v) 1 2 3 5 6 4
2019-02-06
Graph Decomposition Depth-First Exploration of the Entire Graph Alternative graph:
2 8 4 3 9 7 5 6 1
for each v ∈ V do visited[v] ← false for each v ∈ V do if not visited[v] then explore(G, v)
visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v) Run time analysis: Each v is explore’d exactly once. (Why?) In the undirected case, each edge is explore’d down twice. (Why?) In the directed case, each edge is explore’d down once. (Why?) Under the adjacency list representation, this all takes Θ(|V| + |E|) time. (Why?)
Royer (EECS) Graph Decomposition February 6, 2019 12 / 32
(a)
(b)
(c)
Figures from DPV Royer (EECS) Graph Decomposition February 6, 2019 13 / 32
for each v ∈ V do visited[v] ← false; cc[v] ← 0 count ← 1 for each v ∈ V do if not visited[v] then explore(G, v); count ← count + 1
visited[v] ← true previsit(v) for each u adjacent to v do if not visited[u] then explore(G, u) postvisit(v)
cc[v] ← count
Royer (EECS) Graph Decomposition February 6, 2019 14 / 32
procedure previsit(v) pre[v] ← clock clock ← clock + 1 procedure postvisit(v) post[v] ← clock clock ← clock + 1
(a)
(b)
(c)
Figures from DPV Royer (EECS) Graph Decomposition February 6, 2019 15 / 32
Back Forward Cross T r e e
Figure from DPV
(a)
(b)
(c)
(d)
Royer (EECS) Graph Decomposition February 6, 2019 16 / 32
[
]
[
]
[
]
A H B C E D F G
12,15 13,14 1,16 2,11 4,7 5,6 8,9 3,10
Figure from DPV Royer (EECS) Graph Decomposition February 6, 2019 17 / 32
A directed graph G has a cycle ⇐ ⇒ any depth-first search of G finds a back edge.
◮ Suppose G has a cycle. ◮ Suppose u is the first vertex of this cycle a particular DFS finds. ◮ Then the DFS visits all the vertices reachable from u. ◮ In the course of this it must find a back edge.
(Why?)
Royer (EECS) Graph Decomposition February 6, 2019 18 / 32
(a)
(b)
(c)
(⋆) Note: [u ≤G v & v ≤G u] ⇒ [u = v].
Royer (EECS) Graph Decomposition February 6, 2019 19 / 32
Figure from CLRS Royer (EECS) Graph Decomposition February 6, 2019 20 / 32
(a)
(b)
(c)
Royer (EECS) Graph Decomposition February 6, 2019 21 / 32
clock ← 0; topsort ← emptylist for each v ∈ V do: visited[v] ← false; pre[v] ← 0; post[v] ← 0 for each v ∈ V do: if not visited[v] then explore(G, v);
visited[v] ← true previsit(v) for each u adjacent to v do: if not visited[u] then explore(G, u) postvisit(v)
pre[v] ← clock; clock ← clock + 1
post[v] ← clock; clock ← clock + 1; add v to the front of topsort
Royer (EECS) Graph Decomposition February 6, 2019 22 / 32
procedure explore(G, v) visited[v] ← true previsit(v) for each u adjacent to v do: if not visited[u] then explore(G, u) postvisit(v) procedure previsit(v) pre[v] ← clock; clock ← clock + 1 procedure postvisit(v) post[v] ← clock; clock ← clock + 1 add v to the front of topsort
m q r x t u y n
v w p z
Royer (EECS) Graph Decomposition February 6, 2019 23 / 32
I.e., u ∼G u and u ∼G v ⇐ ⇒ v ∼G u and (u ∼G v & v ∼G w) ⇒ u ∼G w.
Royer (EECS) Graph Decomposition February 6, 2019 24 / 32
A B,E D C,F G,H,I,J,K,L A B D C E F G H J K I L A B D C E F G H J K I L Royer (EECS) Graph Decomposition February 6, 2019 25 / 32
Q1: How to find vertex in a sink component? Q2: What to do after that? Observation: Finding a vertex in a source component is easy.
Royer (EECS) Graph Decomposition February 6, 2019 26 / 32
Q1: How to find vertex in a sink component? Q2: What to do after that? Observation: Finding a vertex in a source component is easy.
Royer (EECS) Graph Decomposition February 6, 2019 26 / 32
Q1: How to find vertex in a sink component? Q2: What to do after that? Observation: Finding a vertex in a source component is easy.
Royer (EECS) Graph Decomposition February 6, 2019 26 / 32
CASE: The DFS visits C before C′. Then the DFS visits all of C and C′ before backing out of C. CASE: The DFS visits C′ before C. Then the DFS must visit all of C′ before arriving at C.
Royer (EECS) Graph Decomposition February 6, 2019 27 / 32
Royer (EECS) Graph Decomposition February 6, 2019 28 / 32
Figure from CLRS 1
Do a DFS on GR.
2
Order the vertices v1, . . . , vn by finishing time (biggest to smallest).
3
count ← 1 for i ← 1 to n do if not visited[vi] then explore(G, vi) count ← count + 1 where procedure previsit(v) scc[v] ← count
Royer (EECS) Graph Decomposition February 6, 2019 29 / 32
1
Do a DFS on GR.
2
Order the vertices v1, . . . , vn by finishing time (biggest to smallest).
3
count ← 1 for i ← 1 to n do if not visited[vi] then explore(G, vi) count ← count + 1 where procedure previsit(v) scc[v] ← count
1
2
3
Royer (EECS) Graph Decomposition February 6, 2019 30 / 32
Royer (EECS) Graph Decomposition February 6, 2019 31 / 32
Breadth First Search (BFS) Visit v. Visit all vertices distance 1 from v Visit all vertices distance 2 from v . . .
See: http://www.cs.sunysb.edu/~skiena/combinatorica/animations/search.html
BFS is a queue-based search — DFS is stack based. (More on this in the next chapter.) Game tree search The tree is too big, so you build it as you explore it. You have a heuristic rating function on positions. You next explore the best-rated position not yet visited. This is a priority queue based search.
Royer (EECS) Graph Decomposition February 6, 2019 32 / 32