Advertisement
andruhovski

Graph

Jan 18th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. class Queue {
  3.     constructor() {
  4.         this.dataStore = [];
  5.     }
  6.  
  7.     enqueue (element) {
  8.         this.dataStore.push(element);
  9.     };
  10.  
  11.     dequeue () {
  12.         return this.dataStore.shift();
  13.     };
  14.  
  15.     front () {
  16.         return this.dataStore[0];
  17.     };
  18.  
  19.     back () {
  20.         return this.dataStore[this.dataStore.length - 1];
  21.     };
  22.  
  23.     toString() {
  24.         return this.dataStore.join("\n");
  25.     };
  26.  
  27.     isEmpty (){
  28.             return this.dataStore.length == 0;
  29.     }
  30. }
  31.  
  32.  
  33. class Graph {
  34.     constructor() {
  35.         this.vertices = [];
  36.         this.edges = new Map();
  37.     };
  38.  
  39.     addVertex(vertex) {
  40.         this.vertices.push(vertex);
  41.         this.edges.set(vertex, []);
  42.     };
  43.  
  44.     addEdge(vertexFrom, vertexTo) {
  45.         if (this.edges.has(vertexFrom) && this.edges.has(vertexTo)) {
  46.             this.edges.get(vertexFrom).push(vertexTo);
  47.             this.edges.get(vertexTo).push(vertexFrom);
  48.         }
  49.     };
  50.  
  51.     removeVertex(vertex) {
  52.         // remove edges
  53.         for (let vert in this.edges.get(vertex)) {
  54.             this.removeEdge(vert, vertex);
  55.         }
  56.         this.edges.delete(vertex);
  57.         // remove vertex
  58.         let index = this.vertices.indexOf(vertex);
  59.         this.vertices.splice(index, 1);
  60.     };
  61.  
  62.     removeEdge(vertexFrom, vertexTo) {
  63.         if ((this.edges.has(vertexFrom)) && (this.edges.has(vertexTo))) {
  64.             let index;
  65.  
  66.             index = this.edges.get(vertexFrom).indexOf(vertexTo);
  67.             this.edges.get(vertexFrom).splice(index, 1);
  68.  
  69.             index = this.edges.get(vertexTo).indexOf(vertexFrom);
  70.             this.edges.get[vertexTo].splice(index, 1);
  71.         }
  72.     };
  73.  
  74.     bfs(v, action) {
  75.         let colors = new Map();
  76.         this.vertices.forEach(item => colors.set(item, "white"));
  77.  
  78.         var queue = new Queue();
  79.         queue.enqueue(v);
  80.         while (!queue.isEmpty()) {
  81.             var vertex = queue.dequeue();
  82.             // додати всі білі вершини до черги і позначити їх сірими
  83.             let neighbours = this.edges.get(vertex);
  84.             for (let index in neighbours) {
  85.                 if (colors.find(neighbours[index]) === "white") {
  86.                     queue.enqueue(neighbours[index]);
  87.                     colors.set(neighbours[index], "grey");
  88.                 }
  89.             }
  90.             // обробити вершину
  91.             action(vertex);
  92.             // позначити вершину чорною
  93.             colors.set(vertex, "black");
  94.         }
  95.     }
  96.  
  97.     // for debug purposes
  98.     printData() {
  99.         console.log("Vertices -------------------");
  100.         console.log(this.vertices.join());
  101.  
  102.         console.log("Edges ----------------------");
  103.         this.edges.forEach((value, key) => console.log(key + "--->" + value.join()));
  104.     }
  105. }
  106.  
  107. const graph = new Graph();
  108. graph.addVertex("1");
  109. graph.addVertex("2");
  110. graph.addVertex("3");
  111. graph.addVertex("4");
  112. graph.addVertex("5");
  113. graph.addVertex("6");
  114. graph.addVertex("7");
  115. graph.addVertex("8");
  116. graph.addVertex("9");
  117. graph.printData();
  118.  
  119. graph.addEdge("1", "2");
  120. graph.addEdge("1", "7");
  121. graph.addEdge("1", "8");
  122.  
  123. graph.addEdge("2", "3");
  124. graph.addEdge("2", "6");
  125.  
  126. graph.addEdge("3", "4");
  127. graph.addEdge("3", "5");
  128.  
  129. graph.addEdge("8", "9");
  130. graph.addEdge("8", "12");
  131.  
  132. graph.addEdge("9", "10");
  133. graph.addEdge("9", "11");
  134. graph.printData();
  135. graph.removeEdge("9", "11");
  136.  
  137. graph.printData();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement