Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST."— Presentation transcript:

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

2 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

3 21NIL413132 42 43 1. Definitions and Representations Representations –Adjacency Matrix –Adjacency Lists –Adjacency Multi-lists 2 1 4 3 1 2 3 4 Vertex

4 2. Min-Cost Spanning Tree Greedy (1) Sort E in the non-decreasing order of weights O(eloge) n 2 logn 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 ? 3 1 2 1 1 4 2 3 1 1 6 2 G=(V, E, W) weight

5 Why not local search ? Observation 3 1 15 20 8 2 9

6 F G A B C D E H 1 2 2 1 2 2 1 5 2 4 1 3 5 G = (V, E, W) tree vertices = {A, B, C} = V T fringe vertices = {E,F,D} = V F unseen vertices = {G, H} = V U CE = {{A,D},{B,E},{C,F}}

7 Dijkstra / Prim Algorithm D A C B E 1 4 2 3 1 5 2 7

8 V T = { A } V F = { C, B, D } CE = { { A, C }, { A, B }, { A, D } } 4 3 1 D A C B E 1 4 2 3 1 5 2 7

9 V T = { A, C } V F = { B, D, E } CE = { { A, D }, { C, B }, { C, E } } 2 7 3 D A C B E 1 4 2 3 1 5 2 7

10 V T = { A, B, C } V F = { D, E } CE = { { B, D }, { B, E } } 1 5 D A C B E 1 4 2 3 1 5 2 7

11 V T = { A, B, C, D } V F = { E } CE = { { D, E } } D A C B E 1 4 2 3 1 5 2 7

12 V T = { A, B, C, D, E } V F = Ø CE = Ø D A C B E 1 4 2 3 1 5 2 7

13 SI { Select an arbitrary vertex to start the tree } v T = Ø, v F = Ø, CE = Ø ; v T = v T  {v} ; Update v F and CE ; while v F  Ø do ; e : = min - weight edge in CE ; Update v T, v F, CE ; end {while} No sorting !!! Why this algorithm works ?

14 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, E T ) 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}  E T. [Proof] x y v w T' = (V', E' ) T'  T T= (V, E T ) i) {x, y}  E T trivial ii) {x, y}  E T See the figure.

15 e e

16 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 ; E T := Ø; stuck := false ; V T :={x}; 2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. } while V T  V and not stuck do 3.{ 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 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 vertices and edges. (candidate) Choose new edges. O(n 2 ) Initialize O(1) O(║E║)

17 Time complexity Kruskal Dijkstra & Prim

18 [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

19 Algorithm : Minimum spanning tree(Dijkstra/Prim) 1. { Initialization } Let x be an arbitrary vertex ; E T := Ø ; stuck := false ; V T :={x} ; 2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. } while V T  V and not stuck do 3. { 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} ; 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 } O(1) O(|E|) O(n 2 )  O(|E|+n 2 )

20 2 7 3 3 fringeLink parent adjacencyList fringeWgt status Shaded entries in fringeLink are no longer in use. fringeList = I. [ Minimum spanning tree data structure ] A F I G H E D A G H B I F C B C 2 3 4 7 1 3 6 5 1 6 4 2 2 1 2 4 8 (Adjacency lists are not shown. Nodes are assumed to be in alphabetical order within each list.)

21 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) = 1Contradiction ! why ?   (|E|) 3 2 e 12 14 4 5 9 15 10 11 12 7 G = (V, E) w(f)  2  f  E  {e}

22 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 ?

23

24 Dijkstra’s Algorithm Step 0 { initialization } v 1 := 0 v j := w 1j, j = 2, 3, 4,···, n p := {1}, T := {2, 3, 4,···, n} Step 1 { Designation of permanent of label } find k such that v k = { v j } T := T \{k} P := P  {k} if T = Ø, stop ( if k = d, stop ) Step 2 { Revision of Tentative label } v j := min{ v j, v k + w kj )  j  T go to step 1.

25 3 4 2 1 1 3 2 2 s  1 d  4 w 12 = 1, w 23 = 3, w 24 = 2, w 34 =2 v 1 := 0, v 2 = 1, v 3 = , v 4 =  P := {1}, T := {2, 3, 4} min{v j } = min{v 2, v 3, v 4 }  k=2 Is k=4 ? No !!! T := {2,3,4}/{2} = {3,4} P := P  {2} = {1,2} v 3 = min{v 3, v 2 +w 23 } = min{ , 1+3} = 4 v 4 = min{v 4, v 2 +w 24 } = min{ , 1+2} = 3 min{v j } = min{v 3, v 4 } = min{4, 3} = v 4  k = 4 j  T 1   j  T T := {3,4}/{4} = {3} P := P  {4} = {1,2,4} Is k=4 ? Yes !!!

26 Why does Dijkstra’s Algorithm Work ? s e mi n x y x'x' y'y'

27 4. Traversing Graphs and Digraphs 8 Queen Problem (5,4) (5,8) (5,2) (5,4) (5,3)(5,4) (7,6) (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)

28 A B C C 1 1 1 2 1 2 A B C 1 2 1 C 1 2 1 A B C 1 C 1 1 2 1 2 Depth First Search Breadth First Search

29 C H D B E G F A

30 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 D 6 5 7 3 2 1 4 5 6 1 4 7 2 C H D B E G F A 3 Depth First Search

31 2 6 7 5 1 3 4 Breadth First Search A C 7 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 H D B E G F A D E G H F B

32 Depth First Search undirected graphs Tree edges Back edges (no other types)

33 E H I F G C B A Depth First Search Tree ( Directed Graph ) A G I H C B F E 7 1 2 3 4 5 6 Tree edges Back edges Cross edges Descendant edges (forward) why not in undirected graph? Note : (1) top down (2) left right

34 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.

35 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

36 B D E A C B D E A C A B C B C A C A B D E D E G=(V,E)Adjaciency Lists DSF Tree back edge tree edge C C 1 2 3 4 1 2, 4 3 2


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

Similar presentations


Ads by Google