2018, Fall Pusan National University Ki-Joune Li

Slides:



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

Lecture 15. Graph Algorithms
CHAPTER 6 GRAPHS All the programs in this file are selected from
Chapter 6. Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river.
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
Chapter 8, Part I Graph Algorithms.
Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph.
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.
Graphs & Graph Algorithms 2
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS Data Structures Chapter 6 Graphs.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
IS 2610: Data Structures Graph April 5, 2004.
The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
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.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Chapter 22 Elementary Graph Algorithms
Data Structures 13th Week
Chapter 5 : Trees.
CSE 2331/5331 Topic 9: Basic Graph Alg.
12. Graphs and Trees 2 Summary
C.Eng 213 Data Structures Graphs Fall Section 3.
Introduction to Graphs
Unit 3 Graphs.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph Algorithms Using Depth First Search
Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.
Graph Algorithm.
Lecture 10 Algorithm Analysis
Connected Components Minimum Spanning Tree
Graph Algorithm.
Graphs & Graph Algorithms 2
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Elementary graph algorithms Chapter 22
CSE 373 Data Structures and Algorithms
Algorithms and Data Structures Lecture XII
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
2017, Fall Pusan National University Ki-Joune Li
Text Book: Introduction to algorithms By C L R S
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 16 1 – Graphs Graph Categories Strong Components
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Elementary graph algorithms Chapter 22
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
GRAPH – Definitions A graph G = (V, E) consists of
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 .
GRAPH TRAVERSAL.
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

2018, Fall Pusan National University Ki-Joune Li Graphs 2018, Fall Pusan National University Ki-Joune Li

Graph Definition Some Properties G = (V,E ) where V : a set of vertices and E = { <u ,v > | u ,v  V } : a set of edges Some Properties Equivalence of Graphs Tree a Graph Minimal Graph a b c d e a c b d e

Some Terms Directed Graph Complete Graph Adjacency G is a directed graph iff <u,v >  <v,u > for u  v  V Otherwise G is an undirected graph Complete Graph Suppose nv = number(V ) Undirected graph G is a complete graph if ne = nv (nv - 1) / 2, where ne is the number of edges Adjacency For a graph G=(V,E ) u is adjacent to v iff  e = <u,v > E , where u,v  V If G is undirected, v is adjacent to u otherwise v is adjacent from u

Some Terms Subgraph Path from u to v Connected G’ is a subgraph of a graph G iff V (G’ )  V (G ) and E (G’ )  E (G ) Path from u to v A sequence of vertices u=v0, v1, v2, … vn=v  V , such that <vi ,vi +1>  E for every vi Cycle iff u = v Connected Two vertices u and v are connected iff  a path from u to v Graph G is connected iff for every pair u and v there is a path from u to v Connected Components A connected subgraph of G

Some Terms Strongly Connected In-degree and Out-Degree An directed graph G is strongly connected iff iff for every pair u and v there is a path from u to v and path from v to u DAG: directed acyclic graph (DAG) In-degree and Out-Degree In-degree of v : number of edges coming to v Out-degree of v : number of edges going from v

Representation of Graphs: Matrix Adjacency Matrix A[i, j ] = 1, if there is an edge <vi ,vj > A[i, j ] = 0, otherwise Example Undirected Graph: Symmetric Matrix Space complexity: O (nv2 ) bits In-degree (out-degree) of a node 1 3 2 1

Representation of Graphs: List Adjacency List Each node has a list of adjacent nodes Space Complexity: O (nv + ne ) Inverse Adjacent List 1 2 3 1 3 2 1 2 1 3 2

Weighted Graph: Network For each edge <vi ,vj >, a weight is given Example: Road Network Adjacency Matrix A[i, j ] = wij, if there is an edge <vi ,vj > and wij is the weight A[i, j ] = , otherwise Adjacency List 1 3 2 1.5 1.0 2.3 1.2 1.9  1.5 2.3 1.2 1.0 1.9 1 2 3 1.5 2.3 1.2 1.0 1.9

Graph: Basic Operations Traversal Depth First Search (DFS) Breadth First Search (BFS) Used for search Example Find Yellow Node from 0 1 3 2 5 6 4 7

DFS: Depth First Search void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited; } 1 4 2 3 void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); } 5 6 7 Adjacency List: Time Complexity: O(e) Adjacency Matrix: Time Complexity: O(n2)

DFS: Depth First Search void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited; } 1 4 2 3 void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); } 5 6 7

DFS: Depth First Search void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited; } 1 4 2 3 void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); } 5 6 7

DFS: Depth First Search void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited; } 1 4 2 3 void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); } 5 6 7

DFS: Depth First Search void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited; } 1 4 2 3 void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); } 5 6 7

DFS: Depth First Search void Graph::DFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; DFS(0); delete[] visited; } 1 4 2 3 void Graph::DFS(int v) { visited[v]=TRUE; for each u adjacent to v { if(visited[u]==FALSE) DFS(u); } 5 6 7

BFS: Breadth First Search void Graph::BFS() { visited=new Boolean[n]; for(i=0;i<n;i++)visited[i]=FALSE; Queue *queue=new Queue; queue.insert(v); while(queue.empty()!=TRUE) { v=queue.delete() for every w adjacent to v) { if(visited[w]==FALSE) { queue.insert(w); visited[w]=TRUE; } delete[] visited; 1 4 2 3 5 6 7 Adjacency List: Time Complexity: O(e) Adjacency Matrix: Time Complexity: O(n2)

Spanning Tree A subgraph T of G = (V,E ) is a spanning tree of G iff T is tree and V (T )=V (G ) Finding Spanning Tree: Traversal DFS Spanning Tree BFS Spanning Tree 1 4 2 3 5 6 7 1 1 4 4 2 2 3 3 5 5 6 6 7 7

Articulation Point and Biconnected Components A vertex v of a graph G is an articulation point iff the deletion of v makes G two connected components Biconnected Graph Connected Graph without articulation point Biconnected Components of a graph 1 1 7 4 2 6 1 7 5 3 4 2 2 6 6 5 3

Finding Articulation Point: DFS Tree Back Edge and Cross Edge of DFS Spanning Tree Tree edge: edge of tree Back edge: edge to an ancestor Cross edge: neither tree edge nor back edge No cross edge for DFS spanning tree d a Back edge b f b g a c c d f h e h e Back edge g

Finding Articulation Point: DFS Tree b h d e f c g No cross edge for DFS tree d b f d a c b h a c Cross edge e g e Tree edge h g f

Finding Articulation Point Root is an articulation point if it has at least two children. u is an articulation point if  child of u without back edge to an ancestor of u d d b f Back edge b f a c a c h h e e g Back edge g

Finding Articulation Point by DFS Number DFS number of a vertex: dfn (v ) The visit number by DFS Low number: low (v ) low (v )= min{ dfn (v ), min{dfn (x )| (v, x ): back edge }, min{low (x )| x : child of v } } It means the highest link from v besides tree link v is an articulation Point If v is the root node with at least two children or If v has a child u such that low (u )  dfn (v ) v is the boss of subtree: if v dies, the subtree looses the line 1 d 2, 1 2 low(b) = min{4,1}=1 b 3 4 4,1 a c low(c) = min{4,1}=1 5 5, 1 e Back edge (e,d) where dfs(d)=1 low(e) = min{5,1}=1

Finding Articulation Point by DFS Number d b f 5 a c 2 5 h e 5 g node e has two paths; one along dfs path, and another via back edge.  not articulation point

Finding Articulation Point by DFS Number d b f 5 a c 2 5 h node c has two paths; one along dfs path, and another via a descendant node  not articulation point e 5 g

Finding Articulation Point by DFS Number d b f 5 a c 2 5 h node a has only one path; along dfs path  its parent node is an articulation point since it will be disconnected if its parent is deleted. (node a relies only on its parent.) e 5 g

Computation of dfs (v ) and low (v ) void Graph::DfnLow(x) { num=1; // num: class variable for(i=0;i<n;i++) dfn[i]=low[i]=0; DfnLow(x,-1);//x is the root node } void Graph::DfnLow(int u,v) { dfn[u]=low[u]=num++; for(each vertex w adjacent from u){ if(dfn[w]==0) // unvisited w DfnLow(w,u); low[w]=min(low[u],low[w]); else if(w!=v) low[u]=min(low[u],dfn[w]); //back edge } Adjacency List: Time Complexity: O(e) Adjacency Matrix: Time Complexity: O(n2)

Minimum Spanning Tree G is a weighted graph T is the MST of G iff T is a spanning tree of G and for any other spanning tree of G, C (T ) < C (T’ ) 1 3 2 5 6 4 7 8 12 7 5 1 7 10 12 3 4 4 2 6 8 2 6 15 5 3

Finding MST Greedy Algorithm Kruskal’s Algorithm and Prim’s Algorithm Choosing a next branch which looks like better than others. Not always the optimal solution Kruskal’s Algorithm and Prim’s Algorithm Two greedy algorithms to find MST Globally optimal solution Solution by greedy algorithm, only locally optimal Current state

Greedy Algorithm - Example Destination Point A B Starting Point

Kruskal’s Algorithm X X 7 5 10 1 7 12 3 T is MST 4 4 2 6 8 6 15 Algorithm KruskalMST Input: Graph G Output: MST T Begin T {}; while( n(T)<n-1 and G.E is not empty) { (v,w) smallest edge of G.E; G.E  G.E-{(v,w)}; if no cycle in {(v,w)}T, T {(v,w)}T } if(n(T)<n-1) cout<<“No MST”; End Algorithm 7 X 5 10 1 7 12 X 3 T is MST 4 4 2 6 8 6 15 Time Complexity: O(e loge) 2 5 3

Checking Cycles for Kruskal’s Algorithm 1 2 V 3 4 5 8 6 V1 V2 log n If v  V and w  V , then  cycle, otherwise no cycle w v

Prim’s Algorithm 7 5 10 1 7 12 3 T is the MST 4 4 2 6 8 6 15 Algorithm PrimMST Input: Graph G Output: MST T Begin Vnew {v0}; Enew {}; while( n(Enew)<n-1) { select v such that (u,v) is the smallest edge where u Vnew, v Vnew; if no such v, break; G.E  G.E-{(u,v)}; Enew {(u,v)} Enew; Vnew {v}  Vnew; } if(n(T)<n-1) cout<<“No MST”; End Algorithm 7 5 10 1 7 12 3 T is the MST 4 4 2 6 8 6 15 Time Complexity: O( n 2) 2 5 3

Finding the Edge with Min-Cost Step 1 Step 2 V T TV TV n 1 1 3 n - 1 3 2 2 n + (n-1) +… 1 = O( n2 )

Shortest Path Problem Shortest Path From 0 to all other vertices 1 3 2 1 3 2 5 6 4 7 8 10 15 13 12 11 9 vertex cost 1 5 2 6 3 10 4 8 7 13 11

Shortest Path Problem Shortest Path Step 1 1 3 2 5 6 4 7 vertex cost 1 TV Shortest Path Step 1 1 3 2 5 6 4 7 8 10 15 13 12 11 9 V-TV vertex cost 1 5 2 8 3 10 4 ? 6 7

Shortest Path Problem Shortest Path Step 1 1 3 2 5 6 4 7 vertex cost 1 TV Shortest Path Step 1 1 3 2 5 6 4 7 8 10 15 13 12 11 9 V-TV vertex cost 1 5 2 8 3 10 4 ? 6 7

Shortest Path Problem Shortest Path 5 + 1 ? 8 5 + 3 ? ∞ 5 + 2 ? ∞ TV Shortest Path Step 2 1 3 2 5 6 4 7 8 10 15 13 12 11 9 V-TV vertex cost 1 5 2 86 3 10 4 ?8 ?7 6 ? 7 5 + 1 ? 8 5 + 3 ? ∞ 5 + 2 ? ∞

Shortest Path Problem Shortest Path Step 2 1 3 2 5 6 4 7 vertex cost 1 TV Shortest Path Step 2 1 3 2 5 6 4 7 8 10 15 13 12 11 9 V-TV vertex cost 1 5 2 6 3 10 4 8 7 ?

Shortest Path Problem Shortest Path 6 + 15 ? 10 6 + 6 ? 7 Step 2 1 3 2 TV Shortest Path Step 2 1 3 2 5 6 4 7 8 10 15 13 12 11 9 vertex cost 1 5 2 6 3 10 4 8 7 ? 6 + 15 ? 10 6 + 6 ? 7 V-TV

Shortest Path Problem Shortest Path Step 2 1 3 2 5 6 4 7 vertex cost 1 TV Shortest Path Step 2 1 3 2 5 6 4 7 8 10 15 13 12 11 9 vertex cost 1 5 2 6 3 10 4 8 7 ? V-TV

Finding Shortest Path from Single Source (Nonnegative Weight) 1 2 3 4 10 7 5 6 Algorithm DijkstraShortestPath(G) /* G=(V,E) */ output: Shortest Path Length Array D[n] Begin S {v0}; D[v0]0; for each v in V-{v0}, do D[v]  c(v0,v); while S  V do begin choose a vertex w in V-S such that D[w] is minimum; add w to S; for each v in V-S do D[v]  min(D[v],D[w]+c(w,v)); end End Algorithm w Distance via new node v u S O( n2 )

Finding Shortest Path from Single Source (Nonnegative Weight) 1 2 3 4 9 5 ∞ 1 2 3 4 10 7 5 6 1 2 3 4 10 ∞ 1: {v0} 2: {v0, v1} 9 1 2 3 4 5 1 2 3 4 9 5 1 2 3 4 9 5 4: {v0, v1, v2, v4} 5: {v0, v1, v2, v3, v4} 3: {v0, v1, v2}

Transitive Closure – Warshall’s Algorithm Set of reachable nodes 1 1 1 2 2 2 4 4 4 3 3 3 Am(i,j) = Am-1(i,j) or = Am-1(i,k) and Am-1(k,j)

Transitive Closure – Warshall’s Algorithm Void Graph::TransitiveClosure { for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]=c[i][j]; for(int k=0;k<n;k++) for (int j=0;j<n;j++) a[i][j]= a[i][j]||(a[i][k] && a[k][j]); } O ( n3 ) am(i,j) = am-1(i,j) or = am-1(i,k) and am-1(k,j) A2 A (A10A03)(A11 A13)… (A14A43) 1 1 1 Path of length 1 Path of length 2

Transitive Closure – A Variation All Pairs Shortest Path Void Graph::AllPairsShortestPath { for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]=c[i][j]; for(int k=0;k<n;k++) a[i][j]= min(a[i][j],(a[i][k]+a[k][j])); } O ( n3 )

Activity Networks

AOV – Activity-on-vertex Node 1 is a immediate successor of Node 0 Node 5 is a immediate successor of Node 1 1 4 Node 0  Node 1 2 Node 1  Node 5 3 5 Node 5 is a successor of Node 0 6 7 Node 0 is a predecessor of Node 5 Node 0  Node 5 Partial order: for some pairs (v, w) (v, w  V ), v  w (but not for all pairs) (cf. Total Order: for every pair (v, w) (v, w  V ), v  w or w  v ) No Cycle Transitive and Irreflexive

Topological Order Ordering: (0, 1, 2, 3, 4, 5, 7, 6) Ordering: (0, 1, 2, 3, 4, 5, 7, 6) Ordering: (0, 1, 4, 2, 3, 5, 7, 6) 1 4 2 Both orderings satisfy the partial order 3 5 Topological order: Ordering by partial order 6 7 Ordering: (0, 1, 2, 5, 4, 3, 7, 6): Not a topological order

Topological Ordering 1 1 4 4 2 2 3 5 3 5 6 7 6 7 0, 1 0, 1, 4 0, 1, 4, 2 4 2 2 3 3 3 5 5 5 6 6 6 7 7 7

Topological Ordering O ( n + e ) Void Topological_Ordering_AOV(Digraph G) for each node v in G.V { if v has no predecessor { cout << v; delete v from G.V; deleve (v,w) from G.E; } if G.V is not empty, cout <<“Cycle found\n”; O ( n + e )

AOE – Activity-on-edge PERT (Project Evaluation and Review Technique Node 2 is completed only if every predecessor is completed Required Time: the LONGEST PATH from node 0  Required time of node 1: 5  Required time of node 2: 8  Required time of node 4: 8  Required time of node 5: max(8+11, 5+2, 8+6)=19  Required time of node 7: max(8+9, 19+4)=23  Required time of node 6: max(23+12, 19+6)=35 5 3 1 4 1 8 2 11 2 6 9 5 4 6 7 6 12 5 3 1 4 1 8 2 11 2 6 9 5 (0, 1, 4, 5, 7, 6) : Critical Path By reducing the length on the critical path, we can reduce the length of total path. 4 6 7 6 12