Psst: Open the JavaScript Console and try to play around with these functions:
const graph = new Graph();
graph.addVertex("Tokyo").addVertex("Dallas").addVertex("Aspen").addVertex("Los
Angeles").addVertex("Hong Kong");
graph.addEdge("Tokyo", "Dallas").addEdge("Dallas",
"Aspen").addEdge("Hong Kong", "Tokyo").addEdge("Hong Kong",
"Dallas").addEdge("Los Angeles", "Hong Kong").addEdge("Los Angeles",
"Aspen");
graph.removeEdge("Tokyo", "Dallas")
graph.removeVertex("Dallas")
const g = new Graph();
g.addVertex("A").addVertex("B").addVertex("C").addVertex("D").addVertex("E").addVertex("F");
g.addEdge("A", "B").addEdge("A", "C").addEdge("B", "D").addEdge("C",
"E").addEdge("D", "E").addEdge("D", "F").addEdge("E", "F");
g.depthFirstSearch("A") // should return ["A", "B", "D", "E", "C",
"F"]
g.depthFirstSearchIterative("A") // should return ["A", "C", "E", "F",
"D", "B"]
g.breadthFirstSearch("A") // should return ["A", "B", "C", "D", "E",
"F"]
g.breadthFirstSearchIterative("A") // should return ["A", "B", "C",
"D", "E", "F"]
const g2 = new WeightedGraph();
g2.addVertex("A").addVertex("B").addVertex("C").addVertex("D").addVertex("E").addVertex("F");
g2.addEdge("A", "B", 4).addEdge("A", "C", 2).addEdge("B", "E",
3).addEdge("C", "D", 2).addEdge("C", "F", 4).addEdge("D", "E",
3).addEdge("D", "F" , 1).addEdge("E", "F" , 1);
const g3 = new WeightedGraph();
g3.addVertex("A").addVertex("Z").addVertex("C").addVertex("D").addVertex("E").addVertex("H").addVertex("Q").addVertex("G");
g3.addEdge("A", "Z", 7).addEdge("A", "C", 8).addEdge("Z", "Q",
2).addEdge("C", "G", 4).addEdge("D", "Q", 8).addEdge("E", "H",
1).addEdge("H", "Q" , 3).addEdge("Q", "C" , 6).addEdge("G", "Q" , 9);
g3.dijkstra("A", "E") // should return ["A", "Z", "Q", "H", "E"]
g3.dijkstra("A", "Q") // should return ["A", "Z", "Q"]
g3.dijkstra("A", "G") // should return ["A", "C", "G"]
g3.dijkstra("A", "D") // should return ["A", "Z", "Q", "D"]
g3.bellmanFord("A", "E") // should return ["A", "Z", "Q", "H", "E"]
g3.bellmanFord("A", "Q") // should return ["A", "Z", "Q"]
g3.bellmanFord("A", "G") // should return ["A", "C", "G"]
g3.bellmanFord("A", "D") // should return ["A", "Z", "Q", "D"]
const g4 = new WeightedGraph();
g4.addVertex("A") .addVertex("B") .addVertex("C") .addVertex("D")
.addVertex("E");
g4.addDirectedEdge("A", "B", 9) .addDirectedEdge("A", "D", 2)
.addDirectedEdge("B", "E", -3) .addDirectedEdge("C", "A", 5)
.addDirectedEdge("C", "B", 3) .addDirectedEdge("D", "B", -4)
.addDirectedEdge("D", "E", 6) .addDirectedEdge("E", "C", 7);
g4.bellmanFord("A", "C") // should return ["A", "D", "B", "E", "C"]
const g5 = new WeightedGraph();
g5.addVertex("A") .addVertex("B") .addVertex("C") .addVertex("D")
.addVertex("E");
g5.addDirectedEdge("A", "B", 9) .addDirectedEdge("A", "D", 2)
.addDirectedEdge("B", "E", -3) .addDirectedEdge("C", "A", -5)
.addDirectedEdge("C", "B", 3) .addDirectedEdge("D", "B", -4)
.addDirectedEdge("D", "E", 6) .addDirectedEdge("E", "C", 7);
g5.bellmanFord("A", "C") // Error: Negative cycle exists!