반응형
깊이 우선 탐색(depth-first search: DFS)은 맹목적 탐색방법의 하나로 탐색트리의 최근에 첨가된 노드를 선택하고, 이 노드에 적용 가능한 동작자 중 하나를 적용하여 트리에 다음 수준(level)의 한 개의 자식노드를 첨가하며, 첨가된 자식 노드가 목표노드일 때까지 앞의 자식 노드의 첨가 과정을 반복해 가는 방식이다.
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.
💊 알고리즘 장단점 (Algorithm Advantage/Disadvantage)
😺 장점 (Advantage) |
|
😿 단점 (Disadvantage) |
|
💊 알고리즘 복잡도 (Algorithm Complcomplexity)
⏰ 시간 복잡도 (Time Complcomplexity) | O (|V| + |E|) |
📦 공간 복잡도 (Space Complcomplexity) | O (|V|) |
📄 [자료구조] Reculsive DFS C++ / Pseudo Source Code
void recalsiveDFS(int index, int length) {
isVisit[index] = true;
cout << index << endl;
for (int ii = 0; ii < length; ii++) {
if (!isVisit[ii] && vertaxs[index][ii] == 1) { recalsiveDFS(ii, length); }
}
}
procedure DFS(G,v):
label v as discovered
for all directed edges from v to w that are in G.adjacentEdges(v) do
if vertex w is not labeled as discovered then
recursively call DFS(G,w)
📄 [자료구조] Stack DFS Mode C++ / Pseudo Source Code
void stackDFS(int index, int length) {
stack<int> stx;
stx.push(index);
isVisit[index] = true;
while (!stx.empty()) {
const int box = stx.top(); stx.pop();
for (int ii = 0; ii < length; ii++) {
if (!isVisit[ii] && vertaxs[box][ii] == 1) {
stx.push(ii);
isVisit[ii] = true;
}
}
// Output
cout << box << endl;
}
}
procedure DFS-iterative(G,v):
let S be a stack
S.push(v)
while S is not empty
v = S.pop()
if v is not labeled as discovered:
label v as discovered
for all edges from v to w in G.adjacentEdges(v) do
S.push(w)
🚀 REFERENCE
반응형
'#컴퓨터 과학 [Computer Science] > 알고리즘 [Algorithm]' 카테고리의 다른 글
[알고리즘] 📚 용어 정리 (0) | 2024.03.05 |
---|---|
[Algorithm - Concept] AVL Tree (named after inventors Adelson-Velsky and Landis) (0) | 2019.09.04 |
[Algorithm - Concept] 플로이드-워셜 알고리즘 (Floyd-Warshall Algorithm) (0) | 2019.08.29 |
[Algorithm - Concept] 트라이 알고리즘 (Trie Algorithm) (0) | 2019.08.27 |
[정렬] 퀵 정렬 (Quick Sort) (0) | 2019.04.16 |
댓글