Since last 2 days,i'm trying to find some logic for calculating longest path in graph.I know i can find it easily for DAGs and in general it is polynomial time algorithm.Formally,I want to implement heuristic for computing longest path,morever,if probability p is given with which an edge is present in graph,how can we solve the problem..help...
-
best way to do this is backtracking, in fact you should visit all possible path to find your answer in worst case. – Saeed Amiri Nov 19 '11 at 11:57
4 Answers
Calculating longest path cannot be done in polynomial time as far as I know. Following java implementation of the longest path algorithm finds the longest path of a positive weighted graph for a given source but it takes exponential time in its worst case.
public class LongestPath {
static int times;
public double initLongestPath(ArrayList<Vertex> V, Vertex source) {
for (Vertex u : V) {
u.setVisited(false);
}
return getLongestPath(source);
}
public double getLongestPath(Vertex v) {
++times;
System.out.println(times);
double w, dist, max = 0;
v.setVisited(true);
for (Edge e : v.getOutGoingEdges()) {
if (!e.getToNode().isVisited()) {
dist = e.getWeight() + getLongestPath(e.getToNode());
if (dist > max)
max = dist;
}
}
v.setVisited(false);
return max;
}
}
-
1You can do some optimizations before you run this. For each node, check if it only has two connections. If it does, then remove the node, create an edge between the nodes it was connected to equal to the sum of the paths connected to that node. You can also remove some nodes that only have one connection (adding the cost to get to them to the node they're connected to). Note that if you do that, you can only count up to two, depending on where you start/finish – James McDowell Dec 15 '16 at 20:46
Dijkstra's can't be used on graphs with negative weights - Wiki article on Dijkstra's
Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1956 and published in 1959,1 is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs ...
So you can't negate all edge weights and use Dijkstra's, what you can do is negate all edge weights and use Bellman-Ford algorithm - Wiki article on Bellman-Ford
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.1 It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers
EDIT: The shortest path (with the most negative value) is then the longest path in your original graph.
NOTE: if you have positive cycles in your graph, you will not find a solution since the longest path doesn't exist in such a graph.

- 910
- 11
- 17
You could always just use a breadth first search (BFS) and, whenever you are adding an edge to the graph you have it's cost as the additive inverse (multiply it be -1). This way you are finding the 'shortest path' by using the longest edges. Because you're doing a scalar transform, you're not losing the ability to add within the group (which you do lose if you use the multiplicative inverse).

- 544
- 3
- 15
-
1So P+NP? first show your proof then decide so fast, shortest path as you said is for graph with non negative cycles, see wiki ... – Saeed Amiri Nov 08 '11 at 22:54
-
Assuming there are only positive weights, BFS will work fine. His explanation indicated was that there is a 'probability' that some edges would occur and whilst strictly it's not a reasonable assumption to make (that there would only be positive edges), there is a simple case split: allow negative cycles -> NP-complete ; or disallow them -> use modified (shortest path algorithmn | graph). – Noxville Nov 09 '11 at 06:28
-
But your assumption in too many cases makes negative cycles, so your current approach does not make a sence, and graph with negative cycle does not make a sence in normal situations. – Saeed Amiri Nov 09 '11 at 07:48
-
But I think instead of BFS I can use DFS..i'll run DFS on every node,on atleast one node,DFS will visit to all vertices..that'll be longest path in graph right?But in this approach i'm not getting where can I use probability..?? – username_4567 Nov 09 '11 at 09:13
Invert the weights of the paths and run a shortest path algorithm. The lowest number you get (most negative) is the longest path.

- 33
- 1
- 2
-
You are correct,but probaility with which edge is present in graph is also given...so how can I use probability in calculation? – username_4567 Nov 08 '11 at 15:43
-
1A shortest path algorithm wants few edges with low weight. A longest path algorithm wants many long edges with low weight. A shortest path alg will not help you find a longest path. – Rob Neuhaus Nov 08 '11 at 15:46
-
If I make every edge negative and I run shortest path algorithm,it can give me result I want(because minimzing positive value is same as maximizing negative value)...but how will you use p-"probability with which edge is present in graph" in algorithm.. – username_4567 Nov 08 '11 at 15:49
-
2