-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcourse-graph-weight.js
140 lines (130 loc) · 3.74 KB
/
course-graph-weight.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
class PriorityQueue {
constructor() {
this.values = [];
}
enqueue(val, priority) {
this.values.push({ val, priority });
this.sort();
}
dequeue() {
return this.values.shift();
}
sort() {
this.values.sort((a, b) => a.priority - b.priority);
}
}
class WeightedGraph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex) {
if (!this.adjacencyList[vertex]) this.adjacencyList[vertex] = [];
}
addEdge(v1, v2, weight) {
this.adjacencyList[v1].push({ n: v2, w: weight });
this.adjacencyList[v2].push({ n: v1, w: weight });
}
Dijkstra(start, finish) {
const visited = {};
const previous = {};
const startVertex = start;
let currentVertexes = [{ n: startVertex, w: 0 }];
visited[startVertex] = true;
while (currentVertexes.length) {
const vertex = currentVertexes.shift();
visited[vertex.n] = true;
let pushed = [];
for (let i = 0; i < this.adjacencyList[vertex.n].length; i++) {
const item = this.adjacencyList[vertex.n][i];
if (!visited[item.n]) {
pushed.push(item);
}
}
const filterInx = [];
for (let i = 0; i < pushed.length; i++) {
const we = vertex.w + pushed[i].w;
if (
previous[pushed[i].n] === undefined ||
we < previous[pushed[i].n].w
) {
previous[pushed[i].n] = { n: vertex.n, w: we };
} else {
filterInx.push(i);
}
pushed[i] = { n: pushed[i].n, w: we };
}
pushed = pushed.filter((itm, inx) => !filterInx.includes(inx));
for (let i = 0; i < pushed.length; i++) {
currentVertexes.push(pushed[i]);
}
currentVertexes.sort((a, b) => a.w - b.w);
}
return previous;
}
Dijkstra1(start, finish) {
const nodes = new PriorityQueue();
const distances = {};
const previous = {};
let path = []; //to return at end
let smallest;
//build up initial state
for (let vertex in this.adjacencyList) {
if (vertex === start) {
distances[vertex] = 0;
nodes.enqueue(vertex, 0);
} else {
distances[vertex] = Infinity;
nodes.enqueue(vertex, Infinity);
}
previous[vertex] = null;
}
// as long as there is something to visit
while (nodes.values.length) {
smallest = nodes.dequeue().val;
if (smallest === finish) {
//WE ARE DONE
//BUILD UP PATH TO RETURN AT END
while (previous[smallest]) {
path.push(smallest);
smallest = previous[smallest];
}
break;
}
if (smallest || distances[smallest] !== Infinity) {
for (let neighbor in this.adjacencyList[smallest]) {
//find neighboring node
let nextNode = this.adjacencyList[smallest][neighbor];
//calculate new distance to neighboring node
let candidate = distances[smallest] + nextNode.w;
let nextNeighbor = nextNode.n;
if (candidate < distances[nextNeighbor]) {
//updating new smallest distance to neighbor
distances[nextNeighbor] = candidate;
//updating previous - How we got to neighbor
previous[nextNeighbor] = smallest;
//enqueue in priority queue with new priority
nodes.enqueue(nextNeighbor, candidate);
}
}
}
}
return path.concat(smallest).reverse();
}
}
let g = new WeightedGraph();
g.addVertex("A");
g.addVertex("B");
g.addVertex("C");
g.addVertex("D");
g.addVertex("E");
g.addVertex("F");
g.addEdge("A", "B", 4);
g.addEdge("A", "C", 2);
g.addEdge("B", "E", 3);
g.addEdge("C", "D", 2);
g.addEdge("C", "F", 4);
g.addEdge("D", "E", 3);
g.addEdge("D", "F", 1);
g.addEdge("E", "F", 1);
g.Dijkstra("A", "E");
g.Dijkstra1("A", "E");