depth-first-search

DFS shortest path of a maze in C++

梦想与她 提交于 2019-12-19 11:34:07
问题 I'm having trouble figuring out how exactly to get this to work... I'm attempting to get the shortest path to the goal using a DFS. I know that BFS is better but I was asked to use DFS for this. As you can see, I attempt to make a comparison between all stacks that lead to the end to find the goal, but it does not work, only the first stack that leads to the goal is ever printed... I know somewhere I need to unvisit nodes, but I cannot figure out exactly how. Right now I do get a path to the

DFS shortest path of a maze in C++

别等时光非礼了梦想. 提交于 2019-12-19 11:34:07
问题 I'm having trouble figuring out how exactly to get this to work... I'm attempting to get the shortest path to the goal using a DFS. I know that BFS is better but I was asked to use DFS for this. As you can see, I attempt to make a comparison between all stacks that lead to the end to find the goal, but it does not work, only the first stack that leads to the goal is ever printed... I know somewhere I need to unvisit nodes, but I cannot figure out exactly how. Right now I do get a path to the

Is there way to represent static data in Haskell? Or is there any other elegant algorithm for DFS traversal in Haskell?

蓝咒 提交于 2019-12-19 07:51:18
问题 I am trying to construct DFS tree using recursive algorithm. Pseudo code for this is : DFF(G) Mark all nodes u as unvisited while there is an unvisited node u do DFS(u) . DFS(u) Mark u as visited for each v in u's neighbor do if v is not marked DFS(v) While I can fairly easily accomplish this in imperative language in simple way by constructing some sort of data structure for un/visited nodes, assigning them dynamic allocation or some sort of declaration, for Haskell, it is impossible to do

Finding the longest cycle in a directed graph using DFS

浪子不回头ぞ 提交于 2019-12-18 12:16:50
问题 I need to find the longest cycle in a directed graph using DFS. I once saw this Wikipedia article describing the way of doing this, and I think it approached the problem something like marking the node with one of three states: Node not yet visited, Finished searching the node, and Node visited, but not yet finished visiting. I would be grateful if anyone could share the link with me. By the way, it isn't Tarjan's Algorithm. The problem below is what I'm trying to solve, in case you'd like to

Detecting cycles in a graph using DFS: 2 different approaches and what's the difference

。_饼干妹妹 提交于 2019-12-18 09:55:25
问题 Note that a graph is represented as an adjacency list. I've heard of 2 approaches to find a cycle in a graph: Keep an array of boolean values to keep track of whether you visited a node before. If you run out of new nodes to go to (without hitting a node you have already been), then just backtrack and try a different branch. The one from Cormen's CLRS or Skiena: For depth-first search in undirected graphs, there are two types of edges, tree and back. The graph has a cycle if and only if there

Detecting cycles in a graph using DFS: 2 different approaches and what's the difference

浪尽此生 提交于 2019-12-18 09:55:09
问题 Note that a graph is represented as an adjacency list. I've heard of 2 approaches to find a cycle in a graph: Keep an array of boolean values to keep track of whether you visited a node before. If you run out of new nodes to go to (without hitting a node you have already been), then just backtrack and try a different branch. The one from Cormen's CLRS or Skiena: For depth-first search in undirected graphs, there are two types of edges, tree and back. The graph has a cycle if and only if there

What's the good of using 3 states for a vertex in DFS?

匆匆过客 提交于 2019-12-18 04:54:07
问题 In the explanation of depth-first search (DFS) in Algorithms in a Nutshell (2nd edition) , the author used 3 states for a vertex, say white (not visited), gray (has unvisited neighbors), black (visited). Two states ( white and black ) are enough for a traverse. Why add the gray state? What's it used for? 回答1: This is a variation of the DFS algorithm shown in Introduction to Algorithms by Coerman at al. When you use 3 colors instead of only 2, it gives you more information. Fist, it allows you

code for cycle detection is not finding returning the right number of cycles in directed graph in Java?

不羁的心 提交于 2019-12-13 21:24:23
问题 I have code written for calculating the no of cycles in a directed graph using DFS . The method to check if a cycle exists or not works fine. I now iterate through all the vertices (which I have in a HashMap) and check if a vertex is unvisited, then check if a cycle exists, if so increment the counter by 1. Now the code breaks, it does not give the right number of cycles eg: for the graph with following edges: (A B),(B C),(C E),(E A),(B E) Here is my code; public int getTotalCyclesinDir(){

recursive implementation of depth first search to find path between two nodes in Java

狂风中的少年 提交于 2019-12-13 19:21:58
问题 I am trying to implement the recursive version of DFS for depth first search between two nodes. Here is my code. The method will return true if there is a path that exists and it also updates the prev field of the node to keep track of the path. I have implemented this non-recursive way using stacks and it works fine which is here.. This one is not working. That is it is not giving me the path from Node A to Node B. It always seem to return false. public boolean recursiveDFS(String start,

Number of possible paths in android pattern lock

荒凉一梦 提交于 2019-12-13 19:21:57
问题 How many paths possible in android pattern lock? I thought it can be calculated simply by factorial, with formula (9!)/(9-length)! Examples: For length 9, there are 9*8*7*6*5*4*3*2*1 paths. For length 8, there are 9*8*7*6*5*4*3*2 paths. For length 7, there are 9*8*7*6*5*4*3 paths. etc. Here is the code for calculating this: def paths_of_length(number_of_staring_points, length_of_path): print("number_of_staring_points", number_of_staring_points, "length_of_path", length_of_path) different