|
| 1 | +/* A Graph consists of a finite set of vertices(or nodes) and a set of Edges that connect a pair of nodes. |
| 2 | +A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred |
| 3 | +to as vertices and the edges are lines or arcs that connect any two nodes in the graph |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | +ESSENTIAL GRAPH TERMS |
| 8 | +- Vertex - a node |
| 9 | +- Edge - connection between nodes |
| 10 | +- Weighted/Unweighted - values assigned to distances between vertices |
| 11 | +- Directed/Undirected - directions assigned to distanced between vertices |
| 12 | +*/ |
| 13 | + |
| 14 | +// DIFFERENCES & BIG O |
| 15 | +// OPERATION ADJACENCY LIST ADJACENCY MATRIX |
| 16 | +// Add Vertex O(1) O(|V^2|) |
| 17 | +// Add Edge O(1) O(1) |
| 18 | +// Remove Vertex O(|V| + |E|) O(|V^2|) |
| 19 | +// Remove Edge O(|E|) O(1) |
| 20 | +// Query O(|V| + |E|) O(1) |
| 21 | +// Storage O(|V| + |E|) O(|V^2|) |
| 22 | + |
| 23 | +// Adjacency List |
| 24 | +// Takes up more space (in sparse graphs) |
| 25 | +// Slower to iterate over all edges |
| 26 | +// Faster to lookup specific edge |
| 27 | +// Can take up less space (in sparse graphs) |
| 28 | +// Faster to iterate over all edges |
| 29 | +// Can be slower to lookup specific edge |
| 30 | + |
| 31 | +// Adjacency Matrix |
| 32 | +// Takes up more space (in sparse graphs) |
| 33 | +// Slower to iterate over all edges |
| 34 | +// Faster to lookup specific edge |
| 35 | + |
| 36 | +// WE ARE BUILDING AN UNDIRECTED GRAPH |
| 37 | +class Graph { |
| 38 | + constructor() { |
| 39 | + this.adjacencyList = {}; |
| 40 | + } |
| 41 | + addVertex(vertex) { |
| 42 | + if (!this.adjacencyList[vertex]) this.adjacencyList[vertex] = []; |
| 43 | + } |
| 44 | + addEdge(vertex1, vertex2) { |
| 45 | + this.adjacencyList[vertex1].push(vertex2); |
| 46 | + this.adjacencyList[vertex2].push(vertex1); |
| 47 | + } |
| 48 | + removeEdge(vertex1, vertex2) { |
| 49 | + this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter( |
| 50 | + (v) => v != vertex2 |
| 51 | + ); |
| 52 | + this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter( |
| 53 | + (v) => v != vertex1 |
| 54 | + ); |
| 55 | + } |
| 56 | + remoVertex(vertex) { |
| 57 | + while (this.adjacencyList[vertex].length) { |
| 58 | + let adjacentVertex = this.adjacencyList[vertex].pop(); |
| 59 | + this.removeEdge(vertex, adjacentVertex); |
| 60 | + } |
| 61 | + delete this.adjacencyList[vertex]; |
| 62 | + } |
| 63 | + depthFirstRecursive(start) { |
| 64 | + let result = []; |
| 65 | + let visited = []; |
| 66 | + let adjacencyList = this.adjacencyList; |
| 67 | + (function dfs(vertex) { |
| 68 | + if (!vertex) return null; |
| 69 | + visited[vertex] = true; |
| 70 | + result.push(vertex); |
| 71 | + adjacencyList[vertex].forEach((neighbour) => { |
| 72 | + if (!visited[neighbour]) { |
| 73 | + return dfs(neighbour); |
| 74 | + } |
| 75 | + }); |
| 76 | + })(start); |
| 77 | + return result; |
| 78 | + } |
| 79 | + depthFirstIterative(start) { |
| 80 | + const stack = [start]; |
| 81 | + const result = []; |
| 82 | + const visited = {}; |
| 83 | + let currentVertex; |
| 84 | + visited[start] = true; |
| 85 | + while (stack.length) { |
| 86 | + currentVertex = stack.pop(); |
| 87 | + result.push(currentVertex); |
| 88 | + this.adjacencyList[currentVertex].forEach((neighbor) => { |
| 89 | + if (!visited[neighbor]) { |
| 90 | + visited[neighbor] = true; |
| 91 | + stack.push(neighbor); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + return result; |
| 96 | + } |
| 97 | + breadthFirst(start) { |
| 98 | + let queue = [start]; |
| 99 | + let result = []; |
| 100 | + let visited = {}; |
| 101 | + let currentVertex; |
| 102 | + visited[start] = true; |
| 103 | + while (queue.length) { |
| 104 | + currentVertex = queue.shift(); |
| 105 | + result.push(currentVertex); |
| 106 | + this.adjacencyList[currentVertex].forEach((neighbor) => { |
| 107 | + if (!visited[neighbor]) { |
| 108 | + visited[neighbor] = true; |
| 109 | + queue.push(neighbor); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +let g = new Graph(); |
| 118 | +// g.addVertex("Tokyo"); |
| 119 | +// g.addVertex("Dallas"); |
| 120 | +// g.addEdge("Tokyo", "Dallas"); |
| 121 | +// g.removeEdge("Tokyo", "Dallas"); |
| 122 | +// g.remoVertex("Tokyo"); |
| 123 | + |
| 124 | +g.addVertex("A"); |
| 125 | +g.addVertex("B"); |
| 126 | +g.addVertex("C"); |
| 127 | +g.addVertex("D"); |
| 128 | +g.addVertex("E"); |
| 129 | +g.addVertex("F"); |
| 130 | + |
| 131 | +g.addEdge("A", "B"); |
| 132 | +g.addEdge("A", "C"); |
| 133 | +g.addEdge("B", "D"); |
| 134 | +g.addEdge("C", "E"); |
| 135 | +g.addEdge("D", "E"); |
| 136 | +g.addEdge("D", "F"); |
| 137 | +g.addEdge("E", "F"); |
| 138 | + |
| 139 | +// depthFirstRecursive |
| 140 | +// A |
| 141 | +// / \ |
| 142 | +// B C |
| 143 | +// | | |
| 144 | +// D --- E |
| 145 | +// \ / |
| 146 | +// F |
| 147 | + |
| 148 | +console.log(g.depthFirstRecursive("A")); |
| 149 | +console.log(g.breadthFirst("A")); |
0 commit comments