Advertisement
Naimul_X

Untitled

Sep 22nd, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. ************************************Hasan's Code**********************************
  2.  
  3.  
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8. int dir_graph();
  9. int undir_graph();
  10. int read_graph(int adj_mat[50][50], int n );
  11.  
  12. int main()
  13. {
  14. cout<<"\n\n Graph Adjacency Matrix By Naimul Karim Hredoy \n\n";
  15. int o;
  16. do
  17. {
  18.  
  19. cout<<"\n CHOOSE A OPTION ";
  20. cout<<"\n 1. Directed Graph ";
  21. cout<<"\n 2. Un-Directed Graph ";
  22. cout<<"\n 3. Exit ";
  23. cout<<"\n\n Select a proper option : ";
  24. cin>>o;
  25. switch(o)
  26. {
  27. case 1 :
  28. dir_graph();
  29. break;
  30. case 2 :
  31. undir_graph();
  32. break;
  33. case 3 :
  34. exit(0);
  35. } // switch
  36. }
  37. while(1);
  38. }
  39.  
  40. int dir_graph()
  41. {
  42. int adj_mat[50][50];
  43. int n;
  44. int in_deg, out_deg, i, j;
  45. cout<<"\n How Many Vertices In This Graph? : ";
  46. scanf("%d", &n);
  47. read_graph(adj_mat, n);
  48. cout<<"\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ";
  49. for (i = 1; i <= n ; i++ )
  50. {
  51. in_deg = out_deg = 0;
  52. for ( j = 1 ; j <= n ; j++ )
  53. {
  54. if ( adj_mat[j][i] == 1 )
  55. in_deg++;
  56. }
  57. for ( j = 1 ; j <= n ; j++ )
  58. if (adj_mat[i][j] == 1 )
  59. out_deg++;
  60. printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
  61. }
  62. return 0;
  63. }
  64.  
  65. int undir_graph()
  66. {
  67. int adj_mat[50][50];
  68. int deg, i, j, n;
  69. printf("\n How Many Vertices ? : ");
  70. scanf("%d", &n);
  71. read_graph(adj_mat, n);
  72. printf("\n Vertex \t Degree ");
  73. for ( i = 1 ; i <= n ; i++ )
  74. {
  75. deg = 0;
  76. for ( j = 1 ; j <= n ; j++ )
  77. if ( adj_mat[i][j] == 1)
  78. deg++;
  79. printf("\n\n %5d \t\t %d\n\n", i, deg);
  80. }
  81. return 0;
  82. }
  83.  
  84. int read_graph ( int adj_mat[50][50], int n )
  85. {
  86. int i, j;
  87. char reply;
  88. for ( i = 1 ; i <= n ; i++ )
  89. {
  90. for ( j = 1 ; j <= n ; j++ )
  91. {
  92. if ( i == j )
  93. {
  94. adj_mat[i][j] = 0;
  95. continue;
  96. }
  97. printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j);
  98.  
  99. cin>>reply;
  100. if ( reply == 'y' || reply == 'Y' )
  101. adj_mat[i][j] = 1;
  102. else
  103. adj_mat[i][j] = 0;
  104. }
  105. }
  106. return 0;
  107. }
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement