Question
This code works, however, I would like to read in the Edges from a data file instead of hard coding them like I have done
This code works, however, I would like to read in the Edges from a data file instead of hard coding them like I have done below. How can I make that happen? The language is C++.
#include
#include
#define INT_MAX 10000000
using namespace std;
void DijkstrasTest();
int main(){
DijkstrasTest(); //Call DijkstrasTest function
return 0;
}
class Node;
class Edge;
void Dijkstras();
vector
Node* ExtractSmallest(vector
int Distance(Node* node1, Node* node2);
bool Contains(vector
void PrintShortestRouteTo(Node* destination);
vector
vector
class Node{ //Create a class Node where values are initialized to be filled later
public:
Node(char id)
: id(id), previous(NULL), distanceFromStart(INT_MAX){
nodes.push_back(this);
}
public:
char id;
Node* previous;
int distanceFromStart;
};
class Edge{
public:
Edge(Node* node1, Node* node2, int distance) //Define an edge class
: node1(node1), node2(node2), distance(distance){
edges.push_back(this);
}
bool Connects(Node* node1, Node* node2){ //Define a bool class Connects for Nodes
return (
(node1 == this->node1 &&
node2 == this->node2) ||
(node1 == this->node2 &&
node2 == this->node1));
}
public:
Node * node1;
Node* node2;
int distance;
};
void DijkstrasTest(){ //Create all Nodes and Edges required to complete Dijkstras Algorithm on graph.txt
Node* a = new Node('a');
Node* b = new Node('b');
Node* c = new Node('c');
Node* d = new Node('d');
Node* f = new Node('f');
Node* g = new Node('g');
Node* h = new Node('h');
Node* j = new Node('j');
Node* k = new Node('k');
Node* l = new Node('l');
Node* m = new Node('m');
Node* n = new Node('n');
Node* p = new Node('p');
Node* q = new Node('q');
Node* r = new Node('r');
Node* s = new Node('s');
Node* t = new Node('t');
Node* v = new Node('v');
Node* w = new Node('w');
Node* x = new Node('x');
Node* z = new Node('z');
Edge* e1 = new Edge(a, j, 91);
Edge* e2 = new Edge(a, h, 86);
Edge* e3 = new Edge(a, b, 38);
Edge* e4 = new Edge(a, n, 37);
Edge* e5 = new Edge(a, l, 100);
Edge* e6 = new Edge(a, v, 64);
Edge* e7 = new Edge(a, s, 84);
Edge* e8 = new Edge(a, m, 9);
Edge* e9 = new Edge(a, w, 54);
Edge* e10 = new Edge(a, k, 46);
Edge* e11 = new Edge(b, j, 27);
Edge* e12 = new Edge(b, v, 93);
Edge* e13 = new Edge(b, h, 40);
Edge* e14 = new Edge(b, g, 49);
Edge* e15 = new Edge(b, f, 94);
Edge* e16 = new Edge(b, n, 95);
Edge* e17 = new Edge(b, c, 45);
Edge* e18 = new Edge(b, z, 42);
Edge* e19 = new Edge(b, l, 15);
Edge* e20 = new Edge(c, q, 14);
Edge* e21 = new Edge(c, w, 10);
Edge* e22 = new Edge(c, l, 87);
Edge* e23 = new Edge(c, r, 77);
Edge* e24 = new Edge(c, n, 92);
Edge* e25 = new Edge(c, s, 79);
Edge* e26 = new Edge(c, t, 3);
Edge* e27 = new Edge(c, d, 60);
Edge* e28 = new Edge(c, j, 43);
Edge* e29 = new Edge(d, p, 26);
Edge* e30 = new Edge(d, w, 12);
Edge* e31 = new Edge(d, k, 21);
Edge* e32 = new Edge(d, q, 5);
Edge* e33 = new Edge(d, v, 75);
Edge* e34 = new Edge(d, t, 59);
Edge* e35 = new Edge(d, j, 2);
Edge* e36 = new Edge(d, r, 11);
Edge* e37 = new Edge(f, j, 63);
Edge* e38 = new Edge(f, x, 72);
Edge* e39 = new Edge(f, l, 88);
Edge* e40 = new Edge(f, g, 51);
Edge* e41 = new Edge(f, v, 61);
Edge* e42 = new Edge(f, p, 39);
Edge* e43 = new Edge(f, m, 69);
Edge* e44 = new Edge(f, w, 65);
Edge* e45 = new Edge(g, n, 76);
Edge* e46 = new Edge(g, x, 13);
Edge* e47 = new Edge(g, z, 56);
Edge* e48 = new Edge(g, h, 82);
Edge* e49 = new Edge(g, q, 32);
Edge* e50 = new Edge(g, w, 20);
Edge* e51 = new Edge(g, v, 18);
Edge* e52 = new Edge(h, z, 74);
Edge* e53 = new Edge(h, p, 85);
Edge* e54 = new Edge(h, l, 62);
Edge* e55 = new Edge(h, m, 44);
Edge* e56 = new Edge(h, k, 25);
Edge* e57 = new Edge(h, j, 98);
Edge* e58 = new Edge(h, r, 19);
Edge* e59 = new Edge(j, v, 31);
Edge* e60 = new Edge(j, m, 50);
Edge* e61 = new Edge(j, s, 57);
Edge* e62 = new Edge(j, q, 17);
Edge* e63 = new Edge(j, t, 22);
Edge* e64 = new Edge(j, p, 68);
Edge* e65 = new Edge(k, t, 67);
Edge* e66 = new Edge(k, l, 81);
Edge* e67 = new Edge(k, m, 55);
Edge* e68 = new Edge(k, p, 52);
Edge* e69 = new Edge(k, s, 83);
Edge* e70 = new Edge(k, x, 41);
Edge* e71 = new Edge(l, r, 36);
Edge* e72 = new Edge(l, n, 30);
Edge* e73 = new Edge(l, t, 53);
Edge* e74 = new Edge(l, s, 23);
Edge* e75 = new Edge(l, q, 66);
Edge* e76 = new Edge(m, s, 6);
Edge* e77 = new Edge(m, q, 29);
Edge* e78 = new Edge(m, n, 71);
Edge* e79 = new Edge(m, r, 8);
Edge* e80 = new Edge(m, t, 96);
Edge* e81 = new Edge(n, t, 16);
Edge* e82 = new Edge(n, r, 78);
Edge* e83 = new Edge(n, q, 73);
Edge* e84 = new Edge(n, v, 24);
Edge* e85 = new Edge(p, z, 28);
Edge* e86 = new Edge(p, w, 48);
Edge* e87 = new Edge(p, q, 7);
Edge* e88 = new Edge(p, t, 47);
Edge* e89 = new Edge(q, w, 99);
Edge* e90 = new Edge(q, s, 1);
Edge* e91 = new Edge(q, z, 89);
Edge* e92 = new Edge(r, v, 70);
Edge* e93 = new Edge(r, x, 35);
Edge* e94 = new Edge(r, s, 33);
Edge* e95 = new Edge(s, t, 34);
Edge* e96 = new Edge(s, x, 90);
Edge* e97 = new Edge(t, x, 4);
Edge* e98 = new Edge(t, w, 58);
Edge* e99 = new Edge(v, x, 80);
Edge* e100 = new Edge(w, z, 97);
m->distanceFromStart = 0; // set start node
Dijkstras();
//calculate the shortest tree
PrintShortestRouteTo(a);
PrintShortestRouteTo(b);
PrintShortestRouteTo(c);
PrintShortestRouteTo(d);
PrintShortestRouteTo(f);
PrintShortestRouteTo(g);
PrintShortestRouteTo(h);
PrintShortestRouteTo(j);
PrintShortestRouteTo(k);
PrintShortestRouteTo(l);
PrintShortestRouteTo(m);
PrintShortestRouteTo(n);
PrintShortestRouteTo(p);
PrintShortestRouteTo(q);
PrintShortestRouteTo(r);
PrintShortestRouteTo(s);
PrintShortestRouteTo(t);
PrintShortestRouteTo(v);
PrintShortestRouteTo(w);
PrintShortestRouteTo(x);
PrintShortestRouteTo(z);
}
void Dijkstras(){
while (nodes.size() > 0){ //Calculate the size based on adjacent Nodes
Node* smallest = ExtractSmallest(nodes);
vector
AdjacentRemainingNodes(smallest);
const int size = adjacentNodes->size();
for (int i = 0; i Node* adjacent = adjacentNodes->at(i); int distance = Distance(smallest, adjacent) + smallest->distanceFromStart; if (distance < adjacent->distanceFromStart){ adjacent->distanceFromStart = distance; adjacent->previous = smallest; } } delete adjacentNodes; } } Node* ExtractSmallest(vector int size = nodes.size(); if (size == 0) return NULL; int smallestPosition = 0; Node* smallest = nodes.at(0); for (int i = 1; i Node* current = nodes.at(i); if (current->distanceFromStart < smallest->distanceFromStart){ smallest = current; smallestPosition = i; } } nodes.erase(nodes.begin() + smallestPosition); return smallest; } vector vector const int size = edges.size(); for (int i = 0; i Edge* edge = edges.at(i); Node* adjacent = NULL; if (edge->node1 == node){ adjacent = edge->node2; } else if (edge->node2 == node){ adjacent = edge->node1; } if (adjacent && Contains(nodes, adjacent)){ adjacentNodes->push_back(adjacent); } } return adjacentNodes; } int Distance(Node* node1, Node* node2){ //Return distance between two Nodes const int size = edges.size(); for (int i = 0; i Edge* edge = edges.at(i); if (edge->Connects(node1, node2)){ return edge->distance; } } return -1; } bool Contains(vector const int size = nodes.size(); for (int i = 0; i if (node == nodes.at(i)){ return true; } } return false; } void PrintShortestRouteTo(Node* destination){ //Print the shortest route to the destination Node vector Node* previous = destination; cout << "Distance from start, the shortest distance is: " << destination->distanceFromStart << endl; cout << "shortest path from m is: "; while (previous){ //cout << previous->id << "-"; path.push_back(previous->id); previous = previous->previous; } for (int a = path.size() - 1; a >= 0 ; a--){ cout << path[a] << "-"; } cout << '\b'; cout << ' '; cout << endl; cout << "================================================================================"; cout << endl; } vector vector const int size = edges.size(); for (int i = 0; i Edge* edge = edges.at(i); if (edge->node1 == node){ cout << "adjacent: " << edge->node2->id << endl; adjacentEdges->push_back(edge); } else if (edge->node2 == node){ cout << "adjacent: " << edge->node1->id << endl; adjacentEdges->push_back(edge); } } return adjacentEdges; } void RemoveEdge(vector vector for (it = edges.begin(); it if (*it == edge){ edges.erase(it); return; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started