graph-based algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · •...

24
Graph-based Algorithm – 1

Upload: others

Post on 23-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

Graph-based Algorithm – 1

Page 2: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

State-Space Tree

• State-space tree (상태공간트리)

–문제해결과정의중간상태를각각한노드로나타낸트리

• 상태공간탐색기법

– DFS, BFS

– Backtracking

– Branch-and-bound

– A* algorithm

Page 3: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

DFS, BFS

Page 4: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

1

3

6

4

2

5

1

3

6

4

2

5

1

3

6

4

2

5

TSP 예제 해의 예 최적해

Travelling Salesman Problem의 예• 물건을 판매하기 위해 여행하는 세일즈맨의 문제• 물건을 팔기 위해서 어떤 도시를 먼저 방문해서 최소한의 비용으로 도시들을 모두 순

회하고 돌아올 수 있는 지를 구하는 것• TSP는 가장 대표적인 NP-Hard 문제

• NP-Hard란 Nondeterministic Polynomial - Hard의 약자• 다항식으로 표현될 수 있을 지의 여부가 아직 알려지지 않았다는 의미• NP-Hard란 TSP문제와 같이 모든 경우의 수를 일일히 확인해보는 방법이외에

는 다항식 처럼 답을 풀이 할 수 없는 문제들을 말함

Page 5: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

0 10 10 30 25

10 0 14 21 10

10 18 0 7 9

8 11 7 0 3

14 10 10 3 0

1

2

3

4

5

1 2 3 4 5

1

3

4

2

5

10

10

25 14

1010

2111

30 8

3

3

9

10

7

7

14

18

10

10

TSP와 Adjacency Matrix의 예

Page 6: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

1

1-2-3

1-2-3-4 1-2-3-5

1-2-4

48 44

1-2-5

1-3

1-2-5-4

40

1-2-5-3

45

1-2-5-3-4-11-2-3-4-5-1 1-2-3-5-4-1 1-2-5-4-3-1

1-5-4-2 1-5-4-3

63 63

1-5-4-2-3-1 1-5-4-3-2-1

1-4 1-5

1

2

10 11

3 6

4 5 40 41

12 22 32

1-2-4-5

54

1-2-4-3

61

1-2-4-3-5-1 1-2-4-5-3-1

7 8

1-2

9

1-5-4

39

… … …

State-Space Tree

Page 7: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

S

T

2

3

1

4

5

6

S

1

2 3

4

5

T6

7

8

9987

maze

S

T

그래프로 모델링한 미로

Branching tree

Maze

Page 8: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

maze(v)

{

visited[v] ← YES;

if (v = T) then {print “성공!”; exit( );}

for each x ∈ L(v) ▷ L(v) : 정점 v의 인접 정점 집합

if (visited[x] = NO) then {

prev[x] ← v;

maze(x);

}

}

Page 9: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

Coloring Problem

• Graph에서

– 인접한 vertex는같은색을칠할수없다

– k 개의색상을사용해서전체 graph를칠할수있는가?

Page 10: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

1

3

6

42

5

1

3

6

42

5

(a) 지도 (b) 구역간의 인접관계

(c) 연결관계를 정점과 간선으로 나타낸 것 (d) (c)와 동일한 그래프

Coloring Problem의예: Map Coloring

Page 11: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

kColoring(i , c) ▷ i: 정점, c: color

▷ 질문: 정점 i-1까지는 제대로 칠이 된 상태에서 정점 i를 색 c로 칠하려면 k개의 색으로 충분한가?

{

if (valid(i, c)) then {

color[i] ← c;

if (i = n) then {return TRUE;}

else {

result ← FALSE;

d ← 1; ▷ d: color

while (result = FALSE and d ≤ k) {

result ← kColoring(i+1, d); ▷ i+1: 다음정점

d++;

}

}

return result;

} else {return FALSE;}

}

Page 12: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

valid(i, c) ▷ i: 정점, c: color

▷ 질문: 정점 i-1까지는 제대로 칠이 된 상태에서 정점 i를 색 c로 칠하려면이들과 색이 겹치지 않는가?

{

for j ← 1 to i-1 {

▷ 정점 i와 j 사이에 간선이 있고, 두 정점이 같은 색이면 안된다

if ((i, j) ∈ E and color[j] = c) then return FALSE;

}

return TRUE;

}

Page 13: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

1, 1

3, 1 3, 2

2, 1

1

5

2, 2

3

4

4, 1 4, 276

5, 1

6, 1 6, 29

6, 310 11

8

2

State-Space Tree

1

3

6

42

5

Page 14: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

A* Algorithm

• Best-First-Search– 각 정점이 매력함수값 g(x)를 갖고 있다

– 방문하지 않은 정점들 중 g(x) 값이 가장 매력적인 것부터 방문한다

• A* algorithm은 best-first search에 목적점에 이르는 잔여추정거리를 고려하는 알고리즘이다– Vertex x로부터 목적점에 이르는 잔여거리의 추정치 h(x)는 실제

치보다 크면 안된다

– f(n): 평가함수

– f(n) = g(n) + h(n)• g(n): 출발노드로부터 노드 n까지의 경로비용

• h(n): 노드 n으로부터 목표 노드까지의 추정 경로비용

Page 15: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

Shortest Path 문제

• Remind: Dijkstra algorithm

– 시작점은하나

– 시작점으로부터다른모든 vertex에이르는최단경로를구한다 (목적점이하나가아니다)

• A* algorithm에서는목적점이하나다

Page 16: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

30

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

1030

17 23

25

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

1030

17 23

25

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

∞∞

∞ ∞

s

t

Dijkstra Algorithm의 작동예

Page 17: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

1030

17 23

25

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

1030

17 23

41

25

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

1030

17 23

41

50

25

030

23

25

24

18

28 20

29

25

39

61

20

40

28

1610

19

17

20

17

1030

17 23

41

64

50

25

Page 18: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

030

23

25

24

18

28 20

29

25

39

61

20

40

28

1610

19

17

20

17

1030

17 23

41

64

50

25

030

23

25

24

18

28 20

29

25

39

61

20

40

28

1610

19

17

20

17

1030

17 23

41

64

50

25

Page 19: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

16

25

52

40

39

34

19

19

52

68

61

30

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

(77)

(57)

(71)

(85)

(70)

030

23

25

24

18

28 20

2939

20

40

28

10

19

17

20

17

1030

17 23

25

추정 잔여거리

030

23

25

24

18

28 20

29

25

39

20

40

28

1610

19

17

20

17

∞∞

∞ ∞

A* Algorithm의작동예

Page 20: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

030

23

25

24

18

28 20

29

25

39

61

20

40

28

1610

19

17

20

17

1030

17 23

41

50

25

(60)

(89)

030

23

25

24

18

28 20

29

25

39

61

20

40

28

1610

19

17

2017

1030

17 23

41

50

25

(77)

(57)

(71)

(85)

(70)

(60)

(89)

(77)

(57)

(71)

(85)

(70)

추정잔여거리를 사용함으로써 탐색의 단계가 현저히 줄었다

Page 21: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

1

1-2

1-2-3

1-2-3-4 1-2-3-5

(33)0+33

(33)10+23

(37)24+13

1-2-4

(44)31+13

1-2-5

(33)20+13

1-3 (33)10+23

1-3-2

(44)28+16

1-3-4

(33)17+16

1-3-5(35)19+16

1-2-5-4

40

1-2-5-3

45

1-2-5-3-4-11-2-3-4-5-1 1-2-3-5-4-1 1-2-5-4-3-1

1-3-4-2 1-3-4-5

40

1-3-5-4

43

1-3-5-2

58

1-3-5-2-4-11-3-4-2-5-1 1-3-4-5-2-1 1-3-5-4-2-1

1-4

(53)30+23

1-5

(48)25+23

1

2

8

7 5

3

4 6

48 44 52

TSP 예제를대상으로한A* Algorithm 탐색의예(State-Space Tree)

Page 22: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

예제: 8-퍼즐 문제 (A*)

f(n) = g(n) + h(n)

Page 23: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

Minimum(or Maximum) Spanning Tree – 1

• Kruskal’s algorithm1 A = ∅

2 foreach v ∈ G.V:

3 MAKE-SET(v)

4 foreach (u, v) ordered by weight(u, v), increasing:

5 if FIND-SET(u) ≠ FIND-SET(v):

6 A = A ∪ {(u, v)}

7 UNION(u, v)

8 return A

Page 24: Graph-based Algorithm 1cs.kangwon.ac.kr/.../2_graph_based_algorithm.pdf · 2019. 3. 14. · • Prim’s algorithm 1. Initialize a tree with a s ingle vertex, chosen ar bitrarily

Minimum(or Maximum) Spanning Tree – 2

• Prim’s algorithm

1. Initialize a tree with a single vertex, chosen arbitrarily from the graph.

2. Grow the tree by one edge: Of the edges that connect the tree to vertices not yet in the tree, find the minimum-weight edge, and transfer it to the tree.

3. Repeat step 2 (until all vertices are in the tree).