Skip to content

Commit 710701d

Browse files
dijkstra algo commit
1 parent de97da7 commit 710701d

File tree

3 files changed

+293
-0
lines changed

3 files changed

+293
-0
lines changed

11 dijkstra_version1.js.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
class PriorityQueue {
2+
constructor(){
3+
this.values = [];
4+
}
5+
enqueue(val, priority) {
6+
this.values.push({val, priority});
7+
this.sort();
8+
};
9+
dequeue() {
10+
return this.values.shift();
11+
};
12+
sort() {
13+
this.values.sort((a, b) => a.priority - b.priority);
14+
};
15+
}
16+
17+
class WeightedGraph {
18+
constructor() {
19+
this.adjacencyList = {};
20+
}
21+
addVertex(vertex){
22+
if(!this.adjacencyList[vertex]) this.adjacencyList[vertex] = [];
23+
}
24+
addEdge(vertex1,vertex2, weight){
25+
this.adjacencyList[vertex1].push({node:vertex2,weight});
26+
this.adjacencyList[vertex2].push({node:vertex1, weight});
27+
}
28+
Dijkstra(start, finish){
29+
const nodes = new PriorityQueue();
30+
const distances = {};
31+
const previous = {};
32+
let path = [] //to return at end
33+
let smallest;
34+
//build up initial state
35+
for(let vertex in this.adjacencyList){
36+
if(vertex === start){
37+
distances[vertex] = 0;
38+
nodes.enqueue(vertex, 0);
39+
} else {
40+
distances[vertex] = Infinity;
41+
nodes.enqueue(vertex, Infinity);
42+
}
43+
previous[vertex] = null;
44+
}
45+
// as long as there is something to visit
46+
while(nodes.values.length){
47+
smallest = nodes.dequeue().val;
48+
if(smallest === finish){
49+
//WE ARE DONE
50+
//BUILD UP PATH TO RETURN AT END
51+
while(previous[smallest]){
52+
path.push(smallest);
53+
smallest = previous[smallest];
54+
}
55+
break;
56+
}
57+
if(smallest || distances[smallest] !== Infinity){
58+
for(let neighbor in this.adjacencyList[smallest]){
59+
//find neighboring node
60+
let nextNode = this.adjacencyList[smallest][neighbor];
61+
//calculate new distance to neighboring node
62+
let candidate = distances[smallest] + nextNode.weight;
63+
let nextNeighbor = nextNode.node;
64+
if(candidate < distances[nextNeighbor]){
65+
//updating new smallest distance to neighbor
66+
distances[nextNeighbor] = candidate;
67+
//updating previous - How we got to neighbor
68+
previous[nextNeighbor] = smallest;
69+
//enqueue in priority queue with new priority
70+
nodes.enqueue(nextNeighbor, candidate);
71+
}
72+
}
73+
}
74+
}
75+
return path.concat(smallest).reverse();
76+
}
77+
}
78+
79+
var graph = new WeightedGraph()
80+
graph.addVertex("A");
81+
graph.addVertex("B");
82+
graph.addVertex("C");
83+
graph.addVertex("D");
84+
graph.addVertex("E");
85+
graph.addVertex("F");
86+
87+
graph.addEdge("A","B", 4);
88+
graph.addEdge("A","C", 2);
89+
graph.addEdge("B","E", 3);
90+
graph.addEdge("C","D", 2);
91+
graph.addEdge("C","F", 4);
92+
graph.addEdge("D","E", 3);
93+
graph.addEdge("D","F", 1);
94+
graph.addEdge("E","F", 1);
95+
96+
97+
graph.Dijkstra("A", "E");
98+
99+
// ["A", "C", "D", "F", "E"]

12 dijkstras_version2.js.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
class WeightedGraph {
2+
constructor() {
3+
this.adjacencyList = {};
4+
}
5+
addVertex(vertex){
6+
if(!this.adjacencyList[vertex]) this.adjacencyList[vertex] = [];
7+
}
8+
addEdge(vertex1,vertex2, weight){
9+
this.adjacencyList[vertex1].push({node:vertex2,weight});
10+
this.adjacencyList[vertex2].push({node:vertex1, weight});
11+
}
12+
Dijkstra(start, finish){
13+
const nodes = new PriorityQueue();
14+
const distances = {};
15+
const previous = {};
16+
let path = [] //to return at end
17+
let smallest;
18+
//build up initial state
19+
for(let vertex in this.adjacencyList){
20+
if(vertex === start){
21+
distances[vertex] = 0;
22+
nodes.enqueue(vertex, 0);
23+
} else {
24+
distances[vertex] = Infinity;
25+
nodes.enqueue(vertex, Infinity);
26+
}
27+
previous[vertex] = null;
28+
}
29+
// as long as there is something to visit
30+
while(nodes.values.length){
31+
smallest = nodes.dequeue().val;
32+
if(smallest === finish){
33+
//WE ARE DONE
34+
//BUILD UP PATH TO RETURN AT END
35+
while(previous[smallest]){
36+
path.push(smallest);
37+
smallest = previous[smallest];
38+
}
39+
break;
40+
}
41+
if(smallest || distances[smallest] !== Infinity){
42+
for(let neighbor in this.adjacencyList[smallest]){
43+
//find neighboring node
44+
let nextNode = this.adjacencyList[smallest][neighbor];
45+
//calculate new distance to neighboring node
46+
let candidate = distances[smallest] + nextNode.weight;
47+
let nextNeighbor = nextNode.node;
48+
if(candidate < distances[nextNeighbor]){
49+
//updating new smallest distance to neighbor
50+
distances[nextNeighbor] = candidate;
51+
//updating previous - How we got to neighbor
52+
previous[nextNeighbor] = smallest;
53+
//enqueue in priority queue with new priority
54+
nodes.enqueue(nextNeighbor, candidate);
55+
}
56+
}
57+
}
58+
}
59+
return path.concat(smallest).reverse();
60+
}
61+
}
62+
63+
class PriorityQueue {
64+
constructor(){
65+
this.values = [];
66+
}
67+
enqueue(val, priority){
68+
let newNode = new Node(val, priority);
69+
this.values.push(newNode);
70+
this.bubbleUp();
71+
}
72+
bubbleUp(){
73+
let idx = this.values.length - 1;
74+
const element = this.values[idx];
75+
while(idx > 0){
76+
let parentIdx = Math.floor((idx - 1)/2);
77+
let parent = this.values[parentIdx];
78+
if(element.priority >= parent.priority) break;
79+
this.values[parentIdx] = element;
80+
this.values[idx] = parent;
81+
idx = parentIdx;
82+
}
83+
}
84+
dequeue(){
85+
const min = this.values[0];
86+
const end = this.values.pop();
87+
if(this.values.length > 0){
88+
this.values[0] = end;
89+
this.sinkDown();
90+
}
91+
return min;
92+
}
93+
sinkDown(){
94+
let idx = 0;
95+
const length = this.values.length;
96+
const element = this.values[0];
97+
while(true){
98+
let leftChildIdx = 2 * idx + 1;
99+
let rightChildIdx = 2 * idx + 2;
100+
let leftChild,rightChild;
101+
let swap = null;
102+
103+
if(leftChildIdx < length){
104+
leftChild = this.values[leftChildIdx];
105+
if(leftChild.priority < element.priority) {
106+
swap = leftChildIdx;
107+
}
108+
}
109+
if(rightChildIdx < length){
110+
rightChild = this.values[rightChildIdx];
111+
if(
112+
(swap === null && rightChild.priority < element.priority) ||
113+
(swap !== null && rightChild.priority < leftChild.priority)
114+
) {
115+
swap = rightChildIdx;
116+
}
117+
}
118+
if(swap === null) break;
119+
this.values[idx] = this.values[swap];
120+
this.values[swap] = element;
121+
idx = swap;
122+
}
123+
}
124+
}
125+
126+
class Node {
127+
constructor(val, priority){
128+
this.val = val;
129+
this.priority = priority;
130+
}
131+
}
132+
133+
var graph = new WeightedGraph()
134+
graph.addVertex("A");
135+
graph.addVertex("B");
136+
graph.addVertex("C");
137+
graph.addVertex("D");
138+
graph.addVertex("E");
139+
graph.addVertex("F");
140+
141+
graph.addEdge("A","B", 4);
142+
graph.addEdge("A","C", 2);
143+
graph.addEdge("B","E", 3);
144+
graph.addEdge("C","D", 2);
145+
graph.addEdge("C","F", 4);
146+
graph.addEdge("D","E", 3);
147+
graph.addEdge("D","F", 1);
148+
graph.addEdge("E","F", 1);
149+
150+
151+
graph.Dijkstra("A", "E");
152+
153+
154+
155+

algorithms/12 radix_sort.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function getDigit(num, i) {
2+
return Math.floor(Math.abs(num) / Math.pow(10, i)) % 10;
3+
}
4+
5+
function digitCount(num) {
6+
if (num === 0) return 1;
7+
return Math.floor(Math.log10(Math.abs(num))) + 1;
8+
}
9+
10+
function mostDigits(nums) {
11+
let maxDigits = 0;
12+
for (let i = 0; i < nums.length; i++) {
13+
maxDigits = Math.max(maxDigits, digitCount(nums[i]));
14+
}
15+
return maxDigits;
16+
}
17+
18+
function radixSort(nums){
19+
let maxDigitCount = mostDigits(nums);
20+
for(let k = 0; k < maxDigitCount; k++){
21+
let digitBuckets = Array.from({length: 10}, () => []);
22+
for(let i = 0; i < nums.length; i++){
23+
let digit = getDigit(nums[i],k);
24+
digitBuckets[digit].push(nums[i]);
25+
}
26+
nums = [].concat(...digitBuckets);
27+
}
28+
return nums;
29+
}
30+
31+
radixSort([23,345,5467,12,2345,9852])
32+
33+
34+
35+
36+
37+
38+
39+

0 commit comments

Comments
 (0)