Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1. #include<cstdlib>
  2. #define NULL_VALUE -999999
  3. #define INFINITY 999999
  4. #include<iostream>
  5.  
  6.  
  7. #define WHITE 1
  8. #define GREY 2
  9. #define BLACK 3
  10.  
  11. using namespace std;
  12.  
  13.  
  14. class Queue
  15. {
  16.     int queueInitSize ;
  17.     int queueMaxSize;
  18.     int * data;
  19.     int length;
  20.     int front;
  21.     int rear;
  22. public:
  23.     Queue();
  24.     ~Queue();
  25.     void enqueue(int item);
  26.     int dequeue();
  27.     bool empty();
  28. };
  29.  
  30. Queue::Queue()
  31. {
  32.     queueInitSize = 2 ;
  33.     queueMaxSize = queueInitSize;
  34.     data = new int[queueMaxSize];
  35.     length = 0 ;
  36.     front = 0;
  37.     rear = 0;
  38. }
  39.  
  40.  
  41. void Queue::enqueue(int item)
  42. {
  43.     if (length == queueMaxSize)
  44.     {
  45.         int * tempData ;
  46.         queueMaxSize = 2 * queueMaxSize ;
  47.         tempData = new int[queueMaxSize] ;
  48.         int i, j;
  49.         j = 0;
  50.         for( i = rear; i < length ; i++ )
  51.         {
  52.             tempData[j++] = data[i] ;
  53.         }
  54.         for( i = 0; i < rear ; i++ )
  55.         {
  56.             tempData[j++] = data[i] ;
  57.         }
  58.         rear = 0 ;
  59.         front = length ;
  60.         delete[] data ;
  61.         data = tempData ;
  62.     }
  63.  
  64.     data[front] = item ;
  65.     front = (front + 1) % queueMaxSize ;
  66.     length++ ;
  67. }
  68.  
  69.  
  70. bool Queue::empty()
  71. {
  72.     if(length == 0) return true ;
  73.     else return false ;
  74. }
  75.  
  76.  
  77. int Queue::dequeue()
  78. {
  79.     if(length == 0) return NULL_VALUE ;
  80.     int item = data[rear] ;
  81.     rear = (rear + 1) % queueMaxSize ;
  82.     length-- ;
  83.     return item ;
  84. }
  85.  
  86.  
  87. Queue::~Queue()
  88. {
  89.     if(data)
  90.     {
  91.         delete[] data;
  92.     }
  93.     data = 0;
  94. }
  95.  
  96.  
  97. class Graph
  98. {
  99.     int nVertices, nEdges ;
  100.     bool directed ;
  101.     int ** matrix ;
  102.     int *parent, *color, *Distance;
  103.  
  104. public:
  105.     Graph(bool dir = false);
  106.     ~Graph();
  107.     void setnVertices(int n);
  108.     void addEdge(int u, int v);
  109.     void removeEdge(int u, int v);
  110.     bool isEdge(int u, int v);
  111.     int getDegree(int u, bool out);
  112.     void printAdjVertices(int u);
  113.     bool hasCommonAdjacent(int u, int v);
  114.     int getDist(int u, int v);
  115.     void printGraph();
  116.     void bfs(int source);
  117.     int getOutDegree(int u);
  118.     int getInDegree(int u);
  119. };
  120.  
  121.  
  122. Graph::Graph(bool dir)
  123. {
  124.     nVertices = 0 ;
  125.     nEdges = 0 ;
  126.     matrix = 0 ;
  127.     directed = dir ;
  128.  
  129. }
  130.  
  131. void Graph::setnVertices(int n)
  132. {
  133.     this->nVertices = n ;
  134.  
  135.     matrix = new int*[nVertices];
  136.     for(int i=0;i<nVertices;i++)
  137.     {
  138.         matrix[i] = new int[nVertices];
  139.         for(int j=0;j<nVertices;j++)
  140.             matrix[i][j] = 0;
  141.     }
  142.     parent = new int[nVertices];
  143.     color = new int[nVertices];
  144.     Distance = new int[nVertices];
  145.  
  146. }
  147.  
  148. void Graph::addEdge(int u, int v)
  149. {
  150.     if(u<0 || u>=nVertices || v<0 || v>=nVertices){
  151.             return;
  152.     }
  153.  
  154.     if(directed == false){
  155.         matrix[u][v] = 1;
  156.         matrix[v][u] = 1;
  157.     }
  158.     else{
  159.         matrix[u][v] = 1;
  160.     }
  161.  
  162. }
  163.  
  164. void Graph::removeEdge(int u, int v)
  165. {
  166.     if(isEdge(u,v)==false)
  167.         return;
  168.     else{
  169.         this->nEdges--;
  170.         if(directed == false){
  171.         matrix[u][v] = 0;
  172.         matrix[v][u] = 0;
  173.         }
  174.         else{
  175.         matrix[u][v] = 0;
  176.         }
  177.     }
  178. }
  179.  
  180. bool Graph::isEdge(int u, int v)
  181. {
  182.     if(matrix[u][v]==1)
  183.         return true;
  184.     else if (matrix[v][u]==1)
  185.         return true;
  186.     else
  187.         return false;
  188. }
  189.  
  190. /*int Graph::getDegree(int u, bool out)
  191. {
  192.     ///returns the degree of vertex u
  193.     int count = 0;
  194.     if (out==true){
  195.         for(int i=0;i<nVertices;i++){
  196.             if(matrix[u][i]==1)
  197.                 count++;
  198.         }
  199.     }
  200.     else{
  201.         for(int i=0;i<nVertices;i++){
  202.             if(matrix[i][u]==1)
  203.                 count++;
  204.         }
  205.     }
  206.     return count;
  207.  
  208. }*/
  209. int Graph::getOutDegree(int u)
  210. {
  211.     int count = 0;
  212.     for(int i=0;i<nVertices; i++){
  213.         if(matrix[u][i]==1){
  214.             count++;
  215.         }
  216.     }
  217.     return count;
  218. }
  219.  
  220. int Graph:: getInDegree(int u)
  221. {
  222.     int count = 0;
  223.     for(int i=0;i<nVertices;i++){
  224.         if(matrix[i][u]==1){
  225.             count++;
  226.         }
  227.     }
  228.     return count;
  229. }
  230. void Graph::printAdjVertices(int u)
  231. {
  232.     for(int i=0;i<nVertices;i++){
  233.         if(matrix[u][i]==1){
  234.             cout<<i<<" ";
  235.         }
  236.     }
  237.     cout<<endl;
  238. }
  239.  
  240. bool Graph::hasCommonAdjacent(int u, int v)
  241. {
  242.     for(int i=0;i<nVertices;i++){
  243.         for(int j=0;j<nVertices;j++){
  244.             if(matrix[u][i]==matrix[v][i])
  245.                 return true;
  246.         }
  247.     }
  248.     return false;
  249.  
  250. }
  251.  
  252. void Graph::bfs(int source)
  253. {
  254.     ///write this function
  255.     Queue Q;
  256.  
  257.     for(int i=0;i<nVertices;i++){
  258.         color[i] = WHITE;
  259.         parent[i] = NULL;
  260.         Distance[i] = 0;
  261.     }
  262.     Q.enqueue(source);
  263.     color[source] = GREY;
  264.     while(!Q.empty()){
  265.         int current = Q.dequeue();
  266.         for(int a =0;a<nVertices;a++){
  267.                 if(isEdge(current,a) && color[a]==WHITE){
  268.                     Q.enqueue(a);
  269.                     color[a] = Distance[current]+1;
  270.                     parent[a] =current;
  271.                 }
  272.         }
  273.         color[current] = BLACK;
  274.     }
  275.  
  276.  
  277. }
  278.  
  279. int Graph::getDist(int u, int v)
  280. {
  281.     ///returns the shortest path distance from u to v
  282.     ///must call bfs using u as the source vertex, then use distance array to find the distance
  283.     bfs(u);
  284.     return Distance[v];
  285.     //return INFINITY ;
  286. }
  287.  
  288.  
  289. void Graph::printGraph()
  290. {
  291.     printf("\nNumber of vertices: %d, Number of edges: %d\n", nVertices, nEdges);
  292.     for(int i=0;i<nVertices;i++)
  293.     {
  294.         printf("%d:", i);
  295.         for(int j=0; j<nVertices;j++)
  296.         {
  297.             if(matrix[i][j]==1)
  298.                 printf(" %d", j);
  299.         }
  300.         printf("\n");
  301.     }
  302. }
  303.  
  304. Graph::~Graph()
  305. {
  306.     ///write your destructor here
  307.     for(int i=0;i<nVertices;i++){
  308.         delete[]matrix[i];
  309.     }
  310.     delete[]matrix;
  311.       if(parent) delete []parent;
  312.     parent=0;
  313.     if(color) delete []color;
  314.     color=0;
  315.     if(Distance) delete []Distance;
  316.     Distance=0;
  317.  
  318. }
  319.  
  320.  
  321. ///**********************Graph class ends here******************************
  322.  
  323.  
  324. ///******main function to test your code*************************
  325. int main(void)
  326. {
  327.     int n;
  328.     int choice;
  329.     bool dir;
  330.     printf("Enter your choice:\n");
  331.     printf("1. directed graph   2. undirected graph\n");
  332.     scanf("%d",&choice);
  333.     if(choice == 1)dir = true;
  334.     else if(choice == 2)dir = false;
  335.  
  336.     Graph g(dir);
  337.     printf("Enter number of vertices: ");
  338.     scanf("%d", &n);
  339.     g.setnVertices(n);
  340.  
  341.     while(1)
  342.     {
  343.         cout<<"1. Add edge"<<endl;
  344.         cout<<"2. Remove edge"<<endl;
  345.         cout<<"3. Check if Edge"<<endl;
  346.         cout<<"4. Get Degree"<<endl;
  347.         cout<<"5. Check Adjacent Vertices"<<endl;
  348.         cout<<"6. Print Adjacent Vertices"<<endl;
  349.         cout<<"7. Shortest distance"<<endl;
  350.         cout<<"8. Print Graph"<<endl;
  351.         cout<<"9. Exit"<<endl;
  352.  
  353.         int ch;
  354.         scanf("%d",&ch);
  355.         if(ch==1)
  356.         {
  357.             cout<<"Add Edge"<<endl;
  358.             cout<<"Enter the edges"<<endl;
  359.             int u, v;
  360.             scanf("%d%d", &u, &v);
  361.             g.addEdge(u, v);
  362.         }
  363.         else if(ch==2)
  364.         {
  365.             cout<<"Remove Edge"<<endl;
  366.             cout<<"Enter the edges"<<endl;
  367.             int u, v;
  368.             cin>>u>>v;
  369.             g.removeEdge(u,v);
  370.  
  371.  
  372.         }
  373.         else if(ch==3)
  374.         {
  375.             cout<<"Check if Edge"<<endl;
  376.             cout<<"Enter the edges"<<endl;
  377.             int u, v;
  378.             cin>>u>>v;
  379.             if(g.isEdge(u,v))
  380.                 cout<<"Edge"<<endl;
  381.             else
  382.                 cout<<"No edge"<<endl;
  383.  
  384.         }
  385.         else if(ch==4)
  386.         {
  387.             cout<<"Get Degree"<<endl;
  388.             int u;
  389.             cin>>u;
  390.             cout<<g.getDegree(u, true)<<endl;
  391.         }
  392.         else if(ch==5)
  393.         {
  394.             cout<<"Check Adjacent"<<endl;
  395.             int u, v;
  396.             cin>>u>>v;
  397.             if(g.hasCommonAdjacent(u,v))
  398.                 cout<<"Has adjacent vertices"<<endl;
  399.             else
  400.                 cout<<"Has no adjacent vertices"<<endl;
  401.         }
  402.         else if (ch==6)
  403.         {
  404.             cout<<"Print adjacent vertices"<<endl;
  405.             int u;
  406.             cin>>u;
  407.             cout<<endl;
  408.             g.printAdjVertices(u);
  409.             cout<<endl;
  410.         }
  411.         else if (ch==7)
  412.         {
  413.             cout<<"Shortest distance"<<endl;
  414.             int u,v;
  415.             cin>>u>>v;
  416.             cout<<g.getDist(u,v)<<endl;
  417.         }
  418.         else if (ch==8)
  419.         {
  420.             cout<<"Print Graph"<<endl;
  421.             g.printGraph();
  422.         }
  423.         else if (ch==9)
  424.         {
  425.             break;
  426.         }
  427.     }
  428.  
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement