[jgrapht-users] Traverse complete graph with BreadthFirstIterator stops
Brought to you by:
barak_naveh,
perfecthash
From: Oliver S. <oli...@gm...> - 2010-12-02 16:02:38
|
I need to completely traverse a graph. BreadthFirstIterator seemed fine as the JavaDoc says : > If the specified start vertex is <code>null</code>, iteration will start at an arbitrary vertex and will not be limited, that is, will be able to traverse all the graph The following code will traverse only the first vertex though and will then stop: package com.acme.jgrapht; import org.jgrapht.UndirectedGraph; import org.jgrapht.graph.WeightedMultigraph; import org.jgrapht.traverse.BreadthFirstIterator; public class GraphTester { public static void main(String[] args) { GraphTester graphTester = new GraphTester(); graphTester.iterate(); } private final UndirectedGraph<Vertex, Edge> graph; private void iterate() { BreadthFirstIterator<Vertex, Edge> traverser = new BreadthFirstIterator<Vertex, Edge>(graph, null); if (traverser.hasNext()) { Vertex v = traverser.next(); System.out.println(v); } } public GraphTester() { graph = new WeightedMultigraph<Vertex, Edge>(Edge.class); graph.addVertex(new Vertex(0)); graph.addVertex(new Vertex(1)); graph.addVertex(new Vertex(2)); graph.addVertex(new Vertex(3)); } final class Edge { } final class Vertex { int id; public Vertex(int id) { super(); this.id = id; } public int getId() { return id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Vertex)) return false; Vertex other = (Vertex) obj; if (id != other.id) return false; return true; } @Override public String toString() { return "Vertex [id=" + id + "]"; } } } What can I do? Best regards Oliver Schrenk |