Introduction to C Tom Chao Zhou CSC2100B Data Structures Tutorial 1.
CSC2100B Tutorial 10 Graph Jianye Hao.
-
Upload
lily-sanders -
Category
Documents
-
view
239 -
download
0
description
Transcript of CSC2100B Tutorial 10 Graph Jianye Hao.
![Page 1: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/1.jpg)
1
CSC2100BTutorial 10
Graph
Jianye Hao
![Page 2: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/2.jpg)
2
Outline Graph Adjacency Representation Topological Sort Minimum Spanning Tree
Kruskal’s Algorithm Prim’s Algorithm
Shortest Path Dijkstra’ Algorithm
![Page 3: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/3.jpg)
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)
}
ABCDE
0 0 1 0 10 0 0 1 00 1 0 0 00 0 1 0 00 0 1 1 0
A B C D E
A
B
C D
E
![Page 4: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/4.jpg)
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
A
BC D
E
For example, InDegree for C is 3, OutDegree for C is 1
Graph - adjacency representation
ABCDE
0 0 1 0 10 0 0 1 00 1 0 0 00 0 1 0 00 0 1 1 0
A B C D E
![Page 5: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/5.jpg)
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) }
ABCDE
0 0 1 0 10 0 1 1 01 1 0 1 10 1 1 0 11 0 1 1 0
A B C D E
A
B
C D
E
Graph - adjacency representation
![Page 6: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/6.jpg)
6
Adjacency list
ABCDE
A
B
C D
E
C EDBCC D
Graph - adjacency representation
![Page 7: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/7.jpg)
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.
![Page 8: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/8.jpg)
8
Topological Sort
V1 V2
V7
V3
V6
V4 V5
![Page 9: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/9.jpg)
9
How to find the topologicalordering? 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.
![Page 10: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/10.jpg)
10
V1 V2
V7
V3
V6
V4 V5
V2
V7
V3
V6
V4 V5
V7
V3
V6
V4 V5
V7
V3
V6
V4
V7
V3
V6
V7V6 V6
Topological Order V1 V2 V5 V4 V3 V7 V6
![Page 11: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/11.jpg)
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.
![Page 12: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/12.jpg)
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).
![Page 13: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/13.jpg)
13
Spanning Tree
![Page 14: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/14.jpg)
14
Minimum Spanning Tree
![Page 15: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/15.jpg)
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.
![Page 16: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/16.jpg)
16
MST Algorithm
![Page 17: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/17.jpg)
17
![Page 18: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/18.jpg)
18
Kruskal’s Algorithm
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4Edge Weight<h, g> 1<c, i> 2<g, f> 2<a, b> 4<c, f> 4<g, i> 6<c, d> 7<h, i> 7<a, h> 8<b, c> 8<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
8
2
7 6
4
a
b c
h
d
g f
i e
4
8
7
14
9
1 10
11
2
8
2
7 6
4a
b c
h
d
g f
i e
4
8
7
14
9
1 10
11
2
8
2
7 6
4
a
b c
h
d
g f
i e
4
8
7
14
9
1 10
11
2
8
2
7 6
4a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
![Page 19: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/19.jpg)
19
Kruskal’s Algorithm
Edge Weight<h, g> 1<c, i> 2<g, f> 2<a, b> 4<c, f> 4<g, i> 6<c, d> 7<h, i> 7<a, h> 8<b, c> 8<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
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
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.
![Page 20: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/20.jpg)
20
![Page 21: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/21.jpg)
21
Prim’s Algorithm
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
![Page 22: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/22.jpg)
22
Prim’s Algorithm
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
78
a
b c
h
d
g f
i e
4
8
14
9
1 10
11
2
2
7 6
4
![Page 23: CSC2100B Tutorial 10 Graph Jianye Hao.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1aed7f8b9ab05997c355/html5/thumbnails/23.jpg)
23
The EndAny Questions?