Question

Textbook is : Analysis of algorithms

l el as the visit operation. You should trace it using the graphs in this section to make sure you get the same answer. 4. Wr

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

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

void BFS(int start)

{

    // Visited vector to so that

    // a vertex is not visited more than once

    // Initializing the vector to false as no

    // vertex is visited at the beginning

    vector<bool> visited(v, false);

    vector<int> q;

    q.push_back(start);

  

    // Set source as visited

    visited[start] = true;

  

    int vis;

    while (!q.empty()) {

        vis = q[0];

  

        // Print the current node

        cout << vis << " ";

        q.erase(q.begin());

  

        // For every adjacent vertex to the current vertex

        for (int i = 0; i < v; i++) {

            if (adj[vis][i] == 1 && (!visited[i])) {

  

                // Push the adjacent node to the queue

                q.push_back(i);

  

                // Set

                visited[i] = true;

            }

        }

    }

}

  

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Textbook is : Analysis of algorithms l el as the visit operation. You should trace it...
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