Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with Kruskal's Algorithm

Status
Not open for further replies.

MFLO

Programmer
Nov 24, 2002
4
US
Anyone know how to adjust my program? This is what I have so far:

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(&quot;C:output.txt&quot;);
	
	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 << &quot;Breadth First Traversal starting with &quot; << start_vertex << &quot;:&quot;;
	
	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 << &quot;Shortest Paths&quot; << 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 << &quot; &quot;;
		while ( !temp.empty() )
		{
			file << temp.top() << &quot; &quot;;
			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 &quot;BIG&quot; entry represents NO edge between vertices. Numbers smaller than BIG represent actual edges and weights of the edges.

My assignment is to &quot;fully implement Joseph Kruskal's Algorithm for minimum spanning tree.&quot;

&quot;The output of the program should be in a text file (&quot;Output.txt&quot;) and should be the form of an adjacency matrix.&quot;

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.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top