Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. int G[9][9] = {
  8. {0, 4, 0, 0, 0, 0, 0, 8, 0},
  9. {4, 0, 8, 0, 0, 0, 0, 11, 0},
  10. {0, 8, 0, 7, 0, 4, 0, 0, 2},
  11. {0, 0, 7, 0, 9, 14, 0, 0, 0},
  12. {0, 0, 0, 9, 0, 10, 0, 0, 0},
  13. {0, 0, 4, 14, 10, 0, 2, 0, 0},
  14. {0, 0, 0, 0, 0, 2, 0, 1, 6},
  15. {8, 11, 0, 0, 0, 0, 1, 0, 7},
  16. {8, 0, 2, 0, 0, 0, 6, 7, 0},
  17. };
  18.  
  19. int no_edge = 0, selected[9], x, y, minimum, i, j;
  20.  
  21. memset (selected, false, sizeof (selected));
  22. // selected ==> Starting address of memory to be filled
  23. // false ==> Value to be filled
  24. // sizeof(selected) ==> Number of bytes to be filled starting
  25. // from ptr to be filled
  26.  
  27. //no_edge = 0;
  28.  
  29. selected[0] = true;
  30.  
  31. cout << "Edge" << " : " << "Weight";
  32. cout << endl;
  33. while (no_edge < 9 - 1)
  34. {
  35. minimum = 100;
  36. x = 0;
  37. y = 0;
  38.  
  39. for (i = 0; i < 9; i++)
  40. {
  41. if (selected[i])
  42. {
  43. for (j = 0; j < 9; j++)
  44. {
  45. if (!selected[j] && G[i][j])
  46. {
  47. if (minimum > G[i][j])
  48. {
  49. minimum = G[i][j];
  50. x = i;
  51. y = j;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. cout << x+1 << " - " << y+1 << " : " << G[x][y];
  58. cout << endl;
  59. selected[y] = true;
  60. no_edge++;
  61. }
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement