Backtracking nQueens Problem Monte Carlo Technique Graph Coloring
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits 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) } 4 Computer Algorithms by Yang-Sae Moon
깊이우선검색 (Depth-first search) (2/2) Backtracking 예제 5 Computer Algorithms by Yang-Sae Moon
4 -Queens 문제의 상태공간트리 Backtracking 상태공간트리: State Space Tree 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); } 12 Computer Algorithms by Yang-Sae Moon
4 -Queens 문제의 되추적 상태공간트리 (1/2) 13 Backtracking Computer Algorithms by Yang-Sae Moon
4 -Queens 문제의 되추적 상태공간트리 (2/2) 14 Backtracking Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits 17 Computer Algorithms by Yang-Sae Moon
n-Queens Problem – 알고리즘 (2/3) Backtracking Algorithm for the n-Queens Problem void queens(index i) { index j; if(promising(i)) if(i == n) cout << col[1] through col[n]; else for(j=0; j <= n; j++) { col[i+1] = j; // 다음 위치 선택 queens(i+1); } } 최상위 수준 호출: queens(0); 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) { if(col[i] == col[k] || |col[i]-col[k]| == i-k) swicth = false; k++; } return switch; } 21 Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits 28 Computer Algorithms by Yang-Sae Moon
Monte Carlo Technique – n. Queens (3/4) Backtracking Generic Backtracking Algorithm의 효율 추정 알고리즘 int estimate() { node v; int m, mprod, t, numnodes; 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; } 32 Computer Algorithms by Yang-Sae Moon
Monte Carlo Technique – n. Queens (4/4) 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; 33 Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits 34 Computer Algorithms by Yang-Sae Moon
지도와 평면 그래프 Backtracking 37 Computer Algorithms by Yang-Sae Moon
Graph Coloring – Backtracking 3 -Coloring 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=0; color <= m; color++) { vcolor[i+1] = color; // 다음 정점에 모든 색을 시도해 본다. m_coloring(i+1); } } 최상위 수준 호출: m_coloring(0); 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에 대해 “인접하고, 색이 같은지”의 여부를 조사한다. 40 Computer Algorithms by Yang-Sae Moon
강의 순서 Backtracking 되추적 기술 n-Queens Problem Monte Carlo Technique Graph Coloring Hamiltonian Circuits 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++) { // 모든 정점의 다음 정점을 vcolor[i+1] = j; // 시도해 본다. hamiltonian(i+1); } } 최상위 수준 호출: vindex[0] = 1; hamiltonian(0); 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번째 정점은 else { // (i-1)번째 정점과 인접해야 한다. while(j < i && switch) { if(vindex[i] == vindex[j]) // 정점이 이미 선택되었는지 swicth = false; // 검사한다. j++; } } return switch; } 46 Computer Algorithms by Yang-Sae Moon
- Slides: 47