Re: [jgrapht-users] Traverse complete graph with BreadthFirstIterator stops
Brought to you by:
barak_naveh,
perfecthash
From: John S. <js...@gm...> - 2010-12-03 07:01:12
|
It's a bit hard to iterate without a loop, no? JVS On Thu, Dec 2, 2010 at 8:02 AM, Oliver Schrenk <oli...@gm...> wrote: > 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 > > > > > ------------------------------------------------------------------------------ > Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! > Tap into the largest installed PC base & get more eyes on your game by > optimizing for Intel(R) Graphics Technology. Get started today with the > Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. > http://p.sf.net/sfu/intelisp-dev2dev > _______________________________________________ > jgrapht-users mailing list > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > |