Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. Graph* CreateGraph(int V, int E)
  2. {
  3. Graph* graph = (Graph*)malloc(sizeof(Graph));
  4. graph->V = V;
  5. graph->E = E;
  6. graph->edges = (Edge**)malloc(E * sizeof(Edge*));
  7. for (int i = 0; i < E; i++)
  8. {
  9. graph->edges[i] = (Edge*)malloc(sizeof(Edge));
  10. }
  11.  
  12. int* WeightArr = new int[E];
  13. FillRandomArray(WeightArr, E, 1, E, true);
  14.  
  15. Node** Nodes = (Node**)malloc(sizeof(Node*) * V);
  16. int* Destination = (int*)malloc(sizeof(int) * V);
  17. for (int i = 0; i < V; i++)
  18. {
  19. //Nodes[x] = (Node*)malloc(sizeof(Node));
  20. Nodes[i] = MakeSet(i);
  21. Destination[i] = i;
  22. }
  23.  
  24. srand(time(NULL));
  25. Destination[0] = -1;
  26. int Vg = 1;
  27. int* Source = (int*)malloc(sizeof(int) * V);
  28. Source[0] = 0;
  29.  
  30. int** Aparitions = (int**)malloc(sizeof(int*) * V);
  31. for (int i = 0; i < V; i++)
  32. {
  33. Aparitions[i] = (int*)malloc(sizeof(int) * V);
  34. }
  35. for (int i = 0; i < V; i++)
  36. for (int j = 0; j < V; j++)
  37. {
  38. Aparitions[i][j] = 0;
  39. }
  40. for (int i = 0; i < V - 1; i++)
  41. {
  42. int x = rand() % Vg;
  43. int y = rand() % V;
  44. while (Destination[y] == -1)
  45. {
  46. y++;
  47. y %= V;
  48. }
  49.  
  50. Destination[y] = -1;
  51. Source[Vg] = y;
  52. Vg++;
  53. Aparitions[Source[x]][y] = 1;
  54. Aparitions[y][Source[x]] = 1;
  55. graph->edges[i]->source = Nodes[Source[x]];
  56. graph->edges[i]->destination = Nodes[y];
  57. graph->edges[i]->weight = WeightArr[i];
  58. }
  59.  
  60. for (int i = V - 1; i < E; i++)
  61. {
  62. int x = rand() % V;
  63. int y = rand() % V;
  64. if (y == x)
  65. {
  66. y++;
  67. if (y == V)
  68. y = 0;
  69. }
  70. int tries = 0;
  71. while (Aparitions[x][y] == 1)
  72. {
  73. x = rand() % V;
  74. y = rand() % V;
  75. if (y == x)
  76. {
  77. y++;
  78. if (y == V)
  79. y = 0;
  80. }
  81. }
  82. Aparitions[x][y] = 1;
  83. Aparitions[y][x] = 1;
  84. graph->edges[i]->source = Nodes[x];
  85. graph->edges[i]->destination = Nodes[y];
  86. graph->edges[i]->weight = WeightArr[i];
  87. }
  88. for (int i = 0; i < V; i++)
  89. {
  90. free(Aparitions[i]);
  91. }
  92. free(Aparitions);
  93. free(Nodes);
  94. free(Source);
  95. free(Destination);
  96. return graph;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement