+ All Categories
Home > Documents > CSC2100B Tutorial 10 Graph Jianye Hao.

CSC2100B Tutorial 10 Graph Jianye Hao.

Date post: 08-Jan-2018
Category:
Upload: lily-sanders
View: 239 times
Download: 0 times
Share this document with a friend
Description:
Outline Graph Adjacency Representation Topological Sort Minimum Spanning Tree Kruskal’s Algorithm Prim’s Algorithm Shortest Path Dijkstra’ Algorithm
23
1 CSC2100B Tutorial 10 Graph Jianye Hao
Transcript
Page 1: CSC2100B Tutorial 10 Graph Jianye Hao.

1

CSC2100BTutorial 10

Graph

Jianye Hao

Page 2: 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

Page 3: CSC2100B Tutorial 10 Graph Jianye Hao.

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.

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.

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.

6

Adjacency list

ABCDE

A

B

C D

E

C EDBCC D

Graph - adjacency representation

Page 7: CSC2100B Tutorial 10 Graph Jianye Hao.

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.

8

Topological Sort

V1 V2

V7

V3

V6

V4 V5

Page 9: CSC2100B Tutorial 10 Graph Jianye Hao.

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.

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.

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.

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.

13

Spanning Tree

Page 14: CSC2100B Tutorial 10 Graph Jianye Hao.

14

Minimum Spanning Tree

Page 15: CSC2100B Tutorial 10 Graph Jianye Hao.

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.

16

MST Algorithm

Page 17: CSC2100B Tutorial 10 Graph Jianye Hao.

17

Page 18: CSC2100B Tutorial 10 Graph Jianye Hao.

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.

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.

20

Page 21: CSC2100B Tutorial 10 Graph Jianye Hao.

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.

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.

23

The EndAny Questions?


Recommended