aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2019-11-21 11:41:24 +0100
committerJulian T <julian@jtle.dk>2019-11-21 11:41:24 +0100
commit9eccc3f2f3f03ffdd9d7b985fecf7222bb956df3 (patch)
tree4adbb3eee59f6fbecb1bca0ad297d5090c92a39a
parent8a683b6e70de446d16b809fbcbf395153fc367b9 (diff)
Added dfs impl
-rw-r--r--sem1/algo/workshop2/dfs/Makefile41
-rw-r--r--sem1/algo/workshop2/dfs/dfs.c76
-rw-r--r--sem1/algo/workshop2/dfs/graph.c123
-rw-r--r--sem1/algo/workshop2/dfs/graph.h42
4 files changed, 282 insertions, 0 deletions
diff --git a/sem1/algo/workshop2/dfs/Makefile b/sem1/algo/workshop2/dfs/Makefile
new file mode 100644
index 0000000..790bb94
--- /dev/null
+++ b/sem1/algo/workshop2/dfs/Makefile
@@ -0,0 +1,41 @@
+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.c b/sem1/algo/workshop2/dfs/dfs.c
new file mode 100644
index 0000000..8c4faf6
--- /dev/null
+++ b/sem1/algo/workshop2/dfs/dfs.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <time.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( %c )\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);
+ v->p = u;
+ 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);
+ }
+}
+
+int main() {
+
+ graph_edge(&g, 'M', 'O', 1);
+ graph_edge(&g, 'O', 'P', 1);
+ graph_edge(&g, 'P', 'F', 1);
+
+ graph_edge(&g, 'C', 'F', 1);
+ graph_edge(&g, 'F', 'c', 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
new file mode 100644
index 0000000..502b1a9
--- /dev/null
+++ b/sem1/algo/workshop2/dfs/graph.c
@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "graph.h"
+
+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 = ref;
+ v->color = COLOR_WHITE;
+ v->p = 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
+
+ // 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 ) {
+ return g->vertexes[i]->adj;
+ }
+ }
+ // No next vertex
+ return NULL;
+ }
+
+ // Not finished with this adj list
+ return e->next;
+}
+
+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] ) {
+ return g->vertexes[ref];
+ }
+ }
+
+ return NULL;
+}
+
+void graph_edge(graph_t *g, char from, char to, int weight) {
+ // Does a exists
+ if( g->vertexes[from] == NULL ) {
+ g->vertexes[from] = create_vertex(from);
+ }
+ // What about b
+ if( g->vertexes[to] == NULL ) {
+ g->vertexes[to] = create_vertex(to);
+ }
+
+ // Add edge
+ create_edge(g->vertexes[from], g->vertexes[to], weight);
+}
+
+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) {
+ printf("%c[%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, "%c [label=\"%c\"];\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, "%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);
+ }
+
+ // done
+ fprintf(f, "}\n");
+}
+
diff --git a/sem1/algo/workshop2/dfs/graph.h b/sem1/algo/workshop2/dfs/graph.h
new file mode 100644
index 0000000..67ac1dc
--- /dev/null
+++ b/sem1/algo/workshop2/dfs/graph.h
@@ -0,0 +1,42 @@
+#ifndef GRAPH_H_
+#define GRAPH_H_
+
+#define COLOR_WHITE 0
+#define COLOR_GRAY 1
+#define COLOR_BLACK 2
+
+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;
+
+ struct vertex_struct *p;
+ edge_t *adj;
+} vertex_t;
+
+typedef struct {
+ vertex_t *vertexes[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_