Movatterモバイル変換


[0]ホーム

URL:


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

Commit0fc7b9d

Browse files
committed
Generate adjacency matrix for graph.
1 parentdbb7a64 commit0fc7b9d

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

‎src/data-structures/graph/Graph.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,43 @@ export default class Graph {
158158
returnthis;
159159
}
160160

161+
/**
162+
*@return {object}
163+
*/
164+
getVerticesIndices(){
165+
constverticesIndices={};
166+
this.getAllVertices().forEach((vertex,index)=>{
167+
verticesIndices[vertex.getKey()]=index;
168+
});
169+
170+
returnverticesIndices;
171+
}
172+
173+
/**
174+
*@return {*[][]}
175+
*/
176+
getAdjacencyMatrix(){
177+
constvertices=this.getAllVertices();
178+
constverticesIndices=this.getVerticesIndices();
179+
180+
// Init matrix with zeros.
181+
constadjacencyMatrix=Array(vertices.length).fill(null).map(()=>{
182+
returnArray(vertices.length).fill(0);
183+
});
184+
185+
// Fill the columns.
186+
vertices.forEach((vertex,vertexIndex)=>{
187+
vertex.getNeighbors().forEach((neighbor)=>{
188+
constneighborIndex=verticesIndices[neighbor.getKey()];
189+
adjacencyMatrix[vertexIndex][neighborIndex]=this.isDirected ?
190+
this.findEdge(vertex,neighbor).weight :
191+
1;
192+
});
193+
});
194+
195+
returnadjacencyMatrix;
196+
}
197+
161198
/**
162199
*@return {string}
163200
*/

‎src/data-structures/graph/__test__/Graph.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,85 @@ describe('Graph', () => {
300300
expect(graph.getNeighbors(vertexD).length).toBe(1);
301301
expect(graph.getNeighbors(vertexD)[0].getKey()).toBe(vertexC.getKey());
302302
});
303+
304+
it('should return vertices indices',()=>{
305+
constvertexA=newGraphVertex('A');
306+
constvertexB=newGraphVertex('B');
307+
constvertexC=newGraphVertex('C');
308+
constvertexD=newGraphVertex('D');
309+
310+
constedgeAB=newGraphEdge(vertexA,vertexB);
311+
constedgeBC=newGraphEdge(vertexB,vertexC);
312+
constedgeCD=newGraphEdge(vertexC,vertexD);
313+
constedgeBD=newGraphEdge(vertexB,vertexD);
314+
315+
constgraph=newGraph();
316+
graph
317+
.addEdge(edgeAB)
318+
.addEdge(edgeBC)
319+
.addEdge(edgeCD)
320+
.addEdge(edgeBD);
321+
322+
constverticesIndices=graph.getVerticesIndices();
323+
expect(verticesIndices).toEqual({
324+
A:0,
325+
B:1,
326+
C:2,
327+
D:3,
328+
});
329+
});
330+
331+
it('should generate adjacency matrix for undirected graph',()=>{
332+
constvertexA=newGraphVertex('A');
333+
constvertexB=newGraphVertex('B');
334+
constvertexC=newGraphVertex('C');
335+
constvertexD=newGraphVertex('D');
336+
337+
constedgeAB=newGraphEdge(vertexA,vertexB);
338+
constedgeBC=newGraphEdge(vertexB,vertexC);
339+
constedgeCD=newGraphEdge(vertexC,vertexD);
340+
constedgeBD=newGraphEdge(vertexB,vertexD);
341+
342+
constgraph=newGraph();
343+
graph
344+
.addEdge(edgeAB)
345+
.addEdge(edgeBC)
346+
.addEdge(edgeCD)
347+
.addEdge(edgeBD);
348+
349+
constadjacencyMatrix=graph.getAdjacencyMatrix();
350+
expect(adjacencyMatrix).toEqual([
351+
[0,1,0,0],
352+
[1,0,1,1],
353+
[0,1,0,1],
354+
[0,1,1,0],
355+
]);
356+
});
357+
358+
it('should generate adjacency matrix for directed graph',()=>{
359+
constvertexA=newGraphVertex('A');
360+
constvertexB=newGraphVertex('B');
361+
constvertexC=newGraphVertex('C');
362+
constvertexD=newGraphVertex('D');
363+
364+
constedgeAB=newGraphEdge(vertexA,vertexB,2);
365+
constedgeBC=newGraphEdge(vertexB,vertexC,1);
366+
constedgeCD=newGraphEdge(vertexC,vertexD,5);
367+
constedgeBD=newGraphEdge(vertexB,vertexD,7);
368+
369+
constgraph=newGraph(true);
370+
graph
371+
.addEdge(edgeAB)
372+
.addEdge(edgeBC)
373+
.addEdge(edgeCD)
374+
.addEdge(edgeBD);
375+
376+
constadjacencyMatrix=graph.getAdjacencyMatrix();
377+
expect(adjacencyMatrix).toEqual([
378+
[0,2,0,0],
379+
[0,0,1,7],
380+
[0,0,0,5],
381+
[0,0,0,0],
382+
]);
383+
});
303384
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp