CPSC 212
(Data Structures and Algorithms)
Assignment 7
- Objectives:
- Implement Prim's MST algorithm.
- Due date:
- 10/31/06
- What to hand in:
- Code "tarball" (e.g.,
asg7.tar.gz) including:
README file containing assignment and solution
descriptions, e.g., program design, description of algorithm,
etc., if appropriate.
INSTALL file containing any specific compliation
quirks peculiar to your program, e.g.,
Compilation: make -f Makefile.smirp.
USAGE file containing any specific usage quirks
peculiar to your program, e.g.,
Usage: ./smirp < smirp.in where
./smirp is the executable and
smirp.in is the input.
src/ directory with source code.
- Description:
- Implement Prim's MST algorithm in 2D:
- Read in a list of n 2D points
(or 3D points each with its z-coordinate
set to 0.0), e.g.,
-
vector<Point* > pts;
Point* ptp;
while(std::cin >> (ptp = new Point(0.0,0.0)))
pts.push_back(ptp);
- Resize your graph so that it can hold n nodes, e.g.,
-
graph.resize((int)pts.size());
- Add all verteces to the graph, making sure that for
each vertex sufficient memory is allocated to
hold n-1 adjacent neighbors (for a
complete graph), e.g.,
-
for(int i=0; i<(int)pts.size(); ++i)
graph.addVertex(i,pts[i],(int)pts.size()-1);
- Construct a complete graph, e.g.,
-
for(int i=0; i<(int)pts.size(); ++i)
for(int j=0; j<(int)pts.size(); ++j)
if(i!=j) graph.addEdge(i,j);
- Execute Prim's algorithm using the first node as
the MST root, e.g.,
-
graph.prims(0);
- Retrieve MST edges from graph (there should be twice
as many points as there are MST edges),
e.g.,
-
vector<Point* > mst_pts;
graph.edges(&mst_pts);
- Print out the edges, e.g.,
-
for(int j=0; j<(int)mst_pts.size(); ++j)
std::cout << mst_pts[j];
- Sample input and output can be found here:
smirp.in,
smirp.out.