Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. #define N 4
  4. int viz[N];//varfuri deja vizitate
  5. struct graph {
  6. char v[N];
  7. int e[N][N];//edges
  8.  
  9. };
  10. void DFS(int x,graph G)
  11. {
  12. int i;
  13. viz[x] = 1;
  14. for (i = 0;i < N;i++)
  15. {
  16. if (G.e[x][i] != 0 && viz[i] == 0)
  17. {
  18. //cout << G.e[x][i] << " ";
  19. DFS(i, G);
  20. }
  21. //cout << endl;
  22. }
  23.  
  24. }
  25.  
  26. int Conex(graph G)
  27. {
  28. int i;
  29. DFS(0,G);
  30. for (i = 0;i < N;i++)
  31. if (viz[i] == 0)
  32. return 0;
  33. return 1;
  34. }
  35.  
  36. void printGraph(graph G)
  37. {
  38. for (int i=0;i<N+1;i++)
  39. {
  40. for (int j = 0;j < N + 1;j++)
  41. {
  42. if (i == 0 && j==0)
  43. {
  44. cout << " " << "\t";
  45. }
  46. else
  47. {
  48. if (i == 0)
  49. {
  50. cout << G.v[j-1] << "\t";
  51. }
  52. else
  53. {
  54. if (j == 0)
  55. {
  56. cout << G.v[i-1] << "\t";
  57. }
  58. else
  59. {
  60. cout << G.e[i-1][j-1] << "\t";
  61. }
  62. }
  63. }
  64.  
  65. }
  66.  
  67. cout << endl;
  68.  
  69. cout << endl;
  70. }
  71. }
  72. int checkIfOriented(graph G)
  73. {
  74. int count = 0;
  75. int rand = 0;
  76. for (int i = 0;i < N-1;i++)
  77. {
  78. for (int j = i+1;j < N;j++)
  79. {
  80. if (G.e[i][j] == G.e[j][i])
  81. {
  82. count++;
  83. //cout << G.v[i] << " - " << G.v[j] << endl;
  84. }
  85. }
  86. }
  87. count = count * 2 + N;
  88. if (count == (N*N))
  89. {
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. void printEdges(graph G)
  95. {
  96. for (int i = 0;i < N;i++)
  97. {
  98. for (int j = 0;j < N;j++)
  99. {
  100. if (G.e[i][j] !=0)
  101. {
  102. cout << G.v[i] << " - " << G.v[j] << endl;
  103. }
  104.  
  105. }
  106. cout << endl;
  107. }
  108. }
  109. int main()
  110. {
  111. /*
  112. {{0,1,1,1},
  113. {1,0,0,1},
  114. {1,0,0,1},
  115. {1,1,1,1}}
  116. */
  117. /*
  118. {{0,0,1,0},
  119. {0,0,0,1},
  120. {1,0,0,0},
  121. {0,1,0,0}}
  122. */
  123. /*
  124. { { 0,1,0,0 },
  125. { 0,0,0,1 },
  126. { 1,1,1,0 },
  127. { 0,0,1,0 } }
  128. */
  129. graph G = {
  130. {'A','B','C','D'},
  131. { { 0,1,1,1 },
  132. { 1,0,0,1 },
  133. { 1,0,0,1 },
  134. { 1,1,1,1 } }
  135. };
  136. cout << "--------------------------------" << endl;
  137. printGraph(G);
  138. cout << "--------------------------------"<<endl;
  139. printEdges(G);
  140. cout << "--------------------------------"<<endl;
  141. if (Conex(G) == 0)
  142. {
  143. cout << "Graful nu este conex!" << endl;
  144. }
  145. else
  146. {
  147. cout << "Graful este conex!" << endl;
  148. }
  149. if (checkIfOriented(G) == 1)
  150. {
  151. cout << "Graful este neorientat!" << endl;
  152. }
  153. else
  154. {
  155. cout << "Graful este orientat!" << endl;
  156. }
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement