230 likes | 313 Views
Explore the concepts of Depth First Search (DFS) and Breadth First Search (BFS) in graph theory. Understand how to find strongly connected components using modified DFS. Learn about acyclic graphs and topological ordering. Discover spanning trees and directed paths in graph exploration.
E N D
Graph -definition V : vertex set(頂点集合) A⊆V×V: arc set(枝集合) vertex (頂点) arc(枝) G=(V, A) discrete structure(離散構造)
3.グラフ探索 to examine the vertices and arcs of a graph, systematically. • depth first search(DFS) 深さ優先探索 • breadth first search(BFS) 幅優先探索
DFS(深さ優先探索) from a vertex v already treated, we proceed to any vertex w adjacent to v which was not yet treated and, after having examined w, continue to look for the next unlabeled vertex directly from w.
DFS example true v w Initialization a (= s) Av c d b e g f h
DFS property(1) Each arc in the connected component of s is used exactly once in each direction during DFS a (= s) c d b e g f h
DFS property(2) A’={(p(v), v) | v ∈V} When G is connected, T=(V, A’) is a spanning directed tree with root s. --- Depth first search tree a (= s) c d b e g f h
BFS(幅優先探索) Process all the neighbors of the starting vertex. Then process all the neighbors of neighbors of the starting vertex. And so on.
BFS example Q= [ ] a b c d e f g h v w Av a (= s) c d b e g f h
BFS property (1) Each arc in the connected component of s is used exactly once in each direction during BFS a (= s) c d b e g f h
BFS property (2) A’={(p(v), v) | v ∈V} When G is connected, T=(V, A’) is a spanning directed tree with root s. ---- Breadth first search tree a (= s) c d b e g f h
BFS property (3) If v and v’ are two vertices in the same connected component, there is a path of shortest length l between v and v’. Then v and v’ are said to have distance l = d(v, v’) At the end of BFS, d(s, t) = d(v), if d(v) is defined. a (= s) c d b e g f h
For digraph We can apply DFS and BFS to digraphs. (DFS) Av a (= s) c d b e g f h
Finding strongly connected components We can determine strongly connected components in a digraph G by executing the (modified) DFS both for G and for the directed graph having opposite orientation.
DFS+ nr(v) gives the order in which the vertex v is reached Nr(v) gives the order in which the examination of the vertex v is finished input Graph G and a vertex s begin set nr(v)←0, p(v)←0, Nr(v)←0 for all v∈V; set u(a)←false for all a∈A; set i ←1, v←s, nr(s)←1, j←0; repeat while there exists w∈Av with u(vw)=false do begin choose some w∈Av with u(vw)=false ; set u(vw)←true; ifnr(w) = 0then set p(w)←v, i←i+1, nr(w)←i, v←w; end set j←j+1, Nr(v)← j,v←p(v); untilv=s and u(sw)=true for all w∈As; end.
DFS+ example 6 5 2 3 4 1
DFS+ property in the same tree, but v is not descendant nor ancestor of x and x is accessible from v in different DFS trees v is descendant of x v x x x v v If Nr(v) <Nr(x) for two vertices v, x v is accessible from x
Algorithm for strongly connected component SCC input digraph G begin apply DFS+ to G and obtain Nr; construct the digraph H by reversing the orientation of all arcs of G; set k←0; repeat choose the vertex r in H for which Nr(r) is maximal; apply DFS+ to H where the starting vertex s=r. set k←k+1, Ck←{v ∈V | nr(v) ≠0}; set H←H\Ck; until the vertex set of H is empty; end. C1, C2, …,Ck are vertex sets of strongly connected components
SCC example 6 3 2 5 1 4
SCC correctness two vertices v and w are in the same set Ci two vertices v and w are in the same strong component ⇔ (⇒) v and w are in the same strong component ⇒ there are directed paths from v to w and from w to v in G ⇒ there are directed paths from v to w and from w to v in H ⇒ w.l.o.g. v is reached before w in DFS on H, v∈Ci , then w∈Ci
SCC correctness two vertices v and w are in the same set Ci two vertices v and w are in the same strong component ⇔ (⇒) v and w are in the same set Ci ⇒ v and w are in the same DFS tree Ti Let x be the root of Ti ⇒ Nr(x)>Nr(v) and v is accessible from x Nr(x)>Nr(w) and w is accessible from x ⇒ x is accessible from v x is accessible from w x v w
Acyclic(非閉路)digraph not contain a directed cycle
Acyclic graph example 1 2 4 3 5 topological order :o 7 6 for any arc(v, w), o(v) < o(w) 8 socks undershorts shoes shirt pants tie belts jacket
Topological sort Let G be a directed acyclic graph. Then G contains at least one vertex with din (v) = 0, where din (v) denotes the number of arcs having head v. 4 3 5 1 8 6 2 7 9