Advertisement
HmHimu

GRAPH-ALGO(full)

May 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<list>
  3. #include<iostream>
  4. using namespace std;
  5. int graph[10][10]= {false};
  6. int counter[10];
  7. int n;
  8. list<int>adj[100];
  9. list<int>::iterator p;
  10. int main()
  11. {
  12.     int v,e1=1,e2=1;
  13.     int x,y,i,j;
  14.     printf("Enter Number of Vertex : ");
  15.     scanf("%d",&v);
  16.     i=1;
  17.     while(1)
  18.     {
  19.         printf("Edge %d : ",i);
  20.         scanf("%d %d",&e1,&e2);
  21.         if(e1==0 && e2==0)
  22.         {
  23.             break;
  24.         }
  25.         if(v>=e1 && v>=e2)
  26.         {
  27.             x=e1;
  28.             y=e2;
  29.             graph[x][y]=graph[y][x]=1;
  30.             adj[x].push_back(y);
  31.             adj[y].push_back(x);
  32.         }
  33.         else
  34.         {
  35.             cout<<"Invalid Key "<<endl;
  36.             i--;
  37.         }
  38.         i++;
  39.     }
  40.  
  41.     cout<<"Print Adjancy matrix"<<endl;
  42.     cout<<endl;
  43.     for(i=1; i<=v; i++)
  44.     {
  45.         for(j=1; j<=v; j++)
  46.         {
  47.             cout<<graph[i][j]<<" ";
  48.         }
  49.         cout<<endl;
  50.     }
  51.     cout<<endl;
  52.     cout<<"Print List Graph "<<endl;
  53.     cout<<endl;
  54.     for(i=1; i<=v; i++)
  55.     {
  56.         printf("adj[%d] : ",i);
  57.         for(p=adj[i].begin(); p!=adj[i].end(); p++)
  58.         {
  59.             cout<<"->"<<*p;
  60.         }
  61.         cout<<endl;
  62.     }
  63.  
  64.  
  65.  
  66.     cout<<"Enter a vertex Number : ";
  67.     cin>>n;
  68.     int c=0;
  69.     cout<<endl<<"Direct Link with : ";
  70.     for(i=1; i<=v; i++)
  71.     {
  72.         if(graph[n][i]==1)
  73.         {
  74.             c++;
  75.             cout<<i<<" ";
  76.         }
  77.     }
  78.     cout<<endl;
  79.     cout<<endl<<"Total connected node : "<<c<<endl;
  80.     cout<<endl<<"Total Out Degree : "<<c<<endl;
  81.     cout<<endl<<"Total In Degree : "<<c<<endl;
  82.     loop1:cout<<endl<<"Enter two vertex : ";
  83.     cin>>e1>>e2;
  84.     if(e1>v || e2>v)
  85.     {
  86.         cout<<"invalid key "<<endl;
  87.         goto loop1;
  88.     }
  89.     else
  90.     {
  91.         if(graph[e1][e2]==1)
  92.     {
  93.         cout<<"Yes!They are connected "<<endl;
  94.     }
  95.     else
  96.     {
  97.         cout<<"Not connected "<<endl;
  98.     }
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement