Graph Applications Please help me debug this code, expected output is attached below #include #include #include #include #include #include #include template class Graph; template struct Edge {...



Graph Applications


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



#include
#include
#include
#include
#include


#include
#include
template class Graph;


template
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="">& e) const
 {
 return this->weight <>
 }
 inline bool operator> (const Edge& e) const
 {
 return this->weight > e.weight;
 }
};


template
std::ostream& operator<(std::ostream& os,="" const="">& G)
{
 for (auto i = 1; i < g.vertices();="">
 {
 os < i=""><>
 auto edges = G.outgoing_edges(i);
 for (auto& e : edges)
 os < "{"="">< e.dest="">< ":="" "="">< e.weight="">< "},="">
 os <>
 }
 return os;
}




template
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&& e)
 {
 // Check if the source and destination vertices are within range
 if (e.src >= 1 && e.src <= v="">
 e.dest >= 1 && e.dest <=>
 edge_list.emplace_back(e);
 else
 std::cerr < "vertex="" out="" of="" bounds"=""><>
 }
 // Returns all outgoing edges from vertex v
 auto outgoing_edges(size_t v) const
 {
 std::vector<>> 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="">
 // Can be used as std::cout < obj=""><>
 template
 friend std::ostream& operator<><>(std::ostream& os, const Graph& G);
private:
 size_t V; // Stores number of vertices in graph
 std::vector<>> edge_list;
};




template
auto create_reference_graph()
{
 Graph G(9);
 std::map<>>> 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{ i.first, j.first, j.second });
 return G;
}




template
auto dijkstra_shortest_path(const Graph& G, size_t src, size_t dest)
{
 std::priority_queue, std::vector>,
 std::greater>> heap;
 std::set visited;
 std::vector parent(G.vertices());
 std::vector distance(G.vertices(), std::numeric_limits::max());
 std::vector 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."=""><>
endl;
 break;
 }
 if (visited.find(current_vertex.vertex_ID) == visited.end()) {
 std::cout < "settling="" vertex="" "="">< current_vertex.vertex_id=""><>
 // 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
void test_dijkstra()
{
 auto G = create_reference_graph();
 std::cout < "reference="" graph:"=""><>
 std::cout < g=""><>
 auto shortest_path = dijkstra_shortest_path(G, 1, 6);
 std::cout < "the="" shortest="" path="" between="" 1="" and="" 6="" is:"=""><>
 for (auto v : shortest_path)
 std::cout < v="">< "="">
 std::cout <>
}
int main()
{
 using T = unsigned;
 test_dijkstra();
 return 0;
}


A Microsoft Visual Studio Debug Console<br>Reference graph:<br>O X<br>{2: 2}, {5: 3},<br>{1: 2}, {5: 5}, {4: 1},<br>{4: 2}, {7: 3},<br>{2: 1}, {3: 2}, {5: 2}, {6: 4}, {8: 5},<br>{1: 3}, {2: 5}, {4: 2}, {8: 3},<br>{4: 4}, {7: 4}, {8: 1},<br>{3: 3}, {6: 4},<br>{4: 5}, {5: 3}, {6: 1},<br>1:<br>2:<br>3:<br>4:<br>5:<br>6:<br>7:<br>8:<br>Settling vertex 1<br>Settling vertex 2<br>Settling vertex 5<br>Settling vertex 4<br>Settling vertex 3<br>Settling vertex 8<br>Destination 6 reached.<br>The shortest path between 1 and 6 is:<br>1 2 4 6<br>

Extracted 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
Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here