If we are given a weighted undirected graph in which some nodes are marked as special nodes then how to find for each node v the special node with least cost to reach using Dijkstra?
This solution to a problem uses this concept but i can't figure out how it works. Can someone provide a proof or explanation of it?
Related
Let's say I have a graph with nodes that represent locations. Starting at some node N, I want to reach another node M via the shortest path possible. The catch is that there some nodes I want to avoid, staying some distance from them (say at least D nodes away).
Is there a graph algorithm that can solve the shortest path problem with the node avoidance requirement? Would a weighted graph (with infinite length edges emanating from the to-be-avoided nodes) be a solution here?
Temporarily eliminate the nodes you must avoid and those near them or change the weights of the appropriate edges to infinity. Then use any standard path-finding algorithm.
I have a directed graph with less than 600 nodes, and each node's edge number is less than 8.
Now I need to find a path in this graph which must pass through some given nodes(<50). The order of passing given nodes is free.
I know it's a NPC problem, but I don't know how to solve it.
An approximate solution is also acceptable.
Thank you!
Compute the shortest ways between all pairs of the specific nodes. Then create a new graph that only contains those nodes, with the length of the shortest paths as distances. Now, the problem is "reduced" to Travelling Salesman.
(TSM has a fast 3/2-approximation that utilitizes minimal spanning trees and a matching, if that is good enough - the 50! possibilities are too much in any case)
I came across a problem to find optimal algorithm solving the issue: Finding minimum path in a graph from starting point to starting point(make a cycle) and visiting minimum three different nodes in the graph. For example if we have a graph G(V,E) with V={a,b,c,d,e} and edges E={(a,b,16),(a,c,300),(a,d,1),(b,c,100),(b,e,15),(c,a,10),(e,c,20)} the shortest distance will be 61 and it will visit a->c->e->b->a.
I think of using Dijkstra's algorithm for weighted graph, however I do not know how to implement the part for the constraint to visit minimum 3 nodes? It looks like the Hamiltonian cycle's problem but not using all the nodes but only part of them. Is this NP-complete problem?
Any help would be appreciated.
One easy way to implement this is the following:
precompute all-pair shortest paths (e.g. using Floyd–Warshall or running Dijkstra for each possible start node)
for each tuple (a, b, c) of distinct nodes in the graph, consider the concatenation of shortest paths from a to b, b to c and c to a.
Report the minimum of all examined paths.
The runtime will be dominated by the second step, which has runtime O(n3). So no, the problem is not NP-hard, because the number of different nodes we have to visit is fixed (in this case, 3).
I am trying to solve a problem in which there is an undirected graph with positive weighted edges and I need to find the shortest path that covers all the nodes exactly once given the start and end node. In addition the graph is complete(each node is connected to all the other nodes in the graph).
I have tried searching for an algorithm that could solve this problem but I haven't found one that solves this problem. This is not exactly the traveling sales man problem because of the restriction of the start and end node. I will appreciate any kind of help.
If you're starting at node S and ending at T, add a dummy node D that has zero-weight edges to only S and T. Find an optimal travelling salesman tour on this graph, then remove the dummy node from the tour to get your path.
If you'd like to preserve the graph's completeness property, you can implement the above by adding the dummy node with zero-weight edges to S and T, and with edges to all other nodes having weights larger than the sum of the weights of the n heaviest edges in the graph. For practical purposes, this means setting their weights to Integer.Max or similar.
I have directed graph with lot of cycles, probably strongly connected, and I need to get a minimal cycle from it. I mean I need to get cycle, which is the shortest cycle in graph, and every edge is covered at least once.
I have been searching for some algorithm or some theoretical background, but only thing I have found is Chinese postman algorithm. But this solution is not for directed graph.
Can anybody help me? Thanks
Edit>> All edges of that graph have the same cost - for instance 1
Take a look at this paper - Directed Chinese Postman Problem. That is the correct problem classification though (assuming there are no more restrictions).
If you're just reading into theory, take a good read at this page, which is from the Algorithms Design Manual.
Key quote (the second half for the directed version):
The optimal postman tour can be constructed by adding the appropriate edges to the graph G so as to make it Eulerian. Specifically, we find the shortest path between each pair of odd-degree vertices in G. Adding a path between two odd-degree vertices in G turns both of them to even-degree, thus moving us closer to an Eulerian graph. Finding the best set of shortest paths to add to G reduces to identifying a minimum-weight perfect matching in a graph on the odd-degree vertices, where the weight of edge (i,j) is the length of the shortest path from i to j. For directed graphs, this can be solved using bipartite matching, where the vertices are partitioned depending on whether they have more ingoing or outgoing edges. Once the graph is Eulerian, the actual cycle can be extracted in linear time using the procedure described above.
I doubt that it's optimal, but you could do a queue based search assuming the graph is guaranteed to have a cycle. Each queue entry would contain a list of nodes representing paths. When you take an element off the queue, add all possible next steps to the queue, ensuring you are not re-visiting nodes. If the last node is the same as the first node, you've found the minimum cycle.
what you are looking for is called "Eulerian path". You can google it to find enough info, basics are here
And about algorithm, there is an algorithm called Fleury's algorithm, google for it or take a look here
I think it might be worth while just simply writing which vertices are odd and then find which combo of them will lead to the least amount of extra time (if the weights are for times or distances) then the total length will be every edge weight plus the extra. For example, if the odd order vertices are A,B,C,D try AB&CD then AC&BD and so on. (I'm not sure if this is a specifically named method, it just worked for me).
edit: just realised this mostly only works for undirected graphs.
The special case in which the network consists entirely of directed edges can be solved in polynomial time. I think the original paper is Matching, Euler tours and the Chinese postman (1973) - a clear description of the algorithm for the directed graph problem begins on page 115 (page 28 of the pdf):
When all of the edges of a connected graph are directed and the graph
is symmetric, there is a particularly simple and attractive algorithm for
specifying an Euler tour...
The algorithm to find an Euler tour in a directed, symmetric, connected graph G is to first find a spanning arborescence of G. Then, at
any node n, except the root r of the arborescence, specify any order for
the edges directed away from n so long as the edge of the arborescence
is last in the ordering. For the root r, specify any order at all for the
edges directed away from r.
This algorithm was used by van Aardenne-Ehrenfest and de Bruin to
enumerate all Euler tours in a certain directed graph [ 1 ].