depth-first-search

Is backtracking absolutely necessary for cycle detection using DFS in directed graph?

[亡魂溺海] 提交于 2019-12-06 11:26:22
I came across this SO post where it is suggested that cycle detection using DFS in a directed graph is faster because of backtracking. Here I quote from that link: 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 two

C++ Depth First Search (DFS) Implementation [closed]

落爺英雄遲暮 提交于 2019-12-06 08:46:20
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I am trying to implement the following DFS code described at Competitive Programming 1 book: #include <cstdio> #include <vector> using namespace std; #define MAX 10 #define DFS_BLACK 1 #define DFS_WHITE -1 typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<int> vi; vi dfs_num; vector<vii> adj(MAX); void dfs(int u) { dfs_num[u] = DFS_BLACK; for (int i = 0; i < (int)adj[i].size(); i++) { ii v = adj

Complexity of finding all simple paths using depth first search?

拥有回忆 提交于 2019-12-06 08:04:11
问题 Thanks to everyone replying with ideas and alternate solutions. More efficient ways of solving problems are always welcome, as well as reminders to question my assumptions. That said, I'd like you to ignore for a moment what problem I'm trying to solve with the algorithm, and just help me analyze the big-Oh complexity of my algorithm as I've written it -- an all simple paths in a graph using depth-limited search as described here, and implemented here. Thanks! Edit: This is homework, but I've

What if I do not use G transpose in calculating Strongly Connected Components?

不问归期 提交于 2019-12-06 04:47:22
I am reading Introduction to Algorithms. In 22.5 Strongly Connected Component, the algorithm STRONGLY-CONNECTED-COMPONENT(G) is defined as: Call DFS(G) to compute finishing times u.f for each vertex u Compute G transpose Call DFS(G transpose), but in the main loop of DFS, consider the vertices in order of decreasing u.f(as computed in line 1) Output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component If I change the alogrithm to just using G, without calculating G transpose. Also consider the vertices in order of Increasing u.f

Ruby recursive DFS method

a 夏天 提交于 2019-12-05 20:59:40
I have some troubles with recursive method for depth first search algorithm implementation. Here's the binary tree photo: The method works well with the right side of the tree (55, 89, 144), but it returns nil when it comes to the left side, even though it puts "yes". So, what's wrong with the code? The node is an instance of Node class that has value (integer) and links to the left and right child (other instances of Node class) which is nil if it doesn't have the child from that side. Here's the method code: def depth_first_search(node, target) if node.value == target puts "yes" return node

Depth First Search on 2-D array

杀马特。学长 韩版系。学妹 提交于 2019-12-05 19:05:23
I am trying to learn DFS by creating a program that navigates my ogre through a maze (2d array).This is similar to a dailyprogramming challenge, but I am doing it with just a 1x1 ogre. My maze: static int[][] maze = { {2,1,0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0,0}, {1,0,0,0,0,1,0,1,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,1,1,0,0,0,0,0,0}, {0,0,1,0,0,0,0,1,0,1}, {1,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,0,0,0}, {0,0,0,0,0,1,0,0,0,3}}; Where 2 is my hero (0,0), 3 is my goal (9,9), 1s are obstacles, and 0s are traverseable space. Since I am new to this, I doubt it will be needed, but

Finding all possible paths in graph

大兔子大兔子 提交于 2019-12-05 18:08:57
I'm looking for some algorithm which will help me find all possible paths in a graph. Everything I found so far is not fully satisfying. Let's imagine we have a graph (tree) like this one: And let's use some algorithm like Breadth-First Search or Depth-First Search . In return we'll get something like 1, 2, 4, (2), 5, (2), 6, (2), (1), 3, 7, 8, (7), 9 Which is how we go through this tree and this is not what I'm looking for. I'd love to get all paths, like: 1 1, 2 1, 2, 4 1, 2, 5 1, 2, 6 1, 3 1, 3, 7 1, 3, 7, 8 1, 3, 7, 9 The thing is that I want just to specify root node and algorithm should

Depth-first nested routing in an ASP.NET website

旧街凉风 提交于 2019-12-05 15:07:01
I've been exploring the System.Web.Routing namespace, playing with constraints and such, but I can't see a way to implement this. I'm working on an ASP.NET website ( non-WAP, non-MVC ) using the WebPages/Razor framework. I'm trying to implement a form of "nested routing", where a route can contain child routes that are only attempted if the parent matches; each child attempting to match the "remainder" of the URI. A "depth-first" route match search, if you will. routes.Add(new ParentRoute("{foo}/{*remainder}", new[] { new ParentRoute("{bar}/{*remainder}", new[] { new Route("{baz}"), new Route(

Even length path algorithm

痴心易碎 提交于 2019-12-05 13:19:22
I was asked for my homework to write an efficient algorithm that finds all the vertices in a directed graph which have even length of path to them from the given vertex. This is what I thought of: (It's very similar to "Visit" algorithm of DFS) Visit(vertex u) color[u]<-gray for each v E adj[u] for each w E adj[v] if color[w] = white then print w Visit(w) I think it works but I'm having hard time calculating it's efficiency, especially when the graph is with cycles. Could you help me? If I may suggest an alternative - I would have reduced the problem and use DFS instead of modifying DFS .

How can I calculate the level of a node in a perfect binary tree from its depth-first order index?

梦想与她 提交于 2019-12-05 09:15:33
I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and all leaf nodes are on the same level. Each node has an index in depth-first order. (E.g. in a tree with 3 levels the root node has index 0, the first child has 1, the first child of the first child has 2, the second child of the first child has 3, the second child has 4, the first child of the second child has 5, the second child of the second child has index 6. 0 / \ 1 4 / \ / \ 2 3 5 6 ) I know the size of tree (number of nodes/maximum level), but only the index of a particular node, and