C++
Write a program that outputs the nodes of a graph in a breadth-first traversal.
// Example program
#include <iostream>
#include <string>
#include <list>
#include <queue>
#include <cstdlib>
using namespace std;
struct AdjListNode{ //Adjacency List Node
int dest;
struct AdjListNode* next;
};
struct AdjList{ //Adjacency List
struct AdjListNode* head;
};
class Graph{
private:
int V;
struct AdjList* array;
public:
void BFS(int s);
Graph(int V){
this->V = V;
array = new AdjList [V];
for (int i = 0; i < V; ++i)
array[i].head = NULL;
}
AdjListNode* newAdjListNode(int dest){//create new adjacency list
node
AdjListNode* newNode = new AdjListNode;
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
void addEdge(int src, int dest){ //Add Edge to Graph
AdjListNode* newNode = newAdjListNode(dest);
newNode->next = array[src].head;
array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = array[dest].head;
array[dest].head = newNode;
}
void printGraph(){ //print grao
int v;
for (v = 0; v < V; ++v)
{
AdjListNode* pCrawl = array[v].head;
cout<<"\n Adjacency list of vertex "<<v<<"\n head
";
while (pCrawl)
{
cout<<"-> "<<pCrawl->dest;
pCrawl = pCrawl->next;
}
cout<<endl;
}
}
};
void Graph::BFS(int s)
{
bool *traversed = new bool[V];
for(int i = 0; i < V; i++)
traversed[i] = false;
list<int> q;
traversed[s] = true;
q.push_back(s);
list<int>::iterator i;
while(!q.empty())
{
s = q.front();
cout << s << " ";
q.pop_front();
for(AdjListNode* pCrawl = array[s].head; pCrawl!=NULL;)
{
if(!traversed[pCrawl->dest])
{
traversed[pCrawl->dest] = true;
q.push_back(pCrawl->dest);
}
pCrawl = pCrawl->next;
}
}
}
int main(){
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.printGraph();
cout << "BFS from vertex 0"<<endl;
g.BFS(0);
}
=============================
See Output
Thanks, PLEASE UPVOTE if helpful
C++ Write a program that outputs the nodes of a graph in a breadth-first traversal.
Find the list of vertices following the breadth-first traversal of the graph below starting from vertex A. (Note: when two or more nodes are equally as likely to be selected, select the one that comes first alphabetically). Enter your answer as a list of nodes with no space (or any other separator) between them.
Consider the following graph. ly Which of the following is a valid breadth first traversal of the graph? Select one a. s, d, c, a, b,e b. s, c, b, d, e, a c. s,c,e,b, d, a d. s, c, b, d, a, e Check
a) Perform a depth first traversal of the graph provided with source node d. (Write your answer as node identifiers separated by commas and spaces. Ex: a, b, c, d) b) Perform a breadth first traversal of the graph provided with source node e. (Write your answer as node identifiers separated by commas and spaces. Ex: a, b, c, d) 25 12
For the following graph, give the result of any one breadth-first traversal beginning at A, where the label of a vertex is printed when the vertex is visited. A 8 4 7 1 D 2 3 N E F
For the following graph, give the result of any one breadth-first traversal beginning at A, where the label of a vertex is printed when the vertex is visited. A 00 4 7 B с D 2 2 3 E F
For the following graph, give the result of any one breadth-first traversal beginning at D, where the label of a vertex is printed when the vertex is visited. D E A B с F G
Provide pseudocode for breadth-first (level order) traversal for general trees. All I need is the breadth-first one, I know the post-order one. So far I knew stack, list, Queues. Provide pseudocode for either preorder traversal or post-order traversal for general trees without recursion (hint: use a stack). Also provide pseudocode for breadth-first level order) traversal for general trees (hint: use a data structure that you studied before).
1. Code a breadth First traversal. 2. Code a depth First traversal. Using Python, please include test cases.
1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing the nodes added to the min spanning tree and the order they are added. Using Python. Starter code: from collections import deque class Edge: def __init__(self, endIndex, next = None): self.endIndex = endIndex self.next = next class Node: def __init__(self, name): self.name = name self.visited = False self.connects = None class Graph: def __init__(self): self.nodeList = [] self.size = 20...
discrete 2 question 31 For Esercises 25.28, write the nodes in a breadth first search of the graph for Exercises 21 the node specified 25、 26, g 20. In the computer network in the accompanying figure, the same message is to be broade Dribe ( 21-24 28. e 27. to nodes 4.Е. F and G. One way to do this is to find the shortest path from C to send out multiple copies of the same message. A more etficient...