diff options
author | Julian T <julian@jtle.dk> | 2020-10-12 16:26:42 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-10-12 16:54:48 +0200 |
commit | 4e05a55e373bd315e721d534a1711fec4c0054c5 (patch) | |
tree | 4fc0141760c2730ed79aaa4c7aa60ea45cc66cc0 /sem3/algo/workshop2 | |
parent | b7f9cf43c8a9ab3400cbb30d5e1cadb0c6c2cf23 (diff) |
Moved to linux kernel inspired clang-format
Diffstat (limited to 'sem3/algo/workshop2')
-rw-r--r-- | sem3/algo/workshop2/dfs/dfs.c | 166 | ||||
-rw-r--r-- | sem3/algo/workshop2/dfs/graph.c | 279 | ||||
-rw-r--r-- | sem3/algo/workshop2/dfs/graph.h | 30 | ||||
-rw-r--r-- | sem3/algo/workshop2/dijkstra/dijkstra.c | 189 | ||||
-rw-r--r-- | sem3/algo/workshop2/dijkstra/graph.c | 340 | ||||
-rw-r--r-- | sem3/algo/workshop2/dijkstra/graph.h | 30 |
6 files changed, 517 insertions, 517 deletions
diff --git a/sem3/algo/workshop2/dfs/dfs.c b/sem3/algo/workshop2/dfs/dfs.c index aa80519..b2ce82b 100644 --- a/sem3/algo/workshop2/dfs/dfs.c +++ b/sem3/algo/workshop2/dfs/dfs.c @@ -12,99 +12,97 @@ int step; void dfs_visit(graph_t *g, vertex_t *u, bool doprint) { - if ( doprint ) { - printf("dfs_visit( %s )\n", u->ref); - } - step++; - u->dtime = step; - u->color = COLOR_GRAY; - - // For every adj - edge_t *e = u->adj; - while ( e ) { - vertex_t *v = e->to; - if ( v->color == COLOR_WHITE ) { - graph_edge(&newg, v->ref, u->ref, 1); - dfs_visit(g, v, doprint); - } - e = e->next; - } - - u->color = COLOR_BLACK; - step++; - u->ftime = step; + if (doprint) { + printf("dfs_visit( %s )\n", u->ref); + } + step++; + u->dtime = step; + u->color = COLOR_GRAY; + + // For every adj + edge_t *e = u->adj; + while (e) { + vertex_t *v = e->to; + if (v->color == COLOR_WHITE) { + graph_edge(&newg, v->ref, u->ref, 1); + dfs_visit(g, v, doprint); + } + e = e->next; + } + + u->color = COLOR_BLACK; + step++; + u->ftime = step; } - void dfs(graph_t *g, bool doprint) { - step = 0; - - // For every edge - vertex_t *u = vertex_next(g, NULL); - while ( u ) { - if ( u->color == COLOR_WHITE ) { - dfs_visit(g, u, doprint); - } - u = vertex_next(g, u); - } + step = 0; + + // For every edge + vertex_t *u = vertex_next(g, NULL); + while (u) { + if (u->color == COLOR_WHITE) { + dfs_visit(g, u, doprint); + } + u = vertex_next(g, u); + } } #define BENCH 9999 int main() { + // benchmark + + // Start by generating + for (int i = 0; i < BENCH; i++) { + char from[5]; + char to[5]; + sprintf(from, "%d", i); + sprintf(to, "%d", rand() % BENCH); + graph_edge(&g, from, to, 1); + } + FILE *f = fopen("hej.dot", "w"); + graph_to_dot(f, &g); + + { + // Timing start + clock_t t; + t = clock(); + + dfs(&g, true); + + // Timing stop + t = clock() - t; + double time_taken = ((double)t) / CLOCKS_PER_SEC; // in seconds + printf("Dfs took %f seconds\n", time_taken); + } + + return 0; + + graph_edge(&g, "hej", "med", 1); + graph_edge(&g, "med", "dig", 1); + graph_edge(&g, "dig", "hvordan", 1); + + graph_edge(&g, "det", "hvordan", 1); + graph_edge(&g, "hvordan", "det", 1); + + graph_to_dot(stdout, &g); + + // Timing start + clock_t t; + t = clock(); + + // Dfs slower when printing. + dfs(&g, true); + + // Timing stop + t = clock() - t; + double time_taken = ((double)t) / CLOCKS_PER_SEC; // in seconds + printf("Dfs took %f seconds\n", time_taken); + + //graph_to_dot(stdout, &newg); - // benchmark - - // Start by generating - for (int i = 0; i < BENCH; i++) { - char from[5]; - char to[5]; - sprintf(from, "%d", i); - sprintf(to, "%d", rand() % BENCH); - graph_edge(&g, from, to, 1); - } - FILE *f = fopen("hej.dot", "w"); - graph_to_dot(f, &g); - - { - // Timing start - clock_t t; - t = clock(); - - dfs(&g, true); - - // Timing stop - t = clock() - t; - double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds - printf("Dfs took %f seconds\n", time_taken); - } - - return 0; - - graph_edge(&g, "hej", "med", 1); - graph_edge(&g, "med", "dig", 1); - graph_edge(&g, "dig", "hvordan", 1); - - graph_edge(&g, "det", "hvordan", 1); - graph_edge(&g, "hvordan", "det", 1); - - graph_to_dot(stdout, &g); - - // Timing start - clock_t t; - t = clock(); - - // Dfs slower when printing. - dfs(&g, true); - - // Timing stop - t = clock() - t; - double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds - printf("Dfs took %f seconds\n", time_taken); - - //graph_to_dot(stdout, &newg); - - return 0; + return 0; } diff --git a/sem3/algo/workshop2/dfs/graph.c b/sem3/algo/workshop2/dfs/graph.c index 0ad4467..c1bf871 100644 --- a/sem3/algo/workshop2/dfs/graph.c +++ b/sem3/algo/workshop2/dfs/graph.c @@ -8,186 +8,185 @@ // Hashtable static unsigned int hash(char *s) { - uint32_t hv = 0; - for( int i = 0; s[i] != '\0'; i++ ) { - // take MSB 6 bits of hv and xors with LSB of s[i] - uint32_t v = ( hv >> 26 ) ^ (s[i] & 0x3f); - - // Push those back on hv - hv = (hv << 4) | v; - } - // Return appropriate size - return hv % HASHSIZE; + uint32_t hv = 0; + for (int i = 0; s[i] != '\0'; i++) { + // take MSB 6 bits of hv and xors with LSB of s[i] + uint32_t v = (hv >> 26) ^ (s[i] & 0x3f); + + // Push those back on hv + hv = (hv << 4) | v; + } + // Return appropriate size + return hv % HASHSIZE; } - static void table_insert(graph_t *g, vertex_t *v, char *var) { - unsigned int index = hash(var); + unsigned int index = hash(var); - // Save old value - vertex_t *oldSym = g->hashtable[index]; + // Save old value + vertex_t *oldSym = g->hashtable[index]; - // Make new - g->hashtable[index] = v; + // Make new + g->hashtable[index] = v; - // Link old one - g->hashtable[index]->next = oldSym; + // Link old one + g->hashtable[index]->next = oldSym; } static vertex_t *table_lookup(graph_t *g, char *var) { - unsigned int index = hash(var); - vertex_t *n = g->hashtable[index]; + unsigned int index = hash(var); + vertex_t *n = g->hashtable[index]; - // Look trough list - while (n != NULL && strcmp(n->ref, var) != 0) { - n = n->next; - } + // Look trough list + while (n != NULL && strcmp(n->ref, var) != 0) { + n = n->next; + } - return n; + return n; } static vertex_t *create_vertex(char *ref) { - // Get some space TODO check for null - vertex_t *v = malloc(sizeof(vertex_t)); - - // Set values - v->ref = strdup(ref); - v->color = COLOR_WHITE; - v->next = NULL; - v->adj = NULL; - return v; + // Get some space TODO check for null + vertex_t *v = malloc(sizeof(vertex_t)); + + // Set values + v->ref = strdup(ref); + v->color = COLOR_WHITE; + v->next = NULL; + v->adj = NULL; + return v; } static edge_t *create_edge(vertex_t *from, vertex_t *to, int weight) { - edge_t *old = from->adj; - - // Create new list node - edge_t *e = malloc(sizeof(edge_t)); - e->weight = weight; - e->from = from; - e->to = to; - - // Do new link - e->next = old; - if ( old ) { - e->prev = old->prev; - old->prev = e; - } else { - e->prev = NULL; - } - - if ( e->prev ) { - e->prev->next = e; - } - - from->adj = e; - - return e; + edge_t *old = from->adj; + + // Create new list node + edge_t *e = malloc(sizeof(edge_t)); + e->weight = weight; + e->from = from; + e->to = to; + + // Do new link + e->next = old; + if (old) { + e->prev = old->prev; + old->prev = e; + } else { + e->prev = NULL; + } + + if (e->prev) { + e->prev->next = e; + } + + from->adj = e; + + return e; } // For iterating edges edge_t *edge_next(graph_t *g, edge_t *e) { - if (e == NULL || e->next == NULL ) { - // Find next index - vertex_t *v = e ? e->from : NULL; - - // Find next vertex - v = vertex_next(g, v); - while ( v ) { - // Check if found - if ( v->adj ) { - return v->adj; - } - - v = vertex_next(g, v); - } - - // No next vertex - return NULL; - } - - // Not finished with this adj list - return e->next; + if (e == NULL || e->next == NULL) { + // Find next index + vertex_t *v = e ? e->from : NULL; + + // Find next vertex + v = vertex_next(g, v); + while (v) { + // Check if found + if (v->adj) { + return v->adj; + } + + v = vertex_next(g, v); + } + + // No next vertex + return NULL; + } + + // Not finished with this adj list + return e->next; } vertex_t *vertex_next(graph_t *g, vertex_t *v) { - if ( v == NULL || v->next == NULL) { - // Go to next index in hashtable - int i = v ? hash(v->ref)+1 : 0; - for ( ; i < HASHSIZE; i++) { - if ( g->hashtable[i] ) { - return g->hashtable[i]; - } - } - - // No next - return NULL; - } - - return v->next; + if (v == NULL || v->next == NULL) { + // Go to next index in hashtable + int i = v ? hash(v->ref) + 1 : 0; + for (; i < HASHSIZE; i++) { + if (g->hashtable[i]) { + return g->hashtable[i]; + } + } + + // No next + return NULL; + } + + return v->next; } void graph_edge(graph_t *g, char *from, char *to, int weight) { - vertex_t *fromv, *tov; - // Does a exists - if ( (fromv = table_lookup(g, from)) == NULL ) { - fromv = create_vertex(from); - table_insert(g, fromv, from); - } - // What about b - if ( (tov = table_lookup(g, to)) == NULL ) { - tov = create_vertex(to); - table_insert(g, tov, to); - } - - // Add edge - create_edge(fromv, tov, weight); + vertex_t *fromv, *tov; + // Does a exists + if ((fromv = table_lookup(g, from)) == NULL) { + fromv = create_vertex(from); + table_insert(g, fromv, from); + } + // What about b + if ((tov = table_lookup(g, to)) == NULL) { + tov = create_vertex(to); + table_insert(g, tov, to); + } + + // Add edge + create_edge(fromv, tov, weight); } int graph_print_adj(graph_t *g, char *ref) { - vertex_t *v = table_lookup(g, ref); - if ( v == NULL ) { - return 1; - } - - edge_t *e = v->adj; - printf("[ "); - while (e && e->from->ref == ref) { - printf("%s[%d] ", e->to->ref, e->weight); - e = e->next; - } - printf("]\n"); - - return 0; + vertex_t *v = table_lookup(g, ref); + if (v == NULL) { + return 1; + } + + edge_t *e = v->adj; + printf("[ "); + while (e && e->from->ref == ref) { + printf("%s[%d] ", e->to->ref, e->weight); + e = e->next; + } + printf("]\n"); + + return 0; } 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 ) { - fprintf(f, "%s [label=\"%s\"];\n", v->ref, v->ref); - 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); - e = edge_next(g, e); - } - - // done - fprintf(f, "}\n"); + // print pre stuff + fprintf(f, "digraph coolgraph {\n"); + + // Label all nodes + vertex_t *v = vertex_next(g, NULL); + while (v) { + fprintf(f, "%s [label=\"%s\"];\n", v->ref, v->ref); + 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); + e = edge_next(g, e); + } + + // done + fprintf(f, "}\n"); } - diff --git a/sem3/algo/workshop2/dfs/graph.h b/sem3/algo/workshop2/dfs/graph.h index e169a8a..ad61b1e 100644 --- a/sem3/algo/workshop2/dfs/graph.h +++ b/sem3/algo/workshop2/dfs/graph.h @@ -2,7 +2,7 @@ #define GRAPH_H_ #define COLOR_WHITE 0 -#define COLOR_GRAY 1 +#define COLOR_GRAY 1 #define COLOR_BLACK 2 #define HASHSIZE 128 @@ -11,29 +11,29 @@ struct vertex_struct; // Linked list typedef struct edge_struct { - int weight; - struct vertex_struct *from; - struct vertex_struct *to; - // Linked list stuff - struct edge_struct *next; - struct edge_struct *prev; + int weight; + struct vertex_struct *from; + struct vertex_struct *to; + // Linked list stuff + struct edge_struct *next; + struct edge_struct *prev; } edge_t; typedef struct vertex_struct { - char *ref; - int color; + char *ref; + int color; - int dtime; - int ftime; + int dtime; + int ftime; - edge_t *adj; + edge_t *adj; - // Hash table stuff - struct vertex_struct *next; + // Hash table stuff + struct vertex_struct *next; } vertex_t; typedef struct { - vertex_t *hashtable[128]; + vertex_t *hashtable[128]; } graph_t; int graph_to_dot(FILE *f, graph_t *g); diff --git a/sem3/algo/workshop2/dijkstra/dijkstra.c b/sem3/algo/workshop2/dijkstra/dijkstra.c index c112c6d..d68492a 100644 --- a/sem3/algo/workshop2/dijkstra/dijkstra.c +++ b/sem3/algo/workshop2/dijkstra/dijkstra.c @@ -5,69 +5,70 @@ #include "graph.h" typedef struct list_item { - vertex_t *v; - struct list_item *next; - struct list_item *prev; + vertex_t *v; + struct list_item *next; + struct list_item *prev; } list_item_t; typedef struct { - int len; - struct list_item *begin; - struct list_item *end; + int len; + struct list_item *begin; + struct list_item *end; } list_t; void list_push(list_t *s, vertex_t *v) { - // Create - list_item_t *i = malloc(sizeof(list_item_t)); - i->next = NULL; - i->prev = NULL; - i->v = v; - - // Link - if ( s->len == 0 ) { - s->begin = i; - } else { - s->end->next = i; - i->prev = s->end; - } - - s->end = i; - s->len++; + // Create + list_item_t *i = malloc(sizeof(list_item_t)); + i->next = NULL; + i->prev = NULL; + i->v = v; + + // Link + if (s->len == 0) { + s->begin = i; + } else { + s->end->next = i; + i->prev = s->end; + } + + s->end = i; + s->len++; } 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) ) { - smallest = i; - } - i = i->next; - } - - if ( smallest == s->begin ) { - s->begin = smallest->next; - } else { - smallest->prev->next = smallest->next; - } - - if ( smallest == s->end ) { - s->end = smallest->prev; - } else { - smallest->next->prev = smallest->prev; - } - - vertex_t *v = smallest->v; - free(smallest); - - s->len--; - - return v; + //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)) { + smallest = i; + } + i = i->next; + } + + if (smallest == s->begin) { + s->begin = smallest->next; + } else { + smallest->prev->next = smallest->next; + } + + if (smallest == s->end) { + s->end = smallest->prev; + } else { + smallest->next->prev = smallest->prev; + } + + vertex_t *v = smallest->v; + free(smallest); + + s->len--; + + return v; } graph_t g; @@ -75,59 +76,59 @@ graph_t g; // Assumes u has an edge to 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)) { - e = e->next; - } - - if ( v->dist == -1 || v->dist > (u->dist + e->weight) ) { - v->dist = u->dist + e->weight; - v->p = u; - } + // Get edge between these two guys + edge_t *e = u->adj; + while (e && e->next && strcmp(e->to->ref, v->ref)) { + e = e->next; + } + + 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) { - list_t list; - list.len = 0; - list.end = list.begin = 0; - - s->dist = 0; - - { - vertex_t *v = vertex_next(g, NULL); - while ( v ) { - list_push(&list, v); - v = vertex_next(g, v); - } - } - - while ( list.len ) { - vertex_t *u = list_pop_smallest(&list); - edge_t *e = u->adj; - while ( e ) { - relax(u, e->to ); - e = e->next; - } - } + list_t list; + list.len = 0; + list.end = list.begin = 0; + + s->dist = 0; + + { + vertex_t *v = vertex_next(g, NULL); + while (v) { + list_push(&list, v); + v = vertex_next(g, v); + } + } + + while (list.len) { + vertex_t *u = list_pop_smallest(&list); + edge_t *e = u->adj; + while (e) { + relax(u, e->to); + e = e->next; + } + } } int main() { - int count = graph_from_dot(stdin, &g); + int count = graph_from_dot(stdin, &g); - clock_t start, end; - double cpu_time_used; + clock_t start, end; + double cpu_time_used; - start = clock(); - dijkstra(&g, graph_vertex(&g, "0")); - end = clock(); - cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; + start = clock(); + dijkstra(&g, graph_vertex(&g, "0")); + end = clock(); + cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC; - printf("%d;%f\n", count, cpu_time_used); + printf("%d;%f\n", count, cpu_time_used); - //graph_to_dot(stdout, &g); + //graph_to_dot(stdout, &g); - return 0; + return 0; } diff --git a/sem3/algo/workshop2/dijkstra/graph.c b/sem3/algo/workshop2/dijkstra/graph.c index 51206c4..4f2e88f 100644 --- a/sem3/algo/workshop2/dijkstra/graph.c +++ b/sem3/algo/workshop2/dijkstra/graph.c @@ -8,224 +8,226 @@ // Hashtable static unsigned int hash(char *s) { - uint32_t hv = 0; - for ( int i = 0; s[i] != '\0'; i++ ) { - // take MSB 6 bits of hv and xors with LSB of s[i] - uint32_t v = ( hv >> 26 ) ^ (s[i] & 0x3f); - - // Push those back on hv - hv = (hv << 4) | v; - } - // Return appropriate size - return hv % HASHSIZE; + uint32_t hv = 0; + for (int i = 0; s[i] != '\0'; i++) { + // take MSB 6 bits of hv and xors with LSB of s[i] + uint32_t v = (hv >> 26) ^ (s[i] & 0x3f); + + // Push those back on hv + hv = (hv << 4) | v; + } + // Return appropriate size + return hv % HASHSIZE; } - static void table_insert(graph_t *g, vertex_t *v, char *var) { - unsigned int index = hash(var); + unsigned int index = hash(var); - // Save old value - vertex_t *oldSym = g->hashtable[index]; + // Save old value + vertex_t *oldSym = g->hashtable[index]; - // Make new - g->hashtable[index] = v; + // Make new + g->hashtable[index] = v; - // Link old one - g->hashtable[index]->next = oldSym; + // Link old one + g->hashtable[index]->next = oldSym; } static vertex_t *table_lookup(graph_t *g, char *var) { - unsigned int index = hash(var); - vertex_t *n = g->hashtable[index]; + unsigned int index = hash(var); + vertex_t *n = g->hashtable[index]; - // Look trough list - while (n != NULL && strcmp(n->ref, var) != 0) { - n = n->next; - } + // Look trough list + while (n != NULL && strcmp(n->ref, var) != 0) { + n = n->next; + } - return n; + return n; } static vertex_t *create_vertex(char *ref) { - // Get some space TODO check for null - vertex_t *v = malloc(sizeof(vertex_t)); - - // Set values - v->ref = strdup(ref); - v->color = COLOR_WHITE; - v->p = NULL; - v->dist = -1; - v->adj = NULL; - return v; + // Get some space TODO check for null + vertex_t *v = malloc(sizeof(vertex_t)); + + // Set values + v->ref = strdup(ref); + v->color = COLOR_WHITE; + v->p = NULL; + v->dist = -1; + v->adj = NULL; + return v; } static edge_t *create_edge(vertex_t *from, vertex_t *to, int weight) { - edge_t *old = from->adj; - - // Create new list node - edge_t *e = malloc(sizeof(edge_t)); - e->weight = weight; - e->from = from; - e->to = to; - - // Do new link - e->next = old; - if ( old ) { - e->prev = old->prev; - old->prev = e; - } else { e->prev = NULL; } - - if ( e->prev ) { - e->prev->next = e; - } - - from->adj = e; - - return e; + edge_t *old = from->adj; + + // Create new list node + edge_t *e = malloc(sizeof(edge_t)); + e->weight = weight; + e->from = from; + e->to = to; + + // Do new link + e->next = old; + if (old) { + e->prev = old->prev; + old->prev = e; + } else { + e->prev = NULL; + } + + if (e->prev) { + e->prev->next = e; + } + + from->adj = e; + + return e; } // For iterating edges edge_t *edge_next(graph_t *g, edge_t *e) { - if (e == NULL || e->next == NULL ) { - // Find next index - vertex_t *v = e ? e->from : NULL; - - // Find next vertex - v = vertex_next(g, v); - while ( v ) { - // Check if found - if ( v->adj ) { - return v->adj; - } - - v = vertex_next(g, v); - } - - // No next vertex - return NULL; - } - - // Not finished with this adj list - return e->next; + if (e == NULL || e->next == NULL) { + // Find next index + vertex_t *v = e ? e->from : NULL; + + // Find next vertex + v = vertex_next(g, v); + while (v) { + // Check if found + if (v->adj) { + return v->adj; + } + + v = vertex_next(g, v); + } + + // No next vertex + return NULL; + } + + // Not finished with this adj list + return e->next; } vertex_t *vertex_next(graph_t *g, vertex_t *v) { - if ( v == NULL || v->next == NULL) { - // Go to next index in hashtable - int i = v ? hash(v->ref)+1 : 0; - for ( ; i < HASHSIZE; i++) { - if ( g->hashtable[i] ) { - return g->hashtable[i]; - } - } - - // No next - return NULL; - } - - return v->next; + if (v == NULL || v->next == NULL) { + // Go to next index in hashtable + int i = v ? hash(v->ref) + 1 : 0; + for (; i < HASHSIZE; i++) { + if (g->hashtable[i]) { + return g->hashtable[i]; + } + } + + // No next + return NULL; + } + + return v->next; } void graph_edge(graph_t *g, char *from, char *to, int weight) { - vertex_t *fromv, *tov; - // Does a exists - if ( (fromv = table_lookup(g, from)) == NULL ) { - fromv = create_vertex(from); - table_insert(g, fromv, from); - } - // What about b - if ( (tov = table_lookup(g, to)) == NULL ) { - tov = create_vertex(to); - table_insert(g, tov, to); - } - - // Add edge - create_edge(fromv, tov, weight); + vertex_t *fromv, *tov; + // Does a exists + if ((fromv = table_lookup(g, from)) == NULL) { + fromv = create_vertex(from); + table_insert(g, fromv, from); + } + // What about b + if ((tov = table_lookup(g, to)) == NULL) { + tov = create_vertex(to); + table_insert(g, tov, to); + } + + // Add edge + create_edge(fromv, tov, weight); } int graph_print_adj(graph_t *g, char *ref) { - vertex_t *v = table_lookup(g, ref); - if ( v == NULL ) { - return 1; - } - - edge_t *e = v->adj; - printf("[ "); - while (e && e->from->ref == ref) { - printf("%s[%d] ", e->to->ref, e->weight); - e = e->next; - } - printf("]\n"); - - return 0; + vertex_t *v = table_lookup(g, ref); + if (v == NULL) { + return 1; + } + + edge_t *e = v->adj; + printf("[ "); + while (e && e->from->ref == ref) { + printf("%s[%d] ", e->to->ref, e->weight); + e = e->next; + } + printf("]\n"); + + return 0; } 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 ) { - 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%s];\n", e->from->ref, e->to->ref, e->weight, e->to->p == e->from ? " color=blue" : ""); - e = edge_next(g, e); - } - - // done - fprintf(f, "}\n"); + // print pre stuff + fprintf(f, "digraph coolgraph {\n"); + + // Label all nodes + vertex_t *v = vertex_next(g, NULL); + while (v) { + 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%s];\n", e->from->ref, e->to->ref, + e->weight, e->to->p == e->from ? " color=blue" : ""); + e = edge_next(g, e); + } + + // done + fprintf(f, "}\n"); } // This was created in a hurry sorry int graph_from_dot(FILE *f, graph_t *g) { - char from[100], to[100]; - int weight; - int count = 0; - // just fscanf it - for (;;) { - // Set first to zero - *from = 0; - *to = 0; - weight = 0; - 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); - - graph_edge(g, from, to, weight); - count++; - } - - return count; + char from[100], to[100]; + int weight; + int count = 0; + // just fscanf it + for (;;) { + // Set first to zero + *from = 0; + *to = 0; + weight = 0; + 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); + + graph_edge(g, from, to, weight); + count++; + } + + return count; } vertex_t *graph_vertex(graph_t *g, char *ref) { - return table_lookup(g, ref); + return table_lookup(g, ref); } diff --git a/sem3/algo/workshop2/dijkstra/graph.h b/sem3/algo/workshop2/dijkstra/graph.h index 5c078b8..75b5eab 100644 --- a/sem3/algo/workshop2/dijkstra/graph.h +++ b/sem3/algo/workshop2/dijkstra/graph.h @@ -2,7 +2,7 @@ #define GRAPH_H_ #define COLOR_WHITE 0 -#define COLOR_GRAY 1 +#define COLOR_GRAY 1 #define COLOR_BLACK 2 #define HASHSIZE 128 @@ -11,29 +11,29 @@ struct vertex_struct; // Linked list typedef struct edge_struct { - int weight; - struct vertex_struct *from; - struct vertex_struct *to; - // Linked list stuff - struct edge_struct *next; - struct edge_struct *prev; + int weight; + struct vertex_struct *from; + struct vertex_struct *to; + // Linked list stuff + struct edge_struct *next; + struct edge_struct *prev; } edge_t; typedef struct vertex_struct { - char *ref; - int color; + char *ref; + int color; - int dist; - struct vertex_struct *p; + int dist; + struct vertex_struct *p; - edge_t *adj; + edge_t *adj; - // Hash table stuff - struct vertex_struct *next; + // Hash table stuff + struct vertex_struct *next; } vertex_t; typedef struct { - vertex_t *hashtable[128]; + vertex_t *hashtable[128]; } graph_t; int graph_to_dot(FILE *f, graph_t *g); |