mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Implemented method to generate adjacency list for graph
This commit is contained in:
parent
dc1047df72
commit
66919b48b6
@ -188,6 +188,36 @@ export default class Graph {
|
||||
return adjacencyMatrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {*[{}]}
|
||||
*/
|
||||
getAdjacencyList() {
|
||||
const edges = this.getAllEdges();
|
||||
const adjacencyList = {};
|
||||
|
||||
edges.forEach((edge) => {
|
||||
const { weight } = edge;
|
||||
const startVertexValue = edge.startVertex.value;
|
||||
const endVertexValue = edge.endVertex.value;
|
||||
|
||||
if (adjacencyList[startVertexValue]) {
|
||||
adjacencyList[edge.startVertex.value].push({ weight, value: endVertexValue });
|
||||
} else {
|
||||
adjacencyList[startVertexValue] = [{ weight, value: endVertexValue }];
|
||||
}
|
||||
|
||||
if (!this.isDirected) {
|
||||
if (adjacencyList[endVertexValue]) {
|
||||
adjacencyList[endVertexValue].push({ weight, value: startVertexValue });
|
||||
} else {
|
||||
adjacencyList[endVertexValue] = [{ weight, value: startVertexValue }];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return adjacencyList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
@ -385,4 +385,59 @@ describe('Graph', () => {
|
||||
[Infinity, Infinity, Infinity, Infinity],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should generate adjacency list for undirected graph', () => {
|
||||
const vertexA = new GraphVertex('A');
|
||||
const vertexB = new GraphVertex('B');
|
||||
const vertexC = new GraphVertex('C');
|
||||
const vertexD = new GraphVertex('D');
|
||||
|
||||
const edgeAB = new GraphEdge(vertexA, vertexB, 2);
|
||||
const edgeBC = new GraphEdge(vertexB, vertexC, 1);
|
||||
const edgeCD = new GraphEdge(vertexC, vertexD, 5);
|
||||
const edgeBD = new GraphEdge(vertexB, vertexD, 7);
|
||||
|
||||
const graph = new Graph();
|
||||
graph
|
||||
.addEdge(edgeAB)
|
||||
.addEdge(edgeBC)
|
||||
.addEdge(edgeCD)
|
||||
.addEdge(edgeBD);
|
||||
|
||||
const adjacencyList = graph.getAdjacencyList();
|
||||
|
||||
expect(adjacencyList).toEqual({
|
||||
A: [{ value: 'B', weight: 2 }],
|
||||
B: [{ value: 'A', weight: 2 }, { value: 'C', weight: 1 }, { value: 'D', weight: 7 }],
|
||||
C: [{ value: 'B', weight: 1 }, { value: 'D', weight: 5 }],
|
||||
D: [{ value: 'C', weight: 5 }, { value: 'B', weight: 7 }],
|
||||
});
|
||||
});
|
||||
|
||||
it('should generate adjacency list for directed graph', () => {
|
||||
const vertexA = new GraphVertex('A');
|
||||
const vertexB = new GraphVertex('B');
|
||||
const vertexC = new GraphVertex('C');
|
||||
const vertexD = new GraphVertex('D');
|
||||
|
||||
const edgeAB = new GraphEdge(vertexA, vertexB, 2);
|
||||
const edgeBC = new GraphEdge(vertexB, vertexC, 1);
|
||||
const edgeCD = new GraphEdge(vertexC, vertexD, 5);
|
||||
const edgeBD = new GraphEdge(vertexB, vertexD, 7);
|
||||
|
||||
const graph = new Graph(true);
|
||||
graph
|
||||
.addEdge(edgeAB)
|
||||
.addEdge(edgeBC)
|
||||
.addEdge(edgeCD)
|
||||
.addEdge(edgeBD);
|
||||
|
||||
const adjacencyList = graph.getAdjacencyList();
|
||||
|
||||
expect(adjacencyList).toEqual({
|
||||
A: [{ value: 'B', weight: 2 }],
|
||||
B: [{ value: 'C', weight: 1 }, { value: 'D', weight: 7 }],
|
||||
C: [{ value: 'D', weight: 5 }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user