Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Data Structure in C++ Graphs and Breadth-first Search Here is my unfinished code: #include #include #include #include #include using namespace std; using graph = vector

Data Structure in C++

Graphs and Breadth-first Search

image text in transcribedHere is my unfinished code:

#include #include #include #include #include

using namespace std;

using graph = vector>;

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

struct edge_type { float weight; int destination; };

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

vector> 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 visit) {

list queue;

queue q; vector 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students explore these related Databases questions