6.S096 | January IAP 2014 | Undergraduate

# Effective Programming in C and C++

Assignments

## Sample Solution to Assignment 2, Problem 2

``````
/*
PROG: mst
LANG: C++
*/
#include <vector>
#include <queue>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <unordered_map>

class State {
size_t _node;
double _dist;
public:
inline size_t node()const { return _node; }
inline double dist()const { return _dist; }
};

public:
inline size_t size() const { return _adj.size(); }
void print();
};

inline bool operator<( const State &a, const State &b ) {
return a.dist() > b.dist();
}

size_t nNodes; size_t nEdges; input >> nNodes >> nEdges;

for( size_t e = 0; e < nEdges; ++e ) {
size_t v, w; double weight;
input >> v >> w >> weight;
// Add this edge to both the v and w lists
_adj[v].push_back( State{ w, weight } );
_adj[w].push_back( State{ v, weight } );
}
}

for( size_t i = 0; i < _adj.size(); ++i ) {
std::cout << i << ": ";
for( auto state : _adj[i] ) {
std::cout << "(" << state.node() << ", " << state.dist() << ") ";
}
std::cout << "\n";
}
}

std::unordered_map<int, bool> visited;
std::priority_queue<State> pq;

pq.push( State{ 0, 0.0 } );
double weight = 0.0;

while( visited.size() < adj.size() ) {
auto top = pq.top(); pq.pop();

if( visited.count( top.node() ) == 0 ) {
visited[top.node()] = true;
weight += top.dist();

pq.push( vertex );
}
}
}

return weight;
}

int main() {
std::ifstream input{ "mst.in" };
std::ofstream output{ "mst.out" };

if( input.is_open() ) {
output << std::fixed << std::setprecision( 8 );
output << prim( adj ) << "\n";
} else {
std::cerr << "Could not open mst.in\n";
return 1;
}

return 0;
}
``````

#### Below is the output using the test data:

``````
mst:
1: OK [0.004 seconds]
2: OK [0.004 seconds]
3: OK [0.004 seconds]
4: OK [0.006 seconds]
5: OK [0.093 seconds]
6: OK [0.122 seconds]
7: OK [0.227 seconds]
8: OK [0.229 seconds]
9: OK [0.285 seconds]
10: OK [0.287 seconds]
``````