analysis of algorithms -...

50
アルゴリズムの設計と解析 潤和、佐藤 温(TA2015.4

Upload: doandiep

Post on 31-Mar-2018

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

アルゴリズムの設計と解析

黄 潤和、佐藤 温(TA) 2015.4~

Page 2: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 3: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

What are spanning trees?

3

Page 4: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

Results from BFS and DFS?

Note:

Page 5: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 6: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

6

Page 7: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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.

Page 8: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 9: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

Dijkstra’s Algorithm (pseudo code)

9

Page 10: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 11: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

11

An example

Work in class: Start from B, (1) Draw a SSSP (Single source shortest path) (2) Draw a MST (Minimum spanning tree)?

Page 12: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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.

Page 13: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 14: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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)

Page 15: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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)

Page 16: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

1

3

5

6

6

8

4

5

4

a

b c

d

e

f

2

Work in class (prim’s algorithm)

Page 17: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as
Page 18: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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 }

Page 19: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

Shortest Path 19

Page 20: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

20

Page 21: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

21

Page 22: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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.

Page 23: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 24: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 25: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 26: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 27: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 28: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 29: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 30: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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.

Page 31: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

Apply Kruskal’s algorithm to find the minimum spanning tree

Page 32: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 33: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 34: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 35: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 36: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 37: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

37

Page 38: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 39: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 40: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 41: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 42: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 43: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 44: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 45: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 46: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 47: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 48: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

48

Exercises 11 Ex 11.1 Draw MST applying prim’s algorithm, starting from node u

a

c

b f

e

d

Page 49: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

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

Page 50: Analysis of Algorithms - cis.k.hosei.ac.jpcis.k.hosei.ac.jp/~rhuang/Miccl/Algorithm3/2015-lect11-12-13.pdf · Single source shortest path tree . 6 . ... It can still be regarded as

50