I'm trying to make a quick test of the BellmanFordAlgorithm with a simple directed graph with weights. When I print out the resulting distances, I basically get an array of (DOUBLE.MAX_VALUE).
The code is written in Kotlin but is very easy to read. Could you please let me know where did I do wrong or what am I missing?
Here is the code:
fun main(args: Array<String>) {
val graph = grph.in_memory.InMemoryGrph()
// Creates as key, value map for connecting vertices and their weights.
val edgeSet: MutableMap<Pair<Int, Int>, Double> = mutableMapOf()
edgeSet.put(Pair(5, 4), 2.0)
// vertices 5 and 4 are directly connected with a weight of 4.0
edgeSet.put(Pair(4, 2), 1.0)
edgeSet.put(Pair(2, 1), 3.0)
edgeSet.put(Pair(1, 4), 3.0)
edgeSet.put(Pair(1, 3), 6.0)
edgeSet.put(Pair(3, 4), 2.0)
edgeSet.put(Pair(4, 3), 1.0)
edgeSet.forEach{(key, value)
// adding and edge connecting two vertices given by the key value = <Int, Int> to graph
-> val e: Int = graph.addDirectedSimpleEdge(key.first, key.second)
// Setting e (edge number with width (=weight) property)
graph.getEdgeWidthProperty().setValue(e, value)
}
// In my graph, vertex #5 is the source
println(Arrays.toString(BellmanFordAlgorithm(graph.edgeWidthProperty).compute(graph, 4).distances))
************************************************************************
I get the following result:
[-1, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647]
With Bellman-Ford algo, these are the initial values before the relaxation and other routines have been made.
Will appreciate you help.
Thank you,
Eli Wilner
}