Advertisement
BowserFlash13

Untitled

Jun 19th, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. #define nVertex 30
  7.  
  8. using namespace std;
  9.  
  10. void stvoriGraf(vector<int> adj[], int V)
  11. {
  12. for (int i = 0; i < V; i++)
  13. {
  14. for (int j = 0; j < V; j++)
  15. {
  16. int create_edge = rand() % 7;
  17. if (create_edge == 1 && i != j)
  18. {
  19. adj[i].push_back(j);
  20. //adj[j].push_back(i);
  21. }
  22. }
  23. }
  24. }
  25.  
  26. void ispisiGraf(vector<int> adj[], int V)
  27. {
  28. bool povezani[V][V]; //ovdje cemo pohranjivati vrijednosti 0 ako ne postoji veza a 1 ako postoji
  29.  
  30. for (int i = 0; i < V; i++)
  31. {
  32. for (int j = 0; j < V; j++)
  33. {
  34. povezani[i][j] = 0; //postavljamo sve veze na 0
  35. }
  36. }
  37.  
  38. for (int i = 0; i < V; i++)
  39. {
  40. for (auto j = adj[i].begin(); j != adj[i].end(); j++)
  41. {
  42. povezani[i][*j] = 1; //ako veza postoji postavljamo vrijednost na 1
  43. }
  44. }
  45.  
  46. cout << endl << "Prebrojavanje parova od 3 vrha: " << endl;
  47.  
  48. int brojTrokutova = 0;
  49. for (int a = 0; a < V; a++)
  50. {
  51. for (int b = 0; b < V; b++)
  52. {
  53. if (a != b && povezani[a][b] == 1) //provjera ako veza izmedju i i j postoji onda je 1
  54. {
  55. for (int c = 0; c < V; c++)
  56. {
  57. if (a != c && b != c && povezani[b][c] == 1 && povezani[c][a] == 1) {
  58. //isto tako ako postoje veze b i c, c i a, njihova vrijednost je 1
  59. cout << "postoji trokut s vrhovima: " << a << " " << b << " " << c << endl;
  60. brojTrokutova = brojTrokutova + 1;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. cout << "Broj trokutova: " << brojTrokutova / 3 << endl;
  67.  
  68. for (int i = 0; i < V; i++)
  69. {
  70. for (int j = 0; j < V; j++)
  71. {
  72. cout << povezani[i][j] << " ";
  73. }
  74. cout << endl;
  75. }
  76. }
  77.  
  78. int main()
  79. {
  80. vector<int> adj[nVertex];
  81. stvoriGraf(adj, nVertex);
  82. ispisiGraf(adj, nVertex);
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement