0% found this document useful (0 votes)
7 views

Assignment 4

The document outlines an assignment to create a professional networking platform that connects professionals while minimizing connection efforts. It includes a C program implementing Kruskal's algorithm to find the Minimum Spanning Tree (MST) of a graph representing professionals and their connections. The program takes user input for the number of professionals and their connections, then outputs the MST and the total connection strength cost.

Uploaded by

ddas99263
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 4

The document outlines an assignment to create a professional networking platform that connects professionals while minimizing connection efforts. It includes a C program implementing Kruskal's algorithm to find the Minimum Spanning Tree (MST) of a graph representing professionals and their connections. The program takes user input for the number of professionals and their connections, then outputs the MST and the total connection strength cost.

Uploaded by

ddas99263
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 4

A professional networking platform (like LinkedIn) wants to build an


efficient backbone that connects all professionals in the network while
minimizing the overall introduction effort. Instead of finding the shortest
path between every pair (as in Floyd-Warshall), we now want to select a
subset of connections that:
Ensures everyone is connected (directly or indirectly).
Minimizes the total connection strength cost (since weaker connections
require more effort to establish professional relationships).

SOLUTION :-

#include <stdio.h>
#include <stdlib.h>

// Structure to represent an edge


typedef struct {
int src, dest, weight;
} Edge;

// Structure to represent a graph


typedef struct {
int V, E; // Number of vertices and edges
Edge* edges;
} Graph;

// Structure for Union-Find (Disjoint Set)


typedef struct {
int parent;
int rank;
} Subset;
// Function prototypes
Graph* createGraph(int V, int E);
void mergeSort(Edge arr[], int left, int right);
void merge(Edge arr[], int left, int mid, int right);
int find(Subset subsets[], int i);
void Union(Subset subsets[], int x, int y);
void KruskalMST(Graph* graph);

// Main function
int main() {
int i, j, V, E;

// User input for number of vertices and edges


printf("Enter number of professionals (nodes): ");
scanf("%d", &V);
printf("Enter number of connections (edges): ");
scanf("%d", &E);

Graph* graph = createGraph(V, E);

printf("Enter the connections (source, destination, connection strength):\n");


for (i = 0; i < E; i++) {
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph-
>edges[i].weight);
}

KruskalMST(graph);

free(graph->edges);
free(graph);
return 0;
}

// Function to create a graph


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));
return graph;
}

// Merge Sort function to sort edges by weight


void mergeSort(Edge arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

// Merging function for Merge Sort


void merge(Edge arr[], int left, int mid, int right) {
int i, j;
int n1 = mid - left + 1;
int n2 = right - mid;
Edge* L = (Edge*)malloc(n1 * sizeof(Edge));
Edge* R = (Edge*)malloc(n2 * sizeof(Edge));
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i].weight <= R[j].weight)
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}

while (i < n1)


arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];

free(L);
free(R);
}

// Find operation in Union-Find with path compression


int find(Subset subsets[], int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// Union operation with rank optimization
void Union(Subset subsets[], int x, int y) {
int rootX = find(subsets, x);
int rootY = find(subsets, y);

if (subsets[rootX].rank < subsets[rootY].rank)


subsets[rootX].parent = rootY;
else if (subsets[rootX].rank > subsets[rootY].rank)
subsets[rootY].parent = rootX;
else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
}
}

// Kruskal’s Algorithm for Minimum Spanning Tree


void KruskalMST(Graph* graph) {
int V = graph->V;
Edge result[V - 1]; // MST will have V-1 edges
int e = 0, i = 0;
int v;

// Sort all the edges in non-decreasing order of weight


mergeSort(graph->edges, 0, graph->E - 1);

// Allocate memory for Union-Find subsets


Subset* subsets = (Subset*)malloc(V * sizeof(Subset));
for (v = 0; v < V; v++) {
subsets[v].parent = v;
subsets[v].rank = 0;
}

// Process sorted edges and build MST


while (e < V - 1 && i < graph->E) {
Edge nextEdge = graph->edges[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);

if (x != y) { // If adding this edge does not form a cycle


result[e++] = nextEdge;
Union(subsets, x, y);
}
}

// Print the resulting MST


printf("\nMinimum Spanning Tree:\n");
int totalCost = 0;
for (i = 0; i < e; i++) {
printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight);
totalCost += result[i].weight;
}
printf("Total Connection Strength Cost: %d\n", totalCost);

free(subsets);
}

You might also like