Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2c78eb3

Browse files
committed
restructure Graph vertices to be objects with key and value props
1 parent 207eba0 commit 2c78eb3

File tree

6 files changed

+110
-87
lines changed

6 files changed

+110
-87
lines changed

‎dist/datastructures/Graph.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,90 +8,96 @@ const Queue_1 = __importDefault(require("./Queue"));
88
;
99
;
1010
;
11+
;
1112
class Graph {
1213
constructor() {
13-
this.addVertex = (name) => {
14+
this.addVertex = (name, value = null) => {
15+
const vertex = { key: name, value: value };
1416
if (!this.adjacencyList[name])
1517
this.adjacencyList[name] = [];
18+
if (!this.vertices[name])
19+
this.vertices[name] = vertex;
1620
return this.adjacencyList[name];
1721
};
18-
this.addEdge = (firstVertex,secondVertex, options = { direction: 'bi' }) => {
22+
this.addEdge = (firstKey,secondKey, options = { direction: 'bi' }) => {
1923
// bidirectional
2024
const { direction } = options;
21-
if (!this.adjacencyList[firstVertex] || !this.adjacencyList[secondVertex]) {
25+
if (!this.adjacencyList[firstKey] || !this.adjacencyList[secondKey]) {
2226
return null; // maybe throw error instead?
2327
}
24-
if (!this.adjacencyList[firstVertex].includes(secondVertex)) {
25-
this.adjacencyList[firstVertex].push(secondVertex);
28+
if (!this.adjacencyList[firstKey].find((key)=>key===secondKey)) {
29+
this.adjacencyList[firstKey].push(secondKey);
2630
}
27-
if (direction === 'bi' && !this.adjacencyList[secondVertex].includes(firstVertex)) {
28-
this.adjacencyList[secondVertex].push(firstVertex);
31+
if (direction === 'bi' && !this.adjacencyList[secondKey].find((key)=>key===firstKey)) {
32+
this.adjacencyList[secondKey].push(firstKey);
2933
}
3034
return this.adjacencyList;
3135
};
32-
this.removeEdge = (firstVertex,secondVertex, options = { direction: 'bi' }) => {
36+
this.removeEdge = (firstKey,secondKey, options = { direction: 'bi' }) => {
3337
const { direction } = options;
34-
const firstEdge = this.adjacencyList[firstVertex].findIndex((edge) => edge === secondVertex);
38+
const firstEdge = this.adjacencyList[firstKey].findIndex((key) => key === secondKey);
3539
if (firstEdge !== -1) {
36-
this.adjacencyList[firstVertex].splice(firstEdge, 1);
40+
this.adjacencyList[firstKey].splice(firstEdge, 1);
3741
}
3842
if (direction === 'bi') {
39-
const secondEdge = this.adjacencyList[secondVertex].findIndex((edge) => edge === firstVertex);
43+
const secondEdge = this.adjacencyList[secondKey].findIndex((key) => key === firstKey);
4044
if (secondEdge !== -1) {
41-
this.adjacencyList[secondVertex].splice(secondEdge, 1);
45+
this.adjacencyList[secondKey].splice(secondEdge, 1);
4246
}
4347
}
4448
return this.adjacencyList;
4549
};
46-
this.removeVertex = (vertex, options = { direction: 'bi' }) => {
47-
if (this.adjacencyList[vertex] === undefined)
50+
this.removeVertex = (key, options = { direction: 'bi' }) => {
51+
if (this.adjacencyList[key] === undefined)
4852
return null;
4953
const { direction } = options;
5054
if (direction === 'bi') {
51-
for (let edge of this.adjacencyList[vertex]) {
52-
this.adjacencyList[edge] = this.adjacencyList[edge].filter(v => v !== vertex);
55+
for (let edge of this.adjacencyList[key]) {
56+
this.adjacencyList[edge] = this.adjacencyList[edge].filter(v => v !== key);
5357
}
5458
}
5559
else if (direction === 'mono') {
5660
/* have to iterate over every vertex to delete any mono-directional
5761
references to the removed vertex */
5862
for (let edge of Object.keys(this.adjacencyList)) {
59-
this.adjacencyList[edge] = this.adjacencyList[edge].filter(v => v !== vertex);
63+
this.adjacencyList[edge] = this.adjacencyList[edge].filter(v => v !== key);
6064
}
6165
}
62-
delete this.adjacencyList[vertex];
66+
delete this.adjacencyList[key];
6367
return this.adjacencyList;
6468
};
65-
this.depthFirstTraversal = (vertex, map = v => v) => {
69+
this.depthFirstTraversal = (startKey, map = v => v) => {
6670
// takes a starting vertex, and a map function to call on each vertex
6771
const visited = {};
6872
const results = [];
69-
const helper = (vertex) => {
70-
if (!this.adjacencyList[vertex])
73+
const helper = (vertexKey) => {
74+
if (!this.adjacencyList[vertexKey])
7175
return;
72-
results.push(map(vertex));
73-
visited[vertex] = true;
74-
for (let neighbour of this.adjacencyList[vertex]) {
76+
results.push(map(this.vertices[vertexKey]));
77+
visited[vertexKey] = true;
78+
for (let neighbour of this.adjacencyList[vertexKey]) {
7579
if (!visited[neighbour]) {
7680
helper(neighbour);
7781
}
7882
}
7983
};
80-
helper(vertex);
84+
helper(startKey);
8185
return results;
8286
};
83-
this.iterativeDFS = (startingVertex, map = v => v) => {
87+
this.iterativeDFS = (startKey, map = v => v) => {
88+
if (!this.adjacencyList[startKey])
89+
return null;
8490
let stack = new Stack_1.default();
8591
let results = [];
8692
const visited = {};
87-
stack.push(startingVertex);
93+
stack.push(startKey);
8894
while (stack.length > 0) {
8995
// @ts-ignore // ignore typescript concern that stack.pop will return
9096
// void (only happens if stack.size === 0, avoided by while loop)
9197
let vertex = stack.pop().data;
9298
if (!visited[vertex]) {
9399
visited[vertex] = true;
94-
results.push(map(vertex));
100+
results.push(map(this.vertices[vertex]));
95101
for (let neighbour of this.adjacencyList[vertex]) {
96102
if (!visited[neighbour]) {
97103
stack.push(neighbour);
@@ -101,19 +107,20 @@ class Graph {
101107
}
102108
return results;
103109
};
104-
this.breathFirstTraversal = (startingVertex, map = v => v) => {
110+
this.breathFirstTraversal = (vertexKey, map = v => v) => {
105111
let results = [];
106-
if (!startingVertex)
112+
if (!vertexKey)
107113
return null;
108114
const queue = new Queue_1.default();
109-
const visited = { [startingVertex]: true };
110-
queue.enqueue(startingVertex);
115+
const visited = { [vertexKey]: true };
116+
queue.enqueue(vertexKey);
111117
let dequeuedNode;
112118
while (queue.length) {
113119
dequeuedNode = queue.dequeue();
114120
if (dequeuedNode) {
115-
results.push(map(dequeuedNode.data));
116-
for (let neighbour of this.adjacencyList[dequeuedNode.data]) {
121+
let dequeuedKey = dequeuedNode.data;
122+
results.push(map(this.vertices[dequeuedKey]));
123+
for (let neighbour of this.adjacencyList[dequeuedKey]) {
117124
if (!visited[neighbour]) {
118125
visited[neighbour] = true;
119126
queue.enqueue(neighbour);
@@ -124,6 +131,7 @@ class Graph {
124131
return results;
125132
};
126133
this.adjacencyList = {};
134+
this.vertices = {};
127135
// TODO: change directional to boolean and have it be set in constructor
128136
}
129137
}

‎dist/datastructures/Graph.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ describe('depth first traversal of graphs', () => {
8888
});
8989
test('DFS method returns array of all connected vertices', () => {
9090
populateGraph(graph);
91-
let results = graph.depthFirstTraversal('Alice');
91+
let results = graph.depthFirstTraversal('Alice',(v)=>v.key);
9292
expect(results).toEqual(["Alice", "Cheshire Cat", "Mad Hatter", "March Hare", "Dormouse", "Time"]);
93-
results = graph.depthFirstTraversal('Time'); // different route
93+
results = graph.depthFirstTraversal('Time',(v)=>v.key); // different route
9494
expect(results).toEqual(["Time", "Mad Hatter", "Alice", "Cheshire Cat", "March Hare", "Dormouse"]);
9595
});
9696
});
@@ -100,9 +100,9 @@ describe('iterative implementation of DFS', () => {
100100
});
101101
test('iterative dfs method returns array of all connected vertices in proper order', () => {
102102
populateGraph(graph);
103-
let results = graph.iterativeDFS('Alice');
103+
let results = graph.iterativeDFS('Alice',v=>v.key);
104104
expect(results).toEqual(["Alice", "Dormouse", "March Hare", "Mad Hatter", "Time", "Cheshire Cat"]);
105-
results = graph.iterativeDFS('Time'); // different route
105+
results = graph.iterativeDFS('Time',v=>v.key); // different route
106106
expect(results).toEqual(["Time", "Mad Hatter", "Dormouse", "March Hare", "Alice", "Cheshire Cat"]);
107107
});
108108
});
@@ -112,11 +112,11 @@ describe('breadth first search implementation', () => {
112112
});
113113
test('breath first search returns array of all connected vertices in proper order', () => {
114114
populateGraph(graph);
115-
let results = graph.breathFirstTraversal('Alice');
115+
let results = graph.breathFirstTraversal('Alice',v=>v.key);
116116
expect(results).toEqual(['Alice', 'Cheshire Cat', 'Mad Hatter',
117117
'March Hare', 'Dormouse', 'Time']);
118118
// n.b. time comes last as it has a distance of 2
119-
results = graph.breathFirstTraversal('Time');
119+
results = graph.breathFirstTraversal('Time',v=>v.key);
120120
expect(results).toEqual(['Time', 'Mad Hatter', 'Alice', 'March Hare',
121121
'Dormouse', 'Cheshire Cat']);
122122
// n.b. cheshire cat comes last as it has a distance of 2

‎dist/search/dijkstras.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
// dijkstra's shortest path graph algorithm

‎src/datastructures/Graph.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Graph from './Graph';
1+
import Graph,{vertex} from './Graph';
22

33
let graph = new Graph();
44
beforeEach(() => {
@@ -89,9 +89,9 @@ describe('depth first traversal of graphs', () => {
8989
});
9090
test('DFS method returns array of all connected vertices', () => {
9191
populateGraph(graph);
92-
let results = graph.depthFirstTraversal('Alice');
92+
let results = graph.depthFirstTraversal('Alice',(v)=>v.key);
9393
expect(results).toEqual(["Alice", "Cheshire Cat", "Mad Hatter", "March Hare", "Dormouse", "Time"]);
94-
results = graph.depthFirstTraversal('Time'); // different route
94+
results = graph.depthFirstTraversal('Time',(v)=>v.key); // different route
9595
expect(results).toEqual(["Time", "Mad Hatter", "Alice", "Cheshire Cat", "March Hare", "Dormouse"]);
9696
});
9797
});
@@ -102,9 +102,9 @@ describe('iterative implementation of DFS', () => {
102102
});
103103
test('iterative dfs method returns array of all connected vertices in proper order', () => {
104104
populateGraph(graph);
105-
let results = graph.iterativeDFS('Alice');
105+
let results = graph.iterativeDFS('Alice',v=>v.key);
106106
expect(results).toEqual(["Alice", "Dormouse", "March Hare", "Mad Hatter", "Time", "Cheshire Cat"]);
107-
results = graph.iterativeDFS('Time'); // different route
107+
results = graph.iterativeDFS('Time',v=>v.key); // different route
108108
expect(results).toEqual(["Time", "Mad Hatter", "Dormouse", "March Hare", "Alice", "Cheshire Cat"]);
109109
});
110110
});
@@ -115,11 +115,11 @@ describe('breadth first search implementation', () => {
115115
});
116116
test('breath first search returns array of all connected vertices in proper order', () => {
117117
populateGraph(graph);
118-
let results = graph.breathFirstTraversal('Alice');
118+
let results = graph.breathFirstTraversal('Alice',v=>v.key);
119119
expect(results).toEqual(['Alice', 'Cheshire Cat', 'Mad Hatter',
120120
'March Hare', 'Dormouse', 'Time']);
121121
// n.b. time comes last as it has a distance of 2
122-
results = graph.breathFirstTraversal('Time');
122+
results = graph.breathFirstTraversal('Time',v=>v.key);
123123
expect(results).toEqual(['Time', 'Mad Hatter', 'Alice', 'March Hare',
124124
'Dormouse', 'Cheshire Cat']);
125125
// n.b. cheshire cat comes last as it has a distance of 2

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /