depth-first-search

How to use DFS on an array

隐身守侯 提交于 2019-12-01 20:31:13
I have a 1 dimensional list of values, it looks like this "int[] values'". I beleive I have converted it to a 2d list like this : for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { board[i][j] = values[i * 4 + j]; } } The board is the new 2 dimensional list of values. On the board there are numbers. A 0 means an empty space, a 1 is a green, a 2 is a blue, and a 3 is a red. How would I use depth first search to find a completed path of a certain color? Make a 2D array boolean[][] visited designating the points that you have visited; set all elements to false Go through each point in

Why is Depth-First Search said to suffer from infinite loops?

混江龙づ霸主 提交于 2019-12-01 15:57:36
I have read about DFS and BFS many times but I have this doubt lingering my mind since long. In a lot of articles it is mentioned that DFS can get stuck in infinite loops. As far as I know, this limitation can easily be removed by keeping track of the visited nodes. In fact, in all the books that I have read, this little check is a part of DFS. So why are 'infinite loops' mentioned as a disadvantage of DFS? Is it just because the original DFS algorithm did not have this check for visited nodes? Please explain. (1) In graph search algorithms [used frequently on AI], DFS's main advantage is

Why is Depth-First Search said to suffer from infinite loops?

假如想象 提交于 2019-12-01 14:42:46
问题 I have read about DFS and BFS many times but I have this doubt lingering my mind since long. In a lot of articles it is mentioned that DFS can get stuck in infinite loops. As far as I know, this limitation can easily be removed by keeping track of the visited nodes. In fact, in all the books that I have read, this little check is a part of DFS. So why are 'infinite loops' mentioned as a disadvantage of DFS? Is it just because the original DFS algorithm did not have this check for visited

Genetic algorithm for optimization in game playing agent heuristic evaluation function

余生颓废 提交于 2019-12-01 14:04:44
This is in response to an answer given in this question: How to create a good evaluation function for a game? , particularly by @David (it is the first answer). Background : I am using a genetic algorithm to optimize the hyper parameters in a game playing agent that is using minimax / alpha beta pruning (with iterative deepening). In particular, I would like to optimize the heuristic (evaluation) function parameters using a genetic algorithm. The evaluation function I use is: f(w) = w * num_my_moves - (1-w) * num_opponent_moves The only parameter to optimize is w in [0,1]. Here's how I

trying to find all the path in a graph using DFS recursive in Python

混江龙づ霸主 提交于 2019-12-01 13:24:04
I have found a solution that was posted some times ago and I have tried to apply it to my exercise but it doesn't work. I have a class graph that has nodes and edges and a method childrenOf that gives all the children of a node. All this works fine. This is my code for the DFS search and I want to find all the paths: def myDFS(graph,start,end,path=[]): path=path+[start] if start==end: return path paths=[] for node in graph.childrenOf(start): if node not in path: paths.extend(myDFS(graph,node,end,path)) return paths I only got empty lists. WHere do I need to look at? When I was doing path=myDFS

DFS shortest path of a maze in C++

二次信任 提交于 2019-12-01 12:40:31
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 goal, but not the shortest one. Any help with this would be greatly appreciated. Writing a non

Does this python code employs Depth First Search (DFS) for finding all paths?

我与影子孤独终老i 提交于 2019-12-01 09:49:43
This code is given in python official essays on graph theory . Here's the code: def find_all_paths(graph, start, end, path=[]): path = path + [start] if start == end: return [path] if not graph.has_key(start): return [] paths = [] for node in graph[start]: if node not in path: newpaths = find_all_paths(graph, node, end, path) for newpath in newpaths: paths.append(newpath) return paths I am not adept at python as I haven't yet had enough of practicing and reading in it. Can you please explain the code by relating this to the child-sibling concept in DFS diagram? Thanks. The key to seeing that

Does this python code employs Depth First Search (DFS) for finding all paths?

假如想象 提交于 2019-12-01 07:35:01
问题 This code is given in python official essays on graph theory. Here's the code: def find_all_paths(graph, start, end, path=[]): path = path + [start] if start == end: return [path] if not graph.has_key(start): return [] paths = [] for node in graph[start]: if node not in path: newpaths = find_all_paths(graph, node, end, path) for newpath in newpaths: paths.append(newpath) return paths I am not adept at python as I haven't yet had enough of practicing and reading in it. Can you please explain

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

微笑、不失礼 提交于 2019-12-01 04:43:13
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 so since Haskell's pureness prevents me to change data while passing parameters. data Graph a = Graph [

Finding all possible paths of n length in hexagonal grid

▼魔方 西西 提交于 2019-11-30 22:52:36
Assume that a function takes s (origin hexagon), f (target hexagon) and n (length of the path) values as parameters and outputs list of all possible paths that has n length. To visualize the problem please check the figure below: Let's say our origin ( s ) is the red dotted hex (2, 3) and the target ( f ) is the blue dotted one (5, 2) . We want to reach blue dotted hex in 5 steps ( n = 5 ). Also consider that if a walk reaches a specific hex, it may stay in that hex as well in the next step. In other words, one of the possible paths can be (3, 2) - (4, 2) - (5, 2) - (5, 2) - (5, 2) . It is