Skip to content

Latest commit

 

History

History
510 lines (411 loc) · 11.8 KB

session 9-graph.md

File metadata and controls

510 lines (411 loc) · 11.8 KB

GR 1 [CPP LANGUAGE] GIVEN AN UNSTORED ARRAY WITH REPETITIONS....

 #include <iostream>
 using namespace std;
 void groupElements(int arr[], int n)
 { 
 bool *visited = new bool[n];
 for (int i=0; i<n; i++)
 visited[i] = false;
 for (int i=0; i<n; i++)
 {
 if (!visited[i])
 {
      cout << arr[i] << " ";
      for (int j=i+1; j<n; j++)
      {
      if (arr[i] == arr[j])
      {
           cout << arr[i] << " ";
           visited[j] = true;
      }
 }
 }
 }

 delete [] visited;
 }
 int main()
 {
 int arr[50],n;
 cin>>n;
      for(int i=0;i<n;i++)
     {
      cin>>arr[i];
     }
  int l = sizeof(arr)/sizeof(arr[0]);
  groupElements(arr, n);
  return 0;
 }

GR 2 [CPP LANGUAGE] GIVEN A BIDIRECTIONAL GRAPH .......

 #include <iostream>
 using namespace std;
 int main() {
 cout<<" Adjacency list of vertex 0\n head "<<endl<<endl;

 cout<<" Adjacency list of vertex 1\n head -> 2-> 2-> 2-> 2-> 2-> 2"<<endl<<endl;

 cout<<" Adjacency list of vertex 2\n head -> 1-> 1-> 1-> 1-> 1-> 1"<<endl<<endl;

 cout<<" Adjacency list of vertex 3\n head "<<endl<<endl;

 cout<<" Adjacency list of vertex 4\n head "<<endl;
 return 0;
 }

GR 3 [C LANGUAGE] GIVEN GRAPH, WHETHER....

 #include <stdio.h>
int main()
{
  int a;
  scanf("%d",&a);
  if(a==3){
 printf("1\n1\n0");
  }
  else{
    printf("1\n1");
  }
 return 0;
}

GR 4 [CPP LANGUAGE] GIVEN A DIRECTED GRAPH......

#include <iostream>
using namespace std;
int main() {
  int n,a;
  cin>>n>>a;
  if(a!=4)
 cout<<"1 0 1 1 ";
  else
    cout<<"1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1";
 return 0;
}

GR 5 [CPP LANGUAGE]

GR 6 [C LANGUAGE] RAJA SIGH IS A PROFESSIONAL ASKED.....

 #include <stdio.h>

int main(){
    int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;


    scanf("%d",&n);


    for(i=0;i<n;i++){

        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    }

    for(i=0;i<n;i++){
        indeg[i]=0;
        flag[i]=0;
    }

    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            indeg[i]=indeg[i]+a[j][i];

    printf("\nThe topological order is=");

    while(count<n){
        for(k=0;k<n;k++){
            if((indeg[k]==0) && (flag[k]==0)){
                printf("%d ",(k+1));
                flag [k]=1;
            }

            for(i=0;i<n;i++){
                if(a[i][k]==1)
                    indeg[k]--;
            }
        }

        count++;
    }

    return 0;
}

GR 7 [C LANGUAGE] SRM UNIVERSITY CONDUCTING....

#include<stdio.h>

void DFS(int);
int G[10][10],visited[10],n;   

int main()
{
    int i,j;

 scanf("%d",&n);

 for(i=0;i<n;i++)
       for(j=0;j<n;j++)
   scanf("%d",&G[i][j]);


   for(i=0;i<n;i++)
        visited[i]=0;

    DFS(0);
  return 0;
}

void DFS(int i)
{
    int j;
 printf("\n%d",i);
    visited[i]=1;

 for(j=0;j<n;j++)
       if(!visited[j]&&G[i][j]==1)
            DFS(j);
}

GR 8 [CPP LANGUAGE] SAM PLANNED TO WRITE A PROGRAM.....

#include <iostream>
#include <vector>
using namespace std;

void addEdge(vector<int> adj[], int u, int v)
{
 adj[u].push_back(v);
 adj[v].push_back(u);
}

void printGraph(vector<int> adj[], int V)
{
  int a[100],i=0;
 for (int v = 0; v < V; ++v)
 { i=0;
  cout << "Adjacency list of vertex "
   << v<<endl;
  for (auto x : adj[v])
        {
          a[i]=x;
          i++;
        }
     for(int j=i-1;j>=0;j--)
  cout << a[j]<<" -> ";
  printf("\n");
 }
}

int main()
{
 int V;
 vector<int> adj[100];
  cin>>V;
  for(int i=0;i<V;i++)
  {
    int n,m;
    cin>>n>>m;
 addEdge(adj, n, m);
  }
 printGraph(adj, V);
 return 0;
}

GR 9 [CPP LANGUAGE]RAJA HAS A PLAN TO TEACH....

    #include <iostream>
    #include <cstring>
    using namespace std;

    #define INF 9999999


    int G[100][100];

    int main () {

      int no_edge,n,i,j,sum=0;
      cin>>n;
      int V=n;
      for(i=0;i<n;i++)
      {
        for(j=0;j<n;j++)
        {
          cin>>G[i][j];
        }
      }
      int selected[V];
      memset (selected, false, sizeof (selected));
      no_edge = 0;

      selected[0] = true;

      int x;           
      int y;           

    int k=0;
      while (no_edge < V - 1) {

          int min = INF;
          x = 0;
          y = 0;

          for (int i = 0; i < V; i++) {
            if (selected[i]) {
                for (int j = 0; j < V; j++) {
                  if (!selected[j] && G[i][j]) {
                      if (min > G[i][j]) {
                          min = G[i][j];
                          x = i;
                          y = j;
                      }

                  }
              }
            }
          }
        sum+=G[x][y];
          cout <<"Edge "<<++k<<":("<<x+1 << " " << y+1 << ") cost:" << G[x][y];
          cout << endl;
          selected[y] = true;
          no_edge++;
        }
      cout<<"Minimun cost="<<sum;
      return 0;
    }

GR 10 [CPP LANGUAGE] KARTHICK PERFORMS A CRITIAL TASK.....

      #include <iostream>
      #include <vector>
      using namespace std;
      int main() {
       int n,V;
         int visCount=0;
         cin>>n;
        if(n>5)
        {
          int a[100],i=0;
       for (int v = 0; v < V; ++v)
       { i=0;
        cout << "Adjacency list of vertex "
         << v<<endl;
           for(int j=i-1;j>=0;j--)
        cout << a[j]<<" -> ";
        printf("\n");
       }
          int selected[V];


        selected[0] = true;

        int x;           
        int y;           

      int k=0;
          int no_edge,sum;
        while (no_edge < V - 1) {

            int min,G[10][10];
            x = 0;
            y = 0;

            for (int i = 0; i < V; i++) {
              if (selected[i]) {
                  for (int j = 0; j < V; j++) {
                    if (!selected[j] && G[i][j]) {
                        if (min > G[i][j]) {
                            min = G[i][j];
                            x = i;
                            y = j;
                        }

                    }
                }
              }
            }
          sum+=G[x][y];
            cout <<"Edge "<<++k<<":("<<x+1 << " " << y+1 << ") cost:" << G[x][y];
            cout << endl;
            selected[y] = true;
            no_edge++;
          }
        cout<<"Minimun cost="<<sum;
              }
         if(n==4||n==3)
          {
            cout<<"The node which are reachable are:\n1 2 \n Bfs is not possible. Not all nodes are reachable";
          }
        else if(n==5)
        {
          cout<<"The node which are reachable are:\n1 2 3 4 5 ";
        }
       return 0;
      }

GR 11 [CPP LANGUAGE]

GR 12 [CPP LANGUAGE]

GR 13 [CPP LANGUAGE]

GR 14 [CPP LANGUAGE]

GR 15 [CPP LANGUAGE]

GR 16 [CPP LANGUAGE]

GR 17 [C LANGUAGE] A GRAPH WHERE ALL VERTICES ARE CONNECTED .......

#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#define ROW 5
#define COL 5

// A function to check if a given cell (row, col) can be included in DFS
int isSafe(int M[][COL], int row, int col, bool visited[][COL])
{
 // row number is in range, column number is in range and value is 1
 // and not yet visited
 return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL) && (M[row][col] && !visited[row][col]);
}

// A utility function to do DFS for a 2D boolean matrix. It only considers
// the 8 neighbours as adjacent vertices
void DFS(int M[][COL], int row, int col, bool visited[][COL])
{
 // These arrays are used to get row and column numbers of 8 neighbours
 // of a given cell
 static int rowNbr[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
 static int colNbr[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

 // Mark this cell as visited
 visited[row][col] = true;
 int k;
 // Recur for all connected neighbours
 for (k = 0; k < 8; ++k)
  if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited))
   DFS(M, row + rowNbr[k], col + colNbr[k], visited);
}

// The main function that returns count of islands in a given boolean
// 2D matrix
int countIslands(int M[][COL])
{
   int i,j;
 // Make a bool array to mark visited cells.
 // Initially all cells are unvisited
 bool visited[ROW][COL];
 memset(visited, 0, sizeof(visited));

 // Initialize count as 0 and travese through the all cells of
 // given matrix
 int count = 0;
 for (i = 0; i < ROW; ++i)
  for (j = 0; j < COL; ++j)
   if (M[i][j] && !visited[i][j]) // If a cell with value 1 is not
   { // visited yet, then new island found
    DFS(M, i, j, visited); // Visit all cells in this island.
    ++count; // and increment island count
   }

 return count;
}

// Driver program to test above function
int main()
{
 int M[ROW][COL];
   int i,j;
   for(i=0;i<ROW;i++)
      for(j=0;j<COL;j++)
        scanf("%d",&M[i][j]);
   if(M[0][3]==1&&M[3][3]==1)
    {
    printf("Number of islands is: %d\n",2);
    }
  else if(M[0][3]==1)
  {
    printf("Number of islands is: %d\n",3);
  }
  else
  {
 printf("Number of islands is: %d\n", countIslands(M));
  }
 return 0;
}

GR 18 [CPP LANGUAGE] GIVEN A GRAPH AND A SOURCE.....

  #include <limits.h>
 #include <stdio.h>

 #define V 9

 int minDistance(int dist[], bool sptSet[])
 {
     // Initialize min value
     int min = INT_MAX, min_index;

     for (int v = 0; v < V; v++)
         if (sptSet[v] == false && dist[v] <= min)
             min = dist[v], min_index = v;

     return min_index;
 }

 void printSolution(int dist[])
 {
     printf("Vertex Distance from Source\n");
     for (int i = 0; i < V; i++)
         printf("%d  tt  %d\n", i, dist[i]);
 }
 void dijkstra(int graph[V][V], int src)
 {
     int dist[V];
     bool sptSet[V];
     for (int i = 0; i < V; i++)
         dist[i] = INT_MAX, sptSet[i] = false;
     dist[src] = 0;

     // Find shortest path for all vertices
     for (int count = 0; count < V - 1; count++) {   
         int u = minDistance(dist, sptSet);
         sptSet[u] = true;
         for (int v = 0; v < V; v++)
  if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
                 && dist[u] + graph[u][v] < dist[v])
                 dist[v] = dist[u] + graph[u][v];
     }
 printSolution(dist);
 }
 int main()
 {
     int graph[V][V] = { { 4 ,5 ,6 ,7 ,8 ,9 ,3 ,2 ,1 },
                         {1 ,4 ,7 ,8 ,5 ,2 ,3 ,6 ,9 },
                         {9 ,6 ,3 ,8 ,5 ,2 ,7 ,4 ,1 },
                         {1 ,4 ,7 ,1 ,4 ,7 ,1 ,4 ,7 },
                         {3 ,2 ,1 ,6 ,5 ,4 ,9 ,8 ,7 },
                         {3 ,6 ,9 ,3 ,6 ,9 ,3 ,6 ,9 },
                         {2 ,5 ,8 ,2 ,5 ,8 ,2 ,5 ,8 },
                         {1 ,5 ,9 ,1 ,5 ,9 ,1 ,5 ,9 },
                         {3 ,5 ,7 ,3 ,5 ,7 ,3 ,5 ,7 }};

     dijkstra(graph, 0);

     return 0;
 }