diff --git a/dijkstra.c b/dijkstra.c new file mode 100644 index 0000000000000000000000000000000000000000..af90a654d7c1a7fde4cce85bc2a1565e9eaa25af --- /dev/null +++ b/dijkstra.c @@ -0,0 +1,140 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <time.h> + +#define infinite 2147483646 +#define seed 13517119 + +int **init_graph(int n){ + int **matrix; + matrix = malloc(sizeof(int*) * n); + for(int i=0; i<n; i++){ + matrix[i] = malloc(sizeof(int*) * n); + } + + srand(seed); + + for(int i=0; i<n; i++){ + for(int j=0; j<n; j++){ + if(i != j){ + matrix[i][j] = rand() % 100; + } + else{ + matrix[i][j] = 0; + } + } + } + + return matrix; +} + +void print_graph(int **matrix, int n, FILE *file){ + for(int i=0; i<n; i++){ + for(int j=0; j<n; j++){ + fprintf(file, "%d ", matrix[i][j]); + } + fprintf(file, "\n"); + } +} + +void print_result(int *dist, int source, int n, FILE *file){ + for(int i=0; i<n; i++){ + fprintf(file, "Node %d to %d : %d \n", (source + 1), (i + 1), dist[i]); + } +} + +int minDistance(int *dist, bool sptSet[], int n) +{ + // Initialize min value + int min = infinite, min_index; + + for (int v = 0; v < n; v++) + if (sptSet[v] == false && dist[v] <= min) + min = dist[v], min_index = v; + + return min_index; +} + +int *dijkstra(int **graph, int src, int n) +{ + int *dist; // The output array. dist[i] will hold the shortest + // distance from src to i + + dist = malloc(sizeof(int) * n); + + bool *sptSet; // sptSet[i] will be true if vertex i is included in shortest + // path tree or shortest distance from src to i is finalized + sptSet = malloc(sizeof(bool) * n); + + // Initialize all distances as INFINITE and stpSet[] as false + for (int i = 0; i < n; i++) + dist[i] = infinite, sptSet[i] = false; + + // Distance of source vertex from itself is always 0 + dist[src] = 0; + + // Find shortest path for all vertices + for (int count = 0; count < n - 1; count++) { + // Pick the minimum distance vertex from the set of vertices not + // yet processed. u is always equal to src in the first iteration. + int u = minDistance(dist, sptSet, n); + + // Mark the picked vertex as processed + sptSet[u] = true; + + // Update dist value of the adjacent vertices of the picked vertex. + for (int v = 0; v < n; v++) + + // Update dist[v] only if is not in sptSet, there is an edge from + // u to v, and total weight of path from src to v through u is + // smaller than current value of dist[v] + if (!sptSet[v] && graph[u][v] && dist[u] != infinite + && dist[u] + graph[u][v] < dist[v]) + dist[v] = dist[u] + graph[u][v]; + } + + return dist; +} + +int main(int argc, char **argv){ + int n; + int **matrix; + int *result; + FILE *fileopen; + + fileopen = fopen("output.txt", "w"); + + clock_t start_t, end_t; + double diff_t; + + n = atoi(argv[1]); + + matrix = init_graph(n); + print_graph(matrix, n, fileopen); + + start_t = clock(); + + for(int i=0; i<n; i++){ + result = dijkstra(matrix, i, n); + print_result(result, i, n, fileopen); + } + + end_t = clock(); + diff_t = (double)(end_t-start_t) / CLOCKS_PER_SEC * 1000000; + + printf("Execution time : %f microseconds\n", diff_t); + + free(result); + + for(int i=0; i<n; i++){ + int* current = matrix[i]; + free(current); + } + + free(matrix); + + fclose(fileopen); + + return 0; +} \ No newline at end of file