Greedy Method Minimum Spanning Tree Dijkstras Algorithm for

  • Slides: 69
Download presentation

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s Algorithm for the Shortest Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 2 Computer Algorithms by Yang-Sae Moon

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s Algorithm for the Shortest Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 8 Computer Algorithms by Yang-Sae Moon

연결된 가중치 (포함) 비방향 그래프 Greedy Method v 1 1 v 2 3 3

연결된 가중치 (포함) 비방향 그래프 Greedy Method v 1 1 v 2 3 3 6 4 v 3 v 4 5 2 v 5 Page 10 Computer Algorithms by Yang-Sae Moon

신장 트리의 예 v 1 Greedy Method 1 v 2 3 6 v 4

신장 트리의 예 v 1 Greedy Method 1 v 2 3 6 v 4 v 3 v 1 5 1 v 2 4 v 4 3 v 5 v 3 2 v 5 Page 12 Computer Algorithms by Yang-Sae Moon

최소비용 신장 트리의 예 1 v 1 Greedy Method v 2 3 1 v

최소비용 신장 트리의 예 1 v 1 Greedy Method v 2 3 1 v 1 4 v 3 v 4 3 3 v 4 5 2 v 5 6 4 v 3 2 v 5 앞서의 예제에는 상기 MST 이외에 하나의 MST가 더 있다고 한다. 찾아보기 바람 Page 14 Computer Algorithms by Yang-Sae Moon

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) l

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) l Prim의 알고리즘 l Kruskal의 알고리즘 Dijkstra’s Algorithm for the Shortest Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 17 Computer Algorithms by Yang-Sae Moon

MST – Prim의 알고리즘(구체적) (1/3) Greedy Method 1 v 1 그래프의 인접행렬식 표현 v

MST – Prim의 알고리즘(구체적) (1/3) Greedy Method 1 v 1 그래프의 인접행렬식 표현 v 2 3 3 6 4 v 3 v 4 5 2 v 5 추가적으로 nearest[1. . n]과 distance[1. . n] 배열 유지 • nearest[i] = Y에 속한 노드 중 vi( V-Y)에서 가장 가까운 노드의 인덱스 • distance[i] = vi와 nearest[i]를 잇는 이음선의 가중치 Y wa, b va nearest[a] = b distance[a] = wa, b vb vj vi nearest[i] = j distance[i] = wi, j Page 20 Computer Algorithms by Yang-Sae Moon

MST – Prim의 알고리즘(구체적) (2/3) Greedy Method 알고리즘 void prim(int n, // 입력: 정점의

MST – Prim의 알고리즘(구체적) (2/3) Greedy Method 알고리즘 void prim(int n, // 입력: 정점의 수 const number W[][], // 입력: 그래프의 인접행렬식 표현 set_of_edges& F) // 출력: 그래프의 MST에 속한 이음선의 집합 { 1 v 2 index i, vnear; number min; 3 6 3 edge e; index nearest[2. . n]; 4 v 3 number distance[2. . n]; 2 5 F = empty_set; v 5 for(i=2; i <= n; i++) { // 초기화 nearest[i] = 1; // 초기에는 Y에 노드가 v 1밖에 없음 distance[i] = W[1][i]; // (v 1, vi)의 가중치로 초기화 } // see the next page Page 21 Computer Algorithms by Yang-Sae Moon

MST – Prim의 알고리즘(구체적) (3/3) 1 v 1 Greedy Method 3 3 알고리즘 (계속)

MST – Prim의 알고리즘(구체적) (3/3) 1 v 1 Greedy Method 3 3 알고리즘 (계속) v 2 6 4 v 3 2 v 4 5 repeat(n-1 times) { // n-1개의 정점을 Y에 차례로 추가한다 v 5 min = ; for(i=2; i <= n; i++) { // 각 정점에 대해서 if (0 <= distance[i] < min) { // distance[i]를 검사하여 min = distance[i]; // 가장 가까이 있는 vnear을 vnear = i; // 찾는다. } } e = vnear와 nearest[vnear]를 잇는 이음선; e를 F에 추가; distance[vnear] = -1; // 찾은 노드를 Y에 추가한다. for(i=2; i <= n; i++) if (W[i][vnear] < distance[i]) { // Y에 없는 각 노드에 대해서 distance[i] = W[i][vnear]; // distance[i]와 nearest[i] = vnear; // nearest[i]를 갱신한다. } } // end of repeat } // end of main Page 22 Computer Algorithms by Yang-Sae Moon

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) l

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) l Prim의 알고리즘 l Kruskal의 알고리즘 Dijkstra’s Algorithm for the Shortest Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 30 Computer Algorithms by Yang-Sae Moon

MST – More Improved Algorithms Greedy Method 알고리즘의 시간복잡도는 그 알고리즘을 구현하는데 사용하는 자

MST – More Improved Algorithms Greedy Method 알고리즘의 시간복잡도는 그 알고리즘을 구현하는데 사용하는 자 료구조에 좌우되는 경우도 있다. Page 37 Computer Algorithms by Yang-Sae Moon

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s Algorithm for the Shortest Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 38 Computer Algorithms by Yang-Sae Moon

Dijkstra 알고리즘 (세부적) (2/3) Greedy Method 알고리즘 void dijkstra(int n, const number W[][], set_of_edges&

Dijkstra 알고리즘 (세부적) (2/3) Greedy Method 알고리즘 void dijkstra(int n, const number W[][], set_of_edges& F) { index i, vnear; number min; edge e; index touch[2. . n]; number length[2. . n]; F = empty_set; for(i=2; i <= n; i++) { // 초기화 touch[i] = 1; // 초기에는 Y에 노드가 v 1밖에 없음 length[i] = W[1][i]; // (v 1, vi)의 가중치로 초기화 } // see the next page Page 43 Computer Algorithms by Yang-Sae Moon

Dijkstra 알고리즘 분석 Greedy Method 분석: T(n) (n 2) repeat x 2 -for-loop •

Dijkstra 알고리즘 분석 Greedy Method 분석: T(n) (n 2) repeat x 2 -for-loop • 힙(heap)으로 구현하면 (m lg n)이고, • 피보나찌 힙으로 구현하면 (m + n lg n)이다. 최적여부의 검증(Optimality Proof) • Prim의 알고리즘과 동일하게 증명할 수 있다. Page 45 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 위키 정의 Greedy Method Page 46 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 위키 정의 Greedy Method Page 46 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 예제 (1/2) Greedy Method Page 47 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 예제 (1/2) Greedy Method Page 47 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 예제 (2/2) Greedy Method Page 48 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 예제 (2/2) Greedy Method Page 48 Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 참고 사이트 Page 49 Greedy Method Computer Algorithms by Yang-Sae Moon

A* 알고리즘 – 참고 사이트 Page 49 Greedy Method Computer Algorithms by Yang-Sae Moon

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s Algorithm for the Short Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 50 Computer Algorithms by Yang-Sae Moon

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s

강의 순서 Greedy Method 탐욕적 알고리즘 개요 최소비용 신장 트리 (Minimum Spanning Tree) Dijkstra’s Algorithm for the Short Path Problem 스케줄 짜기 배낭 채우기 문제 (The Knapsack Problem) Page 56 Computer Algorithms by Yang-Sae Moon

Dynamic Programming vs. Greedy Method Page 57 Greedy Method Computer Algorithms by Yang-Sae Moon

Dynamic Programming vs. Greedy Method Page 57 Greedy Method Computer Algorithms by Yang-Sae Moon

Homework #4 Greedy Method Page 69 Computer Algorithms by Yang-Sae Moon

Homework #4 Greedy Method Page 69 Computer Algorithms by Yang-Sae Moon