diff options
-rw-r--r-- | sem1/algo/workshop2/dijkstra/dijkstra.c | 8 | ||||
-rw-r--r-- | sem1/algo/workshop2/dijkstra/graph.c | 19 | ||||
-rw-r--r-- | sem1/algo/workshop2/dijkstra/graph.h | 1 |
3 files changed, 19 insertions, 9 deletions
diff --git a/sem1/algo/workshop2/dijkstra/dijkstra.c b/sem1/algo/workshop2/dijkstra/dijkstra.c index 145acc0..555a125 100644 --- a/sem1/algo/workshop2/dijkstra/dijkstra.c +++ b/sem1/algo/workshop2/dijkstra/dijkstra.c @@ -1,5 +1,6 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "graph.h" typedef struct list_item { @@ -34,12 +35,13 @@ void list_push(list_t *s, vertex_t *v) { } 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( i->v->dist != -1 && i->v->dist < smallest->v->dist ) { + if( smallest->v->dist == -1 || (i->v->dist != -1 && i->v->dist < smallest->v->dist) ) { smallest = i; } i = i->next; @@ -71,7 +73,7 @@ graph_t g; 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 && strcmp(e->to->ref, v->ref)) { e = e->next; } @@ -110,7 +112,7 @@ int main() { graph_from_dot(stdin, &g); - //dijkstra(&g, g.vertexes['a']); + dijkstra(&g, graph_vertex(&g, "0")); graph_to_dot(stdout, &g); diff --git a/sem1/algo/workshop2/dijkstra/graph.c b/sem1/algo/workshop2/dijkstra/graph.c index 8b84c75..8f62981 100644 --- a/sem1/algo/workshop2/dijkstra/graph.c +++ b/sem1/algo/workshop2/dijkstra/graph.c @@ -166,13 +166,13 @@ int graph_to_dot(FILE *f, graph_t *g) { // Label all nodes vertex_t *v = vertex_next(g, NULL); while( v ) { - fprintf(f, "%s [label=\"%s\"];\n", v->ref, v->ref); + fprintf(f, "%s [label=\"%s(%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 ) { - fprintf(f, "%s -> %s [label = %d];\n", e->from->ref, e->to->ref, e->weight); + fprintf(f, "%s -> %s [label = %d%s];\n", e->from->ref, e->to->ref, e->weight, e->to->p == e->from ? " color=blue" : ""); e = edge_next(g, e); } @@ -190,19 +190,26 @@ int graph_from_dot(FILE *f, graph_t *g) { *from = 0; *to = 0; weight = 0; - int c = fscanf(f, "%s -> %s [label = %d];", from, to, &weight); + int c = fscanf(f, "%s -> %s [label=%d];", from, to, &weight); if( c <= 0 ) { break; } if( *from == 0 || *to == 0 ) { continue; } + // Sorry i just want it to work + int tolen = strlen(to); + if( to[tolen-1] == ';' ) { + to[tolen-1] = 0; + } weight = weight ? weight : 1; - printf("Creating edge from %s to %s with w: %d\n", from, to, weight); - - + //printf("Creating edge from %s to %s with w: %d\n", from, to, weight); graph_edge(g, from, to, weight); } } + +vertex_t *graph_vertex(graph_t *g, char *ref) { + return table_lookup(g, ref); +} diff --git a/sem1/algo/workshop2/dijkstra/graph.h b/sem1/algo/workshop2/dijkstra/graph.h index 484fea4..5c078b8 100644 --- a/sem1/algo/workshop2/dijkstra/graph.h +++ b/sem1/algo/workshop2/dijkstra/graph.h @@ -42,5 +42,6 @@ int graph_print_adj(graph_t *g, char *ref); void graph_edge(graph_t *g, char *from, char *to, int weight); edge_t *edge_next(graph_t *g, edge_t *e); vertex_t *vertex_next(graph_t *g, vertex_t *v); +vertex_t *graph_vertex(graph_t *g, char *ref); #endif // GRAPH_H_ |