Okay, so. I have a program that I've been working on for some time now. The project consists of finding the cheapest way to connect all cities - either by airport or road connection. I've used Kruskal's algorithm for this and interpreted that the cities are the graphs vertexes, the roads are edges between cities and that the airports connect to other cities through a "sky" vertex. However, one of the requirements for this project is that, when there's a city that isn't connected either by road or airport, it should give the user the following output: "Insuficient".
Full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Edge {
int src, dest, weight;
};
struct Graph {
int V;
int E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = malloc(sizeof *graph);
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*)malloc(E * sizeof(struct Edge));
return graph;
};
struct subset {
int parent;
int rank;
};
int find(struct subset subsets[], int i)
{
if (subsets[i].parent != i)
subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(struct subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else
{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
int myComp(const void* a, const void* b)
{
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}
void KruskalMST(struct Graph* graph)
{
int V = graph->V;
struct Edge
result[V];
int e = 0;
int i = 0;
qsort(graph->edge, graph->E, sizeof(graph->edge[0]),
myComp);
struct subset* subsets
= (struct subset*)malloc(V * sizeof(struct subset));
for (int v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
while (e < V - 1 && i < graph->E) {
struct Edge next_edge = graph->edge[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
printf("MST edges:\n");
printf("V1 V2 Cost\n");
int minimumCost = 0;
int nRoads = 0;
int nAirports = 0;
for (i = 0; i < e; ++i)
{
printf("%d -- %d == %d\n", result[i].src,
result[i].dest, result[i].weight);
if (result[i].dest == 0) {
nAirports++;
}
else {
nRoads++;
}
minimumCost += result[i].weight;
}
printf("Minimum Spanning Tree total cost: %d\n",minimumCost);
printf("Number de airports: %d\n",nAirports);
printf("Number of roads: %d",nTotal);
}
return;
}
int main()
{
int v = 0;
int a = 0;
int edges = 0;
int r = 0;
int city = 0;
int airport = 0;
int city1 = 0;
int city2 = 0;
int cost = 0;
printf("Insert the number of cities: \n");
scanf("%d", &v);
printf("Insert the number of airports: \n");
scanf("%d", &a);
printf("Insert the number of roads: \n");
scanf("%d", &r);
edges = a + r;
struct Graph* graph = createGraph(v, edges);
for (int i = 0; i < a; i++) {
printf("Insert the city and the cost of building the airport: \n");
scanf(" %d %d", &city, &airport);
graph->edge[i].src = city;
graph->edge[i].dest = 0;
graph->edge[i].weight = airport;
}
for (int j = a; j < edges; j++) {
printf("Insert the cities and the cost of the road: \n");
scanf(" %d %d %d", &city1, &city2, &cost);
graph->edge[j].src = city1;
graph->edge[j].dest = city2;
graph->edge[j].weight = cost;
}
KruskalMST(graph);
return 0;
}
Example where it should not return an MST (Vertex 4 isn't connected):

Example where it should return an MST (All vertexes connected):

So far I tried to look for why it wasn't returning an error when all vertexes weren't connected and I couldn't find the spot. If I got an error I could use the setjmp/longjmp to simulate catching the error but, for that, I need to have an error first.
