-
-
Save codecademydev/1fbfa268fdf52983da5c09fe08141415 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Edge { | |
constructor(start, end, weight = null) { | |
this.start = start; | |
this.end = end; | |
this.weight = weight; | |
} | |
} | |
module.exports = Edge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Edge = require("./Edge.js"); | |
const Vertex = require("./Vertex.js"); | |
class Graph { | |
constructor(isWeighted = false, isDirected = false) { | |
this.vertices = []; | |
this.isWeighted = isWeighted; | |
this.isDirected = isDirected; | |
} | |
addVertex(data) { | |
const newVertex = new Vertex(data); | |
this.vertices.push(newVertex); | |
return newVertex; | |
} | |
removeVertex(vertex) { | |
this.vertices = this.vertices.filter((v) => v !== vertex); | |
} | |
addEdge(vertexOne, vertexTwo, weight) { | |
const edgeWeight = this.isWeighted ? weight : null; | |
if (vertexOne instanceof Vertex && vertexTwo instanceof Vertex) { | |
vertexOne.addEdge(vertexTwo, edgeWeight); | |
if (!this.isDirected) { | |
vertexTwo.addEdge(vertexOne, edgeWeight); | |
} | |
} else { | |
throw new Error("Expected Vertex arguments."); | |
} | |
} | |
removeEdge(vertexOne, vertexTwo) { | |
if (vertexOne instanceof Vertex && vertexTwo instanceof Vertex) { | |
vertexOne.removeEdge(vertexTwo); | |
if (!this.isDirected) { | |
vertexTwo.removeEdge(vertexOne); | |
} | |
} else { | |
throw new Error("Expected Vertex arguments."); | |
} | |
} | |
print() { | |
this.vertices.forEach((vertex) => vertex.print()); | |
} | |
} | |
module.exports = Graph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Graph = require("./Graph.js"); | |
const trainNetwork = new Graph(true, true); | |
const laStation = trainNetwork.addVertex("Los Angeles"); | |
const sfStation = trainNetwork.addVertex("San Francisco"); | |
const nyStation = trainNetwork.addVertex("New York"); | |
const atlStation = trainNetwork.addVertex("Atlanta"); | |
const denStation = trainNetwork.addVertex("Denver"); | |
const calStation = trainNetwork.addVertex("Calgary"); | |
trainNetwork.addEdge(sfStation, laStation, 400); | |
trainNetwork.addEdge(laStation, sfStation, 400); | |
trainNetwork.addEdge(nyStation, denStation, 1800); | |
trainNetwork.addEdge(denStation, nyStation, 1800); | |
trainNetwork.addEdge(calStation, denStation, 1000); | |
trainNetwork.addEdge(denStation, calStation, 1000); | |
trainNetwork.addEdge(atlStation, laStation, 2100); | |
trainNetwork.addEdge(laStation, atlStation, 2100); | |
trainNetwork.addEdge(atlStation, nyStation, 750); | |
trainNetwork.addEdge(nyStation, atlStation, 750); | |
// trainNetwork.addEdge(nyStation, atlStation, 123); | |
trainNetwork.removeEdge(nyStation, denStation); | |
trainNetwork.removeEdge(calStation, denStation); | |
trainNetwork.removeEdge(denStation, calStation); | |
trainNetwork.removeVertex(calStation); | |
trainNetwork.print(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Edge = require("./Edge.js"); | |
class Vertex { | |
constructor(data) { | |
this.data = data; | |
this.edges = []; | |
} | |
addEdge(vertex, weight) { | |
if (vertex instanceof Vertex) { | |
const existingEdge = this.edges.find((edge) => edge.end === vertex); | |
if (!existingEdge) { | |
this.edges.push(new Edge(this, vertex, weight)); | |
} else { | |
throw new Error( | |
`Edge already exists for ${vertex.data}! \nFound: \n\n\t${existingEdge.start.data} --> ${existingEdge.end.data} (${existingEdge.weight})\n\t ` | |
); | |
} | |
} else { | |
throw new Error("Edge start and end must both be Vertex"); | |
} | |
} | |
removeEdge(vertex) { | |
this.edges = this.edges.filter((edge) => edge.end !== vertex); | |
} | |
print() { | |
const edgeList = this.edges.map((edge) => | |
edge.weight !== null ? `${edge.end.data} (${edge.weight})` : edge.end.data | |
); | |
const output = `${this.data} --> ${edgeList.join(", ")}`; | |
console.log(output); | |
} | |
} | |
module.exports = Vertex; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment