Skip to content
Permalink
Browse files
SANDBOX-349: Verify Prim's and Kruskal's algorithms correctness - app…
…lying Rogério Theodoro de Brito's first three patches
  • Loading branch information
britter committed Jan 20, 2014
1 parent cd8e94e commit 86481d7e54d793115557dfccac4a0c5b24c92894
Showing 1 changed file with 150 additions and 0 deletions.
@@ -226,4 +226,154 @@ public void verifyNotConnectedMinimumSpanningTree()
assertEquals( expected, actual );
}

/**
* Test the the minimum spanning tree on a path graph with 4 vertices
* and unit weights.
*/
@Test
public void testP4UniformWeightsMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();

BaseLabeledVertex a = new BaseLabeledVertex( "A" );
BaseLabeledVertex b = new BaseLabeledVertex( "B" );
BaseLabeledVertex c = new BaseLabeledVertex( "C" );
BaseLabeledVertex d = new BaseLabeledVertex( "D" );


input.addVertex( a );
input.addVertex( b );
input.addVertex( c );
input.addVertex( d );

input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 1D ), b );
input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 1D ), c );
input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 1D ), d );


// Expected
MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );

for ( BaseLabeledVertex vertex : input.getVertices() )
{
expected.addVertex( vertex );
}
expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 1D ), b );
expected.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 1D ), c );
expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 1D ), d );


// Actual
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.fromArbitrarySource()
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );

// assert!
assertEquals( expected, actual );
}

/**
* Test the algorithm with a disconnected graph on 4 vertices. In this
* case, we expect the Minimum spanning "tree" to actually be a minimum
* spanning forest with 2 components.
*/
@Test
public void testDisconnectedMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();

BaseLabeledVertex a = new BaseLabeledVertex( "A" );
BaseLabeledVertex b = new BaseLabeledVertex( "B" );
BaseLabeledVertex c = new BaseLabeledVertex( "C" );
BaseLabeledVertex d = new BaseLabeledVertex( "D" );


input.addVertex( a );
input.addVertex( b );
input.addVertex( c );
input.addVertex( d );

input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );


// Expected
MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );

for ( BaseLabeledVertex vertex : input.getVertices() )
{
expected.addVertex( vertex );
}
expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );


// Actual
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.fromArbitrarySource()
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );

// assert!
assertEquals( expected, actual );
}

/**
* Test the the minimum spanning tree on a path graph with 4 vertices
* and non-uniform weights.
*/
@Test
public void testP4NonUniformWeightsMinimumSpanningTree()
{
UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>> input
= new UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>();

BaseLabeledVertex a = new BaseLabeledVertex( "A" );
BaseLabeledVertex b = new BaseLabeledVertex( "B" );
BaseLabeledVertex c = new BaseLabeledVertex( "C" );
BaseLabeledVertex d = new BaseLabeledVertex( "D" );


input.addVertex( a );
input.addVertex( b );
input.addVertex( c );
input.addVertex( d );

input.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
input.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 3D ), c );
input.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );


// Expected
MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> expected =
new MutableSpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double>( new DoubleWeightBaseOperations(), new BaseWeightedEdge<Double>() );

for ( BaseLabeledVertex vertex : input.getVertices() )
{
expected.addVertex( vertex );
}
expected.addEdge( a, new BaseLabeledWeightedEdge<Double>( "a <-> b", 4D ), b );
expected.addEdge( b, new BaseLabeledWeightedEdge<Double>( "b <-> c", 3D ), c );
expected.addEdge( c, new BaseLabeledWeightedEdge<Double>( "c <-> d", 2D ), d );


// Actual
SpanningTree<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual =
minimumSpanningTree( input )
.whereEdgesHaveWeights( new BaseWeightedEdge<Double>() )
.fromArbitrarySource()
.applyingKruskalAlgorithm( new DoubleWeightBaseOperations() );

// assert!
assertEquals( expected, actual );
}


}

0 comments on commit 86481d7

Please sign in to comment.