shortest-path

Finding a shortest path that passes through some arbitrary sequence of nodes?

最后都变了- 提交于 2019-12-03 00:25:08
In this earlier question the OP asked how to find a shortest path in a graph that goes from u to v and also passes through some node w. The accepted answer, which is quite good, was to run Dijkstra's algorithm twice - once to get from u to w and once to get from w to v. This has time complexity equal to two calls to Dijkstra's algorithm, which is O(m + n log n). Now consider a related question - you are given a sequence of nodes u 1 , u 2 , ..., u k and want to find the shortest path from u 1 to u k such that the path passes through u 1 , u 2 , ..., u k in order. Clearly this could be done by

Is there any implementation of bidirectional search for Dijkstra algorithm? [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-03 00:17:55
I am looking for an implementation of bidirectional search (a.k.a. "meet in the middle" algorithm) for Dijkstra (or any other source-to-destination shortest path algorithm) in Java. As bidirectional search processing is trickier than it looks like ( Graph Algorithms, p.26 ), I want to consider an existing implementation before reinventing the wheel! P.S.: I am talking about bidirectional search , not to be confused with a bidirectional graph) Here is an example of a tricky graph: Yes, there is at least in Java: https://github.com/coderodde/GraphSearchPal/blob/master/src/main/java/net/coderodde

How do you use a Bidirectional BFS to find the shortest path?

五迷三道 提交于 2019-12-02 19:32:18
How do you use a Bidirectional BFS to find the shortest path? Let's say there is a 6x6 grid. The start point is in (0,5) and the end point is in (4,1). What is the shortest path using bidirectional bfs? There are no path costs. And it is undirected. amit How does Bi-directional BFS work? Simultaneously run two BFS's from both source and target vertices, terminating once a vertex common to both runs is discovered. This vertex will be halfway between the source and the target. Why is it better than BFS? Bi-directional BFS will yield much better results than simple BFS in most cases. Assume the

Efficient Path finding algorithm avoiding zigzag's [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-02 18:09:53
This question already has an answer here: Pathfinding - A* with least turns 2 answers I am developing a software which connects objects with wires. This wiring has a rule that these wires cannot pass on other objects and no diagonal move is accepted. All of the shortest path algorithms that i know (A*, dijkstra etc.) find this type of paths: I do not want the unnecessary zigzags in the second screenshot. How do i achive this goal? Note: Anyone who want to try the algorithms can use this application. Another Note: This is the exact situation that i do not want. It finds the zigzag path instead

A* Algorithm for very large graphs, any thoughts on caching shortcuts?

天涯浪子 提交于 2019-12-02 17:14:11
I'm writing a courier/logistics simulation on OpenStreetMap maps and have realised that the basic A* algorithm as pictured below is not going to be fast enough for large maps (like Greater London). The green nodes correspond to ones that were put in the open set/priority queue and due to the huge number (the whole map is something like 1-2 million), it takes 5 seconds or so to find the route pictured. Unfortunately 100ms per route is about my absolute limit. Currently, the nodes are stored in both an adjacency list and also a spatial 100x100 2D array. I'm looking for methods where I can trade

What is meant by diameter of a network?

亡梦爱人 提交于 2019-12-02 16:46:17
The diagram shown on this link of the " A graph with 6 vertices and 7 edges where the vertex no 6 on the far-left is a leaf vertex or a pendant vertex. " has DIAMETER 4? right or wrong? Definitions are The diameter of a graph is the maximum eccentricity of any vertex in the graph. That is, it is the greatest distance between any pair of vertices. To find the diameter of a graph, first find the shortest path between each pair of vertices. The greatest length of any of these paths is the diameter of the graph. Diameter, D, of a network having N nodes is defined as the maximum shortest paths

shortest path from goal to root in directed graph with cycles python

时间秒杀一切 提交于 2019-12-02 09:52:38
I want to find the shortest path from goal to root working backwards My input for root is {'4345092': ['6570646', '40586', '484']} My input for goal is {'886619': ['GOAL']} My input for path_holder is an input but it gets converted to dct and is used for this function. I am getting stuck regarding the while loop as it creates the path for me backwards. Right now I can't get q to print because that part of the code isn't being ran. dct is basically a directed graph representation that contains cycles. I can't seem to figure out how to start from GOAL and end up at the root node. I was wondering

Shortest path in a maze with health loss

你。 提交于 2019-12-02 02:41:49
Suppose you have a dungeon, represented by a 2D matrix. You have a start point S (x1,y1) and an end point E (x2, y2). Along the way, some cells have a number in them, which subtract from your health score. Other cells are obstacles that you can't go through. You start with 5 health points, and you need to find the shortest path from S to E where you don't die on the way. I know that Dijikstra is used to find shortest paths. But in this case the shortest path might be one in which you die along the way. How do you find the shortest path where you don't die? Note that there is no advantage to

Find the shortest path with the least number of edges

旧城冷巷雨未停 提交于 2019-12-01 17:58:29
I need to modify Dijkstra's algorithm so that if there are several shortest paths I need to find the one with minimum number of edges on the path. I've been stuck on how to use Dijkstra's method to find multiple shortest paths, how do you do that? doesn't it always output only 1 shortest path? pseudo code or any general direction would be very helpful. Instead of assigning every node with the distance from source you can assign the number of edges traversed so far also. So each node will have (distance,edges) instead of distance only. Everything else works as usual and for every terminal node

Shortest path to visit all nodes

梦想的初衷 提交于 2019-12-01 16:50:43
I'm looking for an algorithm that seems very typical to me, but it seems that the common solutions are all just a little bit different. In an undirected graph, I want the shortest path that visits every node. Nodes can be revisited and I do not have to return to the start node. The Travelling Salesman Problem seems to add the restriction that each node can only be visited once and that the path has to return to where it started. Minimal Spanning Trees may be part of a solution, but such algorithms only provide the tree, not a minimal path. Additionally, because they're trees and therefore have