depth-first-search

deceptively simple implementation of topological sorting in python

陌路散爱 提交于 2019-11-27 01:50:34
问题 Extracted from here we got a minimal iterative dfs routine, i call it minimal because you can hardly simplify the code further: def iterative_dfs(graph, start, path=[]): q = [start] while q: v = q.pop(0) if v not in path: path = path + [v] q = graph[v] + q return path graph = { 'a': ['b', 'c'], 'b': ['d'], 'c': ['d'], 'd': ['e'], 'e': [] } print(iterative_dfs(graph, 'a')) Here's my question, how could you transform this routine into a topological sort method where the routine also becomes

How to implement depth first search for graph with non-recursive aprroach

我与影子孤独终老i 提交于 2019-11-27 00:09:43
Well, I have spent lots of time on this issue. However, I only can find solutions with non-recursive methods for a tree: Non recursive for tree , or recursive method for the graph, Recursive for graph . And lots of tutorials(I don't provide those links here) don't provide the approaches as well. Or the tutorial is totally incorrect. Please help me. Updated: It's really hard to describe: If I have a undirected graph: 1 / | \ 4 | 2 3 / 1-- 2-- 3 --1 is a cycle. At the step: push the neighbors of the popped vertex into the stack WHAT'S THE ORDER OF THE VERTEXES SHOULD BE PUSHED? If the pushed

When is it practical to use Depth-First Search (DFS) vs Breadth-First Search (BFS)?

孤街浪徒 提交于 2019-11-26 22:14:10
问题 I understand the differences between DFS and BFS, but I'm interested to know when it's more practical to use one over the other? Could anyone give any examples of how DFS would trump BFS and vice versa? 回答1: That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items). If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth

Iterative DFS vs Recursive DFS and different elements order

只愿长相守 提交于 2019-11-26 21:29:41
I have written a recursive DFS algorithm to traverse a graph: void Graph<E, N>::DFS(Node n) { std::cout << ReadNode(n) << " "; MarkVisited(n); NodeList adjnodes = Adjacent(n); NodeList::position pos = adjnodes.FirstPosition(); while(!adjnodes.End(pos)) { Node adj = adjnodes.ReadList(pos); if(!IsMarked(adj)) DFS(adj); pos = adjnodes.NextPosition(pos); } } Then I have written an iterative DFS algorithm using a stack: template <typename E, typename N> void Graph<E, N>::IterativeDFS(Node n) { Stack<Node> stack; stack.Push(n); while(!stack.IsEmpty()) { Node u = stack.Read(); stack.Pop(); if(

Why is depth-first search claimed to be space efficient?

和自甴很熟 提交于 2019-11-26 12:05:44
问题 In an algorithms course I\'m taking, it\'s said that depth-first search (DFS) is far more space efficient than breadth-first search (BFS). Why is that? Although they are basically doing the same thing, in DFS we\'re stacking the current node\'s successors while in BFS we\'re enqueueing the successors. 回答1: Your confusion is stemming from the fact that you apparently assume that DFS algorithm can be obtained from BFS algorithm by replacing the FIFO queue with a LIFO stack. This is a popular

Why DFS and not BFS for finding cycle in graphs

前提是你 提交于 2019-11-26 11:48:41
问题 Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph. 回答1: Depth first search is more memory efficient than breadth first search as you can backtrack sooner. It is also easier to implement if you use the call stack but this relies on the longest path not overflowing the stack. Also if your graph is directed then you have to not just remember if you have visited a node or not, but also how

How to Traverse an NLTK Tree object?

元气小坏坏 提交于 2019-11-26 10:02:43
问题 Given a bracketed parse, I could convert it into a Tree object in NLTK as such: >>> from nltk.tree import Tree >>> s = \'(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))\' >>> Tree.fromstring(s) Tree(\'ROOT\', [Tree(\'S\', [Tree(\'NP\', [Tree(\'NNP\', [\'Europe\'])]), Tree(\'VP\', [Tree(\'VBZ\', [\'is\']), Tree(\'PP\', [Tree(\'IN\', [\'in\']), Tree(\'NP\', [Tree(\'DT\', [\'the\']), Tree(\'JJ\', [\'same\']), Tree(\'NNS\', [\'trends\'])])])]),

Iterative DFS vs Recursive DFS and different elements order

时光总嘲笑我的痴心妄想 提交于 2019-11-26 07:57:18
问题 I have written a recursive DFS algorithm to traverse a graph: void Graph<E, N>::DFS(Node n) { std::cout << ReadNode(n) << \" \"; MarkVisited(n); NodeList adjnodes = Adjacent(n); NodeList::position pos = adjnodes.FirstPosition(); while(!adjnodes.End(pos)) { Node adj = adjnodes.ReadList(pos); if(!IsMarked(adj)) DFS(adj); pos = adjnodes.NextPosition(pos); } } Then I have written an iterative DFS algorithm using a stack: template <typename E, typename N> void Graph<E, N>::IterativeDFS(Node n) {

Breadth First Vs Depth First

点点圈 提交于 2019-11-26 03:46:43
问题 When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great. 回答1: These two terms differentiate between two different ways of walking a tree. It is probably easiest just to exhibit the difference. Consider the tree: A / \ B C / / \ D E F A depth first traversal would visit the nodes in this order A, B, D, C, E, F Notice that you go all the way down one leg before moving on. A breadth first traversal would visit