Code covered by the BSD License  

Highlights from
Prims Algorithm

4.11111

4.1 | 9 ratings Rate this file 35 Downloads (last 30 days) File Size: 1.59 KB File ID: #8569

Prims Algorithm

by Vikramaditya Kundur

 

27 Sep 2005 (Updated 30 Sep 2005)

Minimal spanning tree.

| Watch this File

File Information
Description

In the mathematical field of graph theory, a spanning tree of a connected, undirected graph is a tree which includes every vertex of that graph. More generally, a spanning forest of an arbitrary undirected graph is a forest which includes every vertex of the graph. Spanning forests always exist, and can always be constructed so as to have exactly one tree for each connected component. In certain fields of graph theory, involving weighted graphs, it is often useful to find a minimal spanning tree.

Prim's algorithm builds a tree while having the graph connected at all times.

Prim's algorithm maintains two lists, EV which is the vertices already in the tree, and E, the list of edges that makes up the spanning tree. In determining current edges for the tree, we look for a node that's in EV, and on that isn't, such that its path is minimum.
EV = { 0 }

E = { }

while( E has < n-1 edges ) {

  find (u,v) with least cost, such that
u is in EV and v isn't in EV

  if no such edge exists, break

  add v to EV

  add (u,v) to E
}
On termination, if the graph is connected, EV will contain all the nodes in the graph, and E will contain the set edges comprising the minimum spanning tree.

MATLAB release MATLAB 6.1 (R12.1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
12 Oct 2005 Veronika Schöpf  
09 Nov 2005 Nick Cheilakos  
27 Feb 2006 Zack Voulgaris

A bit amateurish. Otherwise, okay.

06 Dec 2006 xueli an  
07 Dec 2006 John Smith

Easy to use.

16 Jul 2007 Nicholas Thompson

Interesting read. I've implemented basically this algorithm in Dark Basic Pro. This code snippet creates a random set of "nodes" (or cities in this case) with non-overlapping eges (Connections in this case)...

<a href="http://codebase.dbp-site.com/code/prims-algorithm-grid-map-generator-24">Prims Algorithm in Dark Basic Pro</a>

26 Nov 2007 Fabrício Oliveira

I haven't seen the code yet, but looks like that will only get the optimum if you allow the algorithm gets also the edges where neither u or v are in EV...

29 Apr 2008 Ricardo Leal  
02 Jun 2011 HARI KRISHNAN  
Please login to add a comment or rating.
Updates
30 Sep 2005

Found an error. Need to update with a new file.

Tag Activity for this File
Tag Applied By Date/Time
specialized Vikramaditya Kundur 22 Oct 2008 08:00:51
plotting Vikramaditya Kundur 22 Oct 2008 08:00:51
prims Vikramaditya Kundur 22 Oct 2008 08:00:51
spanning Vikramaditya Kundur 22 Oct 2008 08:00:51
tree Vikramaditya Kundur 22 Oct 2008 08:00:51
algorithm Vikramaditya Kundur 22 Oct 2008 08:00:51
specialized plot Vikramaditya Kundur 22 Oct 2008 08:00:51
graphics Vikramaditya Kundur 22 Oct 2008 08:00:51
algorithm Ayman Ahmed 26 May 2009 23:16:39
algorithm bkhmym ? 07 Aug 2010 07:06:58
kjh Essabelle Kenn Trilles 19 Aug 2010 05:49:08
454e Essabelle Kenn Trilles 19 Aug 2010 05:49:18

Contact us at files@mathworks.com