1

Here is the code to traverse the graph with BreathFirstIterator:

public class GraphTest {

    public static void main(String[] args) {
        DirectedGraph<Integer, DefaultEdge> graph = 
            new DefaultDirectedGraph <Integer, DefaultEdge>(DefaultEdge.class);

        graph.addVertex(7);
        graph.addVertex(4);
        graph.addVertex(9);
        graph.addVertex(3);
        graph.addVertex(2);
        graph.addVertex(5);


        graph.addEdge(7, 4);
        graph.addEdge(7, 9);
        graph.addEdge(9, 3);
        graph.addEdge(3, 2);
        graph.addEdge(3, 5);

        GraphIterator<Integer, DefaultEdge> iterator = 
                new BreadthFirstIterator<Integer, DefaultEdge>(graph);
        while (iterator.hasNext()) {
            System.out.println( iterator.next() );
        }
    }
}

As expected, the code prints out the list of all visited vertexes: 7,4,9,3,2,5. My problem is - I don't know how can I use this API to obtain a path with removed backtracks of algorithm. For example, for path from 7 to 2, it would output 7->9->3->2, and not just the list of visited vertexes. Stopping it after reaching the destination is obsiously not enough. Have anyone already solved this issue?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
TheMP
  • 8,257
  • 9
  • 44
  • 73

1 Answers1

1

It is possible to get the shortest path between two vertices with DijkstraShortestPath.findPathBetween. It provides an implementation of Dijkstra's shortest path algorithm using ClosestFirstIterator. If there is no such path, it will return null.

Sample code:

DirectedGraph<Integer, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);

graph.addVertex(7);
graph.addVertex(4);
graph.addVertex(9);
graph.addVertex(3);
graph.addVertex(2);
graph.addVertex(5);

graph.addEdge(7, 4);
graph.addEdge(7, 9);
graph.addEdge(9, 3);
graph.addEdge(3, 2);
graph.addEdge(3, 5);

List<DefaultEdge> path = DijkstraShortestPath.findPathBetween(graph, 7, 2);
System.out.println(path); // prints [(7 : 9), (9 : 3), (3 : 2)]
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 1
    I already have a solution for finding shortest path with Dijkstra algorithm, but in this case I don't want the shortest path - I want ANY path that can be found with BFS (probably with the fewest "jumps"). – TheMP Oct 04 '15 at 15:51
  • @Niemand Well the path with the fewest jumps is the shortest path as found by Dijkstra algorithm. So do you want all of them or just the shortest one? – Tunaki Oct 04 '15 at 15:54
  • In this simplified case in question yes, the Dijkstra can be used. But in my real example i have a weighted edge and I want to find the path specifically with breadth first search and ignore the weights, minimazing the number of transfers between nodes. So to conclude - I want to have an algorithm that finds one single path with the fewest number of transfers regardless of the edge weight. BFS seemed like a good choice to me. – TheMP Oct 04 '15 at 16:00
  • @Niemand This is a _completely_ different question that the one you asked. It doesn't involve `DirectedGraph` at all but `WeightedGraph` which is quite different. You do not mention weights at all in your question. – Tunaki Oct 04 '15 at 16:03
  • Maybe I simplified my case too much :) I will try to fix it in a moment – TheMP Oct 04 '15 at 16:08
  • 1
    @Niemand Please don't edit your post as you will change enormously the question that you asked. I suggest you create another question that is more specific to your use-case. This one might be helpful for future visitors wanting to have an example of Dijkstra algorithm. – Tunaki Oct 04 '15 at 16:11
  • OK, I will leave it as-is. The new question moved here - http://stackoverflow.com/questions/32935692/jgrapht-apply-bfs-to-weightedgraph – TheMP Oct 04 '15 at 16:24