Closed giraffesyo closed 6 years ago
Wrote a function to add the weights up:
function calculateWeight(graph, listOfNodes) {
let sum = 0
for (let i = 0; i < listOfNodes.length - 1; i++) {
console.log(`Node: ${listOfNodes[i]} Node+1: ${listOfNodes[i+1]}`)
console.log(
`vertex: ${listOfNodes[i]}, weight to next: ${graph.getEdgeWeight(
listOfNodes[i],
listOfNodes[i + 1]
)}`
)
sum += parseInt(graph.getEdgeWeight(listOfNodes[i], listOfNodes[i + 1]))
}
return sum
}
What this showed me is that the edges were defaulting to 1... On further investigation I checked what was in the serializedGraph
variable and found this:
{ nodes:
[ { id: 'a' },
{ id: 'b' },
{ id: 'c' },
{ id: 'e' },
{ id: 'd' },
{ id: 'f' },
{ id: 'i' },
{ id: 'g' },
{ id: 'h' },
{ id: 'j' } ],
links:
[ { source: 'a', target: 'b' },
{ source: 'b', target: 'c' },
{ source: 'c', target: 'e' },
{ source: 'e', target: 'd' },
{ source: 'e', target: 'a' },
{ source: 'd', target: 'c' },
{ source: 'f', target: 'i' },
{ source: 'i', target: 'j' },
{ source: 'i', target: 'h' },
{ source: 'g', target: 'f' },
{ source: 'h', target: 'g' },
{ source: 'j', target: 'f' } ] }
As you can see, there is no weight data in the serialized graph.
Using the following code:
The weight outputted by
shortestPath()
is 8, but this is incorrect.