introduce shortest path algorithms(korean)

46
Shortest Path HI-ARC Wonjae Kim (First Author) 1

Upload: wonjae-kim

Post on 11-Apr-2017

63 views

Category:

Science


0 download

TRANSCRIPT

Page 1: Introduce shortest path algorithms(Korean)

Shortest PathHI-ARC Wonjae Kim (First Author)

1

Page 2: Introduce shortest path algorithms(Korean)

Purpose• 주어진 그래프에서 최단 경로를 찾자 !

2

Page 3: Introduce shortest path algorithms(Korean)

Table of Content

• Bellman-Ford

• Dijkstra

• Floyd-Warshall

• A*(extra)

3

Page 4: Introduce shortest path algorithms(Korean)

Bellman-Ford• Bellman Ford 가 설계• 시작점에서 모든 점과의 거리 계산• Vertex( 정점 ) 이 N 개 그래프• 시작점에서 도착점까지 간선의 수 최대 N-1• 모든 간선들을 N-1 번 시도해본다 .

• 음수 가중치가 있어도 Ok!• 방향・무방향 Ok! •∙ ・• O(V*E)• V: Vertex

E: Edge 4

Page 5: Introduce shortest path algorithms(Korean)

Proof Bellman-Ford

• Vertex 가 N 개 그래프• V1 에서 최소 N 번을 이동해야 도달할 수 있는 VX 가 있다고 가정 .

• V1 을 제외하고 방문하는 정점의 수는 N 개• V1 포함하면 정점의 수 N+1 ( 모순 !)

5

Page 6: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford(1)

6

∞∞

∞ ∞

Page 7: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford(2)

7

1∞

∞ ∞

Page 8: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford(3)

8

14

2

∞3

Page 9: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford(4)

9

14

2

∞3

5

Page 10: Introduce shortest path algorithms(Korean)

Negative Cycle Bellman-Ford

• 그래프에 존재하는 음수 사이클 파악 가능 !

• 어떻게 ?

• 모든 간선들을 N-1 번 시도해보고 , 한 번 더 시도 !

• 한 정점이라도 값이 감소하면 음수사이클 !

10

Page 11: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford_Negative-Cycle(1)

11

∞ ∞

Page 12: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford_Negative-Cycle(2)

12

-1

-3

1 4N-1 번 후…

Page 13: Introduce shortest path algorithms(Korean)

How it works? Bellman-Ford_Negative-Cycle(3)

13

-1

-3

0 4N 번 째 ! C 가 바뀜 !

Page 14: Introduce shortest path algorithms(Korean)

Pesudo Code Bellman-Ford

14

Page 15: Introduce shortest path algorithms(Korean)

Dijkstra• Dijkstra 가 설계• 시작점에서 모든 점과의 거리 계산• 현재까지 도달한 정점 중 가장 값이 낮은 것을 선택 !• Greedy Algorithm

• 음수 간선이 존재하면 불가능 ( 하나라도 !)• 방향・무방향 Ok!

• O(V2)• Priority Queue( 우선순위 큐 ) 사용 시 O(V log E) 15

Page 16: Introduce shortest path algorithms(Korean)

Dijkstra• Dijkstra 가 설계• 현재까지 도달한 정점 중 가장 값이 낮은 것을 선택• 선택된 노드가 도착점이면 끝 !• Greedy Algorithm

• 음수 간선이 존재하면 불가능 ( 하나라도 )• 방향・무방향 Ok!

• O(V2)• Priority Queue( 우선순위 큐 ) 사용 시 O(V log E) 16

Page 17: Introduce shortest path algorithms(Korean)

Proof

17

S

E

X

Y

Page 18: Introduce shortest path algorithms(Korean)

How it works? Dijkstra(1)

18

1∞

∞ ∞

Page 19: Introduce shortest path algorithms(Korean)

How it works? Dijkstra(2)

19

13

2

∞3

Page 20: Introduce shortest path algorithms(Korean)

How it works? Dijkstra(3)

20

13

2

∞3

5

Page 21: Introduce shortest path algorithms(Korean)

How it works? Dijkstra(4)

21

13

2

∞3

5

Page 22: Introduce shortest path algorithms(Korean)

How it works? Dijkstra(5)

22

13

2

∞3

5

Page 23: Introduce shortest path algorithms(Korean)

How it works? Dijkstra(6)

23

13

2

∞3

5

Page 24: Introduce shortest path algorithms(Korean)

Why it doesn’t works? Dijkstra-Negative Edge(1)

24

2

∞ ∞

Page 25: Introduce shortest path algorithms(Korean)

Why it doesn’t works? Dijkstra-Negative Edge(2)

25

2

4

3 ∞

Page 26: Introduce shortest path algorithms(Korean)

Why it doesn’t works? Dijkstra-Negative Edge(3)

26

2

0

3 6

D 가 B, C 보다도 작아짐 !

Page 27: Introduce shortest path algorithms(Korean)

Pesudo Code Dijkstra

27

Page 28: Introduce shortest path algorithms(Korean)

Floyd-Warshall• 여러 사람이 설계 ( 위키 참고 )• 모든 점에서 모든 점과의 거리 계산• Dynamic Programming

• 음수 간선 존재해도 무방• 음수 사이클 판별 가능

• 방향 ・무방향 Ok!

• O(V3)

28

Page 29: Introduce shortest path algorithms(Korean)

Recursion formula Floyd-Warshall

• 최단 경로의 중간 정점들을 고려• d(k)

ij = wij if k = 0

• d(k)ij = min (d(k-1)

ij, d(k-1)ik + d(k-1)

kj) if k >= 1

• i, j 는 k 를 제외한 모든 수• 구현에서는 고려할 필요 X

• Distance, Sequence Table 을 관리• Distance: 각 정점 간의 거리• Sequence: 갱신된 k 를 저장 29

Page 30: Introduce shortest path algorithms(Korean)

How it works? Floyd-Warshall(1)

30

A B

C D

5

8 3

12

- 2 3 4

1 - 3 4

1 2 - 4

1 2 3 -

0 5 8 1

5 0 2 3

8 2 0 ∞

1 3 ∞ 0

Init

Page 31: Introduce shortest path algorithms(Korean)

How it works? Floyd-Warshall(2)

31

A B

C D

5

8 3

12

- 2 3 4

1 - 3 4

1 2 - 1

1 2 1 -

0 5 8 1

5 0 2 3

8 2 0 9

1 3 9 0

K = 1

Page 32: Introduce shortest path algorithms(Korean)

How it works? Floyd-Warshall(3)

32

A B

C D

5

8 3

12

- 2 2 4

1 - 3 4

2 2 - 2

1 2 2 -

0 5 7 1

5 0 2 3

7 2 0 5

1 3 5 0

K = 2

Page 33: Introduce shortest path algorithms(Korean)

How it works? Floyd-Warshall(4)

33

A B

C D

5

8 3

12

- 2 2 4

1 - 3 4

2 2 - 2

1 2 2 -

0 5 7 1

5 0 2 3

7 2 0 5

1 3 5 0

K = 3

Page 34: Introduce shortest path algorithms(Korean)

How it works? Floyd-Warshall(4)

34

A B

C D

5

8 3

12

- 4 4 4

4 - 3 4

4 2 - 2

1 2 2 -

0 4 6 1

4 0 2 3

6 2 0 5

1 3 5 0

K = 4

Page 35: Introduce shortest path algorithms(Korean)

Backtraking Floyd-Warshall

• Sequence[3][1] = 4• Sequence[3][4] = 2• Sequence[3][2] = 2

• 3 -> 2 -> 4 -> 1• C -> B -> D -> A

35

- 4 4 4

4 - 3 4

4 2 - 2

1 2 2 -

A B

C D

5

8 3

12

Page 36: Introduce shortest path algorithms(Korean)

Negative Cycle Floyd-Warshall

• Bellman Ford 처럼 음수 사이클 파악 가능 !

• 어떻게 ?

• 자기 자신과의 거리가 0 이하면 있다 !• 과정은 생략

36

Page 37: Introduce shortest path algorithms(Korean)

Pesudo Code Floyd-Warshall

37

Page 38: Introduce shortest path algorithms(Korean)

A* A-star

• 요청을 받아서 간단하게 설명 ( 굳굳 !)

• 그래프 / 트리 탐색 알고리즘• BFS / DFS 처럼

• BFS / DFS 처럼 다 탐색하지 않음• Heuristic ( 휴리스틱 ) 함수를 이용하여 개선 !• 함수를 잘 정의 / 성능 ↑

• f(n) = g(n) + h(n)• f(n) – Evaluation Function• g(n) – Cost Function• h(n) – Heuristic Function

• 인공지능 수업에서 등장 38

Page 39: Introduce shortest path algorithms(Korean)

Example

39

E

S

Page 40: Introduce shortest path algorithms(Korean)

Example-DFS

40

15 18 19 20 E(21)

14 17

13 16 8

12 11 10 9 6 7

5

S(0) 1 2 3 4

Page 41: Introduce shortest path algorithms(Korean)

Example-BFS

41

21 16 18 20 E(22)

19 14

17 12 10 9

15 13 11 8 6 7

5

S(0) 1 2 3 4

Page 42: Introduce shortest path algorithms(Korean)

Example-A* Define Function

• Cost Function / g(n)• 출발점 (S) 로부터 이동한 거리

• Heuristic Function / h(n)• 현재 위치와 도착점간의 Manhattan Distance( 맨해턴 거리 )

• Manhanttan Distance = ( x∣ 1-x2 + y∣ ∣ 1-y2 )∣

• Evaluation Function / f(n)• g(n) + h(n)

42

Page 43: Introduce shortest path algorithms(Korean)

Example-A*

43

13 14 15 E(16)

12

11 10 8

9 6 7

5

S(0) 1 2 3 4

Page 44: Introduce shortest path algorithms(Korean)

Summary• Bellman Ford• O(VE)• 한 정점에서 모든 정점 간의 거리• 음수 사이클 판정 가능

• Dijkstra• O(V2) – O(VlogE)• 한 정점에서 모든 정점 간의 거리• 음수 간선 있으면 최적거리 못 구함

• Floyd-Warshall• O(V3)• 모든 정점에서 모든 정점 간의 거리• 음수 사이클 판정 가능

44

Page 45: Introduce shortest path algorithms(Korean)

45

Q&A추천 문제는 카페에

Page 46: Introduce shortest path algorithms(Korean)

Reference• Graph Image 1• http://cs.stackexchange.com/questions/18138/dijkstra-algorithm

-vs-breadth-first-search-for-shortest-path-in-graph

• Graph Image 2• http://cs.stackexchange.com/questions/14248/what-is-the-

significance-of-negative-weight-edges-in-a-graph

46