Skip to content

MST and Prim's Algorithm #35

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

Merged
merged 2 commits into from
May 23, 2016
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
6 changes: 6 additions & 0 deletions algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"topological_sort": "Topological-Sort"
}
},
"mst": {
"name": "Minimum Spanning Tree",
"list": {
"prim": "Prim's Algorithm"
}
},
"search": {
"name": "Search",
"list": {
Expand Down
14 changes: 14 additions & 0 deletions algorithm/mst/prim/desc.json
Original file line number Diff line number Diff line change
@@ -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|<sup>2</sup>)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Prim%27s_algorithm'>Wikipedia</a>"
],
"files": {
"normal": "Finds minimum spanning tree of a given graph."
}
}
32 changes: 32 additions & 0 deletions algorithm/mst/prim/normal/code.js
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 10 additions & 0 deletions algorithm/mst/prim/normal/data.js
Original file line number Diff line number Diff line change
@@ -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);
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