Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.

Similar presentations


Presentation on theme: "Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1."— Presentation transcript:

1 Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1

2 Outline Graph Adjacency Representation Topological Sort Minimum Spanning Tree – Kruskal’s Algorithm – Prim’s Algorithm Shortest Path – Dijkstra’ Algorithm 2

3 – Adjacency matrix G = (V, E) V = { A, B, C, D, E } E = { (A, C), (A, E), (B, D), (C, B), (D, C), (E, C), (E, D) } 3 A B C D E 00101 00010 01000 00100 00110 ABCDE ABCDE Graph - adjacency representation

4 – Degree of a vertex v number of edges incident on that vertex For directed graph, Degree = InDegree + OutDegree – InDegree of a vertex v sum of column v – OutDegree of a vertex v sum of row v 4 A B CD E For example, InDegree for C is 3, OutDegree for C is 1 A B C D E 00101 00010 01000 00100 00110 ABCDE Graph - adjacency representation

5 – Adjacency matrix G = (V, E) V = { A, B, C, D, E } E = { (A, C), (A, E), (B, D), (C, B), (D, C), (E, C), (E, D) } 5 A B C D E 00101 00110 11011 01101 10110 ABCDE ABCDE Graph - adjacency representation

6 – Adjacency list 6 A B C D E ABCDE CE D B C CD Graph - adjacency representation

7 Topological Sort A topological sort of a DAG (Directed Acyclic Graph) G is a linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering. 7

8 Topological Sort 8 V1V1 V2V2 V7V7 V3V3 V6V6 V4V4 V5V5

9 How to find the topological ordering? Define the indegree of a vertex v as the # of edges (u,v). We compute the indegrees of all vertices in the graph. Find any vertex with no incoming edges (or the indegree is 0). We print this vertex, and remove it, along with its edges, from the graph. Then we apply this same strategy to the rest of the graph. 9

10 10 V1V1 V2V2 V7V7 V3V3 V6V6 V4V4 V5V5 V2V2 V7V7 V3V3 V6V6 V4V4 V5V5 V7V7 V3V3 V6V6 V4V4 V5V5 V7V7 V3V3 V6V6 V4V4 V7V7 V3V3 V6V6 V7V7 V6V6 V6V6 Topological OrderV1V1 V2V2 V5V5 V4V4 V3V3 V7V7 V6V6

11 Course prerequisite in university studies – A directed edge (u,v) indicates that course u must be completed before course v may be attempted – A topological ordering of these courses is any course sequence that does not violate the prerequisite requirement. 11 Real Life Application

12 Notes Is topological ordering possible with a cyclic graph? – No, since for two vertices v and w on the cycle, v precedes w and w precedes v. Is the ordering unique? – It is not necessarily unique; any legal ordering will do (e.g., v1, v2, v5, v4, v3, v7, v6 and v1, v2, v5, v4, v7, v3, v6 are topological orderings). 12

13 Spanning Tree 13

14 Minimum Spanning Tree 14

15 Real Life Application One example would be a cable TV company laying cable to a new neighborhood. The graph represents which houses are connected by those cables. A spanning tree for this graph would be a subset of those paths that has no cycles but still connects to every house. There might be several spanning trees possible. A minimum spanning tree would be one with the lowest total cost. 15

16 MST Algorithm 16

17 Kruskal’s Algorithm 17 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 EdgeWeight 1 2 2 4 4 6 7 7 8 8 9 10 11 14 a bc h d gf ie 4 8 7 9 1 10 11 2 8 2 7 6 4 a bc h d gf ie 4 8 7 14 9 1 10 11 2 8 2 7 6 4 a bc h d gf ie 4 8 7 14 9 1 10 11 2 8 2 7 6 4 a bc h d gf ie 4 8 7 14 9 1 10 11 2 8 2 7 6 4 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78

18 Kruskal’s Algorithm 18 EdgeWeight 1 2 2 4 4 6 7 7 8 8 9 10 11 14 a bc h d gf ie 4 8 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 Algorithm will stop here since there are already (n-1) edges found.

19 19

20 Prim’s Algorithm 20 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4

21 Prim’s Algorithm 21 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4 78 a bc h d gf ie 4 8 14 9 1 10 11 2 2 7 6 4

22 22

23 MST on Non-planar Graph In graph theory, a non-planar graph cannot be drawn without edge intersections. 23

24 Prim’s Algorithm 24 a cb de 10 8 14 9 1 2 2 7 6 4 a cb de 10 8 14 9 1 2 2 7 6 4 a cb de 10 8 14 9 1 2 2 7 6 4 a cb de 10 8 14 9 1 2 2 7 6 4 a cb de 10 8 14 9 1 2 2 7 6 4 a cb de 10 8 14 9 1 2 2 7 6 4

25 Kruskal’s Algorithm 25 a cb de 10 8 14 9 1 2 2 7 6 4 How to apply Kruskal’s Algorithm in this non- planar graph to find the minimum spanning tree? Solve it yourself.

26 Shortest Path Problem 26

27 Real Life Application Travelling Salesman Problem It’s the problem of finding the shortest path that goes through every node exactly once, and returns to the start. 27

28 Dijkstra’s Algorithm 28 Dest The shortest path from V 0 to other vertices V1V1 Step 1 V2V2 V3V3 V4V4 VjVj Step 2Step 3Step 4Step 5 S V5V5 {V 0, V 2, V 3, V 4, V 5 } Inf 10 (V 0, V 2 ) Inf 60 (V 0, V 2, V 3 ) 50 (V 0, V 4, V 3 ) 30 (V 0, V 4 ) 30 (V 0, V 4 ) 100 (V 0, V 5 ) 100 (V 0, V 5 ) 90 (V 0, V 4, V 5 ) 60 (V 0, V 4, V 3, V 5 ) V2V4V3V5 {V 0, V 2, V 3, V 4 }{V 0, V 2, V 4 }{V 0, V 2 } V0V0 V5V5 V1V1 V2V2 V3V3 V4V4 20 60 100 50 30 10 5

29 29


Download ppt "Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1."

Similar presentations


Ads by Google