Hi, I need to fix my C++ code and I need to follow the steps of the requirements
ID: 3718850 • Letter: H
Question
Hi, I need to fix my C++ code and I need to follow the steps of the requirements. I have some code but I do not know if it is good. I tried everything and the code does not work. I could not fix it. I would like to have the answer with an screenshot showing that the code is running perfectly since I tried everything and still it does not work.
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.
File.txt
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.
My code:
#include<iostream>
#include<vector>
#include<string>
#include<list>
#include<algorithm>
#include<fstream>
using namespace std;
void addEdge(vector <pair<int, int> > adj[], int u,
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<pair<int, int> > adj[], int V)
{
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
void findShortestPath(vector <pair<int, int> > adj[], int src, int dest, int V, int &total)
{
// Mark all the vertices as not visited
bool *visited = new bool[2 * V];
int *parent = new int[2 * V];
// Initialize parent[] and visited[]
for (int i = 0; i < 2 * V; i++)
{
visited[i] = false;
parent[i] = -1;
}
// Mark the current node as visited and enqueue it
visited[src] = true;
int v, w, smallest = INT_MAX;;
for (int u = 0; u < V - 1; u++)
{
smallest = INT_MAX;
for (int j = 0; j < adj[u].size() - 1; j++)
{
if (adj[u][j].second < adj[u][j + 1].second && visited[adj[u][j].first] == false)
{
if (adj[u][j].second < smallest) {
smallest = adj[u][j].second;
visited[adj[u][j].first] = true;
}
}
else if (visited[adj[u][j + 1].first] == false)
{
if (adj[u][j + 1].second < smallest) {
smallest = adj[u][j + 1].second;
visited[adj[u][j + 1].first] = true;
}
}
if (adj[u][j].first == dest)
{
total += adj[u][j].second;
return;
}
else if (adj[u][j + 1].first == dest)
{
total += adj[u][j + 1].second;
return;
}
}
total += smallest;
}
}
// Driver code
int main()
{
vector<pair<int, int> > adj[6];
int V = 6;
string line;
ifstream myfile("file.txt");
vector<string> vertex;
int count = 1;
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << ' ';
while (line != "-1")
{
char *token = strtok(const_cast<char*>(line.c_str()), " ");
token = strtok(NULL, " ");
vertex.push_back(token);
getline(myfile, line);
cout << line << ' ';
}
V = vertex.size();
getline(myfile, line);
cout << line << ' ';
while (line != "-1")
{
char *token = strtok(const_cast<char*>(line.c_str()), " ");
int u = atoi(token);
int v = atoi(strtok(NULL, " "));
int w = atoi(strtok(NULL, " "));
addEdge(adj, u, v, w);
getline(myfile, line);
cout << 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++;
}
//printGraph(adj, V);
int total = 0;
findShortestPath(adj, s, e, V, total);
cout << "Total path: " << total << endl;
//printGraph(adj, V);
system("pause");
return 0;
}
Error Capture:
Message Build: Debug in Assignment 09 (compiler: GNU GCC Compiler) -- In function 'void findShortestPath(std: :vector*. int, int, int, error 'INT MAX was not declared in this scope varning : comparison betreen signed and unsigned integer expressions-Wsign-compare] warning : unused variable 'r' ?-Wunused-variable]Explanation / Answer
#include<iostream>
#include<vector>
#include<string>
#include<string.h>
#include<list>
#include<algorithm>
#include<fstream>
using namespace std;
void addEdge(vector <pair<int, int> > adj[], int u,
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<pair<int, int> > adj[], int V)
{
int v, w;
for (int u = 0; u < V; u++)
{
cout << u << " - ";
for (vector<pair<int, int> >::iterator 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
void findShortestPath(vector <pair<int, int> > adj[], int src, int dest, int V, int &total)
{
// Mark all the vertices as not visited
bool *visited = new bool[2 * V];
int *parent = new int[2 * V];
// Initialize parent[] and visited[]
for (int i = 0; i < 2 * V; i++)
{
visited[i] = false;
parent[i] = -1;
}
// Mark the current node as visited and enqueue it
visited[src] = true;
int v, w, smallest = INT_MAX;;
for (int u = 0; u < V - 1; u++)
{
smallest = INT_MAX;
for (int j = 0; j < adj[u].size() - 1; j++)
{
if (adj[u][j].second < adj[u][j + 1].second && visited[adj[u][j].first] == false)
{
if (adj[u][j].second < smallest) {
smallest = adj[u][j].second;
visited[adj[u][j].first] = true;
}
}
else if (visited[adj[u][j + 1].first] == false)
{
if (adj[u][j + 1].second < smallest) {
smallest = adj[u][j + 1].second;
visited[adj[u][j + 1].first] = true;
}
}
if (adj[u][j].first == dest)
{
total += adj[u][j].second;
return;
}
else if (adj[u][j + 1].first == dest)
{
total += adj[u][j + 1].second;
return;
}
}
total += smallest;
}
}
// Driver code
int main()
{
vector<pair<int, int> > adj[6];
int V = 6;
string line;
ifstream myfile("file.txt");
vector<string> vertex;
int count = 1;
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << ' ';
while (line != "-1")
{
char *token = strtok(const_cast<char*>(line.c_str()), " ");
token = strtok(NULL, " ");
vertex.push_back(token);
getline(myfile, line);
cout << line << ' ';
}
V = vertex.size();
getline(myfile, line);
cout << line << ' ';
while (line != "-1")
{
char *token = strtok(const_cast<char*>(line.c_str()), " ");
int u = atoi(token);
int v = atoi(strtok(NULL, " "));
int w = atoi(strtok(NULL, " "));
addEdge(adj, u, v, w);
getline(myfile, line);
cout << 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 (vector<string>::iterator it = vertex.begin(); it != vertex.end(); it++)
{
if (*it == start) s = i;
if (*it == end) e = i;
i++;
}
//printGraph(adj, V);
int total = 0;
findShortestPath(adj, s, e, V, total);
cout << "Total path: " << total << endl;
//printGraph(adj, V);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.