diff options
author | Julian T <julian@jtle.dk> | 2020-02-11 12:24:56 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-02-11 12:24:56 +0100 |
commit | 6db1a2cdd3b96731f2e092d55d8c2136eabc52d0 (patch) | |
tree | 2be8fae8ce82d708ed9f00f376dda14420850e80 /sem1/algo/workshop2/dfs | |
parent | 57305119e05559c1c37e903aef89cd43f44c42c9 (diff) |
Rename and cleanup
Diffstat (limited to 'sem1/algo/workshop2/dfs')
-rw-r--r-- | sem1/algo/workshop2/dfs/Makefile | 41 | ||||
-rwxr-xr-x | sem1/algo/workshop2/dfs/dfs | bin | 24960 -> 0 bytes | |||
-rw-r--r-- | sem1/algo/workshop2/dfs/dfs.c | 107 | ||||
-rw-r--r-- | sem1/algo/workshop2/dfs/graph.c | 181 | ||||
-rw-r--r-- | sem1/algo/workshop2/dfs/graph.h | 45 |
5 files changed, 0 insertions, 374 deletions
diff --git a/sem1/algo/workshop2/dfs/Makefile b/sem1/algo/workshop2/dfs/Makefile deleted file mode 100644 index 790bb94..0000000 --- a/sem1/algo/workshop2/dfs/Makefile +++ /dev/null @@ -1,41 +0,0 @@ -CC = gcc -# Enable gdb and pull include files from current dir -CFLAGS = -ggdb -I. -LDFLAGS = - -BINARY = dfs -BUILD_DIR = build - -# Capture c files -c_files = $(wildcard *.c) - -# Convert c names to corrosponding o names -OBJ = $(patsubst %.c, $(BUILD_DIR)/%.o, $(c_files)) - -# $@ is the %.o file and $^ is the %.c file -$(BUILD_DIR)/%.o: %.c - mkdir -p $(dir $@) - $(CC) -c -o $@ $^ $(CFLAGS) - -# $@ becomes left part thus linked -$(BINARY): $(OBJ) - $(CC) -o $@ $^ $(LDFLAGS) - -.PHONY: clean run - -run: $(BINARY) - ./$(BINARY) - -hej.png: hej.dot - dot -Tpng hej.dot > hej.png - -hej.dot: $(BINARY) - ./$(BINARY) > hej.dot - -draw: hej.png - nomacs hej.png - -clean: - rm -f $(OBJ) $(BINARY) - rm hej* - rmdir $(BUILD_DIR) diff --git a/sem1/algo/workshop2/dfs/dfs b/sem1/algo/workshop2/dfs/dfs Binary files differdeleted file mode 100755 index 0d9ff14..0000000 --- a/sem1/algo/workshop2/dfs/dfs +++ /dev/null diff --git a/sem1/algo/workshop2/dfs/dfs.c b/sem1/algo/workshop2/dfs/dfs.c deleted file mode 100644 index 1244895..0000000 --- a/sem1/algo/workshop2/dfs/dfs.c +++ /dev/null @@ -1,107 +0,0 @@ -#include <stdio.h> -#include <stdbool.h> -#include <time.h> -#include <string.h> -#include <stdlib.h> - -#include "graph.h" - -graph_t g; -graph_t newg; -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; -} - - -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); - } -} - -#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); - - return 0; -} diff --git a/sem1/algo/workshop2/dfs/graph.c b/sem1/algo/workshop2/dfs/graph.c deleted file mode 100644 index 0f5048c..0000000 --- a/sem1/algo/workshop2/dfs/graph.c +++ /dev/null @@ -1,181 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#include "graph.h" - -// 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; -} - - -static void table_insert(graph_t *g, vertex_t *v, char *var) { - unsigned int index = hash(var); - - // Save old value - vertex_t *oldSym = g->hashtable[index]; - - // Make new - g->hashtable[index] = v; - - // 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]; - - // Look trough list - while(n != NULL && strcmp(n->ref, var) != 0) { - n = n->next; - } - - 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; -} - -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; -} - -// 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; -} - -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; -} - -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); -} - -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; -} - -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"); -} - diff --git a/sem1/algo/workshop2/dfs/graph.h b/sem1/algo/workshop2/dfs/graph.h deleted file mode 100644 index e169a8a..0000000 --- a/sem1/algo/workshop2/dfs/graph.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef GRAPH_H_ -#define GRAPH_H_ - -#define COLOR_WHITE 0 -#define COLOR_GRAY 1 -#define COLOR_BLACK 2 - -#define HASHSIZE 128 - -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; -} edge_t; - -typedef struct vertex_struct { - char *ref; - int color; - - int dtime; - int ftime; - - edge_t *adj; - - // Hash table stuff - struct vertex_struct *next; -} vertex_t; - -typedef struct { - vertex_t *hashtable[128]; -} graph_t; - -int graph_to_dot(FILE *f, graph_t *g); -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); - -#endif // GRAPH_H_ |