Graph (II) - HKOI

Download Report

Transcript Graph (II) - HKOI

Graph (III)
Trees, Articulation point, Bridge, SCC
GGuy
19-3-2011
Review
Shortest path
Dijkstra
Bellman Ford
Floyd Warshall
Minimum Spanning Tree
Prim’s
Kruskal’s
Tree
• The following four conditions are equivalent:
– G is connected and acyclic
– G is connected and |E| = |V| - 1
– G is acyclic and |E| = |V| - 1
– Between any pair of vertices in G, there exists a
unique path
• G is a tree if at least one of the above
conditions is satisfied
Tree
• |E| = |V| - 1
• Between any pair of vertices, there is a unique path
• Adding an edge between a pair of non-adjacent
vertices creates exactly one cycle
• Removing an edge from the tree breaks the tree into
two smaller trees
Tree
ancestors
root
parent
siblings
descendants
children
Tree Diameter
Find the farthest node from root, say it u
Find the farthest node from u, say it v
(u, v) = Tree diameter
DFS Tree
DFS (vertex u) {
mark u as visited
time = time + 1
birth[u] = time;
for each vertex v directly reachable from u
if v is unvisited
parent[v] = u
DFS (v)
time = time + 1
death[u] = time;
}
DFS forest (Demonstration)
A
B
C
D
E
F
G
H
birth
1
2
3
13
10
4
14
6
death
12
9
8
16
11
5
15
7
parent
-
A
B
-
A
C
D
C
A
D
B
E
F
E
C
A
D
F
H
unvisited
B
visited
H
C
G
visited (dead)
G
Classification of edges
•
•
•
•
A
Tree edge
Forward edge
Back edge
Cross edge
D
B
E
G
C
F
H
• Question: which type of edges is always absent in
an undirected graph?
Determination of edge types
• How to determine the type of an arbitrary
edge (u, v) after DFS?
• Tree edge
– parent [v] = u
• Forward edge
– not a tree edge; and
– birth [v] > birth [u]; and
– death [v] < death [u]
• How about back edge and cross edge?
Determination of edge types
Tree edge Forward Edge
Back Edge
Cross Edge
parent [v] not a tree edge
=u
birth[v] > birth[u]
death[v] < death[u]
birth[v] < birth[u]
death[v] > death[u]
birth[v] < birth[u]
death[v] < death[u]
Articulation Point
For a undirected graph G,
Node u is an Articulation Point if
remove u from G will create more
components.
Articulation Point
Bridge
For a undirected graph G,
Edge (u, v) is an bridge if
remove (u, v) from G will create more
components.
Bridge
Finding AP and Bridge
DFS(vertex u)
time = time + 1
low[u] = vis[u] = time
for each vertex v directly reachable from u
if v is not visited
DFS(v)
low[u] = min(low[u], low[v])
if v is visited and parent of u is not v
low[u] = min(low[u], vis[v])
Finding AP
Find the DFS tree of G and compute low[]
Vertex u is an AP if
1. u is the root and u has >1 children
2. u is not the root and there exists
children v where low[v] >= vis[u]
Finding Bridge
Find the DFS tree of G and compute low[]
Edge (u, v) is a Bridge if
1. v is u children and low[v] > vis[u]
Bi-connected component
A bi-connected component (or 2-connected
component) is a maximal bi-connected subgraph
A bi-connected subgraph is a graph that has no
articulation point
Bi-connected component
Each Bi-connected component is
connected by bridge
Each articulation point belongs to more
than one Bi-connected component
The Bi-connected components form a Tree
Bi-connected component
Strongly Connected Component
In a directed graph G, a Strongly Connected
Component is a subgraph that there is a path
from each vertex to every other vertex in the
same SCC.
Strongly Connected Component
Finding SCC
• Compute the DFS forest of the graph G to get
the death time of the vertices
• Reverse all edges in G to form G’
• Compute a DFS forest of G’, but always choose
the vertex with the latest death time when
choosing the root for a new tree
• The SCCs of G are the DFS trees in the DFS
forest of G’
SCC (Demonstration)
A
B
C
D
E
F
G
H
birth
1
2
3
13
10
4
14
6
death
12
9
8
16
11
5
15
7
parent
-
A
B
-
A
C
D
C
E
F
A
E
B
D
A
D
G
F
B
H
C
G
C
H
SCC (Demonstration)
A
F
D
G
F
C
B
H
C
G
B
D
E
A
E
H
Finding SCC
Assume (u, v) belongs to the same SCC
WLOG, assume u is visited first. Since the exists a
path from u to v, we have
death[u] > death[v]
When we do DFS on G’, we will call DFS(u) first. Since
there exists a path from v to u in G, then there exists
path from u to v in G’, hence v and u will be in the
same tree.
Iterative Depth Searching (IDS)
Bi – Directional Searching (BDS)