Skip to content

Commit de5b771

Browse files
committedMay 3, 2018
Add Bellman-Ford.
1 parent 5788575 commit de5b771

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed
 

‎src/algorithms/graph/bellman-ford/__test__/bellmanFord.test.js

+62-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,68 @@ import Graph from '../../../../data-structures/graph/Graph';
44
import bellmanFord from '../bellmanFord';
55

66
describe('bellmanFord', () => {
7-
it('should find minimum paths to all vertices', () => {
7+
it('should find minimum paths to all vertices for undirected graph', () => {
8+
const vertexA = new GraphVertex('A');
9+
const vertexB = new GraphVertex('B');
10+
const vertexC = new GraphVertex('C');
11+
const vertexD = new GraphVertex('D');
12+
const vertexE = new GraphVertex('E');
13+
const vertexF = new GraphVertex('F');
14+
const vertexG = new GraphVertex('G');
15+
const vertexH = new GraphVertex('H');
16+
17+
const edgeAB = new GraphEdge(vertexA, vertexB, 4);
18+
const edgeAE = new GraphEdge(vertexA, vertexE, 7);
19+
const edgeAC = new GraphEdge(vertexA, vertexC, 3);
20+
const edgeBC = new GraphEdge(vertexB, vertexC, 6);
21+
const edgeBD = new GraphEdge(vertexB, vertexD, 5);
22+
const edgeEC = new GraphEdge(vertexE, vertexC, 8);
23+
const edgeED = new GraphEdge(vertexE, vertexD, 2);
24+
const edgeDC = new GraphEdge(vertexD, vertexC, 11);
25+
const edgeDG = new GraphEdge(vertexD, vertexG, 10);
26+
const edgeDF = new GraphEdge(vertexD, vertexF, 2);
27+
const edgeFG = new GraphEdge(vertexF, vertexG, 3);
28+
const edgeEG = new GraphEdge(vertexE, vertexG, 5);
29+
30+
const graph = new Graph();
31+
graph
32+
.addVertex(vertexH)
33+
.addEdge(edgeAB)
34+
.addEdge(edgeAE)
35+
.addEdge(edgeAC)
36+
.addEdge(edgeBC)
37+
.addEdge(edgeBD)
38+
.addEdge(edgeEC)
39+
.addEdge(edgeED)
40+
.addEdge(edgeDC)
41+
.addEdge(edgeDG)
42+
.addEdge(edgeDF)
43+
.addEdge(edgeFG)
44+
.addEdge(edgeEG);
45+
46+
const { distances, previousVertices } = bellmanFord(graph, vertexA);
47+
48+
expect(distances).toEqual({
49+
H: Infinity,
50+
A: 0,
51+
B: 4,
52+
E: 7,
53+
C: 3,
54+
D: 9,
55+
G: 12,
56+
F: 11,
57+
});
58+
59+
expect(previousVertices.F.getKey()).toBe('D');
60+
expect(previousVertices.D.getKey()).toBe('B');
61+
expect(previousVertices.B.getKey()).toBe('A');
62+
expect(previousVertices.G.getKey()).toBe('E');
63+
expect(previousVertices.C.getKey()).toBe('A');
64+
expect(previousVertices.A).toBeNull();
65+
expect(previousVertices.H).toBeNull();
66+
});
67+
68+
it('should find minimum paths to all vertices for directed graph with negative edge weights', () => {
869
const vertexS = new GraphVertex('S');
970
const vertexE = new GraphVertex('E');
1071
const vertexA = new GraphVertex('A');

‎src/algorithms/graph/dijkstra/__test__/dijkstra.test.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Graph from '../../../../data-structures/graph/Graph';
44
import dijkstra from '../dijkstra';
55

66
describe('dijkstra', () => {
7-
it('should find minimum paths to all vertices', () => {
7+
it('should find minimum paths to all vertices for undirected graph', () => {
88
const vertexA = new GraphVertex('A');
99
const vertexB = new GraphVertex('B');
1010
const vertexC = new GraphVertex('C');
@@ -64,4 +64,54 @@ describe('dijkstra', () => {
6464
expect(previousVertices.A).toBeNull();
6565
expect(previousVertices.H).toBeNull();
6666
});
67+
68+
it('should find minimum paths to all vertices for directed graph with negative edge weights', () => {
69+
const vertexS = new GraphVertex('S');
70+
const vertexE = new GraphVertex('E');
71+
const vertexA = new GraphVertex('A');
72+
const vertexD = new GraphVertex('D');
73+
const vertexB = new GraphVertex('B');
74+
const vertexC = new GraphVertex('C');
75+
const vertexH = new GraphVertex('H');
76+
77+
const edgeSE = new GraphEdge(vertexS, vertexE, 8);
78+
const edgeSA = new GraphEdge(vertexS, vertexA, 10);
79+
const edgeED = new GraphEdge(vertexE, vertexD, 1);
80+
const edgeDA = new GraphEdge(vertexD, vertexA, -4);
81+
const edgeDC = new GraphEdge(vertexD, vertexC, -1);
82+
const edgeAC = new GraphEdge(vertexA, vertexC, 2);
83+
const edgeCB = new GraphEdge(vertexC, vertexB, -2);
84+
const edgeBA = new GraphEdge(vertexB, vertexA, 1);
85+
86+
const graph = new Graph(true);
87+
graph
88+
.addVertex(vertexH)
89+
.addEdge(edgeSE)
90+
.addEdge(edgeSA)
91+
.addEdge(edgeED)
92+
.addEdge(edgeDA)
93+
.addEdge(edgeDC)
94+
.addEdge(edgeAC)
95+
.addEdge(edgeCB)
96+
.addEdge(edgeBA);
97+
98+
const { distances, previousVertices } = dijkstra(graph, vertexS);
99+
100+
expect(distances).toEqual({
101+
H: Infinity,
102+
S: 0,
103+
A: 5,
104+
B: 5,
105+
C: 7,
106+
D: 9,
107+
E: 8,
108+
});
109+
110+
expect(previousVertices.H).toBeNull();
111+
expect(previousVertices.S).toBeNull();
112+
expect(previousVertices.B.getKey()).toBe('C');
113+
expect(previousVertices.C.getKey()).toBe('A');
114+
expect(previousVertices.A.getKey()).toBe('D');
115+
expect(previousVertices.D.getKey()).toBe('E');
116+
});
67117
});

0 commit comments

Comments
 (0)
Please sign in to comment.