diff --git a/README.md b/README.md index 28161b5d..fef45ff8 100644 --- a/README.md +++ b/README.md @@ -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) | | | | diff --git a/prims/prims.js b/prims/prims.js new file mode 100644 index 00000000..728b88f0 --- /dev/null +++ b/prims/prims.js @@ -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();