Question
C++ I need to figure out how to code the last function in my code. I need to use Dijkstra's algorithm. Here is all of
C++ I need to figure out how to code the last function in my code. I need to use Dijkstra's algorithm. Here is all of my code, but I need the last function and any explination to help understand it would be nice! I am pretty confident the rest of my code works, but any times would greatly be appreciated! Here are the directions for the function.
"Calculates the shortest path between the vertex with startLabel and the vertex with endLabel using Dijkstra's Algorithm. A vector is passed into the method that stores the shortest path between the vertices. The return value is the sum of the edges between the start and end vertices on the shortest path."
My code (Please answer in c++):
#include #include
using namespace std;
struct Edge { string src, dest; unsigned long magnit; };
struct Node { string vertices; vector
class Graph { private:
vector
public: Graph(); ~Graph(); void print(); void addEdge(string start, string end, unsigned long magnitude); void addVertex(string vert); void removeVertex(string label); void removeEdge(string label1, string label2); unsigned long shortestPath(string startLabel, string endLabel, vector
};
Graph::Graph() { }
Graph::~Graph() {
}
void Graph::addEdge(string start, string end, unsigned long magnitude) { int count = 0;
for (auto itr = myList.begin(); itr != myList.end(); itr++) { if (myList[count].vertices == start) { Edge *temp = new Edge(); temp->magnit = magnitude; temp->src = start; temp->dest = end; myList[count].mag.push_back(*temp); break; } count++; } }
void Graph::addVertex(string vert) { Node *temp = new Node(); temp->vertices = vert; myList.push_back(*temp); }
void Graph::removeVertex(string label) {
int count = 0; for (auto itr = myList.begin(); itr != myList.end(); itr++) { if (myList[count].vertices == label) { myList.erase(itr); cout << "Succesfully removed Vertex" << endl; break; } int j = 0;
for (auto itr2 = myList[count].mag.begin(); itr2 != myList[count].mag.end(); itr2++) { if (myList[count].mag[j].dest == label) { myList[count].mag.erase(myList[count].mag.begin() + j); break; } j++; } count++; } }
void Graph::removeEdge(string label1, string label2) { int i = 0; int j = 0; int count = 0;
for (auto itr = myList.begin(); itr != myList.end(); itr++) { int j = 0;
if (myList[i].vertices == label1) { for (auto itr2 = myList[count].mag.begin(); itr2 != myList[count].mag.end(); itr2++) { if (myList[i].mag[j].dest == label2) { myList[i].mag.erase(myList[i].mag.begin() + j); break; } j++; } count++; } } }
unsigned long Graph::shortestPath(string startLabel, string endLabel, vector
string newStart = startLabel; unsigned long shortestDistance = 0; return shortestDistance;; }
void Graph::print() { int count = 0; int i = 0; auto itr = myList.begin();
while (itr != myList.end()) {
int j = 0; for (auto itr2 = myList[i].mag.begin(); itr2 != myList[i].mag.end(); itr2++) {
cout << "Source: " << myList[i].mag[j].src << " Magnitude: " << myList[i].mag[j].magnit << " Destination: " << myList[i].mag[j].dest << endl; j++;
} i++; itr++; } }
int main() { Graph g;
vector
for (const auto label : vertices1) g.addVertex(label); for (const auto &tuple : edges1) g.addEdge(get<0>(tuple), get<1>(tuple), get<2>(tuple)); string temp = "1"; string temp2 = "5"; vector
}
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