diff options
Diffstat (limited to 'sem3/algo/mm12')
-rw-r--r-- | sem3/algo/mm12/dijkstra.c | 33 | ||||
-rw-r--r-- | sem3/algo/mm12/graph.c | 47 |
2 files changed, 46 insertions, 34 deletions
diff --git a/sem3/algo/mm12/dijkstra.c b/sem3/algo/mm12/dijkstra.c index 103c700..b4f0f94 100644 --- a/sem3/algo/mm12/dijkstra.c +++ b/sem3/algo/mm12/dijkstra.c @@ -14,7 +14,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; @@ -22,7 +23,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; @@ -33,25 +34,26 @@ 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) +{ list_item_t *smallest = s->begin; // Search list_item_t *i = s->begin; - while( i ) { - if( i->v->dist != -1 && i->v->dist < smallest->v->dist ) { + while ( i ) { + if ( 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; @@ -68,20 +70,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 && e->to->ref != v->ref) { + while (e && e->next && 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; @@ -90,13 +94,13 @@ 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 ) { @@ -106,7 +110,8 @@ void dijkstra(graph_t *g, vertex_t *s) { } } -int main() { +int main() +{ graph_edge(&g, 'a', 'b', 2); graph_edge(&g, 'a', 'f', 1); diff --git a/sem3/algo/mm12/graph.c b/sem3/algo/mm12/graph.c index 299ea24..e478103 100644 --- a/sem3/algo/mm12/graph.c +++ b/sem3/algo/mm12/graph.c @@ -2,7 +2,8 @@ #include <stdlib.h> #include "graph.h" -static vertex_t *create_vertex(char ref) { +static vertex_t *create_vertex(char ref) +{ // Get some space TODO check for null vertex_t *v = malloc(sizeof(vertex_t)); @@ -14,7 +15,8 @@ static vertex_t *create_vertex(char ref) { return v; } -static edge_t *create_edge(vertex_t *from, vertex_t *to, int weight) { +static edge_t *create_edge(vertex_t *from, vertex_t *to, int weight) +{ edge_t *old = from->adj; // Create new list node @@ -25,12 +27,12 @@ static edge_t *create_edge(vertex_t *from, vertex_t *to, int weight) { // Do new link e->next = old; - if( old ) { + if ( old ) { e->prev = old->prev; old->prev = e; } else { e->prev = NULL; } - if( e->prev ) { + if ( e->prev ) { e->prev->next = e; } @@ -40,14 +42,15 @@ static edge_t *create_edge(vertex_t *from, vertex_t *to, int weight) { } // For iterating edges -edge_t *edge_next(graph_t *g, edge_t *e) { - if(e == NULL || e->next == NULL ) { +edge_t *edge_next(graph_t *g, edge_t *e) +{ + if (e == NULL || e->next == NULL ) { // Find next index // Chose starting index 0 if NULL e unsigned char i = e ? e->from->ref+1 : 0; - for( ; i < 128; i++ ) { - if( g->vertexes[i] && g->vertexes[i]->adj ) { + for ( ; i < 128; i++ ) { + if ( g->vertexes[i] && g->vertexes[i]->adj ) { return g->vertexes[i]->adj; } } @@ -59,10 +62,11 @@ edge_t *edge_next(graph_t *g, edge_t *e) { return e->next; } -vertex_t *vertex_next(graph_t *g, vertex_t *v) { +vertex_t *vertex_next(graph_t *g, vertex_t *v) +{ unsigned char ref = v ? v->ref+1 : 0; - for( ; ref < 128; ref++ ) { - if( g->vertexes[ref] ) { + for ( ; ref < 128; ref++ ) { + if ( g->vertexes[ref] ) { return g->vertexes[ref]; } } @@ -70,13 +74,14 @@ vertex_t *vertex_next(graph_t *g, vertex_t *v) { return NULL; } -void graph_edge(graph_t *g, char from, char to, int weight) { +void graph_edge(graph_t *g, char from, char to, int weight) +{ // Does a exists - if( g->vertexes[from] == NULL ) { + if ( g->vertexes[from] == NULL ) { g->vertexes[from] = create_vertex(from); } // What about b - if( g->vertexes[to] == NULL ) { + if ( g->vertexes[to] == NULL ) { g->vertexes[to] = create_vertex(to); } @@ -84,14 +89,15 @@ void graph_edge(graph_t *g, char from, char to, int weight) { create_edge(g->vertexes[from], g->vertexes[to], weight); } -int graph_print_adj(graph_t *g, char ref) { - if( g->vertexes[ref] == NULL ) { +int graph_print_adj(graph_t *g, char ref) +{ + if ( g->vertexes[ref] == NULL ) { return 1; } edge_t *e = g->vertexes[ref]->adj; printf("[ "); - while(e && e->from->ref == ref) { + while (e && e->from->ref == ref) { printf("%c[%d] ", e->to->ref, e->weight); e = e->next; } @@ -100,19 +106,20 @@ int graph_print_adj(graph_t *g, char ref) { return 0; } -int graph_to_dot(FILE *f, graph_t *g) { +int graph_to_dot(FILE *f, graph_t *g) +{ // print pre stuff fprintf(f, "digraph coolgraph {\n"); // Label all nodes vertex_t *v = vertex_next(g, NULL); - while( v ) { + while ( v ) { fprintf(f, "%c [label=\"%c(%d)\"];\n", v->ref, v->ref, v->dist); v = vertex_next(g, v); } // Print all the edges edge_t *e = edge_next(g, NULL); - while( e ) { + while ( e ) { fprintf(f, "%c -> %c [label = %d%s];\n", e->from->ref, e->to->ref, e->weight, e->to->p == e->from ? " color=blue" : ""); e = edge_next(g, e); } |