Prim`s Algorithm

CSC201
Analysis and Design of Algorithms
Problem Solving Problem with Graphs:
Shortest Path and และ Minimum
Spanning Tree
Asst.Prof. Dr.Surasak Mungsing
E-mail: [email protected]
Jul-17
1
Sparse Graphs
In a sparse graph the number of edges is significantly less
than |V| squared.
7/29/2017
2
Dense Graphs
In a dense graph most of the vertices are connected by edges.
7/29/2017
3
Adjacency List Representation
In order to run algorithms on graphs we can use one of two
representations of them. The first is an adjacency list.
Here each vertex has an entry in an array that contains a
linked list to the adjacent vertices.
7/29/2017
4
Adjacency-Matrix Representation
Another method of storing the information about the graph is
called an adjacency-matrix. Here a V by V matrix is formed with
entries 0 or 1.
7/29/2017
5
Directed Graphs
A directed graph the edge (u,v) is distinct from the edge (v,u).
Here is an example:
7/29/2017
6
Matrix Representation
7/29/2017
7
Weighted Graphs
A weighted graph has a real value associated with each
edge as in this graph:
7/29/2017
8
Matrix Representation
7/29/2017
9
Single Source Shortest Path
 กำหนด (un)weighted, directed graph G = {V, E} มำให้
ซึ่งแต่ละ edge มีค่ำ cost (หรื อ weight) เป็ นจำนวนบวก
 Problem: จงหำค่ำรวมของ cost ของเส้นทำงทีส้ นั ที่สุดจำก vertex
ต้นทำงไปยัง vertex ปลำยทำงอื่นๆ
 ควำมยำวของเส้นทำงคือคำรวมของ cost ของedge ต่ำงๆบนเส้นทำง
นั้น
 ทำไมไม่กำหนดเส้นทำงใดเส้นทำงหนึ่งไปยังจุดหมำยปลำยทำง?
 Application: G คือแผนที่เส้นทำงบินของสำยกำรบินซึ่งจะต้องหำ
เส้นทำงบินจำกเมืองที่กำหนดให้ไปยังอีกเมืองอื่นๆโดยใช้เวลำเดินทำงน้อย
ที่สุด
7/29/2017
10
Dijkstra’s Algorithm
 รักษำ set S ของ vertices ซึ่งรู ้ค่ำของเส้นทำงที่ส้ นั ทีสุดจำกต้นทำงแล้ว
 ตอนเริ่ มต้น S มีเพียง vertex ต้นทำง เท่ำนั้น
 ในแต่ละ step, เรำเพิ่ม vertex w ที่เหลือซึ่งมีเส้นทำงจำก vertex ต้นทำง
สั้นทีสุดเข้ำไปใน S
 สมมติวำ่ ทุก edge มี cost เป็ นบวก เรำสำมำรถหำเส้นทำงที่ส้ นั ที่สุดจำกต้น
ทำงไปยัง vertex อื่นโดยผ่ำนเส้นทำงใน S (special path) เท่ำนั้นได้เสมอ
 ในทุกๆ step เรำใช้ array บันทึกค่ำระยะทำงที่ส้ นั ที่สุดของ special path
ไปยังแต่ละ vertex
 เสร็จสิ้นกำรคำนวณเมื่อใน S ครอบคลุมทุก vertex (all paths are special)
7/29/2017
11
Algorithm




Directed graph G={V, E}
V={1, 2, …, n}, Vertex 1 is the source
Cost-adjacency matrix A[0:n][0:n]
Array D[1:n]; at each step D[i] contains the
length of the current shortest special path to
vertex i
 Initially D[i] = A[s][i]
 Array P[1:n] of vertices, such that P[v] contains
the vertex immediately before v in the shortest
path
 Initially P[v]=1, v1
7/29/2017
12
Explanations
 How do we compute D[v]?
 At each step: D[v] := min(D[v], D[w]+A[w][v])
 How do we update P?
 After computing D[v], if D[w]+A[w][v]< D[v]
then P[v] :=w
7/29/2017
13
Dijkstra: Example
G
10
1
100
30
2
5
10
50
3
60
4
20
Iteration
initial
1
2
3
4
S
{1}
{1,2}
{1,2,4}
{1,2,4,3}
{1,2,4,3,5}
w
2
4
3
5
P
[0,1,1,1,1]
[0,1,2,1,1]
[0,1,4,1,4]
[0,1,4,1,3]
[0,1,4,1,3]
D[2]
10
10
10
10
10
Shortest path from 1 to 5?
In reverse direction: 53 4 1
7/29/2017
14
D[3]

60
50
50
50
D[4]
30
30
30
30
30
D[5]
100
100
90
60
60
An Example
inf
inf
7
a
2
2
5
d
inf
4
1
b
s
0
7
4
e
4
inf
inf
7/29/2017
f
3
1
c
5
15
inf
2
inf
7
a
2
2
5
d
4
5
1
b
s
0
inf
7
4
4
7/29/2017
f
3
1
c
5
e
4
inf
16
2
9
7
a
d
2
2
4
4
5
1
b
s
0
inf
7
4
4
7/29/2017
f
3
1
c
5
e
4
inf
17
2
8
7
a
d
2
2
4
4
5
1
b
s
0
inf
7
4
4
7/29/2017
f
3
1
c
5
e
4
7
18
2
8
7
a
d
2
2
4
4
5
1
b
s
0
inf
7
4
4
7/29/2017
f
3
1
c
5
e
4
7
19
2
8
7
a
d
2
2
4
4
5
1
b
s
0
14
7
4
4
7/29/2017
f
3
1
c
5
e
4
7
20
2
8
7
a
d
2
2
4
4
5
1
b
s
0
13
7
4
4
7/29/2017
f
3
1
c
5
e
4
7
21
2
8
7
a
d
2
2
4
4
5
1
b
s
0
13
7
4
4
7/29/2017
f
3
1
c
5
e
4
7
22
Shortest Path Tree
The unique simple path from s to v in the tree is a shortest
path from s to v.
2
8
a
d
2
2
4
b
s
4
f
3
13
0
4
e
c
7/29/2017
5
4
23
7
Minimum Spanning Tree
4
v3
2
5
7/29/2017
v2
3
1
v4
8
v6
(1)
2
v1
10
7
v5
4
v7
1
Prim’s Algorithm
24
6
(2)
Minimum Spanning Tree
4
v3
2
5
7/29/2017
v2
3
1
v4
8
v6
(3)
2
v1
10
7
v5
4
v7
1
Prim’s Algorithm
25
6
(4)
Minimum Spanning Tree
4
v3
2
5
7/29/2017
v2
3
1
v4
8
v6
(5)
2
v1
10
7
v5
4
v7
1
Prim’s Algorithm
26
6
Prim’s
Algorithm
(1)
7/29/2017
(4)
(2)
27
(5)
(3)
(6)
Kruskal’s
Algorithm
7/29/2017
28
Kruskal’s
Algorithm
(1)
(4)
7/29/2017
(2)
(5)
29
(3)
(6)
29-Jul-17
30