From e55aa83c94e9eec6bb351e0604ed5d5297042105 Mon Sep 17 00:00:00 2001 From: TornjV Date: Mon, 23 May 2016 22:48:49 +0200 Subject: [PATCH] MST and Prim's Algorithm --- algorithm/category.json | 6 ++++++ algorithm/mst/prim/desc.json | 14 ++++++++++++++ algorithm/mst/prim/normal/code.js | 32 +++++++++++++++++++++++++++++++ algorithm/mst/prim/normal/data.js | 10 ++++++++++ 4 files changed, 62 insertions(+) create mode 100644 algorithm/mst/prim/desc.json create mode 100644 algorithm/mst/prim/normal/code.js create mode 100644 algorithm/mst/prim/normal/data.js diff --git a/algorithm/category.json b/algorithm/category.json index 528e8276..b2c010a5 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -10,6 +10,12 @@ "topological_sort": "Topological-Sort" } }, + "mst": { + "name": "Minimum Spanning Tree", + "list": { + "prim": "Prim's Algorithm" + } + }, "search": { "name": "Search", "list": { diff --git a/algorithm/mst/prim/desc.json b/algorithm/mst/prim/desc.json new file mode 100644 index 00000000..0f91ca42 --- /dev/null +++ b/algorithm/mst/prim/desc.json @@ -0,0 +1,14 @@ +{ + "Prim's Algorithm": "Greedy algorithm that finds a minimum spanning tree for a weighted undirected graph.", + "Applications": [ + ], + "Complexity": { + "time": "worst O(|V|2)" + }, + "References": [ + "Wikipedia" + ], + "files": { + "normal": "Finds minimum spanning tree of a given graph." + } +} \ No newline at end of file diff --git a/algorithm/mst/prim/normal/code.js b/algorithm/mst/prim/normal/code.js new file mode 100644 index 00000000..62044bbe --- /dev/null +++ b/algorithm/mst/prim/normal/code.js @@ -0,0 +1,32 @@ +function prim(){ + // Finds a tree so that there exists a path between + // every two nodes while keeping the cost minimal + var minD, minI, minJ, sum=0, D=[]; + for(var i = 0; i < G.length; i++) D.push(0); + D[0] = 1; // First node is visited + for(var k = 0; k < G.length-1; k++){ // Searching for k edges + minD = Infinity; + for(i = 0; i< G.length; i++) + if(D[i]) // First node in an edge must be visited + for(var j = 0; j < G.length; j++) + if(!D[j] && G[i][j]){ + tracer._visit(i, j); + // Second node must not be visited and must be connected to first node + if(G[i][j] < minD){ + // Searching for cheapest edge which satisfies requirements + minD = G[i][j]; + minI = i; + minJ = j; + } + tracer._leave(i, j); + } + tracer._visit(minI, minJ); + D[minJ] = 1; // Visit second node and insert it into or tree + sum += G[minI][minJ]; + } + tracer._print("The sum of all edges is: " + sum); +} + +tracer._pace(500); +tracer._print("nodes that belong to minimum spanning tree are: "); +prim(); \ No newline at end of file diff --git a/algorithm/mst/prim/normal/data.js b/algorithm/mst/prim/normal/data.js new file mode 100644 index 00000000..fcd3f951 --- /dev/null +++ b/algorithm/mst/prim/normal/data.js @@ -0,0 +1,10 @@ +var tracer = new WeightedUndirectedGraphTracer(); +/*var G = [ // G[i][j] indicates the weight of the path from the i-th node to the j-th node + [0, 3, 0, 1, 0], + [5, 0, 1, 2, 4], + [1, 0, 0, 2, 0], + [0, 2, 0, 0, 1], + [0, 1, 3, 0, 0] + ];*/ +var G = WeightedUndirectedGraph.random(10, .4, 1, 9); +tracer._setData(G); \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy