depth-first-search

Edge classification in a DFS

无人久伴 提交于 2019-11-29 01:03:22
问题 According to the book (Intro to Algorithm), in dfs, edges are classified as 4 kinds: Tree Edge, if in edge (u,v), v is first discovered, then (u, v) is a tree edge. Back Edge, if ......, v is discovered already and v is an ancestor, then it's a back edge. Forward Edge, if ......, v is discovered already and v is a descendant of u, forward edge it is. Cross Edge, all edges except for the above three. My question is how can I identify whether v is u's ancestor or descendant when I'm trying to

Is Pre-Order traversal on a binary tree same as Depth First Search?

我是研究僧i 提交于 2019-11-29 00:39:27
问题 It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am wrong? Thanks in advance! 回答1: Pre-order is one type of DFS. There are three types of depth-first traversal: pre-order, in-order, and post-order. Check out here for more info. 回答2: It probably depends on the definition and implementation of the depth-first algorithm. The DefaultMutableTreeNode class of the Java Swing's

Topological sort using DFS without recursion

左心房为你撑大大i 提交于 2019-11-28 23:16:52
问题 I know the common way to do a topological sort is using DFS with recursion. But how would you do it using stack<int> instead of recursion? I need to obtain the reversed post-order but I'm kinda stuck: The graph is a vector<vector<int> > adjacency list The following is the DFS which I want to use for topological sort bool visited[MAX]={0}; stack<int> dfs, postOrder; vector<int> newVec; vector<int>::iterator it; for(int i=0;i<MAX;i++){ if(visited[i]==false){ dfs.push(i); } while(!dfs.empty()){

deceptively simple implementation of topological sorting in python

天涯浪子 提交于 2019-11-28 23:15:23
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 "minimal"? I've watched this video and the idea is quite clever so I was wondering if it'd be possible to

Unexpected result of my DFS tree (C++)

☆樱花仙子☆ 提交于 2019-11-28 11:36:36
问题 I have solved this problem!!! I found that if i have to use vector<Node*> children; . But I am not very sure the reason, can someone tell me why? Thanks:) Question: I use test.cpp to generate a tree structure like: The result of (ROOT->children).size() is 2 , since root has two children. The result of ((ROOT->children)[0].children).size() should be 2 , since the first child of root has two children. But the answer is 0 , why? It really confuse for me. test.cpp (This code is runnable in visual

Depth-first flattened collection of an object hierarchy using LINQ

廉价感情. 提交于 2019-11-28 08:58:50
问题 I have an object hierarchy (MasterNode -> ChildNodes) where master and child nodes are of the same type, and there are only two levels (top level and children) like this ('A' is parent of D,E and F, 'B' is parent of G, etc) A--+ | D | E | F | B--+ | G | C--+ H I Suppose I have a MasterNodes as an IEnumerable of the parent objects (A,B,C) and given a parent object X I can get an IEnumerable of its children by X.children I know that I can enumerate all of the leaf (child nodes) with the

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

做~自己de王妃 提交于 2019-11-27 06:38:25
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. 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 misconception - it is simply not true. The classic DFS algorithm cannot be obtained by replacing the BFS queue with

Why DFS and not BFS for finding cycle in graphs

∥☆過路亽.° 提交于 2019-11-27 06:00:43
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. Mark Byers 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 you got there. Otherwise you might think you have found a cycle but in reality all you have is

Find all cycles in graph, redux

泄露秘密 提交于 2019-11-27 02:53:01
问题 I know there are a quite some answers existing on this question. However, I found none of them really bringing it to the point. Some argue that a cycle is (almost) the same as a strongly connected components (s. Finding all cycles in a directed graph) , so one could use algorithms designed for that goal. Some argue that finding a cycle can be done via DFS and checking for back-edges (s. boost graph documentation on file dependencies). I now would like to have some suggestions on whether all

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

时光总嘲笑我的痴心妄想 提交于 2019-11-27 02:28:17
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? Hans-Peter Störr 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 first search (DFS) might take an extremely long time, but BFS could be faster. If the tree is