Anyone know how to adjust my program? This is what I have so far:
The code I have sofar implements the following:
- 2 weighted graphs
- An implementation of the breadth-first-traversal on the first graph
- An implementation of Dijkstra's one vertex, shortest paths
Note the constant used in the program:
const BIG = 2000000000;
This lets me to build an adjacency matrix of each graph where the "BIG" entry represents NO edge between vertices. Numbers smaller than BIG represent actual edges and weights of the edges.
My assignment is to "fully implement Joseph Kruskal's Algorithm for minimum spanning tree."
"The output of the program should be in a text file ("Output.txt"
and should be the form of an adjacency matrix."
The code I have is from a previous assignment and I figured that it would be easier to modify that program rather than start from scratch... but I haven't had much luck so far.
Can anyone help me? I'd appreciate it if you could. Thanks.
Code:
#include <iostream>
#include <queue>
#include <stack>
#include <fstream>
using namespace std;
const BIG = 2000000000;
void Breadth_First_Traversal (int g[][7], int start_vertex, ofstream & file );
void Shortest_Paths (int g[][5], int start_vertex, ofstream & file );
int main( void )
{
int graph1[7][7] =
// 0 1 2 3 4 5 6
{ { BIG, 20, BIG, 10, 35, BIG, BIG }, // 0
{ 20, BIG, 30, 50, BIG, BIG, BIG }, // 1
{ BIG, 30, BIG, 40, BIG, BIG, BIG }, // 2
{ 10, 50, 40, BIG, 5, 35, BIG }, // 3
{ 35, BIG, BIG, 5, BIG, 15, 2 }, // 4
{ BIG, BIG, BIG, 35, 35, BIG, 3 }, // 5
{ BIG, BIG, BIG, BIG, BIG, 3, BIG } }; // 6
int graph2[5][5] =
// 0 1 2 3 4
{ { BIG, 5, 6, 7, 2 }, // 0
{ 5, BIG, 4, 2, 9 }, // 1
{ 6, 4, BIG, BIG, 4 }, // 2
{ 7, 2, BIG, BIG, 13 }, // 3
{ 2, 9, 4, 13, BIG } }; // 4
ofstream file("C:output.txt");
Breadth_First_Traversal ( graph1, 4, file );
Shortest_Paths ( graph2, 3, file );
file.close();
return 0;
}
void Breadth_First_Traversal (int g[][7], int start_vertex, ofstream & file )
{
file << "Breadth First Traversal starting with " << start_vertex << ":";
queue<int> temp;
temp.push(start_vertex);
bool used[7];
for ( int i = 0; i < 7; i++ )
used[i] = (i == start_vertex)? true : false;
while ( !temp.empty() )
{
int V = temp.front();
temp.pop();
file << V; // visit V
for ( int j = 0; j < 7; j++ )
// for all unused neighbors of U of V
{
if (g[V][j] < BIG && !used[j])
{
temp.push(j);
used[j] = true;
}
}
}
file << endl;
}
void Shortest_Paths (int g[][5], int start_vertex, ofstream & file )
{
int distance[5];
bool tobechecked[5];
//initialize
for ( int i = 0; i < 5; i++ )
{
distance[i] = BIG;
tobechecked[i] = true;
}
int predecessor[5];
distance[start_vertex] = 0;
predecessor[start_vertex] = -1;
bool done = false;
while ( !done )
{
int V,
smallest = BIG;
for ( int j = 0; j < 5; j++ )
{
if ( distance[j] < smallest && tobechecked[j] )
{
V = j;
smallest = distance[V];
}
}
tobechecked[V] = false;
for ( int W = 0; W < 5; W++ )
{
if (g[W][V] < BIG && tobechecked [W])
{
if (distance[W] > distance[V] + g[V][W])
{
distance[W] = distance[V] + g[V][W];
predecessor[W] = V;
}
}
}
vertices
int k = 0;
while (k < 5 && !tobechecked[k])
k++;
done = (k == 5);
}
file << "Shortest Paths" << endl;
stack<int> temp;
for ( int l = 0; l < 5; l++ )
{
int m = l;
while ( predecessor[m] != -1 )
{
temp.push(m);
m = predecessor[m];
}
file << start_vertex << " ";
while ( !temp.empty() )
{
file << temp.top() << " ";
temp.pop();
}
file << endl;
}
}
The code I have sofar implements the following:
- 2 weighted graphs
- An implementation of the breadth-first-traversal on the first graph
- An implementation of Dijkstra's one vertex, shortest paths
Note the constant used in the program:
const BIG = 2000000000;
This lets me to build an adjacency matrix of each graph where the "BIG" entry represents NO edge between vertices. Numbers smaller than BIG represent actual edges and weights of the edges.
My assignment is to "fully implement Joseph Kruskal's Algorithm for minimum spanning tree."
"The output of the program should be in a text file ("Output.txt"
The code I have is from a previous assignment and I figured that it would be easier to modify that program rather than start from scratch... but I haven't had much luck so far.
Can anyone help me? I'd appreciate it if you could. Thanks.