Backtracking nQueens Problem Monte Carlo Technique Graph Coloring
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits Page 3 Computer Algorithms by Yang-Sae Moon
깊이우선검색 (Depth-first search) (1/2) Backtracking 루트 노드(root node)를 먼저 방문한 뒤, 그 노드의 모든 후손 노드(descendant)들을 차례로(보통 왼쪽에서 오른쪽으로) 방문한다. (= preorder tree traversal). void depth_first_tree_search(node v) { node u; visit v; for (each child u of v) // from left to right depth_first_tree_search(u) } Page 4 Computer Algorithms by Yang-Sae Moon
Backtracking 예제 Page 5 Computer Algorithms by Yang-Sae Moon
4 -Queens 문제의 상태공간트리 Backtracking 상태공간트리: State Space Tree i번째 말이 j번째 열에… Page 8 Computer Algorithms by Yang-Sae Moon
일반적인 되추적 알고리즘 Backtracking void checknode(node v) { if (promising(v)) if (there is a solution at v) write the solution; else for (each child u of v) checknode(u); } void depth_first_tree_search(node v) { node u; visit v; for (each child u of v) // from left to right depth_first_tree_search(u) } Page 12 Computer Algorithms by Yang-Sae Moon
4 -Queens 문제의 되추적 상태공간트리 (1/2) Page 13 Backtracking Computer Algorithms by Yang-Sae Moon
4 -Queens 문제의 되추적 상태공간트리 (2/2) Page 14 Backtracking Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits Page 17 Computer Algorithms by Yang-Sae Moon
n-Queens Problem – 알고리즘 (2/3) Backtracking Algorithm for the n-Queens Problem void checknode(node v) { if (promising(v)) if (there is a solution at v) write the solution; else for (each child u of v) checknode(u); } void queens(index i) { index j; if(promising(i)) if(i == n) cout << col[1] through col[n]; else for(j=1; j <= n; j++) { col[i+1] = j; // 다음 위치 선택 queens(i+1); } } 최상위 수준 호출: queens(0); Page 20 Computer Algorithms by Yang-Sae Moon
n-Queens Problem – 알고리즘 (3/3) Backtracking Algorithm for the n-Queens Problem (계속) bool promising(index i) { index k = 1; bool switch = true; while(k < i && switch) { // k <= n if(col[i] == col[k] || |col[i]-col[k]| == |i-k|) swicth = false; k++; } return switch; } Page 21 Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits Page 28 Computer Algorithms by Yang-Sae Moon
Monte Carlo Technique – n. Queens (3/4) - skip Backtracking Generic Backtracking Algorithm의 효율 추정 알고리즘 int estimate() { node v; int m, mprod, t, numnodes; // mprod = 각 수준의 m 0 m 1. . . mi-1 // m = 각 수준의 유망 자식 노드 개수 v = 상태공간트리의 루트; numnodes = 1; mprod = 1; while(m != 0) { t = v의 자식노드의 개수; mprod = mprod * m; numnodes = numnodes + mprod * t; m = v의 유망한 자식노드의 개수; if(m != 0) v = 무작위로 선택한 v의 자식노드; } return numnodes; // visit할 노드의 추정치 } Page 32 Computer Algorithms by Yang-Sae Moon
Monte Carlo Technique – n. Queens (4/4) - skip Backtracking n-Queens Algorithm의 효율 추정 알고리즘 int estimate_nqueens(int n) { index i, j, col[1. . n]; int m, mprod, t, numnodes; set_of_index prom_children; } i = 0; numnodes = mprod = 1; while(m != 0 && i != n) { mprod = mprod * m; numnodes = numnodes + mprod * t; i++; m = prom_children = 0; for(j=1; j <= n; j++) { col[i] = j; if(promising(i)) { m++; prom_children = prom_children {j}; } } if(m != 0) j = prom_children에서 무작위로 선택한 인덱스; col[i] = j; } return numnodes; Page 33 Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits Page 34 Computer Algorithms by Yang-Sae Moon
지도와 평면 그래프 Backtracking Page 37 Computer Algorithms by Yang-Sae Moon
Graph Coloring – Backtracking 3 -Coloring Page 38 Computer Algorithms by Yang-Sae Moon
Graph Coloring – Algorithm (1/2) Backtracking Algorithm for the Graph Coloring Problem • 입력: n – 노드의 수, m – 색깔의 수, W[i][j] – 이음선 있으면 true, 그렇지 않으면 false • 출력: vcolor[i] - i번째 노드에 할당된 색(1 ~ m)이다. void m_coloring(index i) { index color; if(promising(i)) if(i == n) cout << vcolor[1] through vcolor[n]; else for(color=1; color <= m; color++) { vcolor[i+1] = color; // 다음 노드에 모든 색을 시도해 본다. m_coloring(i+1); } } 최상위 수준 호출: m_coloring(0); Page 39 Computer Algorithms by Yang-Sae Moon
Graph Coloring – Algorithm (2/2) Backtracking Algorithm for the Graph Coloring Problem (계속) bool promising(index i) { index j = 1; bool switch = true; while(j < i && switch) { if(W[i][j] && vcolor[i] == vcolor[j]) swicth = false; j++; } return switch; } 모든 vertex에 대해 “인접하고, 색이 같은지”의 여부를 조사한다. Page 40 Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits Page 42 Computer Algorithms by Yang-Sae Moon
Hamiltonian Circuits – Algorithm (1/2) Backtracking Algorithm • 입력: n – 노드의 수, W[i][j] – 이음선 있으면 true, 그렇지 않으면 false • 출력: vindex[i] – 경로상에서 i번째 노드의 인덱스 void hamiltonian(index i) { index j; if(promising(i)) if(i == n-1) cout << vindex[0] through vindex[n-1]; else for(j=2; j <= n; j++) { // 모든 노드의 다음 노드를 vindex[i+1] = j; // 시도해 본다. hamiltonian(i+1); // j=2인 이유: 출발점은 제외 } } 최상위 수준 호출: vindex[0] = 1; hamiltonian(0); Page 45 Computer Algorithms by Yang-Sae Moon
Hamiltonian Circuits – Algorithm (2/2) Backtracking Algorithm (계속) bool promising(index i) { index j=1; bool switch = true; if(i == n-1 && !W[vindex[n-1]][vindex[0]]) switch = false; // 첫 번째와 마지막 노드는 이웃해야 한다. else if(i > 0 && !W[vindex[i-1]][vindex[i]]) switch = false; // i번째와 (i-1)번째 노드는 이웃해야 한다. else { while(j < i && switch) { if(vindex[i] == vindex[j]) // 노드가 이미 선택되었는지 swicth = false; // 검사한다. j++; } } return switch; } Page 46 Computer Algorithms by Yang-Sae Moon
- Slides: 47