forked from TrachtmanLior/SystemProgrammingAEx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_mat.c
54 lines (50 loc) · 1.91 KB
/
my_mat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "my_mat.h"
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
void getMatrix(int matrix[][MATRIX_SIZE], int size){
for(int i=0;i<size;i++){
for(int j=0; j<size;j++){
scanf("%d", &matrix[i][j]);
// 0 in matrix means no path between i and j, set it to NO_PATH to make it easier for the algo
if (matrix[i][j] == 0)
matrix[i][j] = NO_PATH;
}
}
}
/* In each cell [i][j] of matrix, this function sets the shortest path
(INFINITY if path doesn't exist), between i and j.
using Floyd-Warshall's algorithm: https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
*/
void floydWarshallAlgorithm(int matrix[][MATRIX_SIZE], int size){
// Run through every vortex, and set new value if there is a shorter path through this vortex
for(int k=0;k<size;k++){
for(int i=0;i<size;i++){
// no reason to check row k
if (i != k) {
for(int j=0;j<size;j++){
// no reason to check col k and no reason to check diagonal of matrix (always NO_PATH)
if (j != k && i!=j){
if(matrix[i][k] != NO_PATH && matrix[k][j] != NO_PATH
&& (matrix[i][j] > matrix[i][k] + matrix[k][j])){
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
}
}
}
}
/* Assuming Floyd's algorithm has already been done.
Returns if there is a path between i and j in matrix.
*/
int existPath(int matrix[][MATRIX_SIZE], int i , int j, int size){
// a path to yourself always exists
return (matrix[i][j] != NO_PATH);
}
/* Assuming Floyd's algorithm has already been done.
Returns the shortest path betweeen i and j in matrix.
*/
int shortestPath(int matrix[][MATRIX_SIZE], int i , int j, int size){
return matrix[i][j] != NO_PATH ? matrix[i][j] : -1;
}