graph -definition v : vertex set (頂点集合) a ⊆ v×v: arc set (枝集合) g=(v, a)...

23
Graph - definition V : vertex set 頂頂頂 A⊆V×V: arc set 頂頂頂頂 () G=(V, A) discrete structure 頂頂頂頂頂 () vertex 頂頂頂 () arc 頂頂 ()

Upload: merry-hensley

Post on 21-Jan-2016

271 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

Graph -definitionV : vertex set (頂点集合)

A⊆V×V: arc set (枝集合)

G=(V, A)     discrete structure (離散構造)

vertex (頂点)

arc (枝)

Page 2: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

3. グラフ探索to examine the vertices and arcs of a graph, systematically.

•  depth first search(DFS)  深さ優先探索•  breadth first search(BFS)   幅優先探索

Page 3: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

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.

Page 4: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

DFS example

a (= s)

bc d

e

fg

h

a b,c,d

b a,e,f

c a

d a,g

e b,f,g

f b,e

g d,e,h

h g

Av

nr p1 0

0 00 0

0 0

0 0

0 0

0 0

0 0

Initialization v w true

2 a

3 b4 e5 e

6 g

7 g

8 a

Page 5: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

DFS property(1)

a (= s)

bc d

e

fg

h

nr p 1 0

2 a

3 b4 e

5 e

6 g

7 g

8 a

Each arc in the connected component of s is used exactly once in each direction during DFS

Page 6: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

DFS property(2)

a (= s)

bc d

e

fg

h

nr p 1 0

2 a

3 b4 e

5 e

6 g

7 g

8 a

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

Page 7: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

BFS (幅優先探索)

Process all the neighbors of the starting vertex. Then process allthe neighbors of neighbors of the starting vertex. And so on.

Page 8: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

a (= s)

bc d

e

fg

h

d p 0 0

0 0

0 00 0

0 0

0 0

0 0

0 0

BFS exampleQ= [ ] v w

a b c d e f g h

a b,c,d

b a,e,f

c a

d a,g

e b,f,g

f b,e

g d,e,h

h g

Av

1 a

1 a1 a

2 b2 b2 d

3 g

Page 9: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

BFS property (1)Each arc in the connected component of s is used exactly once in each direction during BFS

a (= s)

bc d

e

fg

h

d p 0 0

1 a

1 a1 a

2 b2 b2 d

3 g

Page 10: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

BFS property (2)

a (= s)

bc d

e

fg

h

d p 0 0

1 a

1 a1 a

2 b2 b

2 d

3 g

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

Page 11: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

BFS property (3)

a (= s)

bc d

e

fg

h

d p 0 0

1 a

1 a1 a

2 b2 b2 d

3 g

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.

Page 12: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

For digraphWe can apply DFS and BFS to digraphs.

a (= s)

bc d

e

fg

h

nr p 1 0

2 a

7 g3 b

6 d

5 a

8 g

4 a

a b,c,d

b f

c

d g

e b,f

f

g e,h

h

Av(DFS)

Page 13: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

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.

Page 14: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

DFS+

input Graph G and a vertex sbegin 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; if nr(w) = 0 then set p(w)←v, i←i+1, nr(w)←i, v←w; end set j←j+1, Nr(v)← j, v←p(v); until v=s and u(sw)=true for all w∈As;end.

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

Page 15: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

DFS+ example

1

2

3

4

5

6

Page 16: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

DFS+ propertyIf Nr(v) <Nr(x) for two vertices v, x

v

x

in different DFS trees

v

x

v is descendant of x

v

x

in the same tree, butv is not descendant nor ancestor of x

and x is accessible from v

v is accessible from x

Page 17: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

Algorithm for strongly connected component

input digraph Gbegin 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.

SCC

C1, C2, …,Ck are vertex sets of strongly connected components

Page 18: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

SCC example

1

23

4

5

6

Page 19: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

SCC correctnesstwo vertices v and w are in the same strong component

⇔two vertices v and w are in the same set Ci

( )⇒

  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

Page 20: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

SCC correctnesstwo vertices v and w are in the same strong component

⇔two vertices v and w are in the same set Ci

() ⇒

  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

Page 21: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

not contain a directed cycle

Acyclic (非閉路) digraph

Page 22: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

Acyclic graph example

undershorts

pants

belts

shirt

tie

jacket

shoes

socks 12

34

5

67

8

topological order :o

for any arc(v, w), o(v) < o(w)

Page 23: Graph -definition V : vertex set (頂点集合) A ⊆ V×V: arc set (枝集合) G=(V, A) discrete structure (離散構造) vertex (頂点) arc (枝)

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.

1

2

3 4 5

6

7

8

9