Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- class Queue {
- constructor() {
- this.dataStore = [];
- }
- enqueue (element) {
- this.dataStore.push(element);
- };
- dequeue () {
- return this.dataStore.shift();
- };
- front () {
- return this.dataStore[0];
- };
- back () {
- return this.dataStore[this.dataStore.length - 1];
- };
- toString() {
- return this.dataStore.join("\n");
- };
- isEmpty (){
- return this.dataStore.length == 0;
- }
- }
- class Graph {
- constructor() {
- this.vertices = [];
- this.edges = new Map();
- };
- addVertex(vertex) {
- this.vertices.push(vertex);
- this.edges.set(vertex, []);
- };
- addEdge(vertexFrom, vertexTo) {
- if (this.edges.has(vertexFrom) && this.edges.has(vertexTo)) {
- this.edges.get(vertexFrom).push(vertexTo);
- this.edges.get(vertexTo).push(vertexFrom);
- }
- };
- removeVertex(vertex) {
- // remove edges
- for (let vert in this.edges.get(vertex)) {
- this.removeEdge(vert, vertex);
- }
- this.edges.delete(vertex);
- // remove vertex
- let index = this.vertices.indexOf(vertex);
- this.vertices.splice(index, 1);
- };
- removeEdge(vertexFrom, vertexTo) {
- if ((this.edges.has(vertexFrom)) && (this.edges.has(vertexTo))) {
- let index;
- index = this.edges.get(vertexFrom).indexOf(vertexTo);
- this.edges.get(vertexFrom).splice(index, 1);
- index = this.edges.get(vertexTo).indexOf(vertexFrom);
- this.edges.get[vertexTo].splice(index, 1);
- }
- };
- bfs(v, action) {
- let colors = new Map();
- this.vertices.forEach(item => colors.set(item, "white"));
- var queue = new Queue();
- queue.enqueue(v);
- while (!queue.isEmpty()) {
- var vertex = queue.dequeue();
- // додати всі білі вершини до черги і позначити їх сірими
- let neighbours = this.edges.get(vertex);
- for (let index in neighbours) {
- if (colors.find(neighbours[index]) === "white") {
- queue.enqueue(neighbours[index]);
- colors.set(neighbours[index], "grey");
- }
- }
- // обробити вершину
- action(vertex);
- // позначити вершину чорною
- colors.set(vertex, "black");
- }
- }
- // for debug purposes
- printData() {
- console.log("Vertices -------------------");
- console.log(this.vertices.join());
- console.log("Edges ----------------------");
- this.edges.forEach((value, key) => console.log(key + "--->" + value.join()));
- }
- }
- const graph = new Graph();
- graph.addVertex("1");
- graph.addVertex("2");
- graph.addVertex("3");
- graph.addVertex("4");
- graph.addVertex("5");
- graph.addVertex("6");
- graph.addVertex("7");
- graph.addVertex("8");
- graph.addVertex("9");
- graph.printData();
- graph.addEdge("1", "2");
- graph.addEdge("1", "7");
- graph.addEdge("1", "8");
- graph.addEdge("2", "3");
- graph.addEdge("2", "6");
- graph.addEdge("3", "4");
- graph.addEdge("3", "5");
- graph.addEdge("8", "9");
- graph.addEdge("8", "12");
- graph.addEdge("9", "10");
- graph.addEdge("9", "11");
- graph.printData();
- graph.removeEdge("9", "11");
- graph.printData();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement