Skip to main content

Create a program to represent a graph using an adjacency Matrix and perform Breadth-First Search (BFS) to systematically visit all vertices.

CODE:


 #include <iostream>

#include <queue>

#include <vector>


using namespace std;


class Graph {

private:

    int numVertices;

    vector<vector<int>> adjacencyMatrix;


public:

    Graph(int numVertices) {

        this->numVertices = numVertices;

        adjacencyMatrix.resize(numVertices, vector<int>(numVertices, 0));

    }


    void addEdge(int source, int destination) {

        adjacencyMatrix[source][destination] = 1;

        // For undirected graph, add this line as well:

        // adjacencyMatrix[destination][source] = 1;

    }


    void BFS(int startVertex) {

        vector<bool> visited(numVertices, false);

        queue<int> q;


        visited[startVertex] = true;

        q.push(startVertex);


        while (!q.empty()) {

            int currentVertex = q.front();

            cout << currentVertex << " ";

            q.pop();


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

                if (adjacencyMatrix[currentVertex][i] == 1 && !visited[i]) {

                    visited[i] = true;

                    q.push(i);

                }

            }

        }

    }

};


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);


    cout << "Breadth First Traversal (starting from vertex 2): ";

    g.BFS(2);

    cout << endl;


    return 0;

}



Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...