Kruskal’s and Prim’s algorithm on weighted adjacency matrice

This program takes an input WAM, and applies Kruskal’s and Prim’s algorithms to it.

///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <sstream>
///////////////////////////////////////////////////////////////////////////////
struct Edge
{
	int vertOne, vertTwo, weight;
	Edge(int one, int two, int passedWeight)
	{
		this->vertOne = one;
		this->vertTwo = two;
		this->weight = passedWeight;
	}
};

class Vertex
{
	private:
		std::vector<Edge*> edges;
		int position;
	public:
		Vertex(void);
		Vertex(int pos);
		~Vertex();
		Edge* addEdge(int secondVert, int weight);
		std::vector<Edge*> getEdges(void);
		int getPos(void);
		void printEdges(void);
};
///////////////////////////////////////////////////////////////////////////////
int CreateAdjMatrix(std::vector<Vertex*> &theVertices, std::vector<Edge*>
	&theEdges);
void KruskalsAlgorithm(std::vector<Vertex*> theVertices, std::vector<Edge*>
	theEdges);
///////////////////////////////////////////////////////////////////////////////
int main(void)
{
	std::vector<Vertex*> theVertices;
	std::vector<Edge*> theEdges;
	if(CreateAdjMatrix(theVertices, theEdges))
	{
		exit(1);
	}


	std::cout << "Printing adjacency matrix: \n\n\n\n\n\n"; 

        for(auto it : theVertices) 
        {
            it->printEdges();
	}

	std::cout << "\n\n\n\n\n Printing sorted edges \n\n\n\n\n";
	for(auto it : theEdges)
	{
		std::cout << "\n\n\n\nVertex One: " << it->vertOne;
		std::cout << "\nVertex Two: " << it->vertTwo;
		std::cout << "\nWeight: " << it->weight;
	}

	KruskalsAlgorithm(theVertices, theEdges);

	return 0;
}
///////////////////////////////////////////////////////////////////////////////
Vertex::Vertex(void)
{

}
///////////////////////////////////////////////////////////////////////////////
Vertex::Vertex(int pos)
{
	this->position = pos;
}
///////////////////////////////////////////////////////////////////////////////
Vertex::~Vertex()
{
	for(auto it = edges.begin(); it != edges.end(); it++)
	{
		delete (*(it));
	}
}
///////////////////////////////////////////////////////////////////////////////
int Vertex::getPos(void)
{
	return this->position;
}
///////////////////////////////////////////////////////////////////////////////
Edge* Vertex::addEdge(int secondVert, int weight)
{
	Edge *edgeBuff = new Edge(this->getPos(), secondVert, weight);
	this->edges.push_back(edgeBuff);

	return edgeBuff;
}
///////////////////////////////////////////////////////////////////////////////
std::vector<Edge*> Vertex::getEdges(void)
{
	return this->edges;
}
///////////////////////////////////////////////////////////////////////////////
void Vertex::printEdges(void)
{
	for(auto it : this->edges)
	{
		std::cout << it->weight << " ";
	}
	std::cout << std::endl;
	return;
}
///////////////////////////////////////////////////////////////////////////////
int CreateAdjMatrix(std::vector<Vertex*> &theVertices, std::vector<Edge*>
	&theEdges)
{
	std::string inBuff;
	int pos = 0, weight, vertexCount;
	Edge *edgeBuff;
	Vertex *vertBuff = new Vertex;
	theVertices.push_back(vertBuff);

	std::cout << "Enter WAM:\n"; std::getline(std::cin, inBuff); std::istringstream iss(inBuff); // use stringstream to parse while(iss >> weight) // while stringstreaming and casting to int
	{
		pos++;
		edgeBuff = vertBuff->addEdge(pos, weight);
		if(theEdges.size() == 0)
		{
			theEdges.push_back(edgeBuff);
		}
		else
		{
			for(auto it = theEdges.begin(); it != theEdges.end(); it++)
			{
				if((*(it))->weight <= edgeBuff->weight)
				{
					theEdges.insert(it, edgeBuff); // insert into sort
					it = (theEdges.end() - 1); // break the for loop
				}
			}
		}
	}

	vertexCount = pos;
	try
	{
		for(int i = 1; i < vertexCount; i++) { pos = 0; vertBuff = new Vertex(i); // add a new vertex for the row theVertices.push_back(vertBuff); // add it to the vertex vector std::getline(std::cin, inBuff); // parse the input weights std::istringstream iss(inBuff); // use stringstream to parse while(iss >> weight) // while stringstreaming and casting to int
			{
				pos++;
				if(pos > vertexCount) // there are too many vertices
				{
					throw 1;
				}

				edgeBuff = vertBuff->addEdge(pos, weight); // add the edge
				for(auto it = theEdges.begin(); it != theEdges.end(); it++)
				{
					if((*(it))->weight <= edgeBuff->weight)
					{
						theEdges.insert(it, edgeBuff); // insert into sort
						it = (theEdges.end() - 1); // break the for loop
					}
				}
			}
			if(pos != vertexCount) // We know there are too few vertices
			{
				throw 2;
			}
		}
	}
	catch(int n)
	{
		if(n == 2)
		{
			std::cout << "Too few vertices in this row, exiting now\n";
		}

		if(n == 1)
		{
			std::cout << "Too many vertices in this row, exiting now\n";
		}
		return 1;
	}
	return 0;
}
///////////////////////////////////////////////////////////////////////////////
void KruskalsAlgorithm(std::vector<Vertex*> theVertices, 
	std::vector<Edge*> theEdges)
{
	std::vector visitedNode(theVertices.size(), false);

	int i = 0;

	for(auto it = theEdges.end(); it != theEdges.begin(); it--)
	{
		std::cout << "\n\n\n\n" << ++i; 
                if(!visitedNode[(*(it))->vertOne])
		{ 
			visitedNode[(*(it))->vertOne] = true;
		}

		if(!visitedNode[(*(it))->vertTwo])
		{
			 visitedNode[(*(it))->vertTwo] = true;
 		}

 		theEdges.erase(it);
	}	
	return;
}
///////////////////////////////////////////////////////////////////////////////
void PrimsAlgorithm(std::vector<Vertex*> theVertices, theEdges);
{
	Vertex *currentVert;
	bool visitedNodes[theVertices.size()];
	Edge* leastEdge;
	std::vector<Edge*> edgeBuff;
	visitedNodes[0] = true;

	for(int i = 0; i < theVertices.size(); i++) { edgeBuff = theVertices[i]->getEdges();
		leastEdge = edgeBuff[0];
		for(int j = 1; j < theVertices.size(); j++) 
                { 
                        if(edgeBuff[j]->weight < leastEdge->weight)
			{
				if(!visitedNodes[edgeBuff[j]->vertTwo])
				{
					leastEdge = edgeBuff[j];
				}
			}
		}
		visitedNodes[j] = true;
		currentVert = theVertices[j];

	}	
	return;
}

Leave a Reply

Your email address will not be published. Required fields are marked *