Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1.  
  2. #ifndef GRAFURI_GRAF_H
  3. #define GRAFURI_GRAF_H
  4.  
  5. #define NR_NODURI 5
  6.  
  7.  
  8. struct Graf {
  9. char V[NR_NODURI];
  10. int E[NR_NODURI][NR_NODURI];
  11. };
  12.  
  13. void afisareGraf(Graf g);
  14. int grad(Graf g);
  15. int numarMuchii(Graf g);
  16. bool esteOrientat(Graf g);
  17. bool estePonderat(Graf g);
  18. void afisareDrumCelMaiScurt(Graf g, int s, int D[]);
  19.  
  20. #endif
  21.  
  22.  
  23.  
  24. #include <iostream>
  25. #include <list>
  26. #include <math.h>
  27. #include <queue>
  28. #include "Graf.h"
  29.  
  30. using namespace std;
  31.  
  32. void afisareGraf(Graf g)
  33. {
  34. cout << " * ";
  35. for (int i = 0; i < NR_NODURI; ++i)
  36. {
  37. cout << " " << g.V[i] << " ";
  38. }
  39. cout << endl;
  40. for (int i = 0; i < NR_NODURI; ++i)
  41. {
  42. cout << " " << g.V[i] << " ";
  43. for (int j = 0; j < NR_NODURI; ++j)
  44. {
  45. cout << " " << g.E[i][j] << " ";
  46. }
  47. cout << endl;
  48. }
  49. cout << endl;
  50. }
  51.  
  52. int grad(Graf g)
  53. {
  54. return NR_NODURI;
  55. }
  56.  
  57. int numarMuchii(Graf g)
  58. {
  59. int nrMuchii = 0;
  60. for (int i = 0; i < NR_NODURI; ++i)
  61. {
  62. for (int j = 0; j < NR_NODURI; ++j)
  63. {
  64. if (g.E[i][j] != 0)
  65. {
  66. ++nrMuchii;
  67. cout << "(" << g.V[i] << ", " << g.V[j] << "); ";
  68. }
  69. }
  70. }
  71. cout << endl;
  72. cout << "Exista " << nrMuchii << " muchii." << endl;
  73. return nrMuchii;
  74. }
  75.  
  76.  
  77.  
  78. bool esteOrientat(Graf g)
  79. {
  80. for (int i = 0; i < NR_NODURI; ++i)
  81. {
  82. for (int j = 0; j < i; ++j)
  83. {
  84. if (g.E[i][j] != g.E[j][i])
  85. {
  86. return true;
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92.  
  93. bool estePonderat(Graf g)
  94. {
  95. for (int i = 0; i < NR_NODURI; ++i)
  96. {
  97. for (int j = 0; j < NR_NODURI; ++j)
  98. {
  99. if (abs(g.E[i][j]) > 1)
  100. {
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107.  
  108.  
  109. void afisareDrumCelMaiScurt(Graf g, int s, int D[])
  110. {
  111.  
  112. for (int i = 0; i < NR_NODURI; ++i)
  113. {
  114. if (i == s)
  115. {
  116. continue;
  117. }
  118. cout << "Costul minim de la" << g.V[s] << " la " << g.V[i] << ": " << D[i] << endl;
  119. }
  120. }
  121.  
  122.  
  123. #include <iostream>
  124. #include "Graf.h"
  125.  
  126. using namespace std;
  127.  
  128. int main() {
  129. Graf G = {
  130. { 'A', 'B', 'C', 'D', 'E' },
  131. {
  132. { 0 , 3 , 2 , 0 , 0 },
  133. { 0 , 0 , 4 , 2 , 0 },
  134. { 0 , 0 , 1 , 2 , 0 },
  135. { 0 , 1 , 2 , 0 , 1 },
  136. { 0 , 0 , 1 , 0 , 0 }
  137. }
  138. };
  139.  
  140. afisareGraf(G);
  141.  
  142.  
  143. cout << "Muchiile sunt=" << endl;
  144. cout << "Gradul grafului= " << grad(G) << endl;
  145. numarMuchii(G);
  146. cout << endl;
  147.  
  148. if (esteOrientat(G))
  149. {
  150. cout << "Graful este orientat.";
  151. }
  152. else
  153. {
  154. cout << "Graful nu este orientat.";
  155. }
  156. cout << endl;
  157.  
  158. if (estePonderat(G))
  159. {
  160. cout << "Graful este ponderat.";
  161. }
  162. else
  163. {
  164. cout << "Graful nu este ponderat.";
  165. }
  166. cout << endl << endl;
  167.  
  168.  
  169. cout << endl << endl;
  170.  
  171.  
  172.  
  173.  
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement