analysis of algorithms -...
TRANSCRIPT
アルゴリズムの設計と解析
黄 潤和、佐藤 温(TA) 2015.4~
Contents (L11/12/13 – MST algorithm and TSP) • Minimal spanning tree • Prim’s algorithm • Kruskal’s algorithm • Travel salesman problem https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
2
What are spanning trees?
3
Results from BFS and DFS?
Note:
What is a minimal spanning tree?
5
• Weighted spanning tree • Weight of spanning tree = sum of tree edge weights • Any spanning tree whose weight is minimal
Single source shortest path tree
6
Property of MSTs
7
• For any edge: c (e.g. c=(G,H)(5) in blue color) in G but not in T (e.g. in red), there is a simple cycle Y ((H,D), (D,E), (E,I), (I,G)) containing only edge c and edges in spanning tree
• Moreover, weight of c must be greater than or equal to weight of any edge in this cycle.
From SSSP to MST
8
From Dijkstra’s to Prim’s algorithms • Change Dijkstra’s algorithm so the priority of
bridge (fn) is length(f,n) rather than minDistance(f) + length(f,n)
• In other words: - starts with any node - keeps adding smallest edge to
expand its path
Dijkstra’s Algorithm (pseudo code)
9
10
C B
A
E
D
F
0
4 2 8
∞ ∞
4 8
7 1
2 5
2
3 9
C B
A
E
D
F
0
3 2 8
5 11
4 8
7 1
2 5
2
3 9
C B
A
E
D
F
0
3 2 8
5 8
4 8
7 1
2 5
2
3 9
C B
A
E
D
F
0
3 2 7
5 8
4 8
7 1
2 5
2
3 9
C B
A
E
D
F
0
3 2 7
5 8
4 8
7 1
2 5
2
3 9
11
An example
Work in class: Start from B, (1) Draw a SSSP (Single source shortest path) (2) Draw a MST (Minimum spanning tree)?
Dijkstra algorithm from B
2
9
1
6 3
5
4
2 1
1
6
5 2
5
F
A B
C
D
H
G
I
E
2 0
9
7 6
4
8
6
7 4
12
Start from A
A B
Start from B
Results are different.
Prim’s algorithm
13
{ T = φ; U = { }; while (U≠V) { let (u, v) be the lowest cost edge such that u∈U and v∈V - U; T = T ∪{(u, v)} U = U ∪{v} } }
Complexity = O(|E|log(|E|))
http://www.deqnotes.net/acmicpc/prim/ Reference in Japanese
14
1
3
5
6
6
8
4
5
4
∞ ∞
0
∞
∞ ∞ a
b c
d
e
f
2
Work in class (Dijkstra algorithm from a)
15
1
3
5
6
6
8
4
5
4
∞ ∞
0
∞
∞ ∞ a
b c
d
e
f
2
Work in class (Dijkstra algorithm from a)
1
3
5
6
6
8
4
5
4
a
b c
d
e
f
2
Work in class (prim’s algorithm)
18
Kruskal's algorithm (basic part)
(Sort the edges in an increasing order) A:={} while E is not empty do { take an edge (u, v) that is shortest in E and delete it from E if u and v are in different components then add (u, v) to A }
Shortest Path 19
20
21
Kruskal’s v. s. Prim’s algorithms
22
• In Kruskal's algorithm, – The set A is a forest. – The safe edge added to A is always a least-
weight edge in the graph that connects two distinct components.
• In Prim's algorithm, – The set A forms a single tree. – The safe edge added to A is always a least-
weight edge connecting the tree to a vertex not in the tree.
23
Kruskal's algorithm (disjoint set) MST_KRUSKAL(G,w) 1 A:={} 2 for each vertex v in V[G] 3 do MAKE_SET(v) 4 sort the edges of E by non-decreasing weight w 5 for each edge (u,v) in E, in order by non-decreasing weight 6 do if FIND_SET(u) != FIND_SET(v) 7 then A:=A∪{(u,v)} 8 UNION(u,v) 9 return A
Animation of Kruskal’s algorithm http://f.hatena.ne.jp/mickey24/20090614234556
Walk-Through Consider an undirected, weight graph
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10
Sort the edges by increasing edge weight edge dv (D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10 edge dv (B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not generate a cycle
edge dv (D,E) 1 √
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10 edge dv (B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not generate a cycle
edge dv (D,E) 1 √
(D,G) 2 √
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10 edge dv (B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not generate a cycle
edge dv (D,E) 1 √
(D,G) 2 √
(E,G) 3 χ (C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10 edge dv (B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a cycle
Select first |V|–1 edges which do not generate a cycle
edge dv (D,E) 1 √
(D,G) 2 √
(E,G) 3 χ (C,D) 3 √
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10 edge dv (B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not generate a cycle
edge dv (D,E) 1 √
(D,G) 2 √
(E,G) 3 χ (C,D) 3 √
(G,H) 3 √
(C,F) 3
(B,C) 4
5
1
A
H B
F
E
D
C
G 3
2
4
6
3 4
3
4 8
4
3
10 edge dv (B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Work in class: Please continue to finish this algorithm and draw the resulting tree.
Apply Kruskal’s algorithm to find the minimum spanning tree
Minimum Connector Algorithms
Kruskal’s algorithm 1. Select the shortest edge in a
network
2. Select the next shortest edge which does not create a cycle
3. Repeat step 2 until all vertices have been connected
Prim’s algorithm 1. Select any vertex
2. Select the shortest edge
connected to that vertex
3. Select the shortest edge connected to any vertex already connected
4. Repeat step 3 until all vertices have been connected
15/7/7 17時36分 Shortest Path 33
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
Demonstration
http://weierstrass.is.tokushima-u.ac.jp/ikeda/suuri/dijkstra/Prim.shtml
Algorithms in Java
What is travel salesman problem?
34
TSP:
A Salesman wishes to travel around a given set of cities, and return to the beginning, covering the smallest total distance. Create a TSP Tour around all cities
(1) Return to the beginning
(2) No condition to return to the beginning.
It can still be regarded as a TSP by connecting the beginning city and the end city to a ‘dummy’ city at zero distance
A B
Two classifications of TSP
35
A route returning to the beginning is known as a
Hamiltonian Circuit
A route not returning to the beginning is known as a
Hamiltonian Path
Some cases
36
Printed Circuit Board 2392 cities 1987 Padberg and Rinaldi
USA Towns of 500 or more population 13509 cities 1998 Applegate, Bixby, Chvátal and Cook TSP Tour in USA
37
The smallest total distance?
38
Sweden 24978 Cities 2004 Applegate, Bixby, Chvátal, Cook and Helsgaun
Solutions: (1) Try every possibility? (n-1)!, 24977 possibilities (2) Optimizing Methods take very long time (3) Heuristic Methods may not optimal
The Nearest Neighbor Method (Heuristic) - A Greedy method
39
1. Start Anywhere
2. Go to Nearest Unvisited City
3. Continue until all Cities visited
4. Return to Beginning
40
A 42-City Problem The Nearest Neighbour Method (Starting at City 1)
1
21
20
19
29
7
30 28
31 37
32 36
11
9
34
10
39
38
35
12
33
8
27 26
6 24
25
14
15 23
22
2 13
16
3 17 4
18
42
40
41
5
41
The Nearest Neighbour Method (Starting at City 1)
1
21
20
19
29
7
30 28
31 37
32 36
11
9
34
10
39
38
35
12
33
8
27 26
6 24
25
14
15 23
22
2 13
16
3 17 4
18
42
40
41
5
42
The Nearest Neighbour Method (Starting at City 1) Length 1498
1
21
20
19
29
7
30 28
31 37
32 36
11
9
34
10
39
38
35
12
33
8
27 26
6 24
25
14
15 23
22
2 13
16
3 17 4
18
42
40
41
5
43
29
Remove Crossovers
1
21
20
19
7
30 28
31 37
32 36
11
9
34
10
39
38
35
12
33
8
27 26
6 24
25
14
15 23
22
2 13
16
3 17 4
18
42
40
41
5
44
Remove Crossovers
1
21
20
19
29
7
30 28
31 37
32 36
11
9
34
10
39
38
35
12
33
8
27 26
6 24
25
14
15 23
22
2 13
16
3 17 4
18
42
40
41
5
45
Remove Crossovers Length 1453
1
21
20
19
29
7
30 28
31 37
32 36
11
9
34
10
39
38
35
12
33
8
27 26
6 24
25
14
15 23
22
2 13
16
3 17 4
18
42
40
41
5
46
(1) compute a MST (minimum spanning tree) whose weight is a lower bound on the length of an optimal TSP tour. (2) use MST to build a tour whose cost is no more than twice that of MST's weight as long as the cost function satisfies triangle inequality.
Outline of an APPROX-TSP-TOUR
R. B. Muhammad
47
Operation of APPROX-TSP-TOUR Let root be a in following given set of points (graph)
MST-PRIM Preorder
List vertices visited in preorder walk. L = {a, b, c, h, d, e, f, g}
Hamiltonian cycle optimal TSP tour
R. B. Muhammad
48
Exercises 11 Ex 11.1 Draw MST applying prim’s algorithm, starting from node u
a
c
b f
e
d
49
Exercises 11
EX 11.3 (1) What is a TSP problem? (2) Refer the city connection graph in Fig 1, start from the node A, - to draw a MST - to an optimal TSP tour
EX 11.2 Modify the Dijkstra’s algorithm to Prim’s algorithm.
Fig1. City connection graph
12
50