
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
expand_more
expand_more
format_list_bulleted
Question
Help me fix my error in my code the expected output is in the picture.
My Code:
#include <iostream>
#include <vector >
#include <queue>
#include <climits> // Include the <climits> header for INT_MAX
using namespace std;
vector<int> dijkstra(int V, vector<vector<pair<int, int>>> adj, int S) {
vector<int> dist(V, INT_MAX); // dist[i] will hold the shortest distance from S to i
vector<int> prev(V, -1); // prev[i] will hold the penultimate vertex in the shortest path from S to i
// Priority queue to store vertices that are being processed
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
// Initialize source vertex distance as 0 and push it to the priority queue
dist[S] = 0;
pq.push({0, S});
// Dijkstra's algorithm
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
// Visit each neighbor of u
for (auto& edge : adj[u]) {
int v = edge.first; // neighbor vertex
int weight = edge.second; // weight of the edge from u to v
// If a shorter path is found, update distance and penultimate vertex
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
prev[v] = u;
pq.push({dist[v], v});
}
}
}
return prev; // Return the penultimate vertex array
}
//DO NOT edit the code below.
int main() {
int v, e, source;
cout << "Enter the number of vertices and edges: ";
cin >> v >> e;
// Initialize the graph with n vertices
vector<vector<pair<int, int>>> adj(v);
// Read edges and weights
cout << "Enter edges in the format (from to weight):" << endl;
for (int i = 0; i < e; ++i) {
int from, to, weight;
cin >> from >> to >> weight;
adj[from].push_back({to, weight}); // Only add edge from 'from' to 'to'
}
cout << "Enter the source vertex: ";
cin >> source;
// Find shortest paths from source to all vertices
vector<int> prev_vertices = dijkstra(v, adj, source);
// Print shortest paths
cout << "Shortest paths from source vertex " << source << ":\n";
for (int i = 0; i < v; ++i) {
cout << "Vertex " << i << ": ";
if (prev_vertices[i] == -1) {
if (i == source) {
cout << "0\n";
} else {
cout << "No path\n";
}
} else {
// Reconstruct the shortest path
vector<int> path;
int current = i;
while (current != -1) {
path.push_back(current);
current = prev_vertices[current];
}
if (dist[i] == INT_MAX) {
cout << "No path\n";
} else {
cout << "Distance: " << dist[i] << ", Path: ";
for (int j = path.size() - 1; j >= 0; --j) {
cout << path[j] << (j == 0 ? "\n" : " -> ");
}
}
}
}
return 0;
}
![* LAST RUN on 4/18/2024, 10:13:27 AM
Check 1 failed
Output:
Dijkstra.cpp: In function 'int main()':
Dijkstra.cpp:82:17: error: 'dist' was not decla
82 |
if (dist[i] == INT_MAX) {
Expected:
Enter the number of vertices and edges: Enter €
Enter the source vertex: Shortest paths from sc
Vertex 0: 0
Vertex 1: 9
Check 2 failed
Output:
Dijkstra.cpp: In function 'int main()':
Dijkstra.cpp:82:17: error: 'dist' was not decla
82 |
|
if (dist[i] == INT_MAX) {
Expected:
Enter the number of vertices and edges: Enter €
Enter the source vertex: Shortest paths from sc
Vertex 0: 4
Vertex 1: 3
Vertex 2:0](https://content.bartleby.com/qna-images/question/b27ad09a-811a-4663-a78e-f7b328b928a0/0049df6d-3c14-4445-867d-82971d64fd27/8jo348_thumbnail.png)
Transcribed Image Text:* LAST RUN on 4/18/2024, 10:13:27 AM
Check 1 failed
Output:
Dijkstra.cpp: In function 'int main()':
Dijkstra.cpp:82:17: error: 'dist' was not decla
82 |
if (dist[i] == INT_MAX) {
Expected:
Enter the number of vertices and edges: Enter €
Enter the source vertex: Shortest paths from sc
Vertex 0: 0
Vertex 1: 9
Check 2 failed
Output:
Dijkstra.cpp: In function 'int main()':
Dijkstra.cpp:82:17: error: 'dist' was not decla
82 |
|
if (dist[i] == INT_MAX) {
Expected:
Enter the number of vertices and edges: Enter €
Enter the source vertex: Shortest paths from sc
Vertex 0: 4
Vertex 1: 3
Vertex 2:0
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

Knowledge Booster
Similar questions
- The implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forwardHow is an array stored in main memory? How is a linked list stored in main memory? What are their comparative advantages and disadvantages? Give examples of data that would be best stored as an array and as a linked list.arrow_forward(Practice) Write a C++ program that adds equivalent elements of the two-dimensional arrays named first and second. Both arrays should have two rows and three columns. For example, element [1][2] of the resulting array should be the sum of first [1][2]andsecond[1][2]. The first and second arrays should be initialized as follows: first second 16 18 23 24 52 77 54 9111 16 19 59arrow_forward
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningOperations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks ColeSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage

C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning

Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole

Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning

C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr

EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage