breadth-first-search

Same result for two arrays of counters of different algorithms

≯℡__Kan透↙ 提交于 2020-01-23 17:12:30
问题 I'm trying to build up a program comparing number of strokes of algorithms BFS, DFS, A* (which has two heuristics) on a game of fifteen. My counter count the same result for the two arrays of counters of BFS and DFS and both A*. Yet, I'm using actually using four different arrays from a main (class Project) and I'm assigning four different variables for those strokes. The part of the code that isn't right is, to my mind, a while loop which explores the son of a vertice as far as possible (for

Choose numbers which sum to zero

不打扰是莪最后的温柔 提交于 2020-01-14 04:51:30
问题 There is N number of sets, each containing various number of integers, e.g.: (-2, -1, 0), (-1,4), (-2, 2, 3), (-3, -2, 4, 6), (-2) How to pick exactly one number from each set so that these N integers sum to zero? For example: -1, -1, 2, -2, 4, -2 Note there might be no solution or multiple (in which case it doesn't matter which one I choose). I was thinking that I can do breath-first search but was wondering if there are any other, preferably faster, ways to solve this. 回答1: Let dp[i, j] =

breadth first or depth first search

血红的双手。 提交于 2020-01-12 02:21:10
问题 I know how this algorithm works, but cant decide when to use which algorithm ? Are there some guidelines, where one better perform than other or any considerations ? Thanks very much. 回答1: If you want to find a solution with the shortest number of steps or if your tree has infinite height (or very large) you should use breadth first. If you have a finite tree and want to traverse all possible solutions using the smallest amount of memory then you should use depth first. If you are searching

Breadth First enumeration in Gremlin

心不动则不痛 提交于 2020-01-11 04:10:06
问题 I'm trying to get breadth first enumeration working with Gremlin, however I'm having trouble finding a way to output all the steps observed during the enumeration. I can only print out the result of the last iteration. My question would be, given a starting node like this, how can I follow all paths (without knowing the overall depth) using Gremlin and print out everything I find along the way? study=g.v('myId') I have tried the scatter approach, looping approach (although both seem to

Breadth First Search in Prolog

佐手、 提交于 2020-01-11 03:32:28
问题 I'm new to Prolog and currently implementing DFS (depth-first search) and BFS (breadth-first search) algorithms. My DFS works fine as the code below, but the BFS is terminated and aborted when it reaches the leaf node (it doesn't backtrack and continue searching). I also read some sample code about this but there are some functions they don't define like s(Node, NewNode)... so it's hard to understand, or the version use Queue is too complicated. Here is my code: Some ground functions:

Find all nodes in a binary tree on a specific level (Interview Query)

落花浮王杯 提交于 2020-01-11 03:31:05
问题 I mean on a specific level, NOT up to that specific level. Could someone please check my modified BFS algorithm? (most of which is taken from Wikipedia) Queue levelorder(root, levelRequested){ int currentLevel = 0; q = empty queue q.enqueue(root) while not q.empty do{ if(currentLevel==levelRequested) return q; node := q.dequeue() visit(node) if(node.left!=null || node.right!=null){ currentLevel++; if node.left ≠ null q.enqueue(node.left) if node.right ≠ null q.enqueue(node.right) } } } 回答1: I

How to keep track of BFS depth in C++

断了今生、忘了曾经 提交于 2020-01-07 02:38:27
问题 I want to do BFS on a 2D array, and each of the cell can be represented as pair<int,int> . I use queue<pair<int,int>> to keep track of cells at each level, but I find no clever way to keep track of the depth. (I don't want to define another struct to record the depth at each level) How to keep track of depth in breadth first search? Here is a solution in Java. Each level is ended with null , and we increment the depth once we see null . I am wondering if there's a similar elegant solution in

Use list of lists to save every path in a graph

安稳与你 提交于 2020-01-06 06:39:28
问题 I have to implement dfs algorithm to save all paths from a starting node. So for example i have the following graph: i have a list path = [] to save all the paths starting from node 1. So my list will have only the starting node 1: 1, then i will check for neighbors of 1 which is the node 2 and the list will be: [1,2] . Now i check the neighbors of 2 which are 3 and 4. The list now i think that it will be like [[1,2,3], [1,2,4]] and in the end the final list will be [[1,2,3], [1,2,4,5], [1,2

Breadth first search in prolog gives me an error

断了今生、忘了曾经 提交于 2020-01-05 05:45:11
问题 This is my code for breadth first search strategy in prolog : s(a, b). s(a, c). s(b, g). s(b, f). s(c, r). s(c, e). goal(g). solve( Start, Solution) :- breadthlirst( [ [Start] ], Solution). breadthfirst( [ [Node | Path] |_], [Node | Path] ) :- goal( Node). breadthfirst( [ [N | Path] | Paths], Solution) :- bagof([M,N|Path], ( s( N, M), \+ member( M, [N | Path] ) ), NewPaths), conc( Paths, NewPaths, Pathsl), !, breadthfirs( Pathsl, Solution); breadthfirst( Paths, Solution). But when I run this

how can a breadth-first-search-tree include a cross-edge?

我只是一个虾纸丫 提交于 2020-01-04 03:49:24
问题 Well, I know that a breadth-first-search-tree of an undirected graph can't have a back edge. But I'm wondering how can it even have a cross-edge? I'm not able to image a spanning tree of a graph G constructed out of OFS, that contains a cross-edge. 回答1: The process of building a spanning tree using BFS over an undirected graph would generate the following types of edges: Tree edges Cross edges (connecting vertices on different branches) A simple example: Imagine a triangle (a tri-vertice