|
| 1 | +/** |
| 2 | + * @author Rashik Ansar |
| 3 | + * Implemtaion of graph with the help of Adjacency List |
| 4 | + * Its Unweighted graph implemetation |
| 5 | + */ |
| 6 | + |
| 7 | +class Graph { |
| 8 | + constructor() { |
| 9 | + this.adjacencyList = {}; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * Adding a vertex to the graph |
| 14 | + * @param {*} vertex The data of the vertex to add into graph |
| 15 | + * @returns {Graph} |
| 16 | + */ |
| 17 | + addVertex(vertex) { |
| 18 | + if (this.adjacencyList[vertex]) { |
| 19 | + throw Error(`${vertex} already exist in graph... `); |
| 20 | + } else { |
| 21 | + this.adjacencyList[vertex] = []; |
| 22 | + } |
| 23 | + // return this; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Removing a vertex from the graph |
| 28 | + * @param {*} vertex The data of the vertex to remove from graph |
| 29 | + */ |
| 30 | + removeVertex(vertex) { |
| 31 | + if (this.adjacencyList[vertex]) { |
| 32 | + while (this.adjacencyList[vertex].length) { |
| 33 | + const itemInArray = this.adjacencyList[vertex].pop(); |
| 34 | + this.removeEdge(vertex, itemInArray); |
| 35 | + } |
| 36 | + delete this.adjacencyList[vertex]; |
| 37 | + // return this; // Added for testing before traversal methods |
| 38 | + } else { |
| 39 | + throw Error(`${vertex} doesn't exist...`); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adding an edge between two vertices in the graph |
| 45 | + * @param {*} vertex1 |
| 46 | + * @param {*} vertex2 |
| 47 | + */ |
| 48 | + addEdge(vertex1, vertex2) { |
| 49 | + if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) { |
| 50 | + this.adjacencyList[vertex1].push(vertex2); |
| 51 | + this.adjacencyList[vertex2].push(vertex1); |
| 52 | + // return this; |
| 53 | + } else { |
| 54 | + throw Error('Given vertex/vertices might not exist in graph...'); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Removing an existing edge between two vertices in the graph |
| 60 | + * @param {*} vertex1 |
| 61 | + * @param {*} vertex2 |
| 62 | + */ |
| 63 | + removeEdge(vertex1, vertex2) { |
| 64 | + if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) { |
| 65 | + this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter( |
| 66 | + i => i !== vertex2 |
| 67 | + ); |
| 68 | + this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter( |
| 69 | + i => i !== vertex1 |
| 70 | + ); |
| 71 | + // return this; |
| 72 | + } else { |
| 73 | + throw Error('Given vertex/vertices might not exist in graph...'); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Travesal of the graph by breadth-first approach |
| 79 | + * @param {*} start Starting vertex for traversal |
| 80 | + * @returns {[]} |
| 81 | + */ |
| 82 | + breadthFirstTraversal(start) { |
| 83 | + const queue = []; |
| 84 | + const result = []; |
| 85 | + const visited = {}; |
| 86 | + let current; |
| 87 | + queue.push(start); |
| 88 | + visited[start] = true; |
| 89 | + |
| 90 | + while (queue.length) { |
| 91 | + current = queue.shift(); |
| 92 | + result.push(current); |
| 93 | + this.adjacencyList[current].forEach(x => { |
| 94 | + if (!visited[x]) { |
| 95 | + visited[x] = true; |
| 96 | + queue.push(x); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Depth First Traversal (recursive approach) |
| 105 | + * |
| 106 | + * @param {*} start Starting vertex for traversal |
| 107 | + * @returns {[]} |
| 108 | + */ |
| 109 | + DFTrecursuive(start) { |
| 110 | + const visited = {}; |
| 111 | + const result = []; |
| 112 | + const adjacencyList = this.adjacencyList; |
| 113 | + function dfs(vertex) { |
| 114 | + if (!vertex) { |
| 115 | + throw Error('Incorrect starting vertex!'); |
| 116 | + } |
| 117 | + visited[vertex] = true; |
| 118 | + result.push(vertex); |
| 119 | + adjacencyList[vertex].forEach(x => { |
| 120 | + if (!visited[x]) { |
| 121 | + return dfs(x); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + dfs(start); |
| 126 | + return result; |
| 127 | + } |
| 128 | + /** |
| 129 | + * Depth First Traversal (Iterative approach) |
| 130 | + * |
| 131 | + * @param {*} start Starting vertex for traversal |
| 132 | + * @returns {[]} |
| 133 | + */ |
| 134 | + DFTiterative(start) { |
| 135 | + const stack = []; |
| 136 | + const visited = {}; |
| 137 | + const result = []; |
| 138 | + let current; |
| 139 | + |
| 140 | + stack.push(start); |
| 141 | + visited[start] = true; |
| 142 | + while (stack.length) { |
| 143 | + current = stack.pop(); |
| 144 | + result.push(current); |
| 145 | + this.adjacencyList[current].forEach(x => { |
| 146 | + if (!visited[x]) { |
| 147 | + visited[x] = true; |
| 148 | + stack.push(x); |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | + return result; |
| 153 | + } |
| 154 | +} |
0 commit comments