The graphs use two algorithms to either find the shortest path (Dijkstra algorithm) or the minimal spanning tree (Krushal’s method).

I am going to split up the assignment into 2 posts so that can try and covert as much of the assignment without spanning a massive post, so in this post going to talk about loading in the data from the textual files and also displaying the data on screen (graphics), and how I altered the sublcass to allow for more data to be stored for this assignment. In the next post it will be about the Path class implementation with the Dijkstra and Krushal algorithms.

So to start off with, I altered the type definition for the main/parent class of Graph to be called PathfinderGraphMain so that when I created the subclass I could call it the PathfinderGraph, just liked the look of that :). and since the graph types, nodeT / arcT are capable of holding all of the other data (have included them in the code below) I just need to store the graph image which is what the subclass below is doing for me, was trying to avoid using any global variables, so that is all that my subclass is additional storing and accessing using the get/set functions.

typedef Graph<nodeT, arcT> PathfinderGraphMain;
class PathfinderGraph : public PathfinderGraphMain
	string returnJpg() { return jpgimage;}
	void setJpg(string jpg) { jpgimage = jpg;}
	string jpgimage;
// as taken from the graphtypes.h
struct nodeT {
	string name;
	Set<arcT *> arcs;
	pointT loc;
struct arcT {
	nodeT *start, *finish;
	double cost;

here is the sub part of the method that will read in the data from the textual file, I am basically reading in the nodes to start with and then calling the AddNode function (below the code) and also the AddArc (again further down in the code below) which basically just call the main Graph class addNode / addArc functions.

if (contents=="NODES")
		// end at EOF or ARCS
		while (contents != "ARCS" && !inFile.eof())
			contents = ReadLineFromFile(inFile);
			// add the node into 
			if (contents != "ARCS")
				// need to split the contents string
				pointT location;
				int nameI = contents.find_first_of(" ");
				location.x = StringToInteger(contents.substr(nameI, contents.find_last_of(" ")- nameI));
				location.y = StringToInteger(contents.substr(contents.find_last_of(" ")));
				AddNode(contents.substr(0,nameI), location, thePath);
	// end point is the word ARCS
	if (contents == "ARCS")
		// read till the end
		while (!inFile.eof())
			contents = ReadLineFromFile(inFile);
			if (!inFile.eof())
				string startP, endP;
				int nameI = contents.find_first_of(" ");
				startP = contents.substr(0, nameI);
				endP = contents.substr(nameI, contents.find_last_of(" ")-nameI);
				double cost = StringToReal(contents.substr(contents.find_last_of(" ")));
				AddArc(startP, endP, cost, thePath);
void AddNode(string name, pointT location, PathfinderGraph &thePath)
	nodeT *node = new nodeT();
	node->name = name;
	node->loc = location;
void AddArc(string startP, string endP, double cost, PathfinderGraph &thePath)
	nodeT *start = thePath.getNode(startP);
	nodeT *end = thePath.getNode(endP);
	arcT *arc = new arcT();
	arc->cost = cost;
	arc->start = start;
	arc->finish = end;
	arcT *arc2 = new arcT();
	arc2->cost = cost;
	arc2->start = end;
	arc2->finish = start;

To draw to the screen, I have created two methods that will take a set of nodes (nodeT structures) and also the colour that is required to display. Since the Set class has a Iterator I can use the foreach (marco from the CS106B libraries) to loop over the Set class to then just display the nodes/arcs via the CS106B graphics libraries again.

void DrawNodes(Set<nodeT *> nodes, string color)
	foreach (nodeT *node in nodes)
		DrawPathfinderNode(node->loc, color, node->name);
		DrawArcs(node->arcs, color);
void DrawArcs(Set<arcT *> arcs, string color)
	foreach (arcT *arc in arcs)
		DrawPathfinderArc(arc->start->loc, arc->finish->loc, color);

The last part of the graphical aspects is getting the mouse inputs to link to a node on the graphical display, once again using the CS106B libraries for obtaining a point location (x/y coordinates) and then testing to see if this coordinates have clicked on the node location (kinder putting a square around the node of the node radius in size, this NODE_RADIUS is defined in the CS106B libraries and that is what the node will have a circle radius off)

// find the nodes near the mouse cords
// NULL if not found a node, else return a nodeT
nodeT * GetNodeFromMouseCords(PathfinderGraph &thePath, pointT mouseInput)
	nodeT * returnNode;
	foreach (nodeT *node in thePath.getNodeSet())
		pointT location = node->loc;
		if (mouseInput.x >(location.x - NODE_RADIUS) && 
			mouseInput.x <(location.x + NODE_RADIUS))
			if (mouseInput.y > (location.y - NODE_RADIUS) &&
				mouseInput.y < (location.y + NODE_RADIUS))
				return node;
	return NULL;
// need to pull out two places, start and end.
void GetTwoLocationsFromMouse(PathfinderGraph &thePath, nodeT * &startN, nodeT * &finishN)
	pointT mouseClick = pointT();
	cout << "Please choice your starting location "<< endl;
	while ((startN = GetNodeFromMouseCords(thePath, mouseClick)) == NULL)
		mouseClick = GetMouseClick();
	cout << "Start : " << startN->name << endl;
	DrawPathfinderNode(startN->loc, HIGHLIGHT_COLOR, startN->name);
	while (true)
		cout << "Please choice your ending location "<< endl;
		mouseClick = pointT();
		while ((finishN = GetNodeFromMouseCords(thePath, mouseClick)) == NULL)
			mouseClick = GetMouseClick();
		if (startN->name == finishN->name)
			cout << "That is silly, you are already there!"<< endl;
	cout << "Finish : " << finishN->name << endl;
	DrawPathfinderNode(finishN->loc, HIGHLIGHT_COLOR, finishN->name);

after the user has selected two nodes it will highlight the nodes with the HIGHLIGHT_COLOR from the CS106B libraries again (which was the colour red). I shall post next on the Path class and also the two breath-search and minimal spanning trees, (Dijkstra and Krushal’s algorithms).

Leave a Reply

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