From 88d038b5c8899867d09d2e96237104add3216f19 Mon Sep 17 00:00:00 2001 From: m-maksyutin Date: Mon, 25 Jun 2018 14:33:31 +0300 Subject: [PATCH] Fix the findEdge method of the graph (#80) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Fix the findEdge method of the graph --- src/data-structures/graph/Graph.js | 3 +++ src/data-structures/graph/__test__/Graph.test.js | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/data-structures/graph/Graph.js b/src/data-structures/graph/Graph.js index e2ef20b7..8e603c38 100644 --- a/src/data-structures/graph/Graph.js +++ b/src/data-structures/graph/Graph.js @@ -115,6 +115,9 @@ export default class Graph { */ findEdge(startVertex, endVertex) { const vertex = this.getVertexByKey(startVertex.getKey()); + if (!vertex) { + return null; + } return vertex.findEdge(endVertex); } diff --git a/src/data-structures/graph/__test__/Graph.test.js b/src/data-structures/graph/__test__/Graph.test.js index 1c6592ba..be6e9d7f 100644 --- a/src/data-structures/graph/__test__/Graph.test.js +++ b/src/data-structures/graph/__test__/Graph.test.js @@ -87,9 +87,11 @@ describe('Graph', () => { const graphEdgeAB = graph.findEdge(vertexA, vertexB); const graphEdgeBA = graph.findEdge(vertexB, vertexA); - const graphEdgeAC = graph.findEdge(vertexB, vertexC); + const graphEdgeAC = graph.findEdge(vertexA, vertexC); + const graphEdgeCA = graph.findEdge(vertexC, vertexA); expect(graphEdgeAC).toBeNull(); + expect(graphEdgeCA).toBeNull(); expect(graphEdgeAB).toEqual(edgeAB); expect(graphEdgeBA).toEqual(edgeAB); expect(graphEdgeAB.weight).toBe(10); @@ -108,9 +110,11 @@ describe('Graph', () => { const graphEdgeAB = graph.findEdge(vertexA, vertexB); const graphEdgeBA = graph.findEdge(vertexB, vertexA); - const graphEdgeAC = graph.findEdge(vertexB, vertexC); + const graphEdgeAC = graph.findEdge(vertexA, vertexC); + const graphEdgeCA = graph.findEdge(vertexC, vertexA); expect(graphEdgeAC).toBeNull(); + expect(graphEdgeCA).toBeNull(); expect(graphEdgeBA).toBeNull(); expect(graphEdgeAB).toEqual(edgeAB); expect(graphEdgeAB.weight).toBe(10);