commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1298180 - in /commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph: ./ spanning/
Date Wed, 07 Mar 2012 22:40:03 GMT
Author: simonetripodi
Date: Wed Mar  7 22:40:02 2012
New Revision: 1298180

URL: http://svn.apache.org/viewvc?rev=1298180&view=rev
Log:
updated spanning tree algorithms API according to new Mapper to retrieve weights of graph
edges

Added:
    commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
  (with props)
    commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
  (with props)
Modified:
    commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/CommonsGraph.java
    commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeAlgorithmSelector.java
    commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
    commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java

Modified: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/CommonsGraph.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/CommonsGraph.java?rev=1298180&r1=1298179&r2=1298180&view=diff
==============================================================================
--- commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/CommonsGraph.java
(original)
+++ commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/CommonsGraph.java
Wed Mar  7 22:40:02 2012
@@ -45,7 +45,8 @@ import org.apache.commons.graph.scc.Defa
 import org.apache.commons.graph.scc.SccAlgorithmSelector;
 import org.apache.commons.graph.shortestpath.DefaultWeightedEdgesSelector;
 import org.apache.commons.graph.shortestpath.PathWeightedEdgesBuilder;
-import org.apache.commons.graph.spanning.DefaultSpanningTreeSourceSelector;
+import org.apache.commons.graph.spanning.DefaultSpanningWeightedEdgeMapperBuilder;
+import org.apache.commons.graph.spanning.SpanningWeightedEdgeMapperBuilder;
 import org.apache.commons.graph.visit.DefaultVisitSourceSelector;
 import org.apache.commons.graph.visit.VisitSourceSelector;
 
@@ -88,10 +89,10 @@ public final class CommonsGraph<V, E, G 
      * @param graph
      * @return
      */
-    public static <V, WE, W, G extends Graph<V, WE>> SpanningTreeSourceSelector<V,
W, WE, G> minimumSpanningTree( G graph )
+    public static <V, WE, G extends Graph<V, WE>> SpanningWeightedEdgeMapperBuilder<V,
WE, G> minimumSpanningTree( G graph )
     {
         graph = checkNotNull( graph, "Minimum spanning tree can not be calculated on null
graph" );
-        return new DefaultSpanningTreeSourceSelector<V, W, WE, G>( graph );
+        return new DefaultSpanningWeightedEdgeMapperBuilder<V, WE, G>( graph );
     }
 
     /**

Modified: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeAlgorithmSelector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeAlgorithmSelector.java?rev=1298180&r1=1298179&r2=1298180&view=diff
==============================================================================
--- commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeAlgorithmSelector.java
(original)
+++ commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeAlgorithmSelector.java
Wed Mar  7 22:40:02 2012
@@ -31,6 +31,7 @@ import java.util.PriorityQueue;
 import java.util.Set;
 
 import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Mapper;
 import org.apache.commons.graph.SpanningTree;
 import org.apache.commons.graph.VertexPair;
 import org.apache.commons.graph.collections.DisjointSet;
@@ -51,6 +52,8 @@ final class DefaultSpanningTreeAlgorithm
     /** The graph. */
     private final G graph;
 
+    private final Mapper<WE, W> weightedEdges;
+
     /** The start {@link Vertex}. */
     private final V source;
 
@@ -61,9 +64,10 @@ final class DefaultSpanningTreeAlgorithm
      * @param graph the {@link Graph} to be used.
      * @param source the start {@link Vertex}.
      */
-    public DefaultSpanningTreeAlgorithmSelector( final G graph, final V source )
+    public DefaultSpanningTreeAlgorithmSelector( final G graph, Mapper<WE, W> weightedEdges,
final V source )
     {
         this.graph = graph;
+        this.weightedEdges = weightedEdges;
         this.source = source;
     }
 
@@ -87,20 +91,16 @@ final class DefaultSpanningTreeAlgorithm
 
         checkNotNull( weightOperations, "The Boruvka algorithm cannot be calculated with
null weight operations" );
 
-        final MutableSpanningTree<V, WE, W> spanningTree =
-            new MutableSpanningTree<V, WE, W>( weightOperations );
+        final MutableSpanningTree<V, WE, W> spanningTree = new MutableSpanningTree<V,
WE, W>( weightOperations );
 
-        final Set<SuperVertex<V, W, WE, G, WO>> components =
-            new HashSet<SuperVertex<V, W, WE, G, WO>>( graph.getOrder() );
+        final Set<SuperVertex<V, W, WE, G>> components = new HashSet<SuperVertex<V,
W, WE, G>>( graph.getOrder() );
 
-        final Map<V, SuperVertex<V, W, WE, G, WO>> mapping =
-            new HashMap<V, SuperVertex<V, W, WE, G, WO>>( graph.getOrder() );
+        final Map<V, SuperVertex<V, W, WE, G>> mapping = new HashMap<V, SuperVertex<V,
W, WE, G>>( graph.getOrder() );
 
         for ( V v : graph.getVertices() )
         {
             // create a super vertex for each vertex
-            final SuperVertex<V, W, WE, G, WO> sv =
-                new SuperVertex<V, W, WE, G, WO>( v, graph, weightOperations );
+            final SuperVertex<V, W, WE, G> sv = new SuperVertex<V, W, WE, G>(
v, graph, new WeightedEdgesComparator<W, WE>( weightOperations, weightedEdges ) );
 
             components.add( sv );
 
@@ -114,7 +114,7 @@ final class DefaultSpanningTreeAlgorithm
         while ( components.size() > 1 )
         {
             final List<WE> edges = new LinkedList<WE>();
-            for ( SuperVertex<V, W, WE, G, WO> sv : components )
+            for ( SuperVertex<V, W, WE, G> sv : components )
             {
                 // get the minimum edge for each component to any other component
                 final WE edge = sv.getMinimumWeightEdge();
@@ -135,8 +135,8 @@ final class DefaultSpanningTreeAlgorithm
                 final V tail = pair.getTail();
 
                 // find the super vertices corresponding to this edge
-                final SuperVertex<V, W, WE, G, WO> headSv = mapping.get( head );
-                final SuperVertex<V, W, WE, G, WO> tailSv = mapping.get( tail );
+                final SuperVertex<V, W, WE, G> headSv = mapping.get( head );
+                final SuperVertex<V, W, WE, G> tailSv = mapping.get( tail );
 
                 // merge them, if they are not the same
                 if ( headSv != tailSv )
@@ -173,7 +173,7 @@ final class DefaultSpanningTreeAlgorithm
         final Set<V> settledNodes = new HashSet<V>();
 
         final PriorityQueue<WE> orderedEdges =
-            new PriorityQueue<WE>( graph.getSize(), new WeightedEdgesComparator<W,
WE>( weightOperations ) );
+            new PriorityQueue<WE>( graph.getSize(), new WeightedEdgesComparator<W,
WE>( weightOperations, weightedEdges ) );
 
         for ( WE edge : graph.getEdges() )
         {
@@ -217,7 +217,7 @@ final class DefaultSpanningTreeAlgorithm
     {
         checkNotNull( weightOperations, "The Prim algorithm cannot be calculated with null
weight operations" );
 
-        final ShortestEdges<V, WE, W> shortestEdges = new ShortestEdges<V, WE, W>(
graph, source, weightOperations );
+        final ShortestEdges<V, WE, W> shortestEdges = new ShortestEdges<V, WE, W>(
graph, source, weightOperations, weightedEdges );
 
         final PriorityQueue<V> unsettledNodes = new PriorityQueue<V>( graph.getOrder(),
shortestEdges );
         unsettledNodes.add( source );
@@ -236,7 +236,7 @@ final class DefaultSpanningTreeAlgorithm
                 // if the edge has not been already visited and its weight is
                 // less then the current Vertex weight
                 boolean weightLessThanCurrent = !shortestEdges.hasWeight( v ) ||
-                        weightOperations.compare( edge.getWeight(), shortestEdges.getWeight(
v ) ) < 0;
+                        weightOperations.compare( weightedEdges.map( edge ), shortestEdges.getWeight(
v ) ) < 0;
                 if ( settledEdges.add( edge ) && weightLessThanCurrent )
                 {
                     if ( !unsettledNodes.contains( v ) )

Modified: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java?rev=1298180&r1=1298179&r2=1298180&view=diff
==============================================================================
--- commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
(original)
+++ commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningTreeSourceSelector.java
Wed Mar  7 22:40:02 2012
@@ -30,6 +30,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Mapper;
 import org.apache.commons.graph.SpanningTree;
 import org.apache.commons.graph.VertexPair;
 import org.apache.commons.graph.model.MutableSpanningTree;
@@ -44,15 +45,18 @@ import org.apache.commons.graph.weight.O
  * @param <WE> the Graph weighted edges type
  * @param <G> the input Graph type
  */
-public final class DefaultSpanningTreeSourceSelector<V, W, WE, G extends Graph<V, WE>>
+final class DefaultSpanningTreeSourceSelector<V, W, WE, G extends Graph<V, WE>>
     implements SpanningTreeSourceSelector<V, W, WE, G>
 {
 
     private final G graph;
 
-    public DefaultSpanningTreeSourceSelector( G graph )
+    private final Mapper<WE, W> weightedEdges;
+
+    public DefaultSpanningTreeSourceSelector( G graph, Mapper<WE, W> weightedEdges
)
     {
         this.graph = graph;
+        this.weightedEdges = weightedEdges;
     }
 
     /**
@@ -71,7 +75,7 @@ public final class DefaultSpanningTreeSo
     {
         source = checkNotNull( source, "Spanning tree cannot be calculated without expressing
the source vertex" );
         checkState( graph.containsVertex( source ), "Vertex %s does not exist in the Graph",
source );
-        return new DefaultSpanningTreeAlgorithmSelector<V, W, WE, G>( graph, source
);
+        return new DefaultSpanningTreeAlgorithmSelector<V, W, WE, G>( graph, weightedEdges,
source );
     }
 
     /**
@@ -91,9 +95,9 @@ public final class DefaultSpanningTreeSo
             sortedEdge.add( we );
         }
 
-        sort( sortedEdge, reverseOrder( new WeightedEdgesComparator<W, WE>( weightOperations
) ) );
+        sort( sortedEdge, reverseOrder( new WeightedEdgesComparator<W, WE>( weightOperations,
weightedEdges ) ) );
 
-        WeightedGraph<V, WE, W> tmpGraph = new ReverseDeleteGraph<V, WE, W>(
graph, sortedEdge, visitedEdge );
+        Graph<V, WE> tmpGraph = new ReverseDeleteGraph<V, WE>( graph, sortedEdge,
visitedEdge );
 
         for ( Iterator<WE> iterator = sortedEdge.iterator(); iterator.hasNext(); )
         {
@@ -104,7 +108,11 @@ public final class DefaultSpanningTreeSo
 
             try
             {
-                findShortestPath( tmpGraph ).from( vertices.getHead() ).to( vertices.getTail()
).applyingDijkstra( weightOperations );
+                findShortestPath( tmpGraph )
+                    .whereEdgesHaveWeights( weightedEdges )
+                    .from( vertices.getHead() )
+                    .to( vertices.getTail() )
+                    .applyingDijkstra( weightOperations );
             }
             catch ( PathNotFoundException ex )
             {

Added: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java?rev=1298180&view=auto
==============================================================================
--- commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
(added)
+++ commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
Wed Mar  7 22:40:02 2012
@@ -0,0 +1,44 @@
+package org.apache.commons.graph.spanning;
+
+/*
+ * 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 static org.apache.commons.graph.utils.Assertions.checkNotNull;
+
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Mapper;
+
+public final class DefaultSpanningWeightedEdgeMapperBuilder<V, WE, G extends Graph<V,
WE>>
+    implements SpanningWeightedEdgeMapperBuilder<V, WE, G>
+{
+
+    private final G graph;
+
+    public DefaultSpanningWeightedEdgeMapperBuilder( G graph )
+    {
+        this.graph = graph;
+    }
+
+    public <W> SpanningTreeSourceSelector<V, W, WE, G> whereEdgesHaveWeights(
Mapper<WE, W> weightedEdges )
+    {
+        weightedEdges = checkNotNull( weightedEdges, "Function to calculate edges weight
can not be null." );
+        return new DefaultSpanningTreeSourceSelector<V, W, WE, G>( graph, weightedEdges
);
+    }
+
+}

Propchange: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/DefaultSpanningWeightedEdgeMapperBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java?rev=1298180&view=auto
==============================================================================
--- commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
(added)
+++ commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
Wed Mar  7 22:40:02 2012
@@ -0,0 +1,30 @@
+package org.apache.commons.graph.spanning;
+
+/*
+ * 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.Graph;
+import org.apache.commons.graph.Mapper;
+
+public interface SpanningWeightedEdgeMapperBuilder<V, WE, G extends Graph<V, WE>>
+{
+
+    <W> SpanningTreeSourceSelector<V, W, WE, G> whereEdgesHaveWeights( Mapper<WE,
W> weightedEdges );
+
+}

Propchange: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SpanningWeightedEdgeMapperBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java?rev=1298180&r1=1298179&r2=1298180&view=diff
==============================================================================
--- commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java
(original)
+++ commons/sandbox/graph/branches/drop-marker-interfaces-feature/src/main/java/org/apache/commons/graph/spanning/SuperVertex.java
Wed Mar  7 22:40:02 2012
@@ -19,7 +19,6 @@ package org.apache.commons.graph.spannin
  * under the License.
  */
 
-import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
@@ -38,7 +37,7 @@ import org.apache.commons.graph.VertexPa
  * @param <G>  the input Graph type
  * @param <WC> the weight operations
  */
-class SuperVertex<V, W, WE, G extends Graph<V, WE>, WC extends Comparator<W>>
+class SuperVertex<V, W, WE, G extends Graph<V, WE>>
     implements Iterable<V> {
 
     /** The reference to the graph. */
@@ -58,13 +57,13 @@ class SuperVertex<V, W, WE, G extends Gr
      * @param graph the underlying graph
      * @param weightComparator the comparator used to sort the weighted edges
      */
-    public SuperVertex( final V source, final G graph, final WC weightComparator ) {
+    public SuperVertex( final V source, final G graph, final WeightedEdgesComparator<W,
WE> weightComparator ) {
         this.graph = graph;
 
         vertices = new HashSet<V>();
         vertices.add( source );
 
-        orderedEdges = new TreeSet<WE>( new WeightedEdgesComparator<W, WE>( weightComparator
) );
+        orderedEdges = new TreeSet<WE>( weightComparator );
 
         // add all edges for this vertex to the sorted set
         for ( final V w : graph.getConnectedVertices( source )) {
@@ -85,7 +84,7 @@ class SuperVertex<V, W, WE, G extends Gr
      *
      * @param other the {@link SuperVertex} to be merged into this
      */
-    public void merge( final SuperVertex<V, W, WE, G, WC> other ) {
+    public void merge( final SuperVertex<V, W, WE, G> other ) {
         for ( final V v : other.vertices ) {
             vertices.add(v);
         }



Mime
View raw message