-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdijkstra.c
149 lines (119 loc) · 2.79 KB
/
dijkstra.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* dijkstra.c
*
* Created on: Jul 16, 2012
* Author: Manuel Pasieka , mapa17@posgrado.upv.es
*
* Simple Dijkstra algorithm implementation
* http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
*/
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "Dijkstra_tools.h"
long N;
int randInit;
void inputCheck(int argc, char** argv);
void printUsage(int argc, char** argv);
void dijkstra(graph* G, long initial_node, char debug);
int main(int argc, char *argv[])
{
long i;
graph G;
double runtime;
inputCheck(argc, argv);
if(N == 1){
generateTestGraph(&G);
} else {
generateGraph(N, randInit, &G, 0);
}
tick();
dijkstra(&G, 0, 0);
runtime = tack();
//
char *b;
b = malloc(G.N * 5);
if(b == NULL) {perror("malloc"); exit(EXIT_FAILURE); }
sprintf(b,"\nLowest distances!\nD=[");
for(i = 0; i<G.N; i++){
sprintf(&b[strlen(b)], "%d,", G.D[i]);
}
printf("%s]\n", b);
printf("Was working for [%f] sec.\n",runtime);
return EXIT_SUCCESS;
}
void dijkstra(graph* G, long initial_node, char debug)
{
long i,j,k;
long aN; //actualNode
G->D[initial_node] = 0;
aN = initial_node;
printf("Running dijkstra on graph\n");
if(debug)
printGraph(G);
for(i = 0; i < G->N; i++)
{
G->visited[aN] = VISITED;
if(debug){
printf("It[%d] aN [%d]",i, aN); printStatus(G); printf("\n");
}
//Find all nodes connected to aN
for(j=0;j<G->N;j++){
if( (G->node[aN][j] != NO_CONN) ){
if( (G->D[aN] + G->node[aN][j]) < G->D[j] ){
G->D[j] = (G->D[aN] + G->node[aN][j]);
}
}
}
aN = getNextNode(G);
}
printf("Finished Dijkstra\n");
}
void printUsage(int argc, char** argv){
printf("Usage: %s NUMER_OF_POINTS [SRAND_INIT_VALUE]\n",argv[0]);
}
void inputCheck(int argc, char** argv)
{
if ( argc < 2){
printUsage(argc, argv);
exit(EXIT_FAILURE);
}
errno = 0; // To distinguish success/failure after call
N = atol(argv[1]);
/* Check for various possible errors */
if ((errno == ERANGE && (N == LONG_MAX || N == LONG_MIN)) || (errno != 0 && N == 0)) {
perror("atol");
exit(EXIT_FAILURE);
}
if(N <= 0){
printf("Invalid number of points! Number of points must be bigger than zero.\n");
exit(EXIT_FAILURE);
}
//Check for second argument being the random init value
if(argc == 3)
{
errno = 0; // To distinguish success/failure after call
randInit = atol(argv[2]);
/* Check for various possible errors */
if ((errno == ERANGE && (randInit == LONG_MAX || randInit == LONG_MIN)) || (errno != 0 && randInit == 0)) {
perror("atol");
exit(EXIT_FAILURE);
}
} else {
randInit = -1;
}
if(randInit < 0){
time_t t1;
struct tm* t2 = NULL;
int sec;
t1 = time( NULL );
t2 = gmtime(&t1);
sec = t2->tm_sec;
randInit = sec;
}
}