diff options
Diffstat (limited to 'sem3/algo')
36 files changed, 2397 insertions, 0 deletions
diff --git a/sem3/algo/.gitignore b/sem3/algo/.gitignore new file mode 100644 index 0000000..38aee6d --- /dev/null +++ b/sem3/algo/.gitignore @@ -0,0 +1,2 @@ +**/a.out +**/build diff --git a/sem3/algo/graph/Cargo.lock b/sem3/algo/graph/Cargo.lock new file mode 100644 index 0000000..74fba54 --- /dev/null +++ b/sem3/algo/graph/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "graph" +version = "0.1.0" + diff --git a/sem3/algo/graph/Cargo.toml b/sem3/algo/graph/Cargo.toml new file mode 100644 index 0000000..896f437 --- /dev/null +++ b/sem3/algo/graph/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "graph" +version = "0.1.0" +authors = ["Julian T <julian@jtle.dk>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/sem3/algo/graph/src/graph.rs b/sem3/algo/graph/src/graph.rs new file mode 100644 index 0000000..cefd24f --- /dev/null +++ b/sem3/algo/graph/src/graph.rs @@ -0,0 +1,85 @@ +use std::rc::{Rc, Weak}; +use std::cell::RefCell; +use std::vec::Vec; +use std::string::String; +use std::collections::HashMap; +use std::fmt; + +type EdgeRef = RefCell<Edge>; +type VertexRef = RefCell<Vertex>; + +pub struct Edge { + from: Weak<VertexRef>, + to: Weak<VertexRef>, + weight: i32, +} + +pub struct Vertex { + name: String, + pub adj: Vec<Rc<EdgeRef>>, +} + +pub struct Graph { + vertexes: HashMap<String, Rc<VertexRef>>, + edges: Vec<Rc<EdgeRef>>, +} + +impl Vertex { + pub fn new(name: &str) -> VertexRef { + RefCell::new( Vertex { name: String::from(name), adj: Vec::new() }) + } +} + +impl Graph { + pub fn new() -> Graph { + Graph { vertexes: HashMap::new(), edges: Vec::new() } + } + + pub fn edge(&mut self, from: &str, to: &str, weight: i32) { + let from = match self.vertexes.get(from) { + Some(rc) => Rc::clone(rc), + None => self.add_vertex(from), + }; + + let to = match self.vertexes.get(to) { + Some(rc) => Rc::clone(rc), + None => self.add_vertex(to), + }; + + // Create edge + let e = Rc::new(RefCell::new( Edge { from: Rc::downgrade(&from), to: Rc::downgrade(&to), weight } )); + + // Add to stuff + from.borrow_mut().adj.push(Rc::clone(&e)); + self.edges.push(e); + + } + + pub fn borrow_vertex(&self, name: &str) -> Option<Rc<VertexRef>> { + self.vertexes.get(name).and_then(|rc| Some(Rc::clone(rc))) + } + + fn add_vertex(&mut self, name: &str) -> Rc<VertexRef> { + let v = Rc::new( Vertex::new(name) ); + + // Insert in hashmap + self.vertexes.insert(String::from(name), Rc::clone(&v)); + + // Return vertex + v + } + +} + +impl fmt::Display for Edge { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let rc = self.to.upgrade().unwrap(); + write!(f, "{}", Rc::clone(&rc).borrow()) + } +} + +impl fmt::Display for Vertex { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.name) + } +} diff --git a/sem3/algo/graph/src/main.rs b/sem3/algo/graph/src/main.rs new file mode 100644 index 0000000..4f732e7 --- /dev/null +++ b/sem3/algo/graph/src/main.rs @@ -0,0 +1,23 @@ + +mod graph; + +use graph::Graph; + +fn main() { + + let mut g = Graph::new(); + + g.edge("a", "b", 10); + g.edge("a", "c", 10); + g.edge("a", "d", 10); + + // Ahh this is hard + let v = g.borrow_vertex("a").unwrap(); + let v23 = v.borrow(); + let mut it = v23.adj.iter(); + for e in it { + println!("{}", e.borrow()); + } + + println!("Hello, world!"); +} diff --git a/sem3/algo/lek1/merge.c b/sem3/algo/lek1/merge.c new file mode 100644 index 0000000..997bcb8 --- /dev/null +++ b/sem3/algo/lek1/merge.c @@ -0,0 +1,115 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <stdint.h> + +#define DO_PRINT 0 + +void printArr(int *arr, size_t len) { + printf("{"); + for(int i = 0; i < len; i++) { + printf(" %d%s", arr[i], i+1 == len ? " " : ","); + } + printf("}\n"); +} + +int *genArr(size_t len, int div) { + /* Make array */ + int *arr = (int *) malloc(len * sizeof(int)); + + if( arr == NULL) { + return NULL; + } + + /* Fill it with random */ + while( len-- ) { + arr[len] = div - (rand() % (div*2)); + } + + return arr; + +} + +int mergeSort(int *arr, size_t len) { + /* Check if len is one(then it's sorted */ + if (len <= 1) { + return 0; + } + /* Calculate array sizes */ + size_t q = len/2; + size_t rl = len-q; + + /* Make arrays */ + int *left = (int *) malloc(q * sizeof(int)); + if( left == NULL ) { + return 1; + } + int *right = (int *) malloc(rl * sizeof(int)); + if( right == NULL ) { + return 1; + } + + /* Copy data */ + memcpy(left, arr, q * sizeof(int)); + memcpy(right, arr + q, rl * sizeof(int)); + + /* Sort each */ + mergeSort(left, q); + mergeSort(right, rl); + + /* Merge them into arr */ + for(int i=0, j=0, k = 0; k < len; k++) { + if( i < q && ( j == rl || left[i] <= right[j] ) ) { + arr[k] = left[ i++ ]; + } else { + arr[k] = right[ j++ ]; + } + } + + /* Free the pointers */ + free(left); + free(right); + + return 0; +} + +void test(size_t len) { + + int *a = genArr(len, len); + +#if DO_PRINT == 1 + printArr(a, len); + printf("\n\n"); +#endif + + clock_t begin = clock(); + mergeSort(a, len); + clock_t end = clock(); + +#if DO_PRINT == 1 + printArr(a, len); +#endif + + clock_t diff = end - begin; + printf("Sorting %d long array took %d : %f s\n", len, diff, (double)diff/CLOCKS_PER_SEC); + + free(a); +} + +void bench() { + + size_t len = SIZE_MAX; + + srand((unsigned) time(NULL)); + for (int i = 1; i < len; i *= 2) { + test(i); + } +} + +int main(void) { + + bench(); + + return 0; +} diff --git a/sem3/algo/mm10/bfs.c b/sem3/algo/mm10/bfs.c new file mode 100644 index 0000000..982ecef --- /dev/null +++ b/sem3/algo/mm10/bfs.c @@ -0,0 +1,163 @@ +#include <stdio.h> +#include <stdlib.h> + +#define COL_WHITE 0 +#define COL_GRAY 1 +#define COL_BLACK 2 + +typedef struct list_node_t_struct { + struct list_node_t_struct *next; + void *val; +} list_node_t; + +typedef struct vertex_struct { + char ref; + int color; + int dist; + struct vertex_struct *p; + list_node_t *adj; +} vertex_t; + +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 = COL_WHITE; + v->dist = -1; + v->p = NULL; + v->adj = NULL; +} + +vertex_t *add_adj(vertex_t *v, vertex_t *add) { + list_node_t *oldN = v->adj; + + // Create new list node + list_node_t *n = malloc(sizeof(list_node_t)); + n->val = add; + n->next = oldN; + + v->adj = n; + + return add; +} + +void print_adj(vertex_t *v) { + list_node_t *n = v->adj; + printf("[ "); + while(n) { + printf("%c ", *((char *)n->val)); + n = n->next; + } + printf("]\n"); +} + +vertex_t *vertexes[128]; + +void add_edge(char a, char b) { + // Does a exists + if( vertexes[a] == NULL ) { + vertexes[a] = create_vertex(a); + } + // What about b + if( vertexes[b] == NULL ) { + vertexes[b] = create_vertex(b); + } + + // Add edge + add_adj(vertexes[a], vertexes[b]); +} + +typedef struct { + unsigned int len; + list_node_t *in; + list_node_t *out; +} queue_t; + +void enqueue(queue_t *q, vertex_t *v) { + list_node_t *n = malloc(sizeof(list_node_t)); + n->val = v; + + // Add at end + list_node_t *old = q->in; + q->in = n; + if( old ) { + old->next = n; + } + + // Make sure we have an out + if( q->out == NULL ) { + q->out = n; + } + + q->len++; +} + +vertex_t *dequeue(queue_t *q) { + list_node_t *n = q->out; + if( n == NULL ) { + return NULL; + } + vertex_t *v = (vertex_t *)n->val; + + // Move out forward + q->out = n->next; + q->len--; + + // Free node and return + free(n); + return v; +} + +void bfs(vertex_t *s) { + queue_t q = {0, NULL, NULL }; + enqueue(&q, s); + s->dist = 0; + + while( q.len ) { + vertex_t *u = dequeue(&q); + list_node_t *n = u->adj; + while( n ) { + vertex_t *v = (vertex_t *)n->val; + if( v->color == COL_WHITE ) { + v->color = COL_GRAY; + v->dist = u->dist + 1; + v->p = u; + enqueue(&q, v); + } + n = n->next; + } + u->color = COL_BLACK; + } + +} + +int main() { + add_edge('s', 'd'); // S + add_edge('s', 'c'); + add_edge('s', 'a'); + add_edge('d', 'e'); // D + add_edge('d', 'c'); + add_edge('e', 'g'); // E + add_edge('e', 'f'); + add_edge('c', 's'); // C + add_edge('c', 'g'); + add_edge('g', 'f'); // G + + //print_adj(vertexes['d']); + bfs(vertexes['s']); + + char c; + while( (c = getchar()) != EOF) { + if( vertexes[c] != NULL ) { + print_adj(vertexes[c]); + printf("%d\n", vertexes[c]->dist); + } else if (c == 10) { + } else { + printf("Not found\n"); + } + } + + +} diff --git a/sem3/algo/mm12/Makefile b/sem3/algo/mm12/Makefile new file mode 100644 index 0000000..7837354 --- /dev/null +++ b/sem3/algo/mm12/Makefile @@ -0,0 +1,31 @@ +CC = gcc +# Enable gdb and pull include files from current dir +CFLAGS = -ggdb -I. +LDFLAGS = + +BINARY = dijkstra +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) + +clean: + rm -f $(OBJ) $(BINARY) + rmdir $(BUILD_DIR) diff --git a/sem3/algo/mm12/dijkstra.c b/sem3/algo/mm12/dijkstra.c new file mode 100644 index 0000000..103c700 --- /dev/null +++ b/sem3/algo/mm12/dijkstra.c @@ -0,0 +1,132 @@ +#include <stdio.h> +#include <stdlib.h> +#include "graph.h" + +typedef struct list_item { + 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; +} 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++; +} + +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 ) { + 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; + +// 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 && 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; + } + } +} + +int main() { + + graph_edge(&g, 'a', 'b', 2); + graph_edge(&g, 'a', 'f', 1); + + graph_edge(&g, 'b', 'd', 3); + graph_edge(&g, 'b', 'c', 4); + + graph_edge(&g, 'd', 'b', 1); + graph_edge(&g, 'd', 'e', 8); + + graph_edge(&g, 'c', 'a', 7); + graph_edge(&g, 'c', 'd', 7); + graph_edge(&g, 'c', 'f', 2); + + graph_edge(&g, 'e', 'c', 2); + graph_edge(&g, 'e', 'f', 2); + + dijkstra(&g, g.vertexes['a']); + + graph_to_dot(stdout, &g); + + return 0; +} diff --git a/sem3/algo/mm12/graph.c b/sem3/algo/mm12/graph.c new file mode 100644 index 0000000..299ea24 --- /dev/null +++ b/sem3/algo/mm12/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->dist = -1; + 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(%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, "%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/sem3/algo/mm12/graph.h b/sem3/algo/mm12/graph.h new file mode 100644 index 0000000..e957c4d --- /dev/null +++ b/sem3/algo/mm12/graph.h @@ -0,0 +1,34 @@ +#ifndef GRAPH_H_ +#define GRAPH_H_ + +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 dist; + 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_ diff --git a/sem3/algo/mm2/linked/Makefile b/sem3/algo/mm2/linked/Makefile new file mode 100644 index 0000000..881143c --- /dev/null +++ b/sem3/algo/mm2/linked/Makefile @@ -0,0 +1,31 @@ +CC = gcc +# Enable gdb and pull include files from current dir +CFLAGS = -ggdb -I. +LDFLAGS = + +BINARY = linked +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) + +clean: + rm -f $(OBJ) $(BINARY) + rmdir $(BUILD_DIR) diff --git a/sem3/algo/mm2/linked/Readme.md b/sem3/algo/mm2/linked/Readme.md new file mode 100644 index 0000000..679dcf3 --- /dev/null +++ b/sem3/algo/mm2/linked/Readme.md @@ -0,0 +1,22 @@ +# Linked list assignment + +The linked list stuff is in node.c and higher level list functions in llist.c. + +Build with make: + +``` +make +``` + +To build and run + +``` +make run +``` + +To clean up when we are done. + +``` +make clean +``` + diff --git a/sem3/algo/mm2/linked/llist.c b/sem3/algo/mm2/linked/llist.c new file mode 100644 index 0000000..41ab892 --- /dev/null +++ b/sem3/algo/mm2/linked/llist.c @@ -0,0 +1,89 @@ +#include "llist.h" + +#include <string.h> +#include <stdio.h> + + +#define OUT_OFF_BOUNDS 2 + +void llist_init(llist_t *list) { + /* Zero out structure */ + list->head = list->root = NULL; + + list->len = 0; + +} + +void llist_append(llist_t *list, int val) { + /* Insert node after HEAD */ + list->head = node_insert(list->head, val); + + /* Check if was list is empty */ + if( list->len == 0 ) { + /* Set root */ + list->root = list->head; + } + + /* Increase count */ + list->len++; +} + +node_t *llist_get_node(llist_t *list, unsigned int index) { + /* Check if we have it */ + if( index >= list->len ) { + return NULL; + } + + /* Find the best way to go down the list */ + int direc = index > (list->len / 2) ? -1 : 1; + + /* Setup start location */ + int pos = direc > 0 ? 0 : list->len-1; + node_t *node = direc > 0 ? list->root : list->head; + + /* Go to index */ + while( pos != index ) { + /* TODO kinda risky, we trust our math and len */ + node = direc > 0 ? node->next : node->prev; + + pos += direc; + } + + return node; +} + +int llist_get(llist_t *list, unsigned int index) { + /* Get node */ + node_t *node = llist_get_node(list, index); + if( node == NULL ) { + /* Yes i know this is stupid */ + return -1; + } + + /* Return value */ + return node->val; +} + + +int llist_pop(llist_t *list, unsigned int index) { + /* Get node */ + node_t *node = llist_get_node(list, index); + if( node == NULL ) { + /* Yes i know this is stupid */ + return -1; + } + + /* Update root and head if we delete it */ + if( node == list->root ) { + list->root = node->next; + } + if( node == list->head ) { + list->head = node->prev; + } + + /* Keep len up to date */ + list->len--; + + /* Delete stuff */ + return node_pop(node); +} diff --git a/sem3/algo/mm2/linked/llist.h b/sem3/algo/mm2/linked/llist.h new file mode 100644 index 0000000..e52be89 --- /dev/null +++ b/sem3/algo/mm2/linked/llist.h @@ -0,0 +1,22 @@ +#ifndef LIST_H +#define LIST_H + +#include "node.h" + +typedef struct { + node_t *root; + node_t *head; + unsigned int len; +} llist_t; + +void llist_init(llist_t *list); + +void llist_append(llist_t *list, int val); + +node_t *llist_get_node(llist_t *list, unsigned int index); + +int llist_get(llist_t *list, unsigned int index); + +int llist_pop(llist_t *list, unsigned int index); + +#endif diff --git a/sem3/algo/mm2/linked/main.c b/sem3/algo/mm2/linked/main.c new file mode 100644 index 0000000..72b62cc --- /dev/null +++ b/sem3/algo/mm2/linked/main.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include "node.h" +#include "llist.h" + +void list_print(node_t *root) { + int index = 0; + /* Loop through notes and print them */ + while( root != NULL ) { + /* Print a value */ + printf("%d: %d\n", index++, root->val); + + /* Next value */ + root = root->next; + } +} + +/* Remove node */ +int main() { + + /* Do some stuff */ + llist_t list; + llist_init(&list); + + llist_append(&list, 11); // 0 + llist_append(&list, 22); // 1 + llist_append(&list, 33); // 2 + llist_append(&list, 44); // 3 + llist_append(&list, 89); // 4 + llist_append(&list, 12); // 5 + llist_append(&list, 2); // 6 + llist_append(&list, 1); // 7 + llist_append(&list, 7); // 8 + llist_append(&list, 232);// 9 + + list_print(list.root); + printf("%d\n", llist_get(&list, 5)); + llist_pop(&list, 9); + printf("%d\n", llist_get(&list, 7)); + + list_print(list.root); + + while( list.len ) { + printf("Popped %d\n", llist_pop(&list, 0)); + } +} diff --git a/sem3/algo/mm2/linked/node.c b/sem3/algo/mm2/linked/node.c new file mode 100644 index 0000000..cce1be0 --- /dev/null +++ b/sem3/algo/mm2/linked/node.c @@ -0,0 +1,57 @@ +#include "node.h" + +#include <string.h> +#include <stdlib.h> + +/* Insert after node */ +node_t *node_insert(node_t *node, int val) { + /* Create new node */ + node_t *newNode = (node_t *) malloc( sizeof(node_t) ); + if( newNode == NULL ) { + return NULL; + } + + + newNode->val = val; + newNode->prev = node; + + /* Check if there is node before */ + if( node == NULL ) { + /* If not then there is no after */ + newNode->next = NULL; + return newNode; + } + + /* Set next node */ + newNode->next = node->next; + node->next = newNode; + + /* Check node after */ + if( newNode->next != NULL ) { + /* Backlink next node */ + newNode->next->prev = newNode; + } + + return newNode; +} + +/* Pop node */ +int node_pop(node_t *node) { + int val = node->val; + + /* Check prev */ + if( node->prev != NULL ) { + /* Point last node to next node */ + node->prev->next = node->next; + } + + /* Check next */ + if( node->next != NULL ) { + node->next->prev = node->prev; + } + + /* Free memory */ + free(node); + + return val; +} diff --git a/sem3/algo/mm2/linked/node.h b/sem3/algo/mm2/linked/node.h new file mode 100644 index 0000000..027926b --- /dev/null +++ b/sem3/algo/mm2/linked/node.h @@ -0,0 +1,23 @@ +#ifndef NODE_H +#define NODE_H + +typedef struct node_struct { + int val; + struct node_struct *next; + struct node_struct *prev; +} node_t; + +/** @brief Create a new node after specified node + * + * @param node Pointer to node where new node should be inserted, can also be NULL + * @param val Value to add to new node + */ +node_t *node_insert(node_t *node, int val); + +/** @brief Pop node from chain and free's the resource + * + * @return Value in node + */ +int node_pop(node_t *node); + +#endif diff --git a/sem3/algo/mm2/queue.c b/sem3/algo/mm2/queue.c new file mode 100644 index 0000000..a93f76a --- /dev/null +++ b/sem3/algo/mm2/queue.c @@ -0,0 +1,100 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#define EFULL 2 +#define EMPTY 3 + +/* Queue stuff */ +typedef struct { + int head; + int tail; + int len; + int cap; + int *buff; +} queue_t; + +/* Queue functions */ +int queue_init(queue_t *q, size_t cap) { + /* Make the struct and set i to zero */ + memset(q, 0, sizeof(queue_t)); + + /* Allocate the buffer */ + q->buff = (int *) malloc(cap * sizeof(int)); + if( q->buff == NULL ) { + return 1; + } + + /* Set capacity, the rest should be zero form memset */ + q->cap = cap; + return 0; +} + +void queue_free(queue_t *q) { + /* Free the heap buffer */ + free(q->buff); +} + +int queue_place(queue_t *q, int val) { + /* Check if full */ + printf("len: %d\n", q->len); + if( q->len >= q->cap) { + printf("ERR: Full\n"); + return EFULL; + } + + /* Add to queue */ + q->buff[q->head] = val; + + /* Increase values */ + q->head = (q->head+1) % q->cap; + q->len++; + + return 0; +} + +int queue_get(queue_t *q, int *val) { + /* Check if empty */ + if( !q->len ) { + printf("ERR: Empty\n"); + return EMPTY; + } + + /* Read value */ + if( val != NULL ){ + *val = q->buff[q->tail]; + } + + /* Decrease values */ + q->tail = (q->tail+1) % q->cap; + q->len--; + + return 0; +} + +int main(void) { + int in; + char com; + + queue_t q; + queue_init(&q, 16); + + for(;;) { + /* Read a command */ + scanf("%c", &com); + + if( com == 'w') { + printf("> "); + scanf("%d", &in); + queue_place(&q, in); + } else if( com == 'r' ) { + queue_get(&q, &in); + printf("%d\n", in); + } else if( com == 'q' ) { + break; + } + } + + queue_free(&q); + +} diff --git a/sem3/algo/mm3/multiply.c b/sem3/algo/mm3/multiply.c new file mode 100644 index 0000000..e454de3 --- /dev/null +++ b/sem3/algo/mm3/multiply.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <time.h> + +uint64_t mult(uint64_t x, uint64_t y, unsigned int bits) { + /* Check if they expect more than 64 bits. */ + if( bits > 64 ) { + printf("Sorry we cant handle higher than 64 bits\n"); + exit(1); + } else if(bits <= 1) { + return x && y; + } + + unsigned int newBits = bits >> 1; + +#if DO_PRINT == 1 + printf("\n\n bits: %u, New bits: %u\n", bits, newBits); +#endif + + /* Split up into to */ + uint32_t halvMask = ((uint64_t)1 << newBits) - 1; +#if DO_PRINT == 1 + printf("Using mask 0x%08X\n", halvMask); +#endif + uint32_t a = (x >> (newBits)) & halvMask; + uint32_t b = x & halvMask; + uint32_t c = (y >> (newBits)) & halvMask; + uint32_t d = y & halvMask; + +#if DO_PRINT == 1 + printf("A: 0x%08X, B: 0x%08X, C: 0x%08X, D: 0x%08X\n", a, b, c, d); +#endif + + return (mult(a, c, newBits) << bits) + ((mult(a, d, newBits) + mult(b, c, newBits)) << newBits) + mult(b, d, newBits); + +} + + +int main(void) { + + uint32_t a = 0xFFFFFF; + uint8_t b = 55; + uint64_t res; + + clock_t begin = clock(); + res = mult(a, b, 64); + clock_t end = clock(); + + printf("Result: %lld\n", res); + + clock_t diff = end - begin; + printf("Took %d which is %f s\n", diff, (double)diff/CLOCKS_PER_SEC); + + return 0; +} diff --git a/sem3/algo/mm3/opgaver.md b/sem3/algo/mm3/opgaver.md new file mode 100644 index 0000000..a6e560c --- /dev/null +++ b/sem3/algo/mm3/opgaver.md @@ -0,0 +1,49 @@ +# Opgave 1 + +## Exercise 9.2 + +**T(n) = 3T(n/2) + n** + +a = 3 +b = 2 +d(n) = n + +a ? d(b) -> 3 > 2 + +Svaret er derfor O(n^log2(3)) = O(n^1.59) + +**T(n) = 3T(n/2) + n^2** + +3 ? 2^2 -> 3 < 4 + +d(n) = n^2 hvilket betyder at O(n^2) + +**T(n) = 8T(n/2) + n^2** + +8 ? 2^3 -> 8 = 8 + +d(n) = n^3 hvilket betyder at O(n^3 log2(n)) + +## Evercise 9.3 + +**T(n) = 4T(n/3) + n** + +a = 4 +b = 3 +d(n) = n + +4 ? 3 -> 4 > 3 + +Derfor er svaret O(n^log3(4)) = O(n^1.26) + +**T(n) = 4T(n/3) + n^2** + +4 ? 3^2 -> 4 < 9 + +d(n) = n^2 hvilket betyder at O(n^2) + +**T(n) = 9T(n/3) + n^2** + +9 ? 3^2 -> 9 = 9 + +d(n) = n^2 hvilket betyder at O(n^2 log2(n)) diff --git a/sem3/algo/mm6/Makefile b/sem3/algo/mm6/Makefile new file mode 100644 index 0000000..c157cef --- /dev/null +++ b/sem3/algo/mm6/Makefile @@ -0,0 +1,31 @@ +CC = gcc +# Enable gdb and pull include files from current dir +CFLAGS = -ggdb -I. +LDFLAGS = + +BINARY = main +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) + +clean: + rm -f $(OBJ) $(BINARY) + rmdir $(BUILD_DIR) diff --git a/sem3/algo/mm6/main b/sem3/algo/mm6/main Binary files differnew file mode 100755 index 0000000..c666ee9 --- /dev/null +++ b/sem3/algo/mm6/main diff --git a/sem3/algo/mm6/main.c b/sem3/algo/mm6/main.c new file mode 100644 index 0000000..abc8d02 --- /dev/null +++ b/sem3/algo/mm6/main.c @@ -0,0 +1,37 @@ +#include <stdio.h> + +#include "tree.h" + +int main() { + tree_t t; + t.root = 0; + + node_t *a = tree_insert(&t, 1, "Hej"); + + node_t *b = tree_insert(&t, 3, "med"); + + tree_insert(&t, 11, "dig"); + tree_insert(&t, 9, "dig"); + tree_insert(&t, 12, "dig"); + + tree_insert(&t, 10, "hvordan"); + + tree_insert(&t, 8, "det"); + + tree_insert(&t, 4, "branch"); + + tree_insert(&t, 5, "2"); + + + tree_insert(&t, 0, "Og den sidste"); + + tree_insert(&t, 2, "Cool nok"); + tree_print(&t); + + printf("%s\n", tree_search(&t, 10)); + printf("%s\n", tree_search(&t, 11)); + printf("%s\n", tree_search(&t, 1)); + printf("%s\n", tree_search(&t, 0)); + printf("%s\n", tree_search(&t, 99)); + +} diff --git a/sem3/algo/mm6/tree.c b/sem3/algo/mm6/tree.c new file mode 100644 index 0000000..47378f3 --- /dev/null +++ b/sem3/algo/mm6/tree.c @@ -0,0 +1,155 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "tree.h" + +static node_t *create_node(unsigned int index, char *val) { + node_t *node = malloc( sizeof(node_t) ); + memset(node, 0, sizeof(node_t)); + + node->value = val; + node->index = index; +} + +/* Comments are based of dir = CHILD_LEFT */ +void node_rotate(tree_t *tree, node_t *x, int dir) { + /* Node below which we should rotate with */ + node_t *y = x->children[!dir]; + + /* Move y's left to x's right */ + x->children[!dir] = y->children[dir]; + + /* Update parent */ + if( y->children[dir] ) { + y->children[dir]->p = x; + } + + /* Switch around x and y */ + y->p = x->p; + + /* Check if y is new root if not update x's parent */ + if( !y->p ) { + tree->root = y; + } else if( x == x->p->children[CHILD_LEFT]) { + x->p->children[CHILD_LEFT] = y; + } else { + x->p->children[CHILD_RIGHT] = y; + } + + /* Update y's ref */ + y->children[dir] = x; + x->p = y; + +} + +static void insert_fixup(tree_t *tree, node_t *z) { + while( z->p && z->p->black == false ) { + /* Set the right direction */ + int dir = (z->p == z->p->p->children[CHILD_LEFT]) ? CHILD_RIGHT : CHILD_LEFT; + + /* Get uncle */ + node_t *y = z->p->p->children[dir]; + if( y && !y->black ) { + /* Case 1 */ + z->p->black = true; + y->black = true; + z->p->p->black = false; + + z = z->p->p; + } else if( z == z->p->children[dir] ) { + /* Case 2 */ + z = z->p; + node_rotate(tree, z, !dir); + } else { + /* Case 3 */ + z->p->black = true; + z->p->p->black = false; + node_rotate(tree, z->p->p, dir); + } + } + + tree->root->black = true; + +} + +static node_t *insert_recur(node_t *node, unsigned int index, char *val) { + int dir = node->index < index ? CHILD_RIGHT : CHILD_LEFT; + + if( node->children[dir] ) { + return insert_recur(node->children[dir], index, val); + } + + /* Found stop */ + node_t *new = create_node(index, val); + new->p = node; + node->children[dir] = new; + + return new; +} + +node_t *tree_insert_pleb(tree_t *tree, unsigned int index, char *val) { + if( !tree->root ) { + tree->root = create_node(index, val); + return tree->root; + } + + return insert_recur(tree->root, index, val); +} + +node_t *tree_insert(tree_t *tree, unsigned int index, char *val) { + if( !tree->root ) { + tree->root = create_node(index, val); + tree->root->black = true; + return tree->root; + } + + node_t *new = insert_recur(tree->root, index, val); + insert_fixup(tree, new); + + return new; +} + +char *tree_search(tree_t *tree, unsigned int index) { + node_t *z = tree->root; + while( z->index != index ) { + z = z->children[z->index < index ? CHILD_RIGHT : CHILD_LEFT]; + if( !z ) { + return NULL; + } + } + + return z->value; +} + +static inline void print_indent(int num) { + for( int i = 0; i < num; i++ ) { + printf(" "); + } +} + +static inline void print_text(node_t *node, bool bold) { + if( bold ) { + printf("\033[1m%d\033[22m\n", node->index); + } else { + printf("%d\n", node->index); + } + +} + +static void print_recur(node_t *node, int ident) { + if( !node ) { + return; + } + print_recur(node->children[CHILD_RIGHT], ident+1); + + print_indent(ident); + print_text(node, node->black); + + print_recur(node->children[CHILD_LEFT], ident+1); +} + +void tree_print(tree_t *tree) { + print_recur(tree->root, 0); +} + diff --git a/sem3/algo/mm6/tree.h b/sem3/algo/mm6/tree.h new file mode 100644 index 0000000..0d1c5c6 --- /dev/null +++ b/sem3/algo/mm6/tree.h @@ -0,0 +1,31 @@ +#ifndef TREE_H +#define TREE_H + +#include <stdbool.h> + +#define CHILD_LEFT 0 +#define CHILD_RIGHT 1 + +typedef struct node_struct{ + struct node_struct *p; + struct node_struct *children[2]; + unsigned int index; + bool black; + char *value; +} node_t; + +typedef struct { + node_t *root; +} tree_t; + +void tree_print(tree_t *tree); + +node_t *tree_insert(tree_t *tree, unsigned int index, char *val); +node_t *tree_insert_pleb(tree_t *tree, unsigned int index, char *val); + +void node_rotate(tree_t *tree, node_t *x, int dir); + +char *tree_search(tree_t *tree, unsigned int index); + + +#endif diff --git a/sem3/algo/mm9/Untitled.ipynb b/sem3/algo/mm9/Untitled.ipynb new file mode 100644 index 0000000..a2bd59e --- /dev/null +++ b/sem3/algo/mm9/Untitled.ipynb @@ -0,0 +1,126 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Bare lige noget preamble" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import networkx as nx\n", + "from networkx.drawing.nx_agraph import to_agraph\n", + "from nxpd import draw, nxpdParams\n", + "from numpy.linalg\n", + "nxpdParams['show'] = 'ipynb'\n", + "import warnings\n", + "warnings.filterwarnings('ignore')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "G = nx.DiGraph()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "G.add_edge(1, 2)\n", + "G.add_edge(1, 3)\n", + "G.add_node(1)\n", + "G.add_edge(1, 4)\n", + "G.add_edge(1, 4)\n", + "G.add_edge(4, 4)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAASsAAACbCAYAAAA6Gb11AAAABmJLR0QA/wD/AP+gvaeTAAAgAElEQVR4nO3deVhTZ9oG8PtkIYAsAlYWd1R2N0YUWgVEFhGhYNW6i1pGnS5aHdtp1bbqVMft02q17Uxn3NrRgpdUEFC0ggFHFEE/BXdRYEQWUSugEJI83x/94KoCikryJuH9XZd/NAk5N0/h5pyTswhEROA4jtNtcSLWCTiO41qDlxXHcXqBlxXHcXpBwjoApztUKhXKyspQVlaGBw8eQKVSoaqqCkqlEqamppDJZDAxMUHHjh1hb28Pa2tr1pG5doSXVTv0+PFjZGdn4/z588jLy0N+fj5u3LiB8vJyqFSqVr+PsbExunbtCmdnZ3h4eMDd3R2enp5wc3ODIAga/A649kjgnwYaPrVajVOnTiE5ORnp6enIzs5GXV0drK2tG0vG2dkZ9vb2cHBwgK2tLaytrSESiWBubg6JRIJHjx6hrq4OtbW1uH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s36v2+Bz5/tgxx/iqVihISEhrvBt3S/ipBEGjcuHHaiN42ZdXg2rVrFB0dTUZGRmRtbU0LFizQif0PCoWCEhMTKTw8nMRiMdnZ2dH69euppqaGdbQ2xefPlqHOPyMjg0aNGkWCIJBUKm2yv+qDDz7Q8HdARG1dVg1KS0tp1apV1KtXLwJALi4utGTJEsrOztbaavLDhw/pwIEDNGPGjMbt8JEjR1JsbCzV1dVpJQMrfP5sGer8z5w5Q+PGjSORSNRYWkZGRrRq1ao2Tt+sWIGI6CWuk9UqarUaJ06cwP79+xEfH4/CwkJYWlpi2LBhGDZsGDw9PeHh4fHKt4RXKpW4du0a8vLykJWVhYyMDJw9exZqtRo+Pj4YO3Ysxo4di549e7bNN6Yn+PzZMtT5X79+HevXr8e//vUv1NfXY8eOHZgxY0abvPczxGm0rJ6Wl5eH48ePQy6XIzMzEyUlJQB+u1eYk5MT7Ozs0K1bN3Tu3BmWlpaQyWQwNTWFTCZDVVUVlEolqqqq8PDhQxQXF6OsrAxFRUW4evUqFAoFJBIJXF1d4efnB19fX/j6+jbekYXj82fN0OZfVlaGr776CmFhYXjjjTc0tpz/p92yelplZSUuXLiA/Px8XL9+HaWlpbh9+zbKysrw8OFD1NXVoaamBgqFAmZmZpBKpTA3N4eFhQW6dOkCOzs7dO3aFS4uLnB3d4ebmxvT2xTpGz5/tvj8XwjbsuI4jmulOHZ3X+A4jnsBvKw4jtMLvKw4jtMLEgBxrENwHMc9R9b/AfIyRalrdEg/AAAAAElFTkSuQmCC\n", + "text/plain": [ + "<IPython.core.display.Image object>" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "draw(G)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Okay det ser ud til at virke fint. Nu kan vi printe adjecency matrix." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 1 1]\n", + " [0 0 0 0]\n", + " [0 0 0 0]\n", + " [0 0 0 1]]\n" + ] + } + ], + "source": [ + "A = nx.adjacency_matrix(G)\n", + "print(A.todense())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/sem3/algo/workshop2/dfs/Makefile b/sem3/algo/workshop2/dfs/Makefile new file mode 100644 index 0000000..790bb94 --- /dev/null +++ b/sem3/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/sem3/algo/workshop2/dfs/dfs b/sem3/algo/workshop2/dfs/dfs Binary files differnew file mode 100755 index 0000000..0d9ff14 --- /dev/null +++ b/sem3/algo/workshop2/dfs/dfs diff --git a/sem3/algo/workshop2/dfs/dfs.c b/sem3/algo/workshop2/dfs/dfs.c new file mode 100644 index 0000000..1244895 --- /dev/null +++ b/sem3/algo/workshop2/dfs/dfs.c @@ -0,0 +1,107 @@ +#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/sem3/algo/workshop2/dfs/graph.c b/sem3/algo/workshop2/dfs/graph.c new file mode 100644 index 0000000..0f5048c --- /dev/null +++ b/sem3/algo/workshop2/dfs/graph.c @@ -0,0 +1,181 @@ +#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/sem3/algo/workshop2/dfs/graph.h b/sem3/algo/workshop2/dfs/graph.h new file mode 100644 index 0000000..e169a8a --- /dev/null +++ b/sem3/algo/workshop2/dfs/graph.h @@ -0,0 +1,45 @@ +#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_ diff --git a/sem3/algo/workshop2/dijkstra/Makefile b/sem3/algo/workshop2/dijkstra/Makefile new file mode 100644 index 0000000..7837354 --- /dev/null +++ b/sem3/algo/workshop2/dijkstra/Makefile @@ -0,0 +1,31 @@ +CC = gcc +# Enable gdb and pull include files from current dir +CFLAGS = -ggdb -I. +LDFLAGS = + +BINARY = dijkstra +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) + +clean: + rm -f $(OBJ) $(BINARY) + rmdir $(BUILD_DIR) diff --git a/sem3/algo/workshop2/dijkstra/dijkstra.c b/sem3/algo/workshop2/dijkstra/dijkstra.c new file mode 100644 index 0000000..9609343 --- /dev/null +++ b/sem3/algo/workshop2/dijkstra/dijkstra.c @@ -0,0 +1,129 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include "graph.h" + +typedef struct list_item { + 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; +} 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++; +} + +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; +} + +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; + } +} + +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; + } + } +} + +int main() { + + int count = graph_from_dot(stdin, &g); + + 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; + + printf("%d;%f\n", count, cpu_time_used); + + //graph_to_dot(stdout, &g); + + return 0; +} diff --git a/sem3/algo/workshop2/dijkstra/graph.c b/sem3/algo/workshop2/dijkstra/graph.c new file mode 100644 index 0000000..3f24ccf --- /dev/null +++ b/sem3/algo/workshop2/dijkstra/graph.c @@ -0,0 +1,219 @@ +#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->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; +} + +// 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(%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; +} + +vertex_t *graph_vertex(graph_t *g, char *ref) { + return table_lookup(g, ref); +} diff --git a/sem3/algo/workshop2/dijkstra/graph.h b/sem3/algo/workshop2/dijkstra/graph.h new file mode 100644 index 0000000..5c078b8 --- /dev/null +++ b/sem3/algo/workshop2/dijkstra/graph.h @@ -0,0 +1,47 @@ +#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 dist; + struct vertex_struct *p; + + 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_from_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); +vertex_t *graph_vertex(graph_t *g, char *ref); + +#endif // GRAPH_H_ |