views:

438

answers:

3

Hello All,

I'm currently finding the longest path in a directed acyclic positive-weighted graph by negating all edge weights and running Bellman Ford. This is working great. However, I'd like to print the trace of which nodes/edges were used. Any help? The program takes as input the number of nodes, a source, destination and edge weight. Input halts on -1 -1 -1. My code is as follows:

import java.util.Arrays; import java.util.Vector; import java.util.Scanner;

public class BellmanFord { public static int INF = Integer.MAX_VALUE;

// this class represents an edge between two nodes
static class Edge {
 int source; // source node
 int destination; // destination node
 int weight; // weight of the edge
 public Edge() {}; // default constructor
 public Edge(int s, int d, int w) { source = s; destination = d; weight = (w*(-1)); }
}

public static void main(String[] args) 
{
 Scanner input = new Scanner(System.in);
 int inputgood=1;
 int tail;
 int head;
 int weight;
 int count=-1;
 Vector<Edge> edges = new Vector<Edge>(); // data structure to hold graph
 int nnodes = input.nextInt();
 while(inputgood==1)
 {
  tail = input.nextInt();
  head = input.nextInt();
  weight = input.nextInt();

  if(tail!=(-1))
  {
   edges.add(new Edge(tail, head, weight));
   count++;
  }
  if(tail==(-1))
   inputgood=0;
 }
 int start = edges.get(0).source;
 bellmanFord(edges, nnodes, start);
}

public static void bellmanFord(Vector<Edge> edges, int nnodes, int source) {
 // the 'distance' array contains the distances from the main source to all other nodes
 int[] distance = new int[nnodes];
 // at the start - all distances are initiated to infinity
 Arrays.fill(distance, INF);
 // the distance from the main source to itself is 0
 distance[source] = 0;
 // in the next loop we run the relaxation 'nnodes' times to ensure that
 // we have found new distances for ALL nodes
 for (int i = 0; i < nnodes; ++i)
  // relax every edge in 'edges'
  for (int j = 0; j < edges.size(); ++j) {
   // analyze the current edge (SOURCE == edges.get(j).source, DESTINATION == edges.get(j).destination):
   // if the distance to the SOURCE node is equal to INF then there's no shorter path from our main source to DESTINATION through SOURCE
   if (distance[edges.get(j).source] == INF) continue;
   // newDistance represents the distance from our main source to DESTINATION through SOURCE (i.e. using current edge - 'edges.get(j)')
   int newDistance = distance[edges.get(j).source] + edges.get(j).weight;
   // if the newDistance is less than previous longest distance from our main source to DESTINATION
   // then record that new longest distance from the main source to DESTINATION
   if (newDistance < distance[edges.get(j).destination])
    distance[edges.get(j).destination] = newDistance;
  }
 // next loop analyzes the graph for cycles
 for (int i = 0; i < edges.size(); ++i)
  // 'if (distance[edges.get(i).source] != INF)' means:
  // "
  // if the distance from the main source node to the DESTINATION node is equal to infinity then there's no path between them
  // "
  // 'if (distance[edges.get(i).destination] > distance[edges.get(i).source] + edges.get(i).weight)' says that there's a negative edge weight cycle in the graph
  if (distance[edges.get(i).source] != INF && distance[edges.get(i).destination] > distance[edges.get(i).source] + edges.get(i).weight) {
   System.out.println("Cycles detected!");
   return;
  }
 // this loop outputs the distances from the main source node to all other nodes of the graph
 for (int i = 0; i < distance.length; ++i)
  if (distance[i] == INF)
   System.out.println("There's no path between " + source + " and " + i);
  else
   System.out.println("The Longest distance between nodes " + source + " and " + i + " is " + distance[i]);
}

}

A: 

Embarrassingly, I've never implemented Bellman-Ford. However, Dijkstra's can be simply modified to return the actual path used as well as the cost, and your idea of negating weights to find the longest path should work just as well in it as it does in Bellman. I'd suggest taking a look at that if you haven't already.

Donnie
Very good. I've never implemented Dijkstra. Is there a good reference or psuedocode out there?
Chad Ford
A: 

You need to slightly modify what you do in the Bellman Ford implementation:

...
int[] lastNode = new int[nnodes];
lastNode[source] = source;
for (int i = 0; i < nnodes; ++i)
    for (int j = 0; j < edges.size(); ++j) {
        if (distance[edges.get(j).source] == INF) continue;
        int newDistance = distance[edges.get(j).source] + edges.get(j).weight;
        if (newDistance < distance[edges.get(j).destination])
        {
            distance[edges.get(j).destination] = newDistance;
            lastNode[edges.get(j).destination] = edges.get(j).source;
        }
    }

Printing individual paths then becomes:

static void printPath(int source, int end, int[] lastNodes)
{
    if(source!=end)
        printPath(source, lastNodes[end], lastNodes);
    System.out.print(end+" ");
}

Which prints the path in order from source node to end node.

CoderTao
But what if the source node is a greater number than the end. Say node 10 is at the top of the graph and has the longest path to 0. Wouldn't this cause a stack overflow?
Chad Ford
I'm not following your question. The lastNode array is storing an index to the node that led to it (mildly storing the edge that led to the current node). So long as the path has no cycles, then it will be of finite length; and printPath would print it out by tracing the path back from end to start, and then print out in order of start to end. SO, sorry, I don't follow your question; can you be more specific?
CoderTao
So I implement this function and add the array that stores the index of the node that led to it. Then I use this graph as input: 1110 4 1210 5 1110 6 14 8 34 7 35 8 25 7 45 0 26 7 26 0 18 9 68 1 57 9 57 1 67 2 40 1 40 2 99 3 71 3 102 3 8-1 -1 -1The longest path is 10 -> 5 -> 7 -> 1 with length 31. But for some reason the code above causes a stack overflow. I thought from the source (10) being greater than the end node (1)
Chad Ford
I don't beleive so. The only thing that I can think that would cause that is a cycle. Try moving the System.out.print(end+" ") to the start of the printPath method, and see what you get as outputs.
CoderTao
That, and finally getting around to running locally with the input provided; I don't have a stack overflow, and the longest path listed for 10->1 is 10->4->7->1, length 21
CoderTao
I'll give it a shot. Thanks for your help!
Chad Ford
A: 

The common solution for graph algorithms is to maintain parent[edge] -> edge mapping. For edge e the value of parent[e] is the node from which we traverse to e when we create a path optimal in some way.

The array is usually updated in the same way you update index in algorithm to find the largest element in the array, i.e. within if condition when you compare fitness of candidate path with that of current path.

In your case, it's here:

if (newDistance < distance[edges.get(j).destination]) {
   distance[edges.get(j).destination] = newDistance;
   parent[edges.get(j).destination] = edges.get(j).source;
}

After you comupted parent mapping, you may take destination node and traverse it back, recursively building an array [dest, parent[dest], parent[parent[dest]], ... source.

Pavel Shved