Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. int n;
  4. int noe;
  5. int graph_edge [100] [4];
  6. int tree [10] [10];
  7. int sets [100] [10];
  8. int top [100];
  9. using namespace std;
  10.  
  11. void read_graph ();
  12. void initialize_span_t ();
  13. void sort_edges ();
  14. void algorithm ();
  15. int find_node (int);
  16. void print_min_span_t ();
  17.  
  18. void read_graph ()
  19. {
  20. cout << "Алгоритм Крускала"<<endl;
  21. cout << "Введіть кількість вершин графа ";
  22. cin >> n;
  23. noe = 0;
  24. cout << "Введіть відстань між вершинами ";
  25. for (int i = 1; i <= n; i ++)
  26. {
  27. for (int j = i + 1; j <= n; j ++)
  28. {
  29. cout << i << "," << j<<": ";
  30. int w;
  31. cin >> w;
  32. if (w != 0)
  33. {
  34. noe ++;
  35. graph_edge [noe] [1] = i;
  36. graph_edge [noe] [2] = j;
  37. graph_edge [noe] [3] = w;
  38. }
  39. }
  40. }
  41. cout << "\n \n Граф має такі відстані між вершинами: \n";
  42. for (int i = 1; i <= noe; i ++)
  43. {
  44. cout << "<" << graph_edge [i] [1]<< "," << graph_edge [i] [2]<< ">: " << graph_edge [i] [3] << endl;
  45. }
  46. }
  47. void sort_edges ()
  48. {
  49.  
  50. for (int i = 1; i <= noe-1; i ++)
  51. {
  52. for (int j = 1; j <= noe-i; j ++)
  53. {
  54. if (graph_edge [j] [3]> graph_edge [j + 1] [3])
  55. {
  56. int t = graph_edge [j] [1];
  57. graph_edge [j] [1] = graph_edge [j + 1] [1];
  58. graph_edge [j + 1] [1] = t;
  59. t = graph_edge [j] [2];
  60. graph_edge [j] [2] = graph_edge [j + 1] [2];
  61. graph_edge [j + 1] [2] = t;
  62. t = graph_edge [j] [3];
  63. graph_edge [j] [3] = graph_edge [j + 1] [3];
  64. graph_edge [j + 1] [3] = t;
  65. }
  66. }
  67. }
  68. cout << "\n \n Після сортування: \n";
  69. for (int i = 1; i <= noe; i ++)
  70. cout << "" << graph_edge [i] [1]<< "," << graph_edge [i] [2]<< ">: " << graph_edge [i] [3] << endl;
  71. }
  72. void algorithm ()
  73. {
  74. for (int i = 1; i <= n; i ++)
  75. {
  76. sets [i] [1] = i;
  77. top [i] = 1;
  78. }
  79. cout << "\n Початок алгоритму: \n \n";
  80. for (int i = 1; i <= noe; i ++)
  81. {
  82. int p1 = find_node (graph_edge [i] [1]);
  83. int p2 = find_node (graph_edge [i] [2]);
  84. if (p1 != p2)
  85. {
  86. cout << "В дерево включено вершину "<< "<" << graph_edge [i] [1] << ","
  87. << graph_edge [i] [2] << ">" << endl << endl;
  88. tree [graph_edge [i] [1]] [graph_edge [i] [2]] = graph_edge [i] [3];
  89. tree [graph_edge [i] [2]] [graph_edge [i] [1]] = graph_edge [i] [3];
  90. for (int j = 1; j <= top [p2]; j ++)
  91. {
  92. top [p1] ++;
  93. sets [p1] [top [p1]] = sets [p2] [j];
  94. }
  95. top [p2] = 0;
  96. }
  97. else
  98. {
  99. cout << "Включення вершини "<< "<" << graph_edge [i] [1] << ","<< graph_edge [i] [2] << ">" << " формує цикл, отож вона видалена \n \n";
  100. }
  101. }
  102. }
  103. int find_node (int n)
  104. {
  105. for (int i = 1; i <= noe; i ++)
  106. {
  107. for (int j = 1; j <= top [i]; j ++)
  108. {
  109. if (n == sets [i] [j])
  110. return i;
  111. }
  112. }
  113. return 999;
  114. }
  115. void print_min_span_t ()
  116. {
  117. for (int i = 1; i <= n; i ++)
  118. {
  119. for (int j = 1; j <= n; j ++)
  120. cout << tree [i] [j] << " ";
  121. cout << endl;
  122. }
  123. }
  124.  
  125. int main ()
  126. {
  127. setlocale(LC_ALL, "rus");
  128. read_graph ();
  129. sort_edges ();
  130. algorithm ();
  131. print_min_span_t ();
  132. setlocale(LC_ALL, "OCP");
  133. system("pause");
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement