I have an assignment, where I am given a graph and must use a two-dimensional array and find the shortest path from "Macomb" to "Chicago". I am having difficulty figuring out how I should begin.
I have watched a few videos, and I feel that I have a grasp on how Dijkstra's algorithm works, but turning it into code is giving me a tough time.
I've created an adjacency matrix for my graph, where I use "99" to represent non-existant edges, I have an array created for a list of predecessors, and I have an array created for the remaining vertices. I am supposed to output each new addition to the optimal path, and the current cost of each addition to the optimal path. In the end, it should look like this:
Path: Macomb
Cost: 0
Path: Macomb --> Hope
Cost: 2
Path: Macomb --> Hope --> Love
Cost: 4
Path: Macomb --> Hope --> Love --> Peace
Cost: 5
Path: Macomb --> Hope --> Love --> Peace --> Belief
Cost: 6
Path: Macomb --> Hope --> Love --> Peace --> Belief --> Chicago
Cost: 8
and here is the code I have to start with:
#include <iostream>
using namespace std;
int main()
{
int graph[6][6] = {{99,2,9,5,99,99},
{2,99,4,2,99,99},
{9,4,99,1,1,5},
{5,2,1,99,4,99},
{99,99,1,4,99,2},
{99,99,99,99,2,99}};
string pred[6] = {"Macomb", " ", " ", " ", " ", " "};
string cities[5] = {"hope", "peace", "love", "belief", "Chicago"};
int distance[6];
}
I'm not looking for someone to code my homework, but I would appreciate any pushes towards the right direction as this has been a tough assignment. This is for a basic data structures class, and the only #include we've been using is what's in the code, so the less complicated of an answer the better.
Thank you!
Here's my implementation of Dijkstra's algorithm.
https://github.com/Jesusfer2575/Reference/blob/master/dijkstra.cpp
Enjoy it!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.