-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
149 lines (112 loc) · 3.72 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Graphs
class Graph{
constructor(){
this.adjacencyList = {}
}
addVertex(vertex){
// add the item in adjacency list
if(!this.adjacencyList[vertex]) this.adjacencyList[vertex] = [];
}
addEdge(vertex1, vertex2){
// add the item in vertex1 and vertex2
this.adjacencyList[vertex1].push(vertex2);
this.adjacencyList[vertex2].push(vertex1);
}
removeEdge(){
// remove from the vertex 1 and vertex2
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(item => item !== vertex2);
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter(item => item !== vertex1);
}
removeVertex(vertex){
// Loop through the items
Object.keys(this.adjacencyList).forEach(item => {
this.removeEdge(vertex, item);
})
// Remove the individual vertex
delete this.adjacencyList[vertex];
}
// Depth First Search - Recursive
depthFirstSearchRecursive(start){
// list to store the end result to be returned at the very end
let results = [];
// store the visited nodes
let visited = {};
// helper funcion
const dfs = (vertex) => {
// if vertex isn't found, then return
if(!vertex) return null;
// mark the vertex as visited
visited[vertex] = true;
// add vertex to results list
results.push(vertex);
// Loop through the array and recursively call the function on unvisited node
this.adjacencyList[vertex].forEach(neighhour => {
if(!visited[neighhour]){
dfs(neighhour)
}
})
}
// Invoke the helper function
dfs(start);
// return the results array
return results;
}
// Depth First Search - Iterative
depthFirstSearchIterative(startingNode){
// Stack to help us keep track of vertices
let stack = [startingNode];
// List to store the end result which is to be restored at the end
let results = [];
// object to store the visited vertices
let visited={}
// current vertex variable
let currentVertex;
// mark the starting node as visited
visited[startingNode] = true;
// Loop while the stack isn't empty
while(stack.length){
console.log("stack not empty")
// Pop the next value from the stack
const currentVertex = stack.pop();
// add it to the results list
results.push(currentVertex)
this.adjacencyList[currentVertex].forEach(neighhour => {
// If vertex isn't visited
if(!visited[neighhour]){
// mark it as visited
visited[neighhour] = true;
stack.push(neighhour);
}
})
}
console.log(results)
// return the results array
return results;
}
// Breadth First Search
breadthFirstSearch(startingNode){
// Queue and place the starting vertex in it
const queue = [startingNode]
// Visited Nodes
const visitedNodes = {}
// array to store the visisted nodes
// const
}
}
const graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.addEdge("A","B")
graph.addEdge("A","C")
graph.addEdge("B","D")
graph.addEdge("C","E")
graph.addEdge("D","E")
graph.addEdge("D","F")
graph.addEdge("E","F")
console.log(graph)
// graph.depthFirstSearchRecursive("A")
graph.depthFirstSearchIterative("A")