I tried your iterative solution on the test engine (educative.io Run button)
answers:
11
9
then i ran the same solution code verbatim in node.js
5.5
4.5
How could it be exactly half? By eliminating “divide by two”, I got the right answers in node.js.
The reason, I discovered, is that Graph.js used in this problem is “Undirected Graph”, whereas the version saved locally was from previous code challenges.
here is the diff:
addEdge(source, destination) {
if (source < this.vertices && destination < this.vertices){
this.list[source].insertAtHead(destination);
-
}this.list[destination].insertAtHead(source); return this; }
i’d suggest wrapping it in a conditional to be uniform, or naming the implementations UndirectedGraph.js, and DirectedGraph.js.
-
if(undirected) this.list[destination].insertAtHead(source);