Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Graph* CreateGraph(int V, int E)
- {
- Graph* graph = (Graph*)malloc(sizeof(Graph));
- graph->V = V;
- graph->E = E;
- graph->edges = (Edge**)malloc(E * sizeof(Edge*));
- for (int i = 0; i < E; i++)
- {
- graph->edges[i] = (Edge*)malloc(sizeof(Edge));
- }
- int* WeightArr = new int[E];
- FillRandomArray(WeightArr, E, 1, E, true);
- Node** Nodes = (Node**)malloc(sizeof(Node*) * V);
- int* Destination = (int*)malloc(sizeof(int) * V);
- for (int i = 0; i < V; i++)
- {
- //Nodes[x] = (Node*)malloc(sizeof(Node));
- Nodes[i] = MakeSet(i);
- Destination[i] = i;
- }
- srand(time(NULL));
- Destination[0] = -1;
- int Vg = 1;
- int* Source = (int*)malloc(sizeof(int) * V);
- Source[0] = 0;
- int** Aparitions = (int**)malloc(sizeof(int*) * V);
- for (int i = 0; i < V; i++)
- {
- Aparitions[i] = (int*)malloc(sizeof(int) * V);
- }
- for (int i = 0; i < V; i++)
- for (int j = 0; j < V; j++)
- {
- Aparitions[i][j] = 0;
- }
- for (int i = 0; i < V - 1; i++)
- {
- int x = rand() % Vg;
- int y = rand() % V;
- while (Destination[y] == -1)
- {
- y++;
- y %= V;
- }
- Destination[y] = -1;
- Source[Vg] = y;
- Vg++;
- Aparitions[Source[x]][y] = 1;
- Aparitions[y][Source[x]] = 1;
- graph->edges[i]->source = Nodes[Source[x]];
- graph->edges[i]->destination = Nodes[y];
- graph->edges[i]->weight = WeightArr[i];
- }
- for (int i = V - 1; i < E; i++)
- {
- int x = rand() % V;
- int y = rand() % V;
- if (y == x)
- {
- y++;
- if (y == V)
- y = 0;
- }
- int tries = 0;
- while (Aparitions[x][y] == 1)
- {
- x = rand() % V;
- y = rand() % V;
- if (y == x)
- {
- y++;
- if (y == V)
- y = 0;
- }
- }
- Aparitions[x][y] = 1;
- Aparitions[y][x] = 1;
- graph->edges[i]->source = Nodes[x];
- graph->edges[i]->destination = Nodes[y];
- graph->edges[i]->weight = WeightArr[i];
- }
- for (int i = 0; i < V; i++)
- {
- free(Aparitions[i]);
- }
- free(Aparitions);
- free(Nodes);
- free(Source);
- free(Destination);
- return graph;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement