Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Prims Algorithm [JavaScript] #534

Merged
merged 2 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) | |
| [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | | | |
| [Prime Factor](https://en.wikipedia.org/wiki/Prime_factor) | [:white_check_mark:](prime_factor/prime_factor.c) | | [:white_check_mark:](prime_factor/PrimeFactor.java) | [:white_check_mark:](prime_factor/prime_factor.py) | [:white_check_mark:](prime_factor/prime_factor.go) | [:white_check_mark:](prime_factor/primeFactor.js) | |
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | | | |
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | | [:white_check_mark:](prims/prims.js) | |
| [Quick Select](https://en.wikipedia.org/wiki/Quickselect) | [:white_check_mark:](quick_select/quick_select.c) | | [:white_check_mark:](quick_select/QuickSelect.java) | [:white_check_mark:](quick_select/quick_select.py) | | | |
| [Quicksort](https://en.wikipedia.org/wiki/Quicksort) | [:white_check_mark:](quick_sort/quicksort.c) | | [:white_check_mark:](quick_sort/QuickSort.java) | [:white_check_mark:](quick_sort/quick_sort.py) | [:white_check_mark:](quick_sort/quick_sort.go) | [:white_check_mark:](quick_sort/quickSort.js) | [:white_check_mark:](quick_sort/QuickSort.cs) |
| [Radix Sort](http://www.geeksforgeeks.org/radix-sort/) | | | | [:white_check_mark:](radix_sort/radix_sort.py) | | | |
Expand Down
80 changes: 80 additions & 0 deletions prims/prims.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
function minKey (key, visited) {
let min = Number.MAX_VALUE;
let minIdx = -1;
let length = key.length;

for (let i = 0; i < length; i++) {
if (!visited[i] && key[i] < min) {
min = key[i];
minIdx = i;
}
}

return minIdx;
}

function generate (graph) {
/*
* Get the parent nodes in the MST
* :param graph: array which represents the graph
* :return: returns array of parent nodes
*/
let length = graph.length;

// stores the parent of each vertex
let parent = [];
// key value of each vertex
let key = [];
// flag for included in the MST
let mstSet = [];

// initialize arguments
for (let i = 0; i < length; i++) {
mstSet[i] = false;
key[i] = Number.MAX_VALUE;
}

// starting from the first vertex
// the first vertex is the root, so it doesn't have any parent
key[0] = 0;
parent[0] = -1;

for (let i = 0; i < length - 1; i++) {
// minimum key from given vertices
let u = minKey(key, mstSet);
mstSet[u] = true;

// updating the neighbours key
for (let j = 0; j < length; j++) {
if (graph[u][j] !== 0 && !mstSet[j] && graph[u][j] < key[j]) {
parent[j] = u;
key[j] = graph[u][j];
}
}
}

return parent;
}

function main () {
// given graph
let graph = [
[0, 2, 0, 6, 0],
[2, 0, 3, 8, 5],
[0, 3, 0, 0, 7],
[6, 8, 0, 0, 9],
[0, 5, 7, 9, 0]
];

// get the parent nodes of all the vertices
let parent = generate(graph);
let length = graph.length;

// print the MST
console.log('Edge : Weight');
for (let i = 1; i < length; i++) {
console.log(parent[i] + ' - ' + i + ' : ' + graph[i][parent[i]]);
}
}

main();