breadth-first-search

Understanding perf detail when comparing two different implementations of a BFS algorithm

无人久伴 提交于 2019-12-02 06:57:40
The results below are measured using perf on a compute server with 32 cores. I know my implementation is unoptimized but purposely as I want to make comparisons. I understand that graph algorithms tend to have low locality which researchers try to address. I'm unclear of the results, though. The time elapsed is misleading. My implementation runs through a graph with about 4mm nodes in about 10 seconds and the rest of the time pre processing. The optimized version uses the same input and traverses about 10 times with each less than a second each so it's really just pre-processing time. I'm not

Shortest path in matrix with obstacles with cheat paths

被刻印的时光 ゝ 提交于 2019-12-02 05:46:39
First of all this is an assingment and I am not looking for direct answers but instead the complexity of the best solution as you might be thinking it . This is the known problem of shortest path between 2 points in a matrix (Start and End) while having obstacles in the way. Moves acceptables is up,down,left and right . Lets say when moving i carry sth and the cost of each movement is 2 . There are points in the matrix (lets name them B points) where I can leave this sth in one B point and pick it up from another B point . Cost of dumping sth in B point is 1 and cost of picking sth up from a B

BFS for arithmetic operations

£可爱£侵袭症+ 提交于 2019-12-02 05:09:07
Convert a number m to n with minimum operations. The operations allowed were subtraction by 1 and multiplication by 2. For Eg : 4 and 6. Answer is 2. 1st operation : -1 -> 4-1 = 3. 2nd operation : * -> 3 * 2 =6. I am using BFS approach for a particular input (src=26, dst= 5) it is taking long time. Am I doing something wrong? from queue_class import queue class node: def __init__(self, value, level, parent): self.level = level self.value = value self.parent = parent def get_minimum_distance(src, target, q): if src == target: return 0 seen_list = [] data = node(src, 0, -1) q.enqueue(data) while

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

Romania's Cities Breadth-First Search using matlab

佐手、 提交于 2019-12-01 12:23:56
问题 I'm working on a project that referred to Romania cities Breadth-First Search: romania cities map I have a function for creating the neighbors: function [NeighborName PathLength StraightLineDistance] = makeneighbor(x,y,z) NeighborName=x; PathLength=y; % distance from current city to neighbor StraightLineDistance=z; % i want to use it for a* model and in my 'main.m' I have defined city* and neighbors: clc;clear; city1='Arad' [neighbor_name neighbor_distance straight_line_distance]=makeneighbor

Parallelizing a Breadth-First Search

↘锁芯ラ 提交于 2019-12-01 08:43:36
I just taught myself some OpenMP and this could be stupid. Basically I'm trying to parallelize a breadth first search program in c++, with each node taking a long time to process. Here's an example code: queue<node*> q; q.push(head); while (!q.empty()) { qSize = q.size(); for (int i = 0; i < qSize; i++) { node* currNode = q.front(); q.pop(); doStuff(currNode); q.push(currNode); } } The processing function doStuff() is quite expensive and I want to parallelize it. However if I parallelize the for loop by putting #pragma omp parallel for right before the for line, all kinds of weird error pop up

Finding all possible paths from one node to another?

限于喜欢 提交于 2019-12-01 05:46:11
问题 I was trying to find all possible paths, but I am having hard time keeping track of the paths that I have visited. Here is the code so far: public void FindAllPaths(Node startNode, Node endNode) { queue.Enqueue(startNode); while (queue.Count > 0) { var currentNode = queue.Dequeue(); foreach (var edge in currentNode.Edges) { if (edge.Visisted) continue; edge.Visisted = true; queue.Enqueue(edge.TargetNode); } } } 回答1: You have to keep track of the paths visited for each route, not globally. For

What is an efficient way of traversing a graph with bfs using map reduce?

浪子不回头ぞ 提交于 2019-12-01 05:43:42
问题 This was an interview question I got asked by a recruiter, the problem is basically to calculate the shortest path of all node to every node, and my solution was the following initiate all possible edges (without reverse A - B is the same as B-A) Each node will be represent in the following (src, cost, current_list, dest) , the src and dest is basically all the possible edges we initiate earlier Map: for each edge you traverse, you duplicate your tuple and add the current traversed node to

Breadth First enumeration in Gremlin

别来无恙 提交于 2019-12-01 01:34:38
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 require knowledge about the actual length of the path beforehand if I understand correctly) Many thanks! You