0% found this document useful (0 votes)
9 views27 pages

ITP3902 DMS Lec 6 Graph Part2

Lecture 6 covers the Shortest Path Algorithm, specifically Dijkstra's algorithm, which finds the shortest path from a starting vertex S to a target vertex T in a network. It also discusses trees, their properties, and the concept of spanning trees, including minimum spanning trees and Kruskal's algorithm for finding them. The lecture includes examples and exercises to illustrate these concepts.

Uploaded by

applewongba21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views27 pages

ITP3902 DMS Lec 6 Graph Part2

Lecture 6 covers the Shortest Path Algorithm, specifically Dijkstra's algorithm, which finds the shortest path from a starting vertex S to a target vertex T in a network. It also discusses trees, their properties, and the concept of spanning trees, including minimum spanning trees and Kruskal's algorithm for finding them. The lecture includes examples and exercises to illustrate these concepts.

Uploaded by

applewongba21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 6 Shortest Path Algorithm & Trees

Lecture 6 (Graph 2)

Shortest Path Algorithm


&
Trees

ITP3902 Discrete Mathematics & Statistics Page 1


Lecture 6 Shortest Path Algorithm & Trees

Shortest Path Algorithm


• The objective of this algorithm is to find the shortest
path from vertex S to vertex T in a given network.

• We demonstrate the steps of Dijkstra’s algorithm


(戴克斯特拉演算法) by finding the shortest path
and distance from S to T in the network given in the
following example.
荷蘭電腦科學家 Edsger W. Dijkstra 在1956年發現的演算法,並於3年
後在期刊上發表。戴克斯特拉演算法 (以找到S點及T點之間的最短
路徑為目的),每次選取出未存取節點中距離最小的,用該節點更
新其他節點的距離。
ITP3902 Discrete Mathematics & Statistics Page 2
Lecture 6 Shortest Path Algorithm & Trees

Shortest Path Algorithm


Example 1
A

7
1 6
S 4 B
9 3 T
3
C
7
4
1

ITP3902 Discrete Mathematics & Statistics Page 3


Lecture 6 Shortest Path Algorithm & Trees

Example 1 (cont.) Step 1 Initialisation


A 7 (S)

7
1 6
0
4 B 4 (S)
S
9 3 T
3
C 9(S)
7 4
1

D 7(S)
• Assign to vertex S potential 0.
• Label each vertex V reached directly from S with distance from S to V.
• Choose the smallest of these labels, and make it the potential of the
corresponding vertex or vertices.
ITP3902 Discrete Mathematics & Statistics Page 4
Lecture 6 Shortest Path Algorithm & Trees

Example 1 (cont.) Step 2 General Step


A5(SB)
7
1 6
0
S 4 B4 (S)

9 3 T
3
C 7(SB)
7
4
1

D 77 (S)

• For each vertex V, look at each other vertex W reached directly from V until W
already has a smaller label.

ITP3902 Discrete Mathematics & Statistics Page 5


Lecture 6 Shortest Path Algorithm & Trees

Example 1 (cont.) Step 2 General Step (cont.)


A 5 (SB)

7
1 6
0 4 4 (S)
S B
9 3 T 11 (SBA)
7 (SB)
3
7 C
4
1

D 7 (S)

ITP3902 Discrete Mathematics & Statistics Page 6


Lecture 6 Shortest Path Algorithm & Trees

Example 1 (cont.) Step 3 Stop


A 5 (SB)

7
1 6
0 4
S B 4 (S)

9 3 (SB) 3
T 10 (SBC)
7
7 C
4
1
7 (S)
D

• Finally, the shortest path from S to T is SBCT with path length 10.

ITP3902 Discrete Mathematics & Statistics Page 7


Exercise
Find the shortest path from A to F.
State the shortest distance from A to F.

8
Step 1 - Initialisation
Find the shortest path from A to F.
State the shortest distance from A to F.

9
Step 2 – General Step
Find the shortest path from A to F.
State the shortest distance from A to F.

10
Step 2 – General Step (cont.)
Find the shortest path from A to F.
State the shortest distance from A to F.

11
Step 2 – General Step (cont.)
Find the shortest path from A to F.
State the shortest distance from A to F.

12
Step 3 – STOP
Find the shortest path from A to F.
State the shortest distance from A to F.

13
The shortest path is: A – B – D – E – C – F.
The shortest distance is 25 units
Lecture 6 Shortest Path Algorithm & Trees

Trees
A tree is a connected graph which contains no cycles.
Example 2
# of vertices Tree Structure
1, 2, 3
4
5

ITP3902 Discrete Mathematics & Statistics Page 15


Lecture 6 Shortest Path Algorithm & Trees

Properties of Tree

• Every tree with n vertices has exactly n – 1 edges


• Any two vertices in a tree are connected by exactly
one path
• Each edge of a tree is a bridge
• A tree contains no cycles

ITP3902 Discrete Mathematics & Statistics Page 16


Lecture 6 Shortest Path Algorithm & Trees

Spanning Trees
Let G be a connected and undirected graph. A
spanning tree of G is a subgraph of G that connects all
the vertices of G together and is in a tree structure.
Example 3
Possible Spanning Trees of G
v w v w
Graph G
x x
v w
z y z y
x v w

z y x

ITP3902 Discrete Mathematics & Statistics z y Page 17


Lecture 6 Shortest Path Algorithm & Trees

Minimum Spanning Trees


A minimum spanning tree of G is that it is a spanning
tree and uses the minimum weight set of edges to
connect all the vertices in G.
Example 4
v w
Graph G
8
v 11 w 5 x
8 10
9
5 12 x z 5 y
10 12
Minimum Spanning Tree of G
z 5 y
Total weight = 5 + 5 + 8 + 10 = 28

ITP3902 Discrete Mathematics & Statistics Page 18


Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
1. Sort all the edges in ascending order (由小而大順序)
of their weight.
2. Pick the smallest edge. Check if it forms a cycle with
the spanning tree formed so far. If cycle is not
formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (v – 1) edges in the
spanning tree.

ITP3902 Discrete Mathematics & Statistics Page 19


Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
Example 5
E

2 4

D 5 A
6 15
7 9 7 12

C 8 B

ITP3902 Discrete Mathematics & Statistics Page 20


Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
Example 5 (cont.)
Iteration 1 (pick the smallest weight of edge, i.e. DE)

E E

2 4 2

D 5 A D A
6 15
7 9 7 12

C 8 B C B
ITP3902 Discrete Mathematics & Statistics Page 21
Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
Example 5 (cont.)
Iteration 2 (pick the next smallest weight of edge, i.e. AE)

E E

2 4 2 4

D 5 A D A
6 15
7 9 7 12

C 8 B C B
ITP3902 Discrete Mathematics & Statistics Page 22
Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
Example 5 (cont.)
Iteration 3 (pick the next smallest weight of edge, i.e.
CE because AD will form a cycle)
E E

2 4 2 4

D 5 A D A
6 15 6
7 9 7 12

C 8 B C B
ITP3902 Discrete Mathematics & Statistics Page 23
Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
Example 5 (cont.)
Iteration 4 (pick the next smallest weight of edge, i.e.
BC because AC and CD will form a cycle)
E E

2 4 2 4

D 5 A D A
6 15 6
7 9 7 12

C 8 B C 8 B
ITP3902 Discrete Mathematics & Statistics Page 24
Lecture 6 Shortest Path Algorithm & Trees

Greedy Algorithm (Kruskal’s min. spanning


tree algorithm)
Example 5 (cont.)
Stop because (v – 1) edges are in the spanning tree or
all vertices have been connected.
E E
2 4
2 4
5 D A
D A 6
6 15
7 9 7 12
C 8 B
C 8 B Minimum weight = 2 + 4 + 6 + 8 = 20 units
ITP3902 Discrete Mathematics & Statistics Page 25
Exercise:
1
6 5
2 4
5 1 5
3 6 34 2
5 6
6
Find the minimum spanning tree.
State the minimum weight.
Exercise: (cont.) Find the minimum spanning tree.
State the minimum weight.

minimum
spanning tree

Minimum weight = 1 + 4 + 2 + 5 + 3 = 15 units

You might also like