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

Data Structure in C++ Graphs and Breadth-first Search #include <iostream> #inclu

ID: 3839650 • Letter: D

Question

Data Structure in C++

Graphs and Breadth-first Search

#include <iostream>
#include <vector>
#include <list>
#include <functional>
#include <queue>

using namespace std;

using graph = vector<list<int>>;

class adj_list {
public:
adj_list(int node_count) {
edges = vector<list<edge_type>>{node_count};
}

struct edge_type {
float weight;
int destination;
};

void create_edge(int, int, float);
bool has_edge(int, int);
void bfs(int, function<void(int)>);

vector<list<edge_type>> edges;
};

void adj_list::create_edge(int src, int dest, float w = 1) {
edges.at(src).push_back(edge_type{w, dest});
}

bool adj_list::has_edge(int src, int dest) {
for(edge_type& e : edges.at(src))
if(e.destination == dest)
return true;

return false;
};

void adj_list::bfs(int start, function<void(int)> visit) {

list<int> queue;

queue q;
vector<bool> explored{edges.size()};

q.enqueue(start);

while(!q.empty()) {
int n = q.dequeue();

visit(n);

explored.at(n) = true;
for(auto e : edges.at(n))
if(!explored.at(e.destination))
q.enqueue(e.destination);
}
}

Please fix and complete this

Part 1: Adj. list graphs Write an implementation of the adjacency listgraph representation. You may assume that nodes are integers which start at 0 Using your implementation, implement the breadth-first-search algorithm. as a function which takes a graph a starting node and a target node, and searches for a path between them. Your BFS function should return the shortest distance from the source node to the target node, or -1 if no path exists Part 2: Commands Implement the following commands Command Description create nr Create a new empty graph in register r. with n nodes print r Print some representation of the graph in r arc a br n the graph in register r, create an arc from node a to b biarc abr Create a bidirectional arc from ato bin r bfs a br Perform a breadth-first search from a to b in r, printing the distance Write the register commands necessary to create the following graph: and place them in a comment in your source code. What is the distance from node 9 to node 6?

Explanation / Answer

# Store input numbers num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))