Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs 2015, Fall Pusan National University Ki-Joune Li.

Similar presentations


Presentation on theme: "Graphs 2015, Fall Pusan National University Ki-Joune Li."— Presentation transcript:

1 Graphs 2015, Fall Pusan National University Ki-Joune Li

2 STEMPNU 2 Graph Definition  G = (V,E ) where V : a set of vertices and E = { | 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

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

4 STEMPNU 4 Some Terms Subgraph  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=v 0, v 1, v 2, … v n =v  V, such that  E for every v i  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

5 STEMPNU 5 Some Terms Strongly Connected  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

6 STEMPNU 6 Representation of Graphs: Matrix Adjacency Matrix  A[i, j ] = 1, if there is an edge A[i, j ] = 0, otherwise  Example  Undirected Graph: Symmetric Matrix  Space complexity: O (n v 2 ) bits  In-degree (out-degree) of a node 0 1 3 2 0111 0010 0001 0000

7 STEMPNU 7 Representation of Graphs: List Adjacency List  Each node has a list of adjacent nodes  Space Complexity: O (n v + n e )  Inverse Adjacent List 0 1 3 2 0123 12 23 3 0 0 1 2 10 20 3

8 STEMPNU 8 Weighted Graph: Network For each edge, a weight is given  Example: Road Network  Adjacency Matrix A[i, j ] = w ij, if there is an edge and w ij is the weight A[i, j ] = , otherwise  Adjacency List 0 1 3 2 1.5 1.0 2.31.2 1.9  1.52.31.2  1.0   1.9  01 1 2 3 1.522.331.2 21.0 31.9

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

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

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

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

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

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

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

16 STEMPNU 16 BFS: Breadth First Search 0 1 3 2 5 6 4 7 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; } 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; } Adjacency List: Time Complexity: O(e) Adjacency Matrix: Time Complexity: O(n 2 )

17 STEMPNU 17 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 0 1 3 2 5 6 4 7 0 1 3 2 5 6 4 7 0 1 3 2 5 6 4 7

18 STEMPNU 18 Articulation Point  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 Articulation Point and Biconnected Components 0 1 3 2 5 64 7 0 1 3 6 7 1 2 5 426

19 STEMPNU 19 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 a b h d e fc g a b h d e f c g Back edge

20 STEMPNU 20 Finding Articulation Point 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 a b h d e f c g Back edge

21 STEMPNU 21 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 } } 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

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

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

24 STEMPNU Finding Articulation Point by DFS Number 24 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.) a b h d e f c g 0 0 0 2 0 5 5 5

25 STEMPNU 25 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(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 } 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(n 2 )

26 STEMPNU 26 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’ ) 0 1 3 2 5 64 7 7 5 10 2 4 6 3 8 12 15 0 1 3 2 5 64 7 5 2 4 6 3 8 12

27 STEMPNU 27 Finding MST Greedy 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 Current state Solution by greedy algorithm, only locally optimal Globally optimal solution

28 STEMPNU 28 Kruskal’s Algorithm 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 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 0 1 3 2 5 64 7 7 5 10 2 4 6 3 8 12 15 X X T is MST Time Complexity: O(e loge)

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

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

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

32 STEMPNU 32 Shortest Path Problem Shortest Path  From 0 to all other vertices vertexcost 15 26 310 48 57 613 711 0 1 3 2 5 6 4 7 5 8 10 6 15 13 12 11 9 3 2 4 6 1

33 STEMPNU 33 Shortest Path Problem Shortest Path  Step 1 vertexcost 15 28 310 4? 5? 6? 7? 0 1 3 2 5 6 4 7 5 8 6 15 13 12 11 9 3 2 4 6 1 TV V-TV

34 STEMPNU 34 Shortest Path Problem Shortest Path  Step 1 vertexcost 15 28 310 4? 5? 6? 7? 0 1 3 2 5 6 4 7 5 8 6 15 13 12 11 9 3 2 4 6 1 TV V-TV

35 STEMPNU 35 Shortest Path Problem Shortest Path  Step 2 vertexcost 15 28686 310 4?8?8 5?7?7 6? 7? 0 1 3 2 5 6 4 7 5 8 6 15 13 12 11 9 3 2 4 6 1 5 + 1 ? 8 5 + 3 ? ∞ TV V-TV 5 + 2 ? ∞

36 STEMPNU 36 Shortest Path Problem Shortest Path  Step 2 vertexcost 15 28686 310 4?8?8 5?7?7 6? 7? 0 1 3 2 5 6 4 7 5 8 6 15 13 12 11 9 3 2 4 6 1 5 + 1 ? 8 5 + 3 ? ∞ TV V-TV 5 + 2 ? ∞

37 STEMPNU 37 Shortest Path Problem Shortest Path  Step 2 vertexcost 15 28686 310 4?8?8 5?7?7 6? 7? 0 1 3 2 5 6 4 7 5 8 6 15 13 12 11 9 3 2 4 6 1 6 + 15 ? ∞ TV V-TV 6 + 6 ? ∞

38 STEMPNU 38 Finding Shortest Path from Single Source (Nonnegative Weight) Algorithm DijkstraShortestPath(G) /* G=(V,E) */ output: Shortest Path Length Array D[n] Begin S  {v 0 }; D[v 0 ]  0; for each v in V-{v 0 }, do D[v]  c(v 0,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 Algorithm DijkstraShortestPath(G) /* G=(V,E) */ output: Shortest Path Length Array D[n] Begin S  {v 0 }; D[v 0 ]  0; for each v in V-{v 0 }, do D[v]  c(v 0,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 01 2 3 4 2 10 3 7 5 4 6 w uv S O( n 2 )

39 STEMPNU 39 Finding Shortest Path from Single Source (Nonnegative Weight) 0 1 2 3 4 2 10 ∞ ∞ 1: {v 0 } 01 2 3 4 2 9 5 ∞ 2: {v 0, v 1 } 01 2 3 4 2 9 5 9 3: {v 0, v 1, v 2 } 9 01 2 3 4 2 5 9 4: {v 0, v 1, v 2, v 4 } 01 2 34 2 9 5 9 5: {v 0, v 1, v 2, v 3, v 4 } 01 2 3 4 2 10 3 7 5 4 6

40 STEMPNU 40 Transitive Closure 01 2 3 4 00100 10000 01000 00100 00010 A 11100 11100 11100 11100 11110 A+A+ A*A* 01 2 3 4 11100 11100 11100 11110 11111 01 2 3 4  Set of reachable nodes

41 STEMPNU 41 Transitive Closure 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 i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= a[i][j]||(a[i][k] && a[k][j]); } 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 i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= a[i][j]||(a[i][k] && a[k][j]); } O ( n 3 ) 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++) for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= min(a[i][j],(a[i][k]+a[k][j])); } 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++) for(int i=0;i<n;i++) for (int j=0;i<n;j++) a[i][j]= min(a[i][j],(a[i][k]+a[k][j])); } O ( n 3 )

42 STEMPNU Activity Networks 42

43 STEMPNU AOV – Activity-on-vertex 43 0 1 3 2 5 6 4 7 Node 1 is a immediate successor of Node 0 Node 5 is a immediate successor of Node 1 Node 5 is a successor of Node 0 Node 0 is a predecessor of 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 ) Node 0  Node 1 Node 1  Node 5 Node 0  Node 5 No Cycle Transitive and Irreflexive

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

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

46 STEMPNU Topological Ordering 46 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”; } 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 )

47 STEMPNU AOE – Activity-on-edge PERT (Project Evaluation and Review Technique 47 0 1 2 5 6 4 7 5 8 6 12 11 9 3 2 4 6 1 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 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 (0, 1, 4, 5, 7, 6) : Critical Path By reducing the length on the critical path, we can reduce the length of total path. (0, 1, 4, 5, 7, 6) : Critical Path By reducing the length on the critical path, we can reduce the length of total path. 0 1 2 5 6 4 7 5 8 6 12 11 9 3 2 4 6 1


Download ppt "Graphs 2015, Fall Pusan National University Ki-Joune Li."

Similar presentations


Ads by Google