Banana-Leopard/TASTY huggingface dataset:
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check valid coordinate
int validCoord(int x, int y, int n, int m)
{
 
    if (x < 0 || y < 0) {
        return 0;
    }
    if (x >= n || y >= m) {
        return 0;
    }
    return 1;
}
 
// Function to run bfs
void bfs(int n, int m, int data[][8],
                     int x, int y, int color)
{
   
  // Visiting array
  int vis[101][101];
   
  // Initializing all as zero
  memset(vis, 0, sizeof(vis));
   
  // Creating queue for bfs
  queue<pair<int, int> > obj;
   
  // Pushing pair of {x, y}
  obj.push({ x, y });
   
  // Marking {x, y} as visited
  vis[x][y] = 1;
   
  // Until queue is empty
  while (obj.empty() != 1)
  {
 
    // Extracting front pair
    pair<int, int> coord = obj.front();
    int x = coord.first;
    int y = coord.second;
    int preColor = data[x][y];
 
    data[x][y] = color;
     
    // Popping front pair of queue
    obj.pop();
 
    // For Upside Pixel or Cell
    if (validCoord(x + 1, y, n, m)
        && vis[x + 1][y] == 0
        && data[x + 1][y] == preColor)
    {
      obj.push({ x + 1, y });
      vis[x + 1][y] = 1;
    }
     
    // For Downside Pixel or Cell
    if (validCoord(x - 1, y, n, m)
        && vis[x - 1][y] == 0
        && data[x - 1][y] == preColor)
    {
      obj.push({ x - 1, y });
      vis[x - 1][y] = 1;
    }
     
    // For Right side Pixel or Cell
    if (validCoord(x, y + 1, n, m)
        && vis[x][y + 1] == 0
        && data[x][y + 1] == preColor)
    {
      obj.push({ x, y + 1 });
      vis[x][y + 1] = 1;
    }
     
    // For Left side Pixel or Cell
    if (validCoord(x, y - 1, n, m)
        && vis[x][y - 1] == 0
        && data[x][y - 1] == preColor)
    {
      obj.push({ x, y - 1 });
      vis[x][y - 1] = 1;
    }
  }
   
  // Printing The Changed Matrix Of Pixels
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      cout << data[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
 
// Driver Code
int main()
{
  int n, m, x, y, color;
  n = 8;
  m = 8;
 
  int data[8][8] = {
    { 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 1, 1, 1, 1, 1, 0, 0 },
    { 1, 0, 0, 1, 1, 0, 1, 1 },
    { 1, 2, 2, 2, 2, 0, 1, 0 },
    { 1, 1, 1, 2, 2, 0, 1, 0 },
    { 1, 1, 1, 2, 2, 2, 2, 0 },
    { 1, 1, 1, 1, 1, 2, 1, 1 },
    { 1, 1, 1, 1, 1, 2, 2, 1 },
  };
 
  x = 4, y = 4, color = 3;
   
  // Function Call
  bfs(n, m, data, x, y, color);
  return 0;
}

Time complexity: quadratic
Space complexity: quadratic
#include <bits/stdc++.h>
using namespace std;
 
// Function to print all elements of matrix in sorted orderd
void sortedMatrix(int N, vector<vector<int> > Mat)
{
    vector<int> temp;
   
   // Store all elements of matrix into temp
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            temp.push_back(Mat[i][j]);
        }
    }
 
    // Sort the temp
    sort(temp.begin(), temp.end());
 
    // Print the values of temp
    for (int i = 0; i < temp.size(); i++) {
        cout << temp[i] << " ";
    }
}
 
int main()
{
    int N = 4;
    vector<vector<int> > Mat = {
        { 10, 20, 30, 40 },
        { 15, 25, 35, 45 },
        { 27, 29, 37, 48 },
        { 32, 33, 39, 50 },
    };
    sortedMatrix(N, Mat);
 
    return 0;
}
 
// This code is contributed by pratiknawale999

Time complexity: quadratic
Space complexity: quadratic
// A simple C++ program to find sum of all subsquares of
// size k x k
#include <iostream>
using namespace std;
 
// Size of given matrix
#define n 5
 
// A simple function to find sum of all sub-squares of size
// k x k in a given square matrix of size n x n
void printSumSimple(int mat[][n], int k)
{
    // k must be smaller than or equal to n
    if (k > n)
        return;
 
    // row number of first cell in current sub-square of
    // size k x k
    for (int i = 0; i < n - k + 1; i++) {
        // column of first cell in current sub-square of
        // size k x k
        for (int j = 0; j < n - k + 1; j++) {
            // Calculate and print sum of current sub-square
            int sum = 0;
            for (int p = i; p < k + i; p++)
                for (int q = j; q < k + j; q++)
                    sum += mat[p][q];
            cout << sum << "  ";
        }
 
        // Line separator for sub-squares starting with next
        // row
        cout << endl;
    }
}
 
// Driver program to test above function
int main()
{
    int mat[n][n] = {
        { 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
        { 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
        { 5, 5, 5, 5, 5 },
    };
    int k = 3;
    printSumSimple(mat, k);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: quadratic
Space complexity: constant
// An efficient C++ program to find sum of all subsquares of
// size k x k
#include <iostream>
using namespace std;
 
// Size of given matrix
#define n 5
 
// A O(n^2) function to find sum of all sub-squares of size
// k x k in a given square matrix of size n x n
void printSumTricky(int mat[][n], int k)
{
    // k must be smaller than or equal to n
    if (k > n)
        return;
 
    // 1: PREPROCESSING
    // To store sums of all strips of size k x 1
    int stripSum[n][n];
 
    // Go column by column
    for (int j = 0; j < n; j++) {
        // Calculate sum of first k x 1 rectangle in this
        // column
        int sum = 0;
        for (int i = 0; i < k; i++)
            sum += mat[i][j];
        stripSum[0][j] = sum;
 
        // Calculate sum of remaining rectangles
        for (int i = 1; i < n - k + 1; i++) {
            sum += (mat[i + k - 1][j] - mat[i - 1][j]);
            stripSum[i][j] = sum;
        }
    }
 
    // 2: CALCULATE SUM of Sub-Squares using stripSum[][]
    for (int i = 0; i < n - k + 1; i++) {
        // Calculate and print sum of first subsquare in
        // this row
        int sum = 0;
        for (int j = 0; j < k; j++)
            sum += stripSum[i][j];
        cout << sum << "  ";
 
        // Calculate sum of remaining squares in current row
        // by removing the leftmost strip of previous
        // sub-square and adding a new strip
        for (int j = 1; j < n - k + 1; j++) {
            sum += (stripSum[i][j + k - 1]
                    - stripSum[i][j - 1]);
            cout << sum << "  ";
        }
 
        cout << endl;
    }
}
 
// Driver program to test above function
int main()
{
    int mat[n][n] = {
        { 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
        { 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
        { 5, 5, 5, 5, 5 },
    };
    int k = 3;
    printSumTricky(mat, k);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: quadratic
Space complexity: quadratic
// C++ program to find
// transpose of a matrix
#include <bits/stdc++.h>
using namespace std;
#define N 4
 
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            B[i][j] = A[j][i];
}
 
// Driver code
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
 
    // Note dimensions of B[][]
    int B[N][N], i, j;
 
      // Function call
    transpose(A, B);
 
    cout << "Result matrix is \n";
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            cout << " " << B[i][j];
 
        cout << "\n";
    }
    return 0;
}
 
// This code is contributed by shivanisinghss2110

Time complexity: quadratic
Space complexity: quadratic
#include <bits/stdc++.h>
using namespace std;
 
#define N 4
 
// Converts A[][] to its transpose
void transpose(int A[][N])
{
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++)
            swap(A[i][j], A[j][i]);
}
 
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
 
    transpose(A);
 
    printf("Modified matrix is \n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            printf("%d ", A[i][j]);
        printf("\n");
    }
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program for addition
// of two matrices
#include <bits/stdc++.h>
using namespace std;
#define N 4
 
// This function adds A[][] and B[][], and stores
// the result in C[][]
void add(int A[][N], int B[][N], int C[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            C[i][j] = A[i][j] + B[i][j];
}
 
// Driver code
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
 
    int B[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
 
    int C[N][N]; // To store result
    int i, j;
 
    // Function Call
    add(A, B, C);
 
    cout << "Result matrix is " << endl;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            cout << C[i][j] << " ";
        cout << endl;
    }
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: quadratic
// A Memoization based program to find maximum collection
// using two traversals of a grid
#include<bits/stdc++.h>
using namespace std;
#define R 5
#define C 4
 
// checks whether a given input is valid or not
bool isValid(int x, int y1, int y2)
{
    return (x >= 0 && x < R && y1 >=0 &&
            y1 < C && y2 >=0 && y2 < C);
}
 
// Driver function to collect max value
int getMaxUtil(int arr[R][C], int mem[R][C][C], int x, int y1, int y2)
{
    /*---------- BASE CASES -----------*/
    // if P1 or P2 is at an invalid cell
    if (!isValid(x, y1, y2)) return INT_MIN;
 
    // if both traversals reach their destinations
    if (x == R-1 && y1 == 0 && y2 == C-1)
       return (y1 == y2)? arr[x][y1]: arr[x][y1] + arr[x][y2];
 
    // If both traversals are at last row but not at their destination
    if (x == R-1) return INT_MIN;
 
    // If subproblem is already solved
    if (mem[x][y1][y2] != -1) return mem[x][y1][y2];
 
    // Initialize answer for this subproblem
    int ans = INT_MIN;
 
    // this variable is used to store gain of current cell(s)
    int temp = (y1 == y2)? arr[x][y1]: arr[x][y1] + arr[x][y2];
 
    /* Recur for all possible cases, then store and return the
       one with max value */
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1, y2-1));
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1, y2+1));
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1, y2));
 
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1-1, y2));
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1-1, y2-1));
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1-1, y2+1));
 
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1+1, y2));
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1+1, y2-1));
    ans = max(ans, temp + getMaxUtil(arr, mem, x+1, y1+1, y2+1));
 
    return (mem[x][y1][y2] = ans);
}
 
// This is mainly a wrapper over recursive function getMaxUtil().
// This function creates a table for memoization and calls
// getMaxUtil()
int geMaxCollection(int arr[R][C])
{
    // Create a memoization table and initialize all entries as -1
    int mem[R][C][C];
    memset(mem, -1, sizeof(mem));
 
    // Calculation maximum value using memoization based function
    // getMaxUtil()
    return getMaxUtil(arr, mem, 0, 0, C-1);
}
 
// Driver program to test above functions
int main()
{
    int arr[R][C] = {{3, 6, 8, 2},
                     {5, 2, 4, 3},
                     {1, 1, 20, 10},
                     {1, 1, 20, 10},
                     {1, 1, 20, 10},
                    };
    cout << "Maximum collection is " << geMaxCollection(arr);
    return 0;
}

Time complexity: cubic
Space complexity: quadratic
// A Naive Recursive C++ program
// to count paths with exactly
// 'k' coins
#include <bits/stdc++.h>
#define R 3
#define C 3
using namespace std;
 
// Recursive function to count paths with sum k from
// (0, 0) to (m, n)
int pathCountRec(int mat[][C], int m, int n, int k)
{
    // Base cases
    if (m < 0 || n < 0 || k < 0) return 0;
    if (m==0 && n==0) return (k == mat[m][n]);
 
    // (m, n) can be reached either through (m-1, n) or
    // through (m, n-1)
    return pathCountRec(mat, m-1, n, k-mat[m][n]) +
           pathCountRec(mat, m, n-1, k-mat[m][n]);
}
 
// A wrapper over pathCountRec()
int pathCount(int mat[][C], int k)
{
    return pathCountRec(mat, R-1, C-1, k);
}
 
// Driver program
int main()
{
    int k = 12;
    int mat[R][C] = { {1, 2, 3},
                      {4, 6, 5},
                      {3, 2, 1}
                  };
    cout << pathCount(mat, k);
    return 0;
}

Time complexity: linear
Space complexity: quadratic
// A Dynamic Programming based C++ program to count paths with
// exactly 'k' coins
#include <bits/stdc++.h>
#define R 3
#define C 3
#define MAX_K 1000
using namespace std;
 
int dp[R][C][MAX_K];
 
int pathCountDPRecDP(int mat[][C], int m, int n, int k)
{
    // Base cases
    if (m < 0 || n < 0 || k < 0) return 0;
    if (m==0 && n==0) return (k == mat[m][n]);
 
    // If this subproblem is already solved
    if (dp[m][n][k] != -1) return dp[m][n][k];
 
    // (m, n) can be reached either through (m-1, n) or
    // through (m, n-1)
    dp[m][n][k] = pathCountDPRecDP(mat, m-1, n, k-mat[m][n]) +
                  pathCountDPRecDP(mat, m, n-1, k-mat[m][n]);
 
    return dp[m][n][k];
}
 
// This function mainly initializes dp[][][] and calls
// pathCountDPRecDP()
int pathCountDP(int mat[][C], int k)
{
    memset(dp, -1, sizeof dp);
    return pathCountDPRecDP(mat, R-1, C-1, k);
}
 
// Driver Program to test above functions
int main()
{
    int k = 12;
    int mat[R][C] = { {1, 2, 3},
                      {4, 6, 5},
                      {3, 2, 1}
                  };
    cout << pathCountDP(mat, k);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to find minimum initial points to reach
// destination
#include <bits/stdc++.h>
#define R 3
#define C 3
 
using namespace std;
 
int minInitialPoints(int points[][C])
{
    // dp[i][j] represents the minimum initial points player
    // should have so that when starts with cell(i, j)
    // successfully reaches the destination cell(m-1, n-1)
    int dp[R][C];
    int m = R, n = C;
 
    // Base case
    dp[m - 1][n - 1] = points[m - 1][n - 1] > 0
                           ? 1
                           : abs(points[m - 1][n - 1]) + 1;
 
    // Fill last row and last column as base to fill
    // entire table
    for (int i = m - 2; i >= 0; i--)
        dp[i][n - 1]
            = max(dp[i + 1][n - 1] - points[i][n - 1], 1);
    for (int j = n - 2; j >= 0; j--)
        dp[m - 1][j]
            = max(dp[m - 1][j + 1] - points[m - 1][j], 1);
 
    // fill the table in bottom-up fashion
    for (int i = m - 2; i >= 0; i--) {
        for (int j = n - 2; j >= 0; j--) {
            int min_points_on_exit
                = min(dp[i + 1][j], dp[i][j + 1]);
            dp[i][j]
                = max(min_points_on_exit - points[i][j], 1);
        }
    }
 
    return dp[0][0];
}
 
// Driver Program
int main()
{
 
    int points[R][C]
        = { { -2, -3, 3 }, { -5, -10, 1 }, { 10, 30, -5 } };
    cout << "Minimum Initial Points Required: "
         << minInitialPoints(points);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
/* Program to implement a stack using
two queue */
#include <bits/stdc++.h>
 
using namespace std;
 
class Stack {
    // Two inbuilt queues
    queue<int> q1, q2;
 
public:
    void push(int x)
    {
        // Push x first in empty q2
        q2.push(x);
 
        // Push all the remaining
        // elements in q1 to q2.
        while (!q1.empty()) {
            q2.push(q1.front());
            q1.pop();
        }
 
        // swap the names of two queues
        queue<int> q = q1;
        q1 = q2;
        q2 = q;
    }
 
    void pop()
    {
        // if no elements are there in q1
        if (q1.empty())
            return;
        q1.pop();
    }
 
    int top()
    {
        if (q1.empty())
            return -1;
        return q1.front();
    }
 
    int size() { return q1.size(); }
};
 
// Driver code
int main()
{
    Stack s;
    s.push(1);
    s.push(2);
    s.push(3);
 
    cout << "current size: " << s.size() << endl;
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
 
    cout << "current size: " << s.size() << endl;
    return 0;
}
// This code is contributed by Chhavi

Time complexity: linear
Space complexity: linear
// Program to implement a stack
// using two queue
#include <bits/stdc++.h>
using namespace std;
 
class Stack {
    queue<int> q1, q2;
 
public:
    void pop()
    {
        if (q1.empty())
            return;
 
        // Leave one element in q1 and
        // push others in q2.
        while (q1.size() != 1) {
            q2.push(q1.front());
            q1.pop();
        }
 
        // Pop the only left element
        // from q1
        q1.pop();
 
        // swap the names of two queues
        queue<int> q = q1;
        q1 = q2;
        q2 = q;
    }
 
    void push(int x) { q1.push(x); }
 
    int top()
    {
        if (q1.empty())
            return -1;
 
        while (q1.size() != 1) {
            q2.push(q1.front());
            q1.pop();
        }
 
        // last pushed element
        int temp = q1.front();
 
        // to empty the auxiliary queue after
        // last operation
        q1.pop();
 
        // push last element to q2
        q2.push(temp);
 
        // swap the two queues names
        queue<int> q = q1;
        q1 = q2;
        q2 = q;
        return temp;
    }
 
    int size() { return q1.size(); }
};
 
// Driver code
int main()
{
    Stack s;
    s.push(1);
    s.push(2);
    s.push(3);
 
    cout << "current size: " << s.size() << endl;
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    cout << "current size: " << s.size() << endl;
    return 0;
}
// This code is contributed by Chhavi

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// Stack Class that acts as a queue
class Stack {
 
    queue<int> q;
 
public:
    void push(int data);
    void pop();
    int top();
    int size();
    bool empty();
};
 
// Push operation
void Stack::push(int data)
{
    //  Get previous size of queue
    int s = q.size();
 
    // Push the current element
    q.push(data);
 
    // Pop all the previous elements and put them after
    // current element
 
    for (int i = 0; i < s; i++) {
        // Add the front element again
        q.push(q.front());
 
        // Delete front element
        q.pop();
    }
}
 
// Removes the top element
void Stack::pop()
{
    if (q.empty())
        cout << "No elements\n";
    else
        q.pop();
}
 
// Returns top of stack
int Stack::top() { return (q.empty()) ? -1 : q.front(); }
 
// Returns true if Stack is empty else false
bool Stack::empty() { return (q.empty()); }
 
int Stack::size() { return q.size(); }
 
int main()
{
    Stack st;
    st.push(1);
    st.push(2);
    st.push(3);
    cout << "current size: " << st.size() << "\n";
    cout << st.top() << "\n";
    st.pop();
    cout << st.top() << "\n";
    st.pop();
    cout << st.top() << "\n";
    cout << "current size: " << st.size();
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
struct QNode {
    int data;
    QNode* next;
    QNode(int d)
    {
        data = d;
        next = NULL;
    }
};
 
struct Queue {
    QNode *front, *rear;
    Queue() { front = rear = NULL; }
 
    void enQueue(int x)
    {
 
        // Create a new LL node
        QNode* temp = new QNode(x);
 
        // If queue is empty, then
        // new node is front and rear both
        if (rear == NULL) {
            front = rear = temp;
            return;
        }
 
        // Add the new node at
        // the end of queue and change rear
        rear->next = temp;
        rear = temp;
    }
 
    // Function to remove
    // a key from given queue q
    void deQueue()
    {
        // If queue is empty, return NULL.
        if (front == NULL)
            return;
 
        // Store previous front and
        // move front one node ahead
        QNode* temp = front;
        front = front->next;
 
        // If front becomes NULL, then
        // change rear also as NULL
        if (front == NULL)
            rear = NULL;
 
        delete (temp);
    }
};
 
// Driven Program
int main()
{
 
    Queue q;
    q.enQueue(10);
    q.enQueue(20);
    q.deQueue();
    q.deQueue();
    q.enQueue(30);
    q.enQueue(40);
    q.enQueue(50);
    q.deQueue();
    cout << "Queue Front : " << (q.front)->data << endl;
    cout << "Queue Rear : " << (q.rear)->data;
}
// This code is contributed by rathbhupendra

Time complexity: constant
Space complexity: constant
// Program to print BFS traversal from a given
// source vertex. BFS(int s) traverses vertices
// reachable from s.
#include<bits/stdc++.h>
using namespace std;
 
// This class represents a directed graph using
// adjacency list representation
class Graph
{
    int V;    // No. of vertices
 
    // Pointer to an array containing adjacency
    // lists
    vector<list<int>> adj;  
public:
    Graph(int V);  // Constructor
 
    // function to add an edge to graph
    void addEdge(int v, int w);
 
    // prints BFS traversal from a given source s
    void BFS(int s); 
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj.resize(V);
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
void Graph::BFS(int s)
{
    // Mark all the vertices as not visited
    vector<bool> visited;
    visited.resize(V,false);
 
    // Create a queue for BFS
    list<int> queue;
 
    // Mark the current node as visited and enqueue it
    visited[s] = true;
    queue.push_back(s);
 
    while(!queue.empty())
    {
        // Dequeue a vertex from queue and print it
        s = queue.front();
        cout << s << " ";
        queue.pop_front();
 
        // Get all adjacent vertices of the dequeued
        // vertex s. If a adjacent has not been visited,
        // then mark it visited and enqueue it
        for (auto adjecent: adj[s])
        {
            if (!visited[adjecent])
            {
                visited[adjecent] = true;
                queue.push_back(adjecent);
            }
        }
    }
}
 
// Driver program to test methods of graph class
int main()
{
    // Create a graph given in the above diagram
    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 << "Following is Breadth First Traversal "
         << "(starting from vertex 2) \n";
    g.BFS(2);
 
    return 0;
}

Time complexity: linear
Space complexity: linear