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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*
!!DESCRIPTION!! Dijkstras Algorithm
!!ORIGIN!! testsuite
!!LICENCE!! Public Domain
!!AUTHOR!! Groepaz/Hitmen
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef NULL
#define NULL ((void*)0)
#endif
#define DIJKSTRA_INFINITY (0xffff)
#define DIJKSTRA_FLAG_UNVISITED (0x0)
/*
#define DIJKSTRA_FLAG_OPEN (0x1)
*/
#define DIJKSTRA_FLAG_CLOSED (0x2)
typedef struct _DIJKSTRA_EDGE {
struct _DIJKSTRA_NODE *NEXTNODE;
unsigned short DISTANCE;
} DIJKSTRA_EDGE;
typedef struct _DIJKSTRA_NODE {
DIJKSTRA_EDGE *EDGES;
unsigned char TAG;
unsigned char FLAG;
unsigned short MINDIST;
struct _DIJKSTRA_NODE *PREVIOUS;
} DIJKSTRA_NODE;
/* init with graph, startnode, working-array */
void Dijkstra_Init(const DIJKSTRA_NODE *graph,DIJKSTRA_NODE *start,DIJKSTRA_NODE *nodes);
/* call main algo with working-array */
void Dijkstra_Search(DIJKSTRA_NODE *nodes);
/* print path, call with working-array, endnode */
void Dijkstra_Path(DIJKSTRA_NODE *nodes,DIJKSTRA_NODE *end);
/* print table, call with working-array, current node */
void Dijkstra_Table(DIJKSTRA_NODE *nodes,DIJKSTRA_NODE *current);
/* internally used routines */
unsigned short Dijkstra_Distance(DIJKSTRA_NODE *currnode,DIJKSTRA_NODE *nextnode);
void Dijkstra_Relax(DIJKSTRA_NODE *currnode,DIJKSTRA_NODE *nextnode);
DIJKSTRA_NODE *Dijkstra_NextCheapest(DIJKSTRA_NODE *graph);
unsigned short Dijkstra_CountUnvisited(DIJKSTRA_NODE *graph);
/* define to get printed info as the algorithm proceeds */
#define DIJKSTRA_PRINTDEBUG
/* the working array */
DIJKSTRA_NODE mynodes[0x10];
/* test-network data (mypoints and myedges) */
const DIJKSTRA_EDGE myedges_A[]={
{&mynodes[1],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_B[]={
{&mynodes[0],1},
{&mynodes[2],2},
{&mynodes[3],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_C[]={
{&mynodes[1],2},
{&mynodes[5],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_D[]={
{&mynodes[1],1},
{&mynodes[4],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_E[]={
{&mynodes[6],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_F[]={
{&mynodes[7],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_G[]={
{&mynodes[8],1},
{&mynodes[7],4},
{NULL}
};
const DIJKSTRA_EDGE myedges_H[]={
{&mynodes[9],1},
{&mynodes[6],4},
{NULL}
};
const DIJKSTRA_EDGE myedges_I[]={
{&mynodes[10],5},
{NULL}
};
const DIJKSTRA_EDGE myedges_J[]={
{&mynodes[10],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_K[]={
{&mynodes[11],1},
{NULL}
};
const DIJKSTRA_EDGE myedges_L[]={
{&mynodes[10],1},
{NULL}
};
const DIJKSTRA_NODE mypoints[]={
{(DIJKSTRA_EDGE *)&myedges_A[0],'A'},
{(DIJKSTRA_EDGE *)&myedges_B[0],'B'},
{(DIJKSTRA_EDGE *)&myedges_C[0],'C'},
{(DIJKSTRA_EDGE *)&myedges_D[0],'D'},
{(DIJKSTRA_EDGE *)&myedges_E[0],'E'},
{(DIJKSTRA_EDGE *)&myedges_F[0],'F'},
{(DIJKSTRA_EDGE *)&myedges_G[0],'G'},
{(DIJKSTRA_EDGE *)&myedges_H[0],'H'},
{(DIJKSTRA_EDGE *)&myedges_I[0],'I'},
{(DIJKSTRA_EDGE *)&myedges_J[0],'J'},
{(DIJKSTRA_EDGE *)&myedges_K[0],'K'},
{(DIJKSTRA_EDGE *)&myedges_L[0],'L'},
{NULL}
};
/*
* initialize working-array
*/
void Dijkstra_Init(const DIJKSTRA_NODE *graph,DIJKSTRA_NODE *start,DIJKSTRA_NODE *nodes) {
while(graph->EDGES!=NULL) {
nodes->EDGES=graph->EDGES;
nodes->TAG=graph->TAG;
nodes->FLAG=DIJKSTRA_FLAG_UNVISITED;
nodes->MINDIST=DIJKSTRA_INFINITY;
nodes->PREVIOUS=NULL;
graph++;nodes++;
}
/*
start->FLAG=DIJKSTRA_FLAG_OPEN;
start->PREVIOUS=NULL;
*/
start->MINDIST=0;
}
/*
* compute the distance between two Nodes in the Graph
*/
unsigned short Dijkstra_Distance(DIJKSTRA_NODE *currnode,DIJKSTRA_NODE *nextnode){
DIJKSTRA_EDGE *edge;
edge=currnode->EDGES;
while(edge!=NULL) {
if(edge->NEXTNODE == nextnode){
return(edge->DISTANCE);
}
edge++;
}
return(DIJKSTRA_INFINITY);
}
/*
* 'relax' one node against another
*/
void Dijkstra_Relax(DIJKSTRA_NODE *currnode,DIJKSTRA_NODE *nextnode){
unsigned short newdist;
#ifdef DIJKSTRA_PRINTDEBUG
printf("relax >%c< to >%c<\n",currnode->TAG,nextnode->TAG);
#endif
newdist=currnode->MINDIST+Dijkstra_Distance(currnode,nextnode);
if((nextnode->MINDIST)>(newdist)){
nextnode->MINDIST=newdist;
nextnode->PREVIOUS=currnode;
}
}
/*
* find the yet unprocessed Node with the currently
* smallest estimated MINDIST
*/
DIJKSTRA_NODE *Dijkstra_NextCheapest(DIJKSTRA_NODE *graph){
unsigned short mindist;
DIJKSTRA_NODE *node;
node=NULL;
mindist=DIJKSTRA_INFINITY;
while(graph->EDGES!=NULL) {
if(graph->FLAG!=DIJKSTRA_FLAG_CLOSED){
if(!(mindist<graph->MINDIST)){
mindist=graph->MINDIST;
node=graph;
}
}
graph++;
}
#ifdef DIJKSTRA_PRINTDEBUG
if(node!=NULL) printf("next cheapest Node: >%c<\n",node->TAG);
#endif
return(node);
}
/*
* count number of Nodes that are left for processing
*/
unsigned short Dijkstra_CountUnvisited(DIJKSTRA_NODE *graph){
unsigned short num;
num=0;
while(graph->EDGES!=NULL) {
if(graph->FLAG!=DIJKSTRA_FLAG_CLOSED){
num++;
}
graph++;
}
return(num);
}
/*
* Dijkstra-Algorithmus main processing
*/
void Dijkstra_Search(DIJKSTRA_NODE *graph){
DIJKSTRA_NODE *currnode,*nextnode;
DIJKSTRA_EDGE *edge;
currnode=graph;
while(Dijkstra_CountUnvisited(graph)>0){
edge=currnode->EDGES;
while(edge->NEXTNODE!=NULL){
nextnode=edge->NEXTNODE;
if(nextnode->FLAG!=DIJKSTRA_FLAG_CLOSED){
/*
nextnode->FLAG=DIJKSTRA_FLAG_OPEN;
*/
Dijkstra_Relax(currnode,nextnode);
#ifdef DIJKSTRA_PRINTDEBUG
Dijkstra_Table(graph,currnode);
#endif
}
edge++;
}
currnode=Dijkstra_NextCheapest(graph);
currnode->FLAG=DIJKSTRA_FLAG_CLOSED;
}
}
/*
* print the Path from start Node to one other Node
*/
void Dijkstra_Path(DIJKSTRA_NODE *graph,DIJKSTRA_NODE *end){
DIJKSTRA_NODE *currnode,*nextnode;
printf("Path from >%c< to >%c< : ",end->TAG,graph->TAG);
currnode=end;
while(currnode->PREVIOUS!=NULL){
printf(">%c< ",currnode->TAG);
currnode=currnode->PREVIOUS;
}
printf(">%c<\n",currnode->TAG);
}
/*
* print working-array as a table
*/
void Dijkstra_Table(DIJKSTRA_NODE *graph,DIJKSTRA_NODE *current){
DIJKSTRA_NODE *g;
printf("----------------------\n");
printf("Node |");
g=graph;while(g->EDGES!=NULL) {
printf("-->%c<-|",g->TAG);
g++;
}
printf("\n");
printf("MinDist |");
g=graph;while(g->EDGES!=NULL) {
printf(" %5u|",g->MINDIST);
g++;
}
printf("\n");
printf("Flag |");
g=graph;while(g->EDGES!=NULL) {
switch(g->FLAG){
/*
case DIJKSTRA_FLAG_OPEN:
printf("opened|");
break;
*/
case DIJKSTRA_FLAG_CLOSED:
printf("closed|");
break;
default:
if(g->MINDIST!=DIJKSTRA_INFINITY){
printf("opened|");
} else {
printf("------|");
}
break;
}
g++;
}
printf("\n");
printf("Previous|");
g=graph;while(g->EDGES!=NULL) {
if(g->PREVIOUS==NULL)
printf("------|");
else
printf(" (%c) |",g->PREVIOUS->TAG);
g++;
}
printf("\n");
printf("----------------------\n");
}
int main(void)
{
/* init with graph, startnode, working-array */
Dijkstra_Init(&mypoints[0],&mynodes[0],&mynodes[0]);
/* call main algo with working-array */
Dijkstra_Search(&mynodes[0]);
/* print table, call with working-array, endnode */
Dijkstra_Table(&mynodes[0],&mynodes[11]);
/* print path, call with working-array, endnode */
Dijkstra_Path(&mynodes[0],&mynodes[11]);
return 0;
}
|