You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/06/13 21:36:16 UTC
svn commit: r1135242 - in
/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit:
./ GraphVisitHandler.java Visit.java package-info.java
Author: simonetripodi
Date: Mon Jun 13 19:36:16 2011
New Revision: 1135242
URL: http://svn.apache.org/viewvc?rev=1135242&view=rev
Log:
first checkin of a clean implementation of (Breadth|Depth)-first search algorithms
Added:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java (with props)
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java (with props)
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java (with props)
Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java?rev=1135242&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java Mon Jun 13 19:36:16 2011
@@ -0,0 +1,62 @@
+package org.apache.commons.graph.visit;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Vertex;
+
+/**
+ * Description of the Interface
+ */
+public interface GraphVisitHandler<V extends Vertex, E extends Edge<V>>
+{
+
+ /**
+ * Description of the Method
+ */
+ void discoverGraph( Graph<V, E> graph );
+
+ /**
+ * Description of the Method
+ */
+ void discoverVertex( V vertex );
+
+ /**
+ * Description of the Method
+ */
+ void discoverEdge( E edge );
+
+ /**
+ * Description of the Method
+ */
+ void finishEdge( E edge );
+
+ /**
+ * Description of the Method
+ */
+ void finishVertex( V vertex );
+
+ /**
+ * Description of the Method
+ */
+ void finishGraph( Graph<V, E> graph );
+
+}
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/GraphVisitHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java?rev=1135242&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java Mon Jun 13 19:36:16 2011
@@ -0,0 +1,137 @@
+package org.apache.commons.graph.visit;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.Set;
+import java.util.Stack;
+
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Vertex;
+
+/**
+ * Contains different implementations of Graph visitor algorithms.
+ */
+public final class Visit
+{
+
+ /**
+ *
+ *
+ * @param <V>
+ * @param <E>
+ * @param graph
+ * @param source
+ * @param handler
+ */
+ public final <V extends Vertex, E extends Edge<V>> void breadthFirstSearch( Graph<V, E> graph, V source,
+ GraphVisitHandler<V, E> handler )
+ {
+ handler.discoverGraph( graph );
+
+ LinkedList<V> vertexQueue = new LinkedList<V>();
+ vertexQueue.add( source );
+
+ Set<V> visitedVetices = new HashSet<V>();
+ visitedVetices.add( source );
+
+ while ( !vertexQueue.isEmpty() )
+ {
+ V v = vertexQueue.remove();
+
+ handler.discoverVertex( v );
+
+ for ( E e : graph.getEdges( v ) )
+ {
+ V w = e.getTail();
+
+ if ( !visitedVetices.add( w ) )
+ {
+ handler.discoverEdge( e );
+
+ vertexQueue.addFirst( v );
+
+ handler.finishEdge( e );
+ }
+ }
+
+ handler.finishVertex( v );
+ }
+
+ handler.finishGraph( graph );
+ }
+
+ /**
+ *
+ *
+ * @param <V>
+ * @param <E>
+ * @param graph
+ * @param source
+ * @param handler
+ */
+ public final <V extends Vertex, E extends Edge<V>> void depthFirstSearch( Graph<V, E> graph, V source,
+ GraphVisitHandler<V, E> handler )
+ {
+ handler.discoverGraph( graph );
+
+ Stack<V> vertexStack = new Stack<V>();
+ vertexStack.push( source );
+
+ Set<V> visitedVetices = new HashSet<V>();
+ visitedVetices.add( source );
+
+ while ( !vertexStack.isEmpty() )
+ {
+ V v = vertexStack.pop();
+
+ handler.discoverVertex( v );
+
+ for ( E e : graph.getEdges( v ) )
+ {
+ V w = e.getTail();
+
+ if ( !visitedVetices.add( w ) )
+ {
+ handler.discoverEdge( e );
+
+ vertexStack.push( w );
+
+ handler.finishEdge( e );
+ }
+ }
+
+ handler.finishVertex( v );
+ }
+
+ handler.finishGraph( graph );
+ }
+
+ /**
+ * Hidden constructor, this class can't be instantiated
+ */
+ private Visit()
+ {
+ // do nothing
+ }
+
+}
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java?rev=1135242&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java Mon Jun 13 19:36:16 2011
@@ -0,0 +1,23 @@
+/**
+ * Visit algorithms implementation.
+ */
+package org.apache.commons.graph.visit;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain