Skip to main content

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

CODE:


 #include <iostream>

#include <vector>

#include <stack>


using namespace std;


class Graph {

private:

    int numVertices;

    vector<vector<int>> adjacencyMatrix;


public:

    Graph(int n) : numVertices(n) {

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

    }


    void addEdge(int u, int v) {

        adjacencyMatrix[u][v] = 1;

        adjacencyMatrix[v][u] = 1; // Assuming undirected graph

    }


    void DFS(int startVertex) {

        vector<bool> visited(numVertices, false);

        stack<int> stack;


        visited[startVertex] = true;

        stack.push(startVertex);


        cout << "Depth First Search starting from vertex " << startVertex << ": ";


        while (!stack.empty()) {

            int currentVertex = stack.top();

            stack.pop();

            cout << currentVertex << " ";


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

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

                    visited[i] = true;

                    stack.push(i);

                }

            }

        }

        cout << endl;

    }

};


int main() {

    Graph g(6); // Change the value to the number of vertices in your graph


    g.addEdge(0, 1);

    g.addEdge(0, 2);

    g.addEdge(1, 3);

    g.addEdge(1, 4);

    g.addEdge(2, 4);

    g.addEdge(3, 5);

    g.addEdge(4, 5);


    g.DFS(0);


    return 0;

}



Popular posts from this blog

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) {                ...

Create a BST and find inorder successor and inorder predecessor of specific node.

CODE: #include <iostream> using namespace std; // Node definition struct Node {     int data;     Node* left;     Node* right;     Node(int val) {         data = val;         left = nullptr;         right = nullptr;     } }; // Insertion in BST Node* insert(Node* root, int val) {     if (root == nullptr) {         return new Node(val);     }     if (val < root->data) {         root->left = insert(root->left, val);     } else {         root->right = insert(root->right, val);     }     return root; } // Finding inorder successor Node* findInorderSuccessor(Node* root, Node* target) {     if (target->right != nullptr) {         Node* current = target->right;         while (current->l...