|
|
Depth-first searchDepth-first search (DFS) is an algorithm for traversing or searching a tree data structure, tree structure, or graph (data structure). Intuitively, you start at the root (selecting some node as the root in the graph case) and explore as far as possible along each branch before backtracking. Formally, DFS is an uninformed search algorithm that progresses by expanding the first child node of the search tree data structure that appears and thus going deeper and deeper until a goal state is found, or it hits a node that has no children. Then the search backtracking and starts off on the next node. All freshly expanded nodes are added to a LIFO queue (stack (computing)) for expansion. * DFS is complete: if the tree is finite, then a solution would be found if one exists. * DFS is not optimality, even with a finite tree and all non-negative path costs. Space complexity of DFS is much lower compared to BFS (breadth-first search). It also lends itself much better to heuristics methods of choosing a likely-looking branch. When searching large graphs that can not be fully contained in memory, DFS suffers from non-termination when the length of a path in the search tree is infinite. The simple solution of "remember which nodes I have already seen" doesn't work because there can be insufficient memory. This can be solved by maintaining an increasing limit on the depth of the tree, which is called iterative deepening depth-first search. For the following graph: a depth-first search starting at A, assuming that the left edges in the shown graph are chosen before right edges, and assuming the search remembers previously-visited nodes and will not repeat them (since this is a small graph), will result in the search visiting the nodes in the following order: A, B, D, F, E, C, G. If the search did not remember previously visited nodes, but given the other conditions in the previous paragraph, the search would proceed as A, B, D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E cycle and never reaching C or G. Iterative deepening prevents this loop and will reach the following nodes on the following depths, assuming it proceeds left-to-right as above: *0: A *1: A (repeated), B, C, E (Note that iterative deepening has now seen C, when a conventional depth-first search did not.) *2: A, B, D, F, C, G, E, F (Note that it still sees C, but that it came later. Also note that it sees E via a different path, and loops back to F twice.) *3: A, B, D, F, E, C, G, E, F, B For this graph, as more depth is added, the two cycles "ABFE" and "AEFB" will simply get longer before the algorithm gives up and tries another branch. ==Pseudocode== function DFS(Start, Goal) push(Stack,Start) while Stack is not empty var Node := Pop(Stack) Color(Node, Grey) if Node = Goal return Node for Child in Expand(Node) if Node.color = White push(Stack, Child) Color(Node, Black) == See Also == * Breadth-first search * List of algorithms == External links == * [http://www.boost.org/libs/graph/doc/depth_first_search.html C++ Boost Graph Library: Depth-First Search] * [http://www.cs.duke.edu/csed/jawaa/DFSanim.html Depth-First Search Animation (for a directed graph)] Search algorithms Trees (structure) Depth-first searchChanged from "DFS is optimal" to "DFS is not optimal". Please Confirm. ----- can you show me the program in c for dfs ----- Whoever "corrected" the page to put E on the end of the path, please do not un-correct it again. DFS will follow the link from F to E before getting to E from A. The ''apparent'' tree view does not reflect the actual DFS tree, and I chose it that way deliberately for didactic purposes. User:Jerf ----- Can you explain what ''all non-negative path costs'' is refering to ? ----- DFS's can be preformed for graphs in general as well, not just Trees. Though if you're thinking specifically of the technique as used in state-space search's I guess not. Perhaps, these two concepts should be seperated? User:Thesilverbail 01:39, 17 Jan 2004 (UTC) ''They're awfully closely related... I've tried to improve the situation in my last edit, but there still needs to be some more seperation of "depth-first traversal" and "depth-first search". User:Jerf'' ----- Why does the page say that DFS is a method for traversing trees, and then goes on to give an example of a general graph? (as it clearly has a cycle it is not a tree). The page should either state that it is a method for traversing graphs, or change the example to a tree. See other meanings of words starting from letter: DDA | DB | DC | DE | DF | DG | DH | DI | DJ | DK | DL | DM | DN | DO | DP | DR | DS | DT | DU | DW | DX | DY | DZ |Words begining with Depth-first_search: Depth-first_search Depth-first_search
Sponsored links: praca, nurkowanie.
|
These materials are based on Wikipedia and licensed under the GNU FDL
YouTube.com videos better site than Turbo Tax 2007 |
|
|