Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC2100B Tutorial 10 Graph Jianye Hao.

Similar presentations


Presentation on theme: "CSC2100B Tutorial 10 Graph Jianye Hao."— Presentation transcript:

1 CSC2100B Tutorial 10 Graph Jianye Hao

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

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

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

5 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 1 C 1 1 1 1 D 1 1 1 E 1 1 1 A E

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

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.

8 Topological Sort V1 V2 V3 V4 V5 V6 V7

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.

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

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

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

13 Spanning Tree

14 Minimum Spanning Tree

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.

16 MST Algorithm

17 17 17

18 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 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 20

21 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 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 The End Any Questions?


Download ppt "CSC2100B Tutorial 10 Graph Jianye Hao."

Similar presentations


Ads by Google