Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

36
Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST

description

21NIL Definitions and Representations Representations –Adjacency Matrix –Adjacency Lists –Adjacency Multi-lists Vertex

Transcript of Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Page 1: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Graph and Digraph

Sung Yong ShinTC Lab.

CS Dept., KAIST

Page 2: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Contents

1. Definitions and Representations

2. A Minimum Spanning Tree Algorithm

3. A Shortest-Path Algorithm

4. Traversing Graphs and Digraphs

5. Biconnected Components of a Graph

6. Strongly Connected Components of a Digraph

Page 3: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

21

NIL41

31

32

NIL42

NIL43 NIL

1. Definitions and Representations

• Representations– Adjacency Matrix– Adjacency Lists– Adjacency Multi-lists

2

1

4

3

1

234

Vertex

Page 4: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

2. Min-Cost Spanning Tree

• Greedy (1) Sort E in the non-decreasing order of weights

O(eloge) n2logn Why? (2) Choose n–1 edges from the sorted list of E as long as the current selected edge does not form a cycle with the previously-chosen edges

O(e + eG(n)) Kruskal’s algorithm !!! Any Better way ?

31

2

1

1

4

2

3

11

62

G=(V, E, W)

weight

Page 5: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Why not local search ?

Observation

31 15

2082

9

Page 6: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

F

G

A

BC

D

E

H

12

2

12

2

1 5

24

13

5

G = (V, E, W)

tree vertices = {A, B, C} = VT

fringe vertices = {E,F,D} = VF

unseen vertices = {G, H} = VU CE = {{A,D},{B,E},{C,F}}

Page 7: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Dijkstra / Prim Algorithm

D

A C

B

E

1

4 2

3

1 5

2

7

Page 8: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

VT = { A }

VF = { C, B, D }

CE = { { A, C }, { A, B }, { A, D } }

4 31

D

A C

B

E

1

4 2

3

1 5

2

7

Page 9: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

VT = { A, C }

VF = { B, D, E }

CE = { { A, D }, { C, B }, { C, E } }

2 73

D

A C

B

E

1

4 2

3

1 5

2

7

Page 10: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

VT = { A, B, C }

VF = { D, E }

CE = { { B, D }, { B, E } } 1 5

D

A C

B

E

1

4 2

3

1 5

2

7

Page 11: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

VT = { A, B, C, D }

VF = { E }

CE = { { D, E } }

D

A C

B

E

1

4 2

3

1 5

2

7

Page 12: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

VT = { A, B, C, D, E }

VF = Ø

CE = Ø

D

A C

B

E

1

4 2

3

1 5

2

7

Page 13: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

SI { Select an arbitrary vertex to start the tree } vT = Ø, vF = Ø, CE = Ø ; vT = vT {v} ; Update vF and CE ;

while vF Ø do ; e : = min - weight edge in CE ; Update vT, vF, CE ; end {while}

No sorting !!! Why this algorithm works ?

Page 14: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Lemma : Let G = ( V, E, W ) be a connected weighted graph and E' E be a subset of the edges in some minimum spanning tree T = ( V, ET ) for G. Let V' be the vertices incident with edges in E'. If { x, y } is an edge of minimum weight such that x V' and y V', then E' {x, y} ET.

[Proof]

x

y

vw

T' = (V', E' )T' T

T= (V, ET)

i) {x, y} ET trivialii) {x, y} ET

See the figure.

Page 15: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

x

y

y

y

y

e

e

Page 16: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Algorithm : Minimum spanning tree(Dijkstra/Prim)Input : G = (V,E,W), a weighted graph.Output : The edges in a minimum spanning tree.1. { Initialization } Let x be an arbitrary vertex ; ET:= Ø; stuck := false ; VT:={x};

2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. } while VT V and not stuck do3. { Replace some candidate edges. }

for each fringe vertex y adjacent to x doif W(xy) < W(the candidate edge e incident with y) then

xy replaces e as the candidate edge for y ;end { if }

end{for} 4. { Find new fringe vertices and candidate edges. }

for each unseen y adjacent to x doy is now a fringe vertex and xy is a candidate ;

end { for } ;5. { Ready to choose next edge. }

if there are no candidates then stuck := true { no spanning tree} ;else

6. { Choose next edge. }Find a candidate edge e, with minimum weight ;x := the fringe vertex incident with e.Add x and e to the tree.{ x and e are no longer fringe and candidate. }

end { if }end { while }

Update candidate edges

Add new fringe verticesand edges. (candidate)

Choose new edges.O(n2)

InitializeO(1)

O(║E║)

Page 17: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Time complexity

Kruskal Dijkstra & Prim

))(log( neGeeO

)neO 2(

n)neO log(

Page 18: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

[proof] ( By induction on # of edges chosen )

( m = 1 )

( m = k ) Assume that all the edges so far selected are in a min-cost spanning tree for G.

( m = k+1) By the previous Lemma, the result follows.

Theorem : Dijkstra/Prim Algorithm correctly constructs a min-cost spanning tree.

min-cost edge

Page 19: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Algorithm : Minimum spanning tree(Dijkstra/Prim)

1. { Initialization } Let x be an arbitrary vertex ; ET:= Ø ; stuck := false ; VT:={x} ; 2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. } while VT V and not stuck do3. { Replace some candidate edges. } for each fringe vertex y adjacent to x do if W(xy) < W(the candidate edge e incident with y) then xy replaces e as the candidate edge for y ; end { if } end{for} 4. { Find new fringe vertices and candidate edges. } for each unseen y adjacent to x do y is now a fringe vertex ; xy is now a candidate ; end { for } ; 5. { Ready to choose next edge. } if there are no candidates then stuck := true { no spanning tree} ; else6. { Choose next edge. } Find a candidate edge e, with minimum weight ; x := the fringe vertex incident with e. Add x and e to the tree. { x and e are no longer fringe and candidate. } end { if } end { while }

O(1)

O(|E|)

O(n2)

O(|E|+n2)

Page 20: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

2

73

3A intree B nil 2 A intree C F 4 B fringe D unseen E unseen F nil 7 A fringe G F 3 A intree H C 3 G fringe I H 1 G fringe

fringeLink parent adjacencyList fringeWgt status

Shaded entries in fringeLink areno longer in use. fringeList = I.

[ Minimum spanning tree data structure ]

A

FI

G

H

E D

A

GH

B

I

F

C

B

C

2

3

4

7

1

3

6

5 16 4

2

2

1

24

8

(Adjacency lists are not shown. Nodes are assumed to be in alphabetical order within each list.)

Page 21: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Lower Bound (|E|) Why ? (1) a trivial lower bound O(|V| + |E|) = O(|E|) (2) adversary argument

Every edge is required to be examined at least once. Why ? Suppose that some edge e is not examined at all. (see below)

e must not be contained in a min-cost spanning tree. why ? Now, w(e) = 1 Contradiction ! why ? (|E|)

3 2e12

144

5 9

1510

1112 7

G = (V, E)w(f) 2 fE{e}

Page 22: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

3. A shortest path algorithm

P : Given G = (V, E, W) and s,d V, find a shortest path between s and d.

Does the Dijkstra/Prim algorithm also work for solving the shortest path finding problem? No !!!

Why ?

Page 23: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.
Page 24: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Dijkstra’s Algorithm

minTj

Step 0 { initialization } v1 := 0 vj := w1j, j = 2, 3, 4,···, n p := {1}, T := {2, 3, 4,···, n} Step 1 { Designation of permanent of label } find k such that vk = { vj } T := T \{k} P := P {k} if T = Ø , stop ( if k = d, stop ) Step 2 { Revision of Tentative label } vj := min{ vj, vk + wkj ) j T go to step 1.

Page 25: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

3

421 1

3

22

s 1 d 4w12 = 1, w23 = 3, w24 = 2, w34 =2

v1 := 0, v2 = 1, v3 = , v4 = P := {1}, T := {2, 3, 4}min{vj} = min{v2, v3, v4}

k=2 Is k=4 ? No !!!

T := {2,3,4}/{2} = {3,4}P := P {2} = {1,2}v3 = min{v3, v2+w23} = min{, 1+3} = 4v4 = min{v4, v2+w24} = min{, 1+2} = 3min{vj} = min{v3, v4} = min{4, 3} = v4

k = 4

jT 1

jT

T := {3,4}/{4} = {3}P := P {4} = {1,2,4}

Is k=4 ? Yes !!!

Page 26: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Why does Dijkstra’s Algorithm Work ?

s

emin

xy

x'

y'

Page 27: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

4. Traversing Graphs and Digraphs

8 Queen Problem

(5,4) (5,8) (5,2) (5,4) (5,3) (5,4)

(7,6)

(6,4)(6,4)

(7,6)

(4,8)(4,7)(4,2)

(3,5) (3,6)

(2,7)(2,6)(2,5)(2,4)(2,3) (2,8)

(1,1) (1,2) (1,3)(1,4) (1,5) (1,6) (1,7) (1,8)

start

Recursion

Depth First Search

Q

Q

Q

Q

Q

· · · · ···· · · ··

· · ·· ·

· (3,7) (3,8)

Page 28: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

A

B C

C

1

1 1 2

1 2

AB

C12

1C

12

1

ABC1

C112

12

Depth First Search Breadth First Search

Page 29: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

C

HDB E G

FA

Page 30: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

DFS tree

procedure DFS (AdjList : HeaderList; v : VertexType) var s: Stack w: VertexType begin s:= Ø visit, mark, and stack v; while s!= Ø do while there are unmarked vertex w adjacent to Top(s) do visit, mark, and stack w end{while}; pop s; end{outer while} end{DFS}

H

B

G

E

F

A

C D6

5

7

321

4

561

4 72

C

HDB E G

FA

3

Depth First Search

Page 31: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

26

7

51

34

Breadth First Search

A

C7

1 2

3 4 5 6

BFS tree

procedure BFS(AdjList : HeaderList; v : VertexType) var Q: Queue w: VertexType begin Q:= Ø visit and mark v; insert v in Q; while Q Ø do x := Front(Q); for each unmarked vertex w adjacent to x do visit and mark w; insert w in Q; end{for}; end{while} end{BFS}

C

HDB E G

FA

D E G

H

FB

Page 32: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Depth First Search

undirected graphs

Tree edges Back edges (no other types)

Page 33: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

E

HIF G C

BA

Depth First Search Tree ( Directed Graph )

A

G I

H

C

B F

E

71

2

3

45

6

Tree edgesBack edgesCross edgesDescendant edges (forward)

why not in undirected graph?

Note : (1) top down (2) left right

Page 34: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

• How to process vertices– first visited ①– back up to a vertex ②– back up from a vertex ③

• How to process edges– tree edges– back edges– descendant edges– cross edges

A Generalized Depth-first Search Skeleton

v① ③

Depending on applications, we have to take proper action for each of cases.

Page 35: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

Input: G=(V,E), a graph or digraph represented by the adjacency list structure described with V={1,2,…,n} var mark : array[VertexType] of integer; markValue : integer; procedure DFS(v: VertexType); {Does a depth-first search beginning at the vertex v, marking the vertices with markValue.} var w: VertexType; ptr: NodePointer; begin {process vertex when first encountered (like preorder).} mark[v] := markValue; ptr := adjacencyList[v]; while ptr nil do w := ptr.vertex {Processing for every edge. (If G is undirected, each edge is encountered twice; an algorithm may have to distinguish the two encounters.)} if mark[w] = 0 {unmarked} then {Processing for tree edges, vw.}vertex link DFS(w); {Processing when backing up to v (like inorder) } else {Processing for nontree edges. (If G is undirected, an algorithm may have to distinguish the case where w is the parent fo v.) } end{if} ptr := ptr.link end{while} {Processing when backing up from v (like postorder) } end{DFS}

Algorithm : General Depth-first Search Skeleton

Page 36: Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.

B

D E

A

C

B

D E

A

C

AB

C

BC

A

CA

B

D

E

D

E

G=(V,E) Adjaciency Lists DSF Tree

back edge

tree edge

C

C

1

2

3

4

1

2, 43

2