-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
125 lines (115 loc) · 2.75 KB
/
Copy pathgraph.js
File metadata and controls
125 lines (115 loc) · 2.75 KB
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
class Graph {
constructor(puzzle) {
this.puzzle = puzzle;
this.solutions = [];
this.numNodes = puzzle.length;
this.adjList = new Map();
}
addNode(v) {
this.adjList.set(v, []);
}
removeNode(v) {
for (var i = 0; i < this.adjList.size; i++) {
this.removeEdge(v, i);
}
this.adjList.delete(v);
}
addEdge(v, w) {
if (this.adjList.get(v).length == 0 || this.adjList.get(v).indexOf(w) == -1) {
this.adjList.get(v).push(w);
}
}
removeEdge(v, w) {
let vArr = this.adjList.get(v);
let wArr = this.adjList.get(w);
let indexw = vArr.indexOf(w);
let indexv = wArr.indexOf(v);
if (indexw > -1) {
vArr.splice(indexw, 1);
wArr.splice(indexv, 1);
}
}
initialize() {
for (let i = 0; i < this.numNodes; i++) {
this.addNode(i);
}
for (let i = 0; i < this.numNodes; i++) {
let r = (i + this.puzzle[i]) % this.numNodes;
let l = (i - this.puzzle[i]) % this.numNodes;
if (l < 0) {
l += this.numNodes;
}
this.addEdge(i, r);
if (r != l) {
this.addEdge(i, l);
}
}
}
printGraph() {
let keys = this.adjList.keys();
for (let i of keys) {
let values = this.adjList.get(i);
let conc = "";
for (let j of values) {
conc += j + " ";
}
console.log(i + " -> " + conc);
}
}
bfs(start) {
let queue = [start];
let visited = [start];
while (queue.length != 0) {
let v = queue.shift();
let current = this.adjList.get(v);
for (let w of current) {
if (visited.indexOf(w) === -1) {
queue.push(w);
visited.push(w);
}
}
}
if (visited.length === this.adjList.size)
return visited;
else
return null;
}
dfs(start) {
let visited = [];
var stack = [];
var s = [];
for (var i = 0; i < this.numNodes; i++) {
visited[i] = false;
}
s = this.dfsHelper(start, visited, stack, s);
return s;
}
dfsHelper(v, visited, stack, s) {
visited[v] = true;
stack.push(v);
if (stack.length == this.numNodes) {
//this.solutions.push(stack.slice());
s.push(stack.slice());
}
let neighbors = this.adjList.get(v);
for (var w of neighbors) {
if (!visited[w]) {
s = this.dfsHelper(w, visited, stack, s);
}
}
if (stack.length != this.numNodes) {}
stack.pop();
visited[v] = false;
return s;
}
solve() {
for (var i = 0; i < this.numNodes; i++) {
let s = this.dfs(i);
if (s.length != 0) {
for (var j of s) {
this.solutions.push(j);
}
}
}
}
}