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

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph Algorithms. Recursive DFS DFS(v:Integer) visit and mark v; while there is an unmarked vertex w adjacent to v do DFS(w) endwhile end DFS (Global.
Chapter 23 Minimum Spanning Trees
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
IS 2610: Data Structures Graph April 5, 2004.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Chapter 2 Graph Algorithms.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
CC 215 Data Structures Graph Searching
CS120 Graphs.
Graph Algorithms Using Depth First Search
Graphs Chapter 13.
Graphs.
CS 583 Analysis of Algorithms
Chapter 11 Graphs.
Lecture 12 Algorithm Analysis
CSE 373 Data Structures Lecture 16
Autumn 2016 Lecture 10 Minimum Spanning Trees
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Lecture 12 Algorithm Analysis
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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

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

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 ? G=(V, E, W) weight

Why not local search ? Observation

F G A B C D E H 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}}

Dijkstra / Prim Algorithm D A C B E

V T = { A } V F = { C, B, D } CE = { { A, C }, { A, B }, { A, D } } D A C B E

V T = { A, C } V F = { B, D, E } CE = { { A, D }, { C, B }, { C, E } } D A C B E

V T = { A, B, C } V F = { D, E } CE = { { B, D }, { B, E } } 1 5 D A C B E

V T = { A, B, C, D } V F = { E } CE = { { D, E } } D A C B E

V T = { A, B, C, D, E } V F = Ø CE = Ø D A C B E

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 ?

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.

e e

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║)

Time complexity Kruskal Dijkstra & Prim

[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

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 )

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 (Adjacency lists are not shown. Nodes are assumed to be in alphabetical order within each list.)

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 G = (V, E) w(f)  2  f  E  {e}

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 ?

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.

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 !!!

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

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)

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

C H D B E G F A

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 C H D B E G F A 3 Depth First Search

Breadth First Search A C 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

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

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

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.

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

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 , 4 3 2