Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI2100 Data Structures Tutorial 12

Similar presentations


Presentation on theme: "CSCI2100 Data Structures Tutorial 12"— Presentation transcript:

1 CSCI2100 Data Structures Tutorial 12
Graph Revisited Yang Zhengwei CSCI2100 Data Structures Tutorial 12 1 1

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

3 Graph - adjacency representation
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) } B A B C D E A 1 1 C D B 1 C 1 D 1 E 1 1 A E 3

4 Graph - adjacency representation
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 A B C D E A B C D E A 1 1 B 1 C 1 D 1 E 1 1 For example, InDegree for C is 3, OutDegree for C is 1 4

5 Graph - adjacency representation
B C D E 1 5

6 Graph - adjacency representation
Adjacency list B A C E B D C D C B D C E C D A E 6

7 Graph - adjacency representation
Adjacency list 7

8 Graph - Complexity 8

9 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. u v 9 9

10 Topological Sort Topological Order: V1 V2 V5 V4 V3 V7 V6 V1 V2 V3 V4
10 10

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

12 Topological Order V1 V2 V5 V4 V3 V7 V6 V1 V2 V2 V3 V4 V5 V3 V4 V5 V6
12 12

13

14 Real Life Application 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. 14 14

15 Spanning Tree 15 15

16 Minimum Spanning Tree 16 16

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

18 MST Algorithm 18 18

19 Kruskal’s Algorithm a b c h d g f i e a b c h d g f i e a b c h d g f
7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 Edge Weight <h, g> 1 <c, i> 2 <g, f> <a, b> 4 <c, f> <g, i> 6 <c, d> 7 <h, i> <a, h> 8 <b, c> <d, e> 9 <e, f> 10 <b, h> 11 <d, f> 14 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 19 19

20 Algorithm will stop here since there are already (n-1) edges found.
Kruskal’s Algorithm a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 Edge Weight <h, g> 1 <c, i> 2 <g, f> <a, b> 4 <c, f> <g, i> 6 <c, d> 7 <h, i> <a, h> 8 <b, c> <d, e> 9 <e, f> 10 <b, h> 11 <d, f> 14 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 Algorithm will stop here since there are already (n-1) edges found. 20 20

21 21 21

22 Prim’s Algorithm a b c h d g f i e a b c h d g f i e a b c h d g f i e
7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 22 22

23 Prim’s Algorithm a b c h d g f i e a b c h d g f i e a b c h d g f i e
7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 23 23

24 24 24

25 Shortest Path Problem 25 25

26 The shortest path from V0 to other vertices
Dijkstra’s Algorithm V5 60 100 V0 V4 30 10 20 10 V1 V3 50 5 V2 Dest The shortest path from V0 to other vertices Step 1 Step 2 Step 3 Step 4 Step 5 V1 Inf Inf Inf Inf Inf V2 10 (V0, V2) V3 Inf 60 (V0, V2, V3) 50 (V0, V4, V3) V4 30 (V0, V4) 30 (V0, V4) V5 100 (V0, V5) 100 (V0, V5) 90 (V0, V4, V5) 60 (V0, V4, V3, V5) Vj V2 V4 V3 V5 S {V0, V2} {V0, V2, V4} {V0, V2, V3, V4} {V0, V2, V3, V4, V5} 26 26

27 27 27


Download ppt "CSCI2100 Data Structures Tutorial 12"

Similar presentations


Ads by Google