ASSIGNMENT 3
In a professional networking platform (like LinkedIn), people are
connected through various degrees of relationships. Each connection has a
strength score that indicates the quality of the relationship. Your task is to
develop an algorithm that finds the strongest and shortest path between
every pair of professionals in the network.
Problem Definition:
You are given a weighted, directed graph where:
Nodes (People): Represent individual professionals.
Edges (Connections): Represent direct relationships between professionals.
Weights (Connection Strengths): A numerical value that represents how
strong a relationship is.
Lower weight = Stronger connection (e.g., 1 = best friend, 10 = distant
acquaintance).
Higher weight = Weaker connection (e.g., 100 = barely know each other).
If two professionals are not directly connected, the weight is ∞ (infinity).
Your task is to implement the Floyd-Warshall Algorithm to determine the
strongest and shortest connection path between every pair of professionals.
SOLUTION :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define INF INT_MAX
void floydWarshall(int **graph, int n) {
int i, j, k;
int **dist = (int **)malloc(n * sizeof(int *));
for (i = 0; i < n; i++) {
dist[i] = (int *)malloc(n * sizeof(int));
for (j = 0; j < n; j++) {
dist[i][j] = graph[i][j];
}
}
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
printf("Shortest & Strongest Connection Path Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (dist[i][j] == INF)
printf("INF ");
else
printf("%d ", dist[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
free(dist[i]);
}
free(dist);
}
int main() {
int n, i, j;
printf("Enter number of professionals: ");
scanf("%d", &n);
int **graph = (int **)malloc(n * sizeof(int *));
for (i = 0; i < n; i++) {
graph[i] = (int *)malloc(n * sizeof(int));
}
printf("Enter adjacency matrix (use INF for no connection):\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
char val[10];
scanf("%s", val);
if (val[0] == 'I' && val[1] == 'N' && val[2] == 'F')
graph[i][j] = INF;
else
graph[i][j] = atoi(val);
}
}
floydWarshall(graph, n);
for (i = 0; i < n; i++) {
free(graph[i]);
}
free(graph);
return 0;
}