Advertisement
Mr_kindle

AdjacencyMatrixAllOperation

Oct 21st, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 20
  4.  
  5. int graph_type,n,max_edges,i,j,origin,destination;
  6. int adj[MAX][MAX];
  7. void create_graph();
  8. void display();
  9. void insert_edge(int, int);
  10. void delete_edge(int, int);
  11.  
  12. int main()
  13. {   int choice;
  14.     create_graph();
  15.     while(1)
  16.     {   printf("1.Insert an edge: \n");
  17.         printf("2.Delete an edge: \n");
  18.         printf("3.Display: \n");
  19.         printf("4.Exit: \n");
  20.         printf("Enter your choice:  ");
  21.         scanf("%d", & choice);
  22.         switch(choice)
  23.             {   case 1: printf("Enter an edge to be inserted:  ");
  24.                         scanf("%d %d",&origin,&destination);
  25.                         insert_edge(origin,destination);
  26.                         break;
  27.                 case 2: printf("Enter an edge to be deleted:  ");
  28.                         scanf("%d %d",&origin,&destination);
  29.                         delete_edge(origin,destination);
  30.                         break;
  31.                 case 3: display();
  32.                         break;
  33.                 case 4: exit(1);
  34.                 default:printf("Wrong choice \n");
  35.                         break;
  36.             }//switch
  37.     }//while
  38.     return 0;
  39. }//main
  40.  
  41. void create_graph()
  42. {
  43.     printf("Enter Graph type '1' for undirected and '2' for directed graph:  ");
  44.     scanf("%d",&graph_type);
  45.     printf("Enter number of vertices:  ");
  46.     scanf("%d",&n);
  47.     if (graph_type == 1)
  48.         max_edges = n*(n-1)/2;
  49.     else
  50.         max_edges = n*(n-1);
  51.        
  52.     for(i=0;i<=max_edges;i++)
  53.     {   printf("Enter edges (origin & destination), Enter -1 -1 to exit:  ");
  54.         //enter value as 'int' space 'int' because here is '%d' space '%d'
  55.         scanf("%d %d",&origin,&destination);
  56.         if(origin == -1 && destination == -1)
  57.             break;
  58.         if(origin>=n || destination >= n || origin < 0 || destination < 0)//checking for limitations
  59.         {   printf("Invalid data please enter again: \n");
  60.             i--;
  61.         }
  62.         else
  63.             adj[origin][destination] = 1;
  64.         if(graph_type == 1) // since for undirected graph origin & destination are exchangable
  65.             adj[destination][origin] =1;
  66.     }
  67. }//end of create_graph
  68.  
  69. void display()
  70. {   printf("\nThe adjacent matrix is following:  \n\n");
  71.     for(i=0;i<n;i++)
  72.     {
  73.             for(j=0;j<n;j++)
  74.             printf("%4d",adj[i][j]);
  75.             printf("\n");
  76.         }
  77. }//end of display
  78.  
  79. void insert_edge(int origin, int destination)
  80. {   if(origin < 0 || origin >= n)
  81.     {   printf("Origin does not exist:  \n");
  82.         return;
  83.     }  
  84.     if(destination < 0 || destination >= n)
  85.     {   printf("Destination does not exist:  \n");
  86.         return;
  87.     }
  88.     adj[origin][destination] = 1;
  89.     if (graph_type == 1)
  90.         adj[destination][origin] =1;   
  91. }//end of insert_edge
  92.  
  93. void delete_edge(int origin, int destination)
  94. {   if(origin < 0 || origin >= n || destination < 0 || destination >= n || adj[origin][destination]==0)
  95.     {   printf("This edge does not exist: \n");
  96.         return;
  97.     }
  98.     adj[origin][destination] = 0;
  99.     if (graph_type == 1)
  100.     adj[destination][origin] =0;
  101. }//end of delete_edge
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement