Advertisement
Guest User

yhj

a guest
Mar 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. //... A Program to represent a Graph by using an Adjacency Matrix method
  2. # include < stdio.h >
  3. # include < conio.h >
  4. void main()
  5. {
  6. int option;
  7. clrscr();
  8. do
  9. { printf("\n A Program to represent a Graph by using an ");
  10. printf("Adjacency Matrix method \n ");
  11. printf("\n 1. Directed Graph ");
  12. printf("\n 2. Un-Directed Graph ");
  13. printf("\n 3. Exit ");
  14. printf("\n\n Select a proper option : ");
  15. scanf("%d", &option);
  16. switch(option)
  17. {
  18. case 1 : dir_graph();
  19. break;
  20. case 2 : undir_graph();
  21. break;
  22. case 3 : exit(0);
  23. } // switch
  24. }while(1);
  25. } // main
  26. int dir_graph()
  27. {
  28. int adj_mat[50][50];
  29. int n;
  30. int in_deg, out_deg, i, j;
  31. printf("\n How Many Vertices ? : ");
  32. scanf("%d", &n);
  33. read_graph (adj_mat, n);
  34. printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
  35. for (i = 1; i < = n ; i++ )
  36. {
  37. in_deg = out_deg = 0;
  38. for ( j = 1 ; j <= n ; j++ )
  39. {
  40. if ( adj_mat[j][i] == 1 )
  41. in_deg++;
  42. } // for
  43. for ( j = 1 ; j <= n ; j++ )
  44. if (adj_mat[i][j] == 1 )
  45. out_deg++;
  46. 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);
  47. } // for
  48. return;
  49. } // dir_graph
  50. int undir_graph()
  51. {
  52. int adj_mat[50][50];
  53. int deg, i, j, n;
  54. printf("\n How Many Vertices ? : ");
  55. scanf("%d", &n);
  56. read_graph(adj_mat, n);
  57. printf("\n Vertex \t Degree ");
  58. for ( i = 1 ; i <= n ; i++ )
  59. {
  60. deg = 0;
  61. for ( j = 1 ; j <= n ; j++ )
  62. if ( adj_mat[i][j] == 1)
  63. deg++;
  64. printf("\n\n %5d \t\t %d\n\n", i, deg);
  65. } // for
  66. return;
  67. } // undir_graph
  68. int read_graph ( int adj_mat[50][50], int n )
  69. {
  70. int i, j;
  71. char reply;
  72. for ( i = 1 ; i <= n ; i++ )
  73. {
  74. for ( j = 1 ; j <= n ; j++ )
  75. {
  76. if ( i == j )
  77. {
  78. adj_mat[i][j] = 0;
  79. continue;
  80. } // if
  81. printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j);
  82. flushall();
  83. scanf("%c", &reply);
  84. if ( reply == 'y' || reply == 'Y' )
  85. adj_mat[i][j] = 1;
  86. else
  87. adj_mat[i][j] = 0;
  88. } // for
  89. } // for
  90. return;
  91. } // read_graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement