Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I have an error in my C++ program and also I want to see if I am in good

ID: 3715026 • Letter: H

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.

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<iostream>

#include<vector>

#include<string>

#include<list>

#include<algorithm>

#include<fstream>

using namespace std;

vector<pair<int, int> > adj[6];

int V = 6;

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

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<int> queue;

// 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<string> vertex;

int count = 1, V;

vector<pair<int, int> > adj[6];

if (myfile.is_open())

{

while (getline(myfile, line))

{

//cout << line << ' ';

while (line != "-1")

{

char *token = strtok_s(const_cast<char*>(line.c_str()), " ");

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<char*>(line.c_str()), " ");

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;

}

Explanation / Answer

// Program to display the minimum path between any two vertices in a given graph.

#include<iostream>

#include <list>

using namespace std;

// Directed graph using adjacency list representation class

class GraphC

{

int Vert; //number of vertices to be used

list<int> *adj; // Pointer to an array having adjacent lists

public:

GraphC(int Vert); // Defining constructor

bool isReachable(int s, int d); // to check if the edge is reachable

void addEdge(int Vert, int w); //Adding an edge to graph

};

GraphC::GraphC(int Vert)

{

this->Vert = Vert;

adj = new list<int>[Vert];

}

void GraphC::addEdge(int Vert, int w)

{

adj[Vert].push_back(w); //adding w to Vert’s list.

}

// Function to check whether d is reachable from s( BFS based)

bool GraphC::isReachable(int s, int d)

{

if (s == d)

return true;

//marking all the vertices as not visited

bool *visited = new bool[Vert];

for (int i = 0; i < Vert; i++)

visited[i] = false;

//creating a queue for BFS

list<int> queue;

//Get all adjacent vertices of a vertex

list<int>::iterator i;

//marking the current node as visited and enqueue it one by one

visited[s] = true;

queue.push_back(s);

while (!queue.empty())

{

//dequeuing a vertex from queue

s = queue.front();

queue.pop_front(); // printing it

for (i = adj[s].begin(); i != adj[s].end(); ++i)

{

//adjacent node is the destination node, then

if (*i == d)

return true;

//Continue to do BFS

if (!visited[*i])

{

visited[*i] = true;

queue.push_back(*i);

}

}

}

//completing without visiting d

return false;

}

// Driver program

int main()

{

// Creating a graph

GraphC g(4);

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(2, 0);

g.addEdge(2, 3);

g.addEdge(3, 3);

int u = 1, v = 3;

if(g.isReachable(u, Vert))

cout<< " There is a path from " << u << " to " << Vert;

else

cout<< " There is no path from " << u << " to " << Vert;

u = 3, v = 1;

if(g.isReachable(u, Vert))

cout<< " There is a path from " << u << " to " << Vert;

else

cout<< " There is no path from " << u << " to " << Vert;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote