c++,algorithm,graph,shortest-path,floyd-warshall

The reason why you get very big negative values is integer overflow. If there is no edge, you set D[i][j]=INT_MAX. But then if (D[i][j] > D[i][k] + D[k][j]) { D[i][j] = D[i][k] + D[k][j]; if there is no edge from i to k and from k to j, then the...

java,graph,shortest-path,floyd-warshall

The answer depends on the data type that you use to represent the weight. If it is double, you would be safe to use Double.POSITIVE_INFINITY. If it is an integer, pick a value that you do not use, e.g. negative one if your graph does not allow negative edges. An...

algorithm,tree,dijkstra,shortest-path,bellman-ford

Dijkstra's algorithm requires the edge lengths to be nonnegative, while Bellman-Ford only requires the nonexistence of cycles of negative length.

algorithm,shortest-path,path-finding

Yes, your algorithm works but you can optimize it for the case when [number of food packets] << [number of squares in the grid]. eg. In the above graph. distances = new int[ROWS][COLS]; for each food-packet on the grid use a shortest path algorithm to find the distance to/from each...

algorithm,graph,graph-algorithm,shortest-path

Here is a simple solution: Sort the edges by their weights. Start adding them one by one(from the lightest to the heaviest) until X and Y become connected. To check if they are connected, you can use a union-find data structure. The time complexity is O(E log E). A proof...

There's no hard and fast rule as it can vary depending on your hardware and your configuration. The more memory that you throw at it, the better it performs. See this guide for information about tuning Neo4j: http://neo4j.com/developer/guide-performance-tuning/...

algorithm,optimization,computer-science,dijkstra,shortest-path

If I understand you correctly, you want to find the tree of minimal weight in the graph that contains all the vertices of E and at least one vertex from S. The problem is called general Steiner tree, and it is NP-hard. So the best you can probably hope for...

java,algorithm,data-structures,graph,shortest-path

If you simply have (just) a list of points, and you need to do this once, then there is no more efficient way. But if you have to do this procedure lots of times for lots of different points, then there are ways to improve the efficiency. For example: create...

c++,boost,graph,path,shortest-path

it depends on how many nodes you have , as you mentioned your nodes are around O(10^4) and edges are O(10^4) which is good so in BOOST LIBRARY DOCS it sasy The time complexity is O(V log V + E). so if you put V = 10^4 and E =...

algorithm,data-structures,shortest-path,maze,bfs

Your idea is wrong. I haven't read the code because what you describe will fail even if implemented perfectly. Consider something like this: x.... ..... ..*** ....* *...* You will traverse the maze like this: x.... ..... ..123 ....4 *...5 Then go from 5 to the bottom-left * and back...

algorithm,dijkstra,shortest-path

Actually for an unweighted, undirected graph, you don't need to use Dijkstra, a simple BFS shall serve the purpose. Following method checks whether e is a part of atleast one shortest path from s to t: Compute the shortest path from s to e and the shortest path from e...

c++,algorithm,graph,shortest-path

Here is a solution which likely to be fast enough: 1)Run shortest path search algorithm from every blue node(this can be done in O(X * (E log N))) to compute pairwise distances. 2)Build a new graph with zero vertex and blue vertices only(X + 1 vertices). Add edges using pairwise...

algorithm,sorting,dijkstra,shortest-path

Think about making a star graph (a graph with a single central node and a bunch of "outer ring" nodes, where each outer ring node has one edge connecting it to the central node) with n outer ring nodes. Then, link up each node vi in the outer ring to...

algorithm,graph,dijkstra,shortest-path,breadth-first-search

This is laborous case work: A < B and 0 and N-1 are joined by A -> trivial. B < A and 0 and N-1 are joined by B -> trivial. B < A and 0 and N-1 are joined by A -> Do BFS on graph with only K...

neo4j,relationship,shortest-path

You could also have kept your metadata information on the relationships. For your needs, this should work: MATCH p = shortestPath((start)-[:PARTICIPATES|:REFERENCES*]->(end)) RETURN nodes(p) ...

r,graph-theory,igraph,shortest-path

I know that my title doesn't fully account for what my question really was. So I'm answering it myself since I just got it to work. #Mean local efficiency (global efficiency for each node) gn<-graph.neighborhood(my.graph,1) #list with subgraphs of directly connected graphs names(gn)<-V(my.graph)$name local.eff<-numeric(length(gn)) for (i in 1:length(gn)){ gn[[i]]<-gn[[i]] -...

algorithm,c++11,shortest-path,bfs

The problem is with memset(d,INF,sizeof(d)); memset() only fills memory with a byte value. Here it will wind up filling the array with the least significant byte of the int value INF. To fix it, create an explicit for loop or use std::fill() instead....

algorithm,graph-theory,graph-algorithm,shortest-path

I think you might consider a dynamic programming approach. At iteration 0, let s_0 = {(origin_0, origin_1)}. For iteration k+1, let s_k+1 = {(x,y) | x != y, exists an (prev_x, prev_y) in s_k s.t. e(prev_x, x) in E and e(prev_y, y) in E}. This should proceed forward with |s|...

I think you want the graph diameter which is the maximum of all-pairs shortest paths. https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.distance_measures.diameter.html

prolog,shortest-path,iterative-deepening

This is a classical case for iterative deepening. Just enter at the toplevel: ?- length(Path, N), path(0, 2, Path). The first answer will be of shortest length. And that's what you can do in Prolog very elegantly: You are starting to enumerate an infinite set, hoping that you will find...

algorithm,dijkstra,shortest-path,traveling-salesman

You can reduce your graph to a TSP and then invoke a TSP algorithm on it: Use Floyd-Warshall algorithm to find the distance u,v for ALL pairs of vertices u and v. Create a new graph, containing only the "desired" vertices, and set the weight between two such vertices u...

Basically you can model it using each city/airport as a node and the flights as the connections in between. The weight of the connections/flights is then the time. If you assume that all stopovers are 30 minutes (in a simplified model) then you can add an additional cost per visited...

algorithm,max,dijkstra,shortest-path

This falls under the issue of the Longest Path Problem. In other words, there is no efficient way to find max path (in your case, max profits) in an unweighted graph structure. However, you mentioned that it was a weighted graph, so you might still be able to still do...

java,algorithm,path,shortest-path,a-star

The heuristic function must be admissible(that is, it should never overestimate the distance to the goal). Once you start dividing the length of an edge by speedlimit, the real distance to the goal might be less than the Euclidean distance to it(if speedlimit > 1). It breaks the algorithm. How...

algorithm,data-structures,graph,tree,shortest-path

Here is a pseudo code of a linear solution: res = 0 // A path from a node to itself has a weight 0 // Runs depth first search from the v vertex and returns the weight of // the shortest path that starts in a descendant of v and...

It sounds like you’re taking the shortest path from wherever you find yourself currently, and not calculating the total distance to get to a node. Let’s step through it in detail. Dijkstra’s algorithm sets up two sets of nodes: visited (with known distances) and unvisited (with tentative distances). We start...

algorithm,graph,time-complexity,graph-algorithm,shortest-path

This can be solved in time O(nm + n2 log n) by running Dijkstra's algorithm on an appropriately-constructed graph. To motivate the intuition for where we're going, let's assume for now that the optimal path makes at most one color change and starts by following a red edge. The path...

java,algorithm,shortest-path,path-finding,a-star

Given your graph G=(V,E) where each v in V stands for a cell, and each edge (u,v) is possible move, you can create a new (weighted) graph G'=(V',E') as follows: V' = V_u U V_d U V_l U V_r - each group stands for the type of move you have...

database,neo4j,shortest-path,graph-traversal

If all relationships with the ROUTE_ID property have a specific relationship type, say 'ROUTE', then you can do this: START n=node(5), m=node(45) MATCH p=shortestPath(n-[:ROUTE*..1000]->m) RETURN p,length(p); Otherwise, you can do this: START n=node(5), m=node(45) MATCH p=shortestPath(n-[r*..1000]->m) WHERE all(x IN r WHERE has(x.ROUTE_ID)) RETURN p,length(p); The former approach should be much...

time-complexity,shortest-path,breadth-first-search

Your analysis seems correct. Please refer to the following references. http://axon.cs.byu.edu/~martinez/classes/312/Slides/Paths.pdf https://courses.engr.illinois.edu/cs473/sp2011/lectures/03_class.pdf...

python,algorithm,circle,shortest-path

Your problem is a variant of a Hamiltonian Cycle Problem, which is NP-Complete, so there is no known efficient solution to it (and most believe a solution does not exist, but it is not proven yet) The Hamiltonian Cycle Problem says: Given a graph G=(V,E), find if there is a...

python,algorithm,graph,shortest-path

You could use dijkstra's algorithm to find the distance. Here's one way to do using networkx In [28]: import networkx as nx Create a grpah with nodes a, b, c where links are a, b and 'a, c' In [29]: g = nx.Graph() In [30]: g.add_edge('a', 'b') In [31]: g.add_edge('a',...

java,algorithm,data-structures,shortest-path

Implementation for Dijkstra's Algorithm in Java The place in code where the vertex are initialized, you initialize all your bus routes. In place where adjacency matrix is defined, for each of your stop initialize with stops where buses are available. Now run the algorithm. You might keep additional info along...

c++,algorithm,data-structures,dijkstra,shortest-path

In minDistance(), if (includ[v] == false && dist[v] <= min) min = dist[v]; min_index = v; You missed { }. min_index = v; is executed no matter the condition is true or false....

algorithm,shortest-path,path-finding

If I understood the problem correctly, you want to step on every block once and also step on all the blocks. So if you think of this as a graph, with every block being represented by a vertex (and every pair of adjacent blocks -horizontally, vertically or diagonally- consists and...

algorithm,graph,shortest-path,bfs

Consider a graph like this: A---(3)-----B | | \-(1)-C--(1)/ The shortest path from A to B is via C (with a total weight of 2). A normal BFS will take the path directly from A to B, marking B as seen, and A to C, marking C as seen. At...

math,geometry,shortest-path,angle,smallbasic

Surely that's just setting the direction based on which angle you choose. If you're working out the angle/direction from a1 to a2, the following pseudo-code should give you what you need: # precondition: [a1,a2] >= 0 angle = ((a2 % 360) - (a1 % 360) + 360) % 360 direction...

algorithm,data-structures,graph,dijkstra,shortest-path

The flag is not needed. Just look at this section of pseudocode: if v is not yet scanned: alt = dist[u] + length(u, v) if alt < dist[v] dist[v] ← alt prev[v] ← u Q.decrease_priority(v,alt) end if end if If you check the different parts: alt seems to be a...

I'm pretty sure you need to keep track of the step-count along with the vertex in your queue. In other words, rather than queuing up just the vertex, include the number of steps taken to reach that vertex: queue = [(start, 0)] #[ ((x, y), steps-so-far) ] Then, in the...

algorithm,maps,dijkstra,shortest-path,backtracking

Actually the matrix isn't required. The graph can just aswell be generated on runtime. The image can be converted into a map of walls and open spots by simply converting it into a two-colored image, where one color represents walls. This would look like this for your requirements: define node:...

performance,matlab,image-processing,shortest-path

Approach #1 : Connecting with the centroid points %// Read image, convert to binary and remove some whitish border across it im = im2bw(imread('http://i.stack.imgur.com/vUsrl.png')); BW = im(3:end-2,3:end-2); figure, imshow(BW), title('Starting/Original Image') %// Find centroid points for each blob cpts = reshape(round(struct2array(regionprops(BW,'Centroid'))),2,[])'; %//' %// Initialize 2 groups- "hungry" & "feeder" groups,...

python,algorithm,graph,shortest-path,minimization

Think of each unoccupied square on the board as a node in a graph, with edges connecting squares that a knight can move between in a single turn. Then you can use the search algorithm of your choice to find the shortest path in this graph.

algorithm,shortest-path,floyd-warshall

The longest possible valid path can have length = (n - 1) * 1000. Thus, "infinity" must be strictly greater than this value. And there is no need to handle it specially as long as 2 * infinity fits into the type you are using to store distances.

c++,algorithm,dijkstra,shortest-path

Map the coordinates to integer vertices using a hashmap. Now you have a graph with nodes as single numbers. Apply dijkstra's algorithm. Time complexity : O(V) for converting to integer vertices. O(V^2) for running dijkstra's algorithm. Therefore O(V^2) total complexity. Pseudo code: int cntr = 0; for(Edge e : graph){...

algorithm,graph,computer-science,graph-algorithm,shortest-path

Edited my answer based on counter example by David Eisenstat. The example you give in your question is not a good example of when Dijkstra will not work. I believe you can do this by modifying Dijkstra. The key is to keep track of multiple alternatives for each Vertex. Not...

java,traversal,dijkstra,shortest-path,maze

All I can see from looking at your code is that you're not mainlining your state properly. You have a method called moveToPosition() but it's called for every node - and you end up adding every x/y combination to the paths List. To be honest a lot of this doesn't...

algorithm,graph,shortest-path,depth-first-search,breadth-first-search

If anyone is interested in how I solved this here is my solution: def dfs(self, v): if v.successor is None: # this is the root node v.cost = 0 v.visited = True for adj in v.adj: if adj.visited is False: # set the parent adj.successor = v adj.cost = v.cost...

c++,algorithm,graph,shortest-path,floyd-warshall

Floyd-Warshall algorithm would be very inefficient for such a sparse graph. The graph is sparse because every vertex connected to no more than 4 other vertices. In a dense graph a vertex can be connected to up to N-1 other vertices, where N is the number of vertices in the...

algorithm,shortest-path,directed-acyclic-graphs,floyd-warshall

Yes, Floyd Warshall can give a correct answer for your problems, can be proved like using Floyd Warshall to find the shortest path between all pairs in graph. Or you can multiply each edges with (-1), and solve your problem like finding the shortest path between all pairs, then multiply...

c++,vector,graph,dijkstra,shortest-path

looks like you have two types: heap_node, and a node class. I'm not sure which type is adj, but either way, you cannot make the vector type an instance. it has to be a type. So either make it typedef struct heap_node HEAP_NODE_STRUCT vector<HEAP_NODE_STRUCT> or c hange the heap_node struct...

To get All paths from one node to multiple nodes MATCH p = shortestPath((s:skill)-[r]->(s1:skill)) WHERE id(s) = 123 AND id(s1) IN [1,2,3] RETURN p This is the one way to get paths between one-to-many. I hope this is what you need....

arrays,algorithm,shortest-path

Lee's algorithm: http://en.wikipedia.org/wiki/Lee_algorithm It's essentially a BF search, here's an example: http://www.oop.rwth-aachen.de/documents/oop-2007/sss-oop-2007.pdf To implement it effectively, check out: Change FloodFill-Algorithm to get Voronoi Territory for two data points? - when I say mark, you mark it with the number on the position you came from + 1. For example, if...

algorithm,graph,dijkstra,shortest-path

If you look at the pseudocode from the Wikipedia link you gave, you'll see an array in there called prev[]. This array contains, for each node v in the graph, the previous node u in the shortest path between the source node s and v. (This array is also called...

c++,multidimensional-array,dijkstra,shortest-path,adjacency-matrix

Converting an image into an adjacency matrix is completely unnecessary; the only thing you need is the "neighbor-ness" information which is already implicitly present. The neighbors of any pixel i,j are represented by the pixels at (i-1,j), (i+1,j), (i,j-1), (i,j+1) subject to image boundaries. You don't need a matrix to...

No, there shouldn't be an infinite cycle. Visited and unvisited nodes in the graph are kept track of, and a visited node will never be visited again. In your example: Mark all nodes as unvisited, except for the starting node which is visited Starting with A, visit B, mark B...