Please help me debug this code, expected output is attached below

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Graph Applications  
 

Please help me debug this code, expected output is attached below

 
#include <string>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <limits>
#include <queue>
template<typename T> class Graph;

template<typename T>
struct Edge
{
 size_t src;
 size_t dest;
 T weight;
 // To compare edges, only compare their weights,
 // and not the source/destination vertices
 inline bool operator< (const Edge<T>& e) const
 {
 return this->weight < e.weight;
 }
 inline bool operator> (const Edge<T>& e) const
 {
 return this->weight > e.weight;
 }
};

template <typename T>
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
{
 for (auto i = 1; i < G.vertices(); i++)
 {
 os << i << ":\t";
 auto edges = G.outgoing_edges(i);
 for (auto& e : edges)
 os << "{" << e.dest << ": " << e.weight << "}, ";
 os << std::endl;
 }
 return os;
}


template<typename T>
class Graph
{
public:
 // Initialize the graph with N vertices
 Graph(size_t N) : V(N)
 {}
 // Return number of vertices in the graph
 auto vertices() const
 {
 return V;
 }
 // Return all edges in the graph
 auto& edges() const
 {
 return edge_list;
 }
 void add_edge(Edge<T>&& e)
 {
 // Check if the source and destination vertices are within range
 if (e.src >= 1 && e.src <= V &&
 e.dest >= 1 && e.dest <= V)
 edge_list.emplace_back(e);
 else
 std::cerr << "Vertex out of bounds" << std::endl;
 }
 // Returns all outgoing edges from vertex v
 auto outgoing_edges(size_t v) const
 {
 std::vector<Edge<T>> edges_from_v;
 for (auto& e : edge_list)
 {
 if (e.src == v)
 edges_from_v.emplace_back(e);
 }
 return edges_from_v;
 }
 // Overloads the << operator so a graph be written directly to a stream
 // Can be used as std::cout << obj << std::endl;
 template <typename T>
 friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
private:
 size_t V; // Stores number of vertices in graph
 std::vector<Edge<T>> edge_list;
};


template <typename T>
auto create_reference_graph()
{
 Graph<T> G(9);
 std::map<unsigned, std::vector<std::pair<size_t, T>>> edges;
 edges[1] = { {2, 2}, {5, 3} };
 edges[2] = { {1, 2}, {5, 5}, {4, 1} };
 edges[3] = { {4, 2}, {7, 3} };
 edges[4] = { {2, 1}, {3, 2}, {5, 2}, {6, 4}, {8, 5} };
 edges[5] = { {1, 3}, {2, 5}, {4, 2}, {8, 3} };
 edges[6] = { {4, 4}, {7, 4}, {8, 1} };
 edges[7] = { {3, 3}, {6, 4} };
 edges[8] = { {4, 5}, {5, 3}, {6, 1} };
 for (auto& i : edges)
 for (auto& j : i.second)
 G.add_edge(Edge<T>{ i.first, j.first, j.second });
 return G;
}


template <typename T>
auto dijkstra_shortest_path(const Graph<T>& G, size_t src, size_t dest)
{
 std::priority_queue<Label<T>, std::vector<Label<T>>,
 std::greater<Label<T>>> heap;
 std::set<int> visited;
 std::vector<size_t> parent(G.vertices());
 std::vector<T> distance(G.vertices(), std::numeric_limits<T>::max());
 std::vector<size_t> shortest_path;
 heap.emplace(src, 0);
 parent[src] = src;
 // Search for the destination vertex in the graph
 while (!heap.empty()) {
 auto current_vertex = heap.top();
 heap.pop();
 // If the search has reached the destination vertex
 if (current_vertex.vertex_ID == dest) {
 std::cout << "Destination " << current_vertex.vertex_ID << " reached." << std::
endl;
 break;
 }
 if (visited.find(current_vertex.vertex_ID) == visited.end()) {
 std::cout << "Settling vertex " << current_vertex.vertex_ID << std::endl;
 // For each outgoing edge from the current vertex,
 // create a label for the destination vertex and add it to the heap
 for (auto e : G.outgoing_edges(current_vertex.vertex_ID)) {
 auto neighbor_vertex_ID = e.dest;
 auto new_distance_to_dest=current_vertex.distance_from_source + e.weight;
 // Check if the new path to the destination vertex
// has a lower cost than any previous paths found to it, if // yes, then this path should b
e preferred
 if (new_distance_to_dest < distance[neighbor_vertex_ID]) {
 heap.emplace(neighbor_vertex_ID, new_distance_to_dest);
 parent[e.dest] = current_vertex.vertex_ID;
 distance[e.dest] = new_distance_to_dest;
 }
 }
 visited.insert(current_vertex.vertex_ID);
 }
 }
 // Construct the path from source to the destination by backtracking
 // using the parent indexes
 auto current_vertex = dest;
 while (current_vertex != src) {
 shortest_path.push_back(current_vertex);
 current_vertex = parent[current_vertex];
 }
 shortest_path.push_back(src);
 std::reverse(shortest_path.begin(), shortest_path.end());
 return shortest_path;
}


template<typename T>
void test_dijkstra()
{
 auto G = create_reference_graph<T>();
 std::cout << "Reference graph:" << std::endl;
 std::cout << G << std::endl;
 auto shortest_path = dijkstra_shortest_path<T>(G, 1, 6);
 std::cout << "The shortest path between 1 and 6 is:" << std::endl;
 for (auto v : shortest_path)
 std::cout << v << " ";
 std::cout << std::endl;
}
int main()
{
 using T = unsigned;
 test_dijkstra<T>();
 return 0;
}
A Microsoft Visual Studio Debug Console
Reference graph:
O X
{2: 2}, {5: 3},
{1: 2}, {5: 5}, {4: 1},
{4: 2}, {7: 3},
{2: 1}, {3: 2}, {5: 2}, {6: 4}, {8: 5},
{1: 3}, {2: 5}, {4: 2}, {8: 3},
{4: 4}, {7: 4}, {8: 1},
{3: 3}, {6: 4},
{4: 5}, {5: 3}, {6: 1},
1:
2:
3:
4:
5:
6:
7:
8:
Settling vertex 1
Settling vertex 2
Settling vertex 5
Settling vertex 4
Settling vertex 3
Settling vertex 8
Destination 6 reached.
The shortest path between 1 and 6 is:
1 2 4 6
Transcribed Image Text:A Microsoft Visual Studio Debug Console Reference graph: O X {2: 2}, {5: 3}, {1: 2}, {5: 5}, {4: 1}, {4: 2}, {7: 3}, {2: 1}, {3: 2}, {5: 2}, {6: 4}, {8: 5}, {1: 3}, {2: 5}, {4: 2}, {8: 3}, {4: 4}, {7: 4}, {8: 1}, {3: 3}, {6: 4}, {4: 5}, {5: 3}, {6: 1}, 1: 2: 3: 4: 5: 6: 7: 8: Settling vertex 1 Settling vertex 2 Settling vertex 5 Settling vertex 4 Settling vertex 3 Settling vertex 8 Destination 6 reached. The shortest path between 1 and 6 is: 1 2 4 6
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY