CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
-
date post
20-Dec-2015 -
Category
Documents
-
view
219 -
download
3
Transcript of CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
CSE 780 Algorithms
Advanced Algorithms
Graph Alg.DFS
Topological sort
CSE 780 Algorithms
Objectives
On completion of this lecture, students should be able to:
1. Write a dfs algorithm
2. Apply dfs in topological sort and finding strongly-
connected components.
CSE 780 Algorithms
Breadth-first Search
Works for both directed and undirected graph Starting from source node s, visits remaining
nodes of graph from small distance to large distance
Produce a BF-tree Return distance between s to any reachable node
in time O(|V| + |E|)
CSE 780 Algorithms
Depth-first Search
Breadth-first search: Go as broad as possible at each node
Depth-first search (a different strategy): Go as deep as possible first
CSE 780 Algorithms
More Formally
Again, a node white: unvisited gray: discovered but not finished black: finished (explored)
For every node v V d[v]: time v is discovered f[v]: time v is finished (all edges in v’s adjacency list
are explored) f[v] - d[v] = time from grey to black
CSE 780 Algorithms
Pseudo-code
Time complexity
CSE 780 Algorithms
Example
CSE 780 Algorithms
Depth-first Forest
Consider all edges (p(u), u) When it happens: p(u) is grey, u is white A forest, called Depth-first forest Depends on the order of vertices Example from previous page
Property: Start (finish) time for each tree: same order as if we
visit nodes in pre-order (post-order) tree walk
CSE 780 Algorithms
Properties of DFS
Parenthesis Theorem: Any two nodes u and v, one of the following 3 cases:
(1) u is descendant of v, and d[v] < d[u] < f[u] < f[v] (2) v is descendant of u, and d[u] < d[v] < f[v] < f[u] (3) u is not descendant of v, neither is v a descendant of u, and d[u] < f[u] < d[v] < f[v] or d[v] < f[v] < d[u] < f[u]
u is descendant of v iff d[v] < d[u] < f[u] < f[v] White-path Theorem
u is descendant of v iff at the time of d[v], there is a all white path from v to u.
CSE 780 Algorithms
Classification of Edges
Four types of an edge (u,v) Tree edge Non-tree edges:
Back edge: u is descendant of v Forward edge: v is descendent of u Cross edge: others
Distinguish by the color of v Example
b
dc
e
f g
h
j
a
k
CSE 780 Algorithms
Theorem
In a DFS of an undirected graph G, every edge of G is either a tree edge or a back edge.
b
dc
e
f g
h
j
a
k
CSE 780 Algorithms
Connected Components
Undirected graph DF forests represents the set of connected components
Directed graph Does the tree rooted at u include all nodes accessible
from u ?
CSE 780 Algorithms
Directed Acyclic Graph
DAG: directed acyclic graph Determine whether a directed graph is DAG or not
How?
A directed graph G is a dag iff a DFS of G yieldsno back edges.
How about an undirected acyclic graph?
CSE 780 Algorithms
Topological Sort
A topological sort of a dag G = (V, E) A linear ordering A of all vertices from V If edge (u,v) E => A[u] < A[v]
undershorts
pants
beltshirt
tie
jacket
shoes
socks
watch
CSE 780 Algorithms
Topological Sort
Using DFS
Topological-Sort(G) Call DFS(G) to compute finishing times f[v] for each v Output vertices in descreasing order of finishing time
Time complexity O (|V| + |E|)
CSE 780 Algorithms
Exampleundershort
s
pants
beltshirt
tie
jacket
shoes
socks
watch
11, 16
12, 15
6, 7
3, 4
2, 5
1, 813, 14
17, 18
9, 10
socks , undershots , pants , shoes , watch , shirt , belt , tie , jacket
CSE 780 Algorithms
Correctness
Theorem Topological-Sort(G) produces a topological sort of a
directed acyclic graph G
Proof: Consider any edge (u, v)
Goal: f[v] < f[u] Three possible types of edges:
Tree edge, forward edge, cross edge
CSE 780 Algorithms
Connected Components
An undirected graph is connected if every pair of vertices is connected by a path
A graph that is not connected is naturally decomposed into several connected components.
A connected components is a maximal set of vertices, which are all connected.
CSE 780 Algorithms
Connected Components (cont.)
The graph below has 3 connected components
{1,2,5}, {3,6} and {4}.An undirected graph is connected if it has exactly
one connected component.
CSE 780 Algorithms
Strongly-connected components
CSE 780 Algorithms
Strongly-connected components
The above graph has 3 strongly connected components {1,2,4,5}, {3} and {6}
A directed graph is strongly connected if it has only one strongly connected component.
CSE 780 Algorithms
Transpose of a Graph
The transpose of a directed graph G = (V,E) is the graph GT = (V, ET)
where ET = {(u,v): (v,u) Є E}. ET consists of the edges of G with their directions
reversed. G and GT have exactly the same strongly
connected components.
CSE 780 Algorithms
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSE 780 Algorithms
CSE 780 Algorithms
Corollary 22.15
Let C and C’ be distinct strongly connected components in directed graph G = (V,E). Suppose that there is an edge (u,v) ET, where u C and
v C’. Then f(C) < f(C’)
Note: f(C) = max u C {f[u]}
CSE 780 Algorithms
Theorem 22.16STRONGLY-CONNECTED-COMPONENTS(G) correctly computes the strongly connected components of a directed graph G.Proof.We argue by induction that the vertices of each tree form a strongly connected component.The inductive hypothesis: The first k trees produced in line 3 are strongly connected.
CSE 780 Algorithms
The basis for the induction, when k=0, is trivial.The inductive step: assume that each of the first trees produced is a strongly connected component.Consider the (k+1)st tree.Let the root of this tree be vertex u,and let u be in a strongly connected component C.Because of how we choose roots in line 3,f[u] = f(C) > f(C’) for any strongly connected component C’ that has yet to be visited.
CSE 780 Algorithms
By inductive hypothesis, at the time that the searchvisits u, all other vertices of C are white.Therefore, all other vertices of C are descendants of u in its tree.Moreover, by inductive hypothesis and corollary 22.15, any edge in GT that leave C must be to strongly connected components that have already visited.Thus, no vertex in any strongly connected component other than C will be a descendant of u during the depth-first search of GT. Thus, the vertices of the depth-first tree in GT that is rooted at u form exactly one strongly connected component.
CSE 780 Algorithms
Summary
Graph search/traversal method
Breadth-first search Discover nodes in shortest distance (# links) e.g, chess
Depth-first search Parenthesis theorem e.g, topological sort