Question: Hello, I have an error in my C++ program and also I want to see if I am in good path working towards the requeriment.
Hello, I have an error in my C++ program and also I want to see if I am in good path working towards the requeriment.
Requeriments:
Requeriments:
Graphs
Min path
DESCRIPTION
Write a program that will display the minimum path between any two vertices in a given graph. The program will input the graph from a file provided by the user. It will prompt the user for entering the name of the start vertex and the end vertex. It will find the minimum path between the two vertices. It will display the minimum path showing all the vertices along the path. It will also display the total distance along the minimum path.
TESTING
Test the program for two data sets included in this module as text files. For each data set, carry out a number of test runs as listed below.
Data Set 1
Test the program for the data set 1 listed. Enter this data into a file and input the data from the file.
The data consists of two parts. In the first part, each line represents a vertex. It contain two values. The first value specifies the vertex number. The second value specifies the vertex name. The first part ends when a line contains -1 by itself. In the second part, each line represents an edge. It contains three values. The first two values specify two vertices connecting the edge. The third value specifies the weight of the edge. This part ends when a line contains -1 by itself.
0 SF
1 LA
2 CHICAGO
3 NY
4 PARIS
5 LONDON
-1
0 1 80
0 2 200
0 3 300
1 2 230
1 5 700
2 3 180
3 4 630
3 5 500
4 5 140
-1
Data Set 1: Test Run 1
Run the program using SF as the start vertex. Program input/output dialog is shown below. The user input is shown in bold.
Enter Start Vertex: SF
Enter End Vertex :LONDON
Min Path: SF LA LONDON 780
Data Set 1: Test Run 2
Enter Start Vertex: SF
Enter End Vertex : PARIS
Min Path: SF LA LONDON PARIS 920
Data Set 2:
Data set 2 is available in a file attached.
For data set 2, run the program several times: each time use the same vertex (the first vertex input) as the start vertex and a different vertex as the destination vertex till each vertex has been used as the destination vertex.
IMPLEMENTATION
MinPath algorithm finds the minimum path from a specified start vertex to a specified target (destination) vertex in a graph and displays the following.
The list of all the vertices in the minimum path from the start vertex to the target vertex.
The total distance from the start vertex to the target vertex along the above path.
Methodology
MinPath algorithm accomplishes the above by using a variation of BreadthFirst traversal algorithm covered in a previous assignment with the following additional provisions.
It uses a priority queue (PmainQ) instead of a regular queue.
In the queue, it enqueues a vertex as an item describing the vertex. The vertex item contains the following fields:
Total Accumulated Distance: The total distance from the start vertex to this vertex.
Path List: The list of all vertices in the path from the start vertex to this vertex. The list of vertices is stored as an array of ints (vertex numbers).
Path Length: The length of the above array.
In the priority queue (PmainQ), the vertex items are enqueued in ascending order of the Total Accumulated Distance values in the item vertices.
The item vertices are enqueued and dequeued form the PmainQ using a procedure similar to the BreadthFirst algorithm and is presented below.
Code:
#include
#include
#include
#include
#include
#include
using namespace std;
vector
int V = 6;
void addEdge(vector
int v, int wt)
{
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}
// Print adjacency list representaion ot graph
void printGraph(vector
{
int v, w;
for (int u = 0; u < V; u++)
{
cout << u << " - ";
for (auto it = adj[u].begin(); it != adj[u].end(); it++)
{
v = it->first;
w = it->second;
cout << v << " ="
<< w << " ";
}
cout << " ";
}
}
// This function mainly does BFS and prints the
// shortest path from src to dest. It is assumed
// that weight of every edge is 1
int findShortestPath(int src, int dest)
{
// Mark all the vertices as not visited
bool *visited = new bool[2 * V];
int *parent = new int[2 * V];
static int total = 0;
// Initialize parent[] and visited[]
for (int i = 0; i < 2 * V; i++)
{
visited[i] = false;
parent[i] = -1;
}
// Create a queue for BFS
list
// Mark the current node as visited and enqueue it
visited[src] = true;
queue.push_back(src);
while (!queue.empty())
{
// Dequeue a vertex from queue and print it
int s = queue.front();
if (s == dest)
return total;
queue.pop_front();
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
for (auto it = adj[s].begin(); it != adj[s].end(); it++)
{
if (!visited[it->first])
{
visited[it->first] = true;
queue.push_back(it->first);
total += it->second;
parent[it->first] = s;
}
}
}
return total;
}
// Driver code
int main()
{
string line;
ifstream myfile("file.txt");
vector
int count = 1, V;
vector
if (myfile.is_open())
{
while (getline(myfile, line))
{
//cout << line << ' ';
while (line != "-1")
{
char *token = strtok_s(const_cast
token = strtok_s(NULL, " ");
vertex.push_back(token);
getline(myfile, line);
}
V = vertex.size();
getline(myfile, line);
while (line != "-1")
{
char *token = strtok_s(const_cast
int u = atoi(token);
int v = atoi(strtok_s(NULL, " "));
int w = atoi(strtok_s(NULL, " "));
addEdge(adj, u, v, w);
getline(myfile, line);
}
}
myfile.close();
}
else cout << "Unable to open file";
string start, end;
cout << "Enter Start Vertex : ";
cin >> start;
cout << "Enter End Vertex : ";
cin >> end;
int s, e;
int i = 0;
for (auto it = vertex.begin(); it != vertex.end(); it++)
{
if (*it == start) s = i;
if (*it == end) e = i;
i++;
}
cout << findShortestPath(s, e) << endl;
printGraph(adj, V);
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
