Skip to content

Commit fcb452d

Browse files
feat: implement undirected graph data structure class
1 parent 4d9fe74 commit fcb452d

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

src/graph/Graph.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export default interface Graph<T> {
2+
numberOfNodes: number;
3+
adjacentList: object;
4+
addVertex(node: string): boolean;
5+
addEdge(vertex: string, node: string): boolean;
6+
hasEdge(vertex: string, node: string): boolean;
7+
displayConnections(): void;
8+
}
9+
10+
export default class Graph<T> implements Graph<T> {
11+
public constructor() {
12+
this.numberOfNodes = 0;
13+
this.adjacentList = {};
14+
}
15+
16+
public addVertex(node: string): boolean {
17+
// Check if vertex already exists on adjacentList
18+
if (!this.adjacentList[node]) {
19+
this.adjacentList[node] = [];
20+
} else {
21+
throw new Error('The node already exists');
22+
}
23+
this.numberOfNodes++;
24+
return true;
25+
}
26+
27+
public addEdge(vertex: string, node: string): boolean {
28+
// If vertex1 exists in adjacentList add vertex2 to the adjacents
29+
// If vertex2 exists in adjacentList add vertex1 to the adjacents
30+
let hasEdge = this.hasEdge(vertex, node);
31+
if (!hasEdge) {
32+
this.adjacentList[vertex].push(node);
33+
this.adjacentList[node].push(vertex);
34+
} else {
35+
throw new Error('The node does not exist');
36+
}
37+
return true;
38+
}
39+
40+
public hasEdge(vertex: string, node: string): boolean {
41+
let adjacents = this.adjacentList[vertex];
42+
for (let i = 0; i < adjacents.length; i++) {
43+
if (node === adjacents[i]) return true;
44+
}
45+
return false;
46+
}
47+
48+
public displayConnections(): void {
49+
const allNodes = Object.keys(this.adjacentList);
50+
for (let node of allNodes) {
51+
let nodeConnections = this.adjacentList[node];
52+
let connections = '';
53+
let vertex: string;
54+
for (vertex of nodeConnections) {
55+
connections += vertex + ' ';
56+
}
57+
console.log(node + ' --> ' + connections);
58+
}
59+
}
60+
}

src/graph/__tests__/Graph.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Graph from '../Graph';
2+
3+
describe('Graphs', () => {
4+
test('create an empty graph', () => {
5+
const graph = new Graph();
6+
7+
expect(graph).toBeDefined();
8+
});
9+
10+
test('add a new vertex to the graph', () => {
11+
const graph = new Graph();
12+
13+
graph.addVertex('0');
14+
graph.addVertex('1');
15+
graph.addVertex('2');
16+
graph.addVertex('3');
17+
graph.addVertex('4');
18+
graph.addVertex('5');
19+
graph.addVertex('6');
20+
21+
expect(graph.numberOfNodes).toBe(7);
22+
expect(graph.adjacentList[0]).toEqual([]);
23+
expect(graph.adjacentList[1]).toEqual([]);
24+
expect(graph.adjacentList[2]).toEqual([]);
25+
expect(graph.adjacentList[3]).toEqual([]);
26+
expect(graph.adjacentList[4]).toEqual([]);
27+
expect(graph.adjacentList[5]).toEqual([]);
28+
expect(graph.adjacentList[6]).toEqual([]);
29+
});
30+
31+
test('throws error if vertex already exists', () => {
32+
try {
33+
const graph = new Graph();
34+
35+
graph.addVertex('0');
36+
graph.addVertex('1');
37+
graph.addVertex('2');
38+
39+
expect(graph.addVertex('2')).toThrowError();
40+
} catch (error) {
41+
expect(error).toBeInstanceOf(Error);
42+
expect(error).toHaveProperty('message', 'The node already exists');
43+
}
44+
});
45+
46+
test('add edges to graph', () => {
47+
const graph = new Graph();
48+
49+
graph.addVertex('0');
50+
graph.addVertex('1');
51+
graph.addVertex('2');
52+
graph.addVertex('3');
53+
graph.addVertex('4');
54+
graph.addVertex('5');
55+
graph.addVertex('6');
56+
57+
graph.addEdge('3', '1');
58+
graph.addEdge('3', '4');
59+
graph.addEdge('4', '2');
60+
graph.addEdge('4', '5');
61+
graph.addEdge('1', '2');
62+
graph.addEdge('1', '0');
63+
graph.addEdge('0', '2');
64+
graph.addEdge('6', '5');
65+
66+
expect(graph.hasEdge('3', '4')).toBeTruthy();
67+
expect(graph.hasEdge('3', '1')).toBeTruthy();
68+
expect(graph.hasEdge('4', '5')).toBeTruthy();
69+
expect(graph.hasEdge('1', '2')).toBeTruthy();
70+
});
71+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ import './doubly-linked-list/DoublyLinkedList';
66
import './stack/StackClass';
77
import './queue/QueueClass';
88
import './tree/binary-search/BinarySearchTree';
9+
import './graph/Graph';

0 commit comments

Comments
 (0)