diff options
Diffstat (limited to 'sem3/algo/workshop2/dijkstra/dijkstra.c')
-rw-r--r-- | sem3/algo/workshop2/dijkstra/dijkstra.c | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/sem3/algo/workshop2/dijkstra/dijkstra.c b/sem3/algo/workshop2/dijkstra/dijkstra.c index 9609343..c112c6d 100644 --- a/sem3/algo/workshop2/dijkstra/dijkstra.c +++ b/sem3/algo/workshop2/dijkstra/dijkstra.c @@ -16,7 +16,8 @@ typedef struct { struct list_item *end; } list_t; -void list_push(list_t *s, vertex_t *v) { +void list_push(list_t *s, vertex_t *v) +{ // Create list_item_t *i = malloc(sizeof(list_item_t)); i->next = NULL; @@ -24,7 +25,7 @@ void list_push(list_t *s, vertex_t *v) { i->v = v; // Link - if( s->len == 0 ) { + if ( s->len == 0 ) { s->begin = i; } else { s->end->next = i; @@ -35,26 +36,27 @@ void list_push(list_t *s, vertex_t *v) { s->len++; } -vertex_t *list_pop_smallest(list_t *s) { +vertex_t *list_pop_smallest(list_t *s) +{ //printf("beginning %s(%d)\n", s->begin->v->ref, s->begin->v->dist); list_item_t *smallest = s->begin; // Search list_item_t *i = s->begin; - while( i ) { - if( smallest->v->dist == -1 || (i->v->dist != -1 && i->v->dist < smallest->v->dist) ) { + while ( i ) { + if ( smallest->v->dist == -1 || (i->v->dist != -1 && i->v->dist < smallest->v->dist) ) { smallest = i; } i = i->next; } - if( smallest == s->begin ) { + if ( smallest == s->begin ) { s->begin = smallest->next; } else { smallest->prev->next = smallest->next; } - if( smallest == s->end ) { + if ( smallest == s->end ) { s->end = smallest->prev; } else { smallest->next->prev = smallest->prev; @@ -71,20 +73,22 @@ vertex_t *list_pop_smallest(list_t *s) { graph_t g; // Assumes u has an edge to v -void relax(vertex_t *u, vertex_t *v) { +void relax(vertex_t *u, vertex_t *v) +{ // Get edge between these two guys edge_t *e = u->adj; - while(e && e->next && strcmp(e->to->ref, v->ref)) { + while (e && e->next && strcmp(e->to->ref, v->ref)) { e = e->next; } - if( v->dist == -1 || v->dist > (u->dist + e->weight) ) { + if ( v->dist == -1 || v->dist > (u->dist + e->weight) ) { v->dist = u->dist + e->weight; v->p = u; } } -void dijkstra(graph_t *g, vertex_t *s) { +void dijkstra(graph_t *g, vertex_t *s) +{ list_t list; list.len = 0; list.end = list.begin = 0; @@ -93,24 +97,24 @@ void dijkstra(graph_t *g, vertex_t *s) { { vertex_t *v = vertex_next(g, NULL); - while( v ) { + while ( v ) { list_push(&list, v); v = vertex_next(g, v); } } - while( list.len ) { + while ( list.len ) { vertex_t *u = list_pop_smallest(&list); edge_t *e = u->adj; - while( e ) { + while ( e ) { relax(u, e->to ); e = e->next; } } } -int main() { - +int main() +{ int count = graph_from_dot(stdin, &g); clock_t start, end; |