Question

Please provide C language code no c++ ,txt file also needed

with comment

Finish task 5Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph usi• The traversal requirements are: 1. Your traversal needs to start from the node with maximum degree (if there are multiple n

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

Code:

#include <iostream>
#include <fstream>
#include <vector>
#include <list>

using namespace std;

int main(int argc, char** argv)
{
// open file
// ifstream fin("graph.txt");
// for command line inputfile, use the following
ifstream fin(argv[1]);
  
// read number of nodes
int n, node, edge;
string edges;
fin >> n;
  
// create adjacency list for graph
vector<int> adj[n];
char c;
  
// start reading the lines
for(int i=0; i<n; i++)
{
// read node first
fin >> node;
// read the current line
getline(fin, edges);
// split the line into required data
for(int i=0; i<edges.size(); i++)
{
if(edges[i] == ' ')
edge = 0; // prepare for new number
else if(edges[i] == ',')
adj[node-1].push_back(edge-1); // end of the number
else
edge = edge * 10 + (int) (edges[i] - '0'); // add digit to number
}
// last edge
adj[node-1].push_back(edge-1);
}
  
// close file
fin.close();
  
// start the bfs
// Mark all the vertices as not visited
bool visited[n];
// also find the starting node
int s = 0;
for(int i = 0; i < n; i++)
{
visited[i] = false;
  
// starting node is the node with maximum degree
if(adj[i].size() >= adj[s].size())
s = i;
}
  
// Create a queue for BFS
list<int> queue;
  
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
  
cout << "BFS:";
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << " " << (s+1);
queue.pop_front();
  
// Get all adjacent vertices of the dequeued vertex s.
// visit the adjacency node in opposite order
// for the highest child has the most priority
// If a adjacent has not been visited, then mark it visited and enqueue it
for (int i = adj[s].size()-1; i >= 0; --i)
{
if (!visited[adj[s][i]])
{
visited[adj[s][i]] = true;
queue.push_back(adj[s][i]);
}
}
}
cout << endl;
return 0;
}

Sample output:

input

ол во ЈЕ л дw N Ел PPN NuA uw

output

BFS: 5 4 2 1 3

Code screenshot

Add a comment
Know the answer?
Add Answer to:
Please provide C language code no c++ ,txt file also needed with comment Finish task 5...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT