0% found this document useful (0 votes)
13 views4 pages

ASSIGNMENT 3

The document outlines an assignment to develop an algorithm using the Floyd-Warshall method to find the strongest and shortest paths between professionals in a networking platform. It describes the structure of the input as a weighted directed graph where nodes represent professionals and edges represent their connections with weights indicating relationship strengths. The provided C code implements this algorithm, allowing users to input the number of professionals and their connection strengths in an adjacency matrix format.

Uploaded by

ddas99263
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

ASSIGNMENT 3

The document outlines an assignment to develop an algorithm using the Floyd-Warshall method to find the strongest and shortest paths between professionals in a networking platform. It describes the structure of the input as a weighted directed graph where nodes represent professionals and edges represent their connections with weights indicating relationship strengths. The provided C code implements this algorithm, allowing users to input the number of professionals and their connection strengths in an adjacency matrix format.

Uploaded by

ddas99263
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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;
}

You might also like