package org.psjava.example;

import junit.framework.Assert;

import org.junit.Test;
import org.psjava.algo.graph.shortestpath.SingleSourceShortestPathResult;
import org.psjava.algo.graph.shortestpath.SingleSourceShortestPath;
import org.psjava.ds.graph.AdjacencyListableDirectedWeightedGraphFactory;
import org.psjava.ds.graph.MutableDirectedWeightedGraph;
import org.psjava.goods.GoodSingleSourceShortestPath;
import org.psjava.math.ns.IntegerNumberSystem;

public class ShortestPathExample {

	@Test
	public void example() {

		// Let's construct a graph.

		MutableDirectedWeightedGraph<Integer> graph = new MutableDirectedWeightedGraph<Integer>();
		graph.insertVertex("A");
		graph.insertVertex("B");
		graph.insertVertex("C");
		graph.insertVertex("D");
		graph.addEdge("A", "C", 100);
		graph.addEdge("A", "B", 10);
		graph.addEdge("B", "C", 20);

		// Let's calculate distances from 'A', the single source.
		// Choose algorithm, and calculate. The good one should be dijkstra
		// algorithm with binary heap.

		SingleSourceShortestPath algo = GoodSingleSourceShortestPath.getInstance();
		SingleSourceShortestPathResult<Integer> result = algo.calc(AdjacencyListableDirectedWeightedGraphFactory.create(graph), "A", IntegerNumberSystem.getInstance());

		int distanceAToC = result.getDistance("C");

		Assert.assertEquals(30, distanceAToC);
	}
}

@@ -78,24 +78,133 @@
 unit.Test;%0D%0A
+import org.psjava.algo.graph.shortestpath.BellmanFord;%0D%0Aimport org.psjava.algo.graph.shortestpath.Dijkstra;%0D%0A
 import org.p
@@ -327,32 +327,100 @@
 eShortestPath;%0D%0A
+import org.psjava.ds.graph.AdjacencyListableDirectedWeightedGraph;%0D%0A
 import org.psjav
@@ -528,32 +528,78 @@
 WeightedGraph;%0D%0A
+import org.psjava.ds.heap.BinaryHeapFactory;%0D%0A
 import org.psjav
@@ -1096,16 +1096,144 @@
 %22, 20);%0D
+%0A%09%09AdjacencyListableDirectedWeightedGraph%3CInteger%3E adjacencyList = AdjacencyListableDirectedWeightedGraphFactory.create(graph);%0D
 %0A%0D%0A%09%09// 
@@ -1267,16 +1267,9 @@
 rom 
-'A', the
+a
  sin
@@ -1278,16 +1278,22 @@
 e source
+ - 'A'
 .%0D%0A%09%09// 
@@ -1318,50 +1318,14 @@
 and 
-calculate. The good one should be dijkstra
+do it.
 %0D%0A%09%09
@@ -1389,16 +1389,21 @@
 ath algo
+rithm
  = GoodS
@@ -1501,15 +1501,20 @@
 algo
+rithm
 .calc(
-A
+a
 djac
@@ -1525,54 +1525,8 @@
 List
-ableDirectedWeightedGraphFactory.create(graph)
 , %22A
@@ -1657,16 +1657,153 @@
 eAToC);%0D
+%0A%0D%0A%09%09// The good one should be Dijkstra's Algorithm but you can specify.%0D%0A%09%09new Dijkstra(new BinaryHeapFactory());%0D%0A%09%09new BellmanFord();%0D
 %0A%09%7D%0D%0A%7D%0D%0A

improve comment