Skip to content

Commit

Permalink
Finished DFS for directed graphs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwasham committed Jul 31, 2016
1 parent a16d837 commit df2a60e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
81 changes: 74 additions & 7 deletions graphs/graphs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ UndirectedGraph* ugraph_create(const int v) {
g->adjacency_list[i] = malloc(sizeof(struct neighbors));
assert(g->adjacency_list[i]);

g->adjacency_list[i]->count = 0;
g->adjacency_list[i]->length = 0;
g->adjacency_list[i]->capacity = 0;
}

Expand All @@ -39,36 +39,103 @@ void ugraph_add_edge(UndirectedGraph* g, const int src, const int dest) {

void _ugraph_append_edge(UndirectedGraph* g, const int src, const int dest) {
// resizes array if needed
if (g->adjacency_list[src]->count == g->adjacency_list[src]->capacity) {
if (g->adjacency_list[src]->length == g->adjacency_list[src]->capacity) {
g->adjacency_list[src]->capacity = g->adjacency_list[src]->capacity * 2 + 1;

g->adjacency_list[src] = realloc(g->adjacency_list[src], sizeof(struct neighbors) + sizeof(int) * g->adjacency_list[src]->capacity);
g->adjacency_list[src] =
realloc(g->adjacency_list[src],
sizeof(struct neighbors) +
sizeof(int) * g->adjacency_list[src]->capacity);
}

g->adjacency_list[src]->list[g->adjacency_list[src]->count++] = dest;
g->adjacency_list[src]->list[g->adjacency_list[src]->length++] = dest;

g->e++;
}

void ugraph_print_debug(UndirectedGraph* g) {
void ugraph_print_dfs(UndirectedGraph* g) {
int visited[g->v];
memset(visited, 0, sizeof(visited));

// will find all vertices if graph is connected
ugraph_dfs(g, visited, 0);
}

void ugraph_dfs(UndirectedGraph* g, int visited[], const int vertex) {
visited[vertex] = 1;
printf("%d -> ", vertex);

for (int e = 0; e < g->adjacency_list[vertex]->count; ++e) {
for (int e = 0; e < g->adjacency_list[vertex]->length; ++e) {
int u = g->adjacency_list[vertex]->list[e];

if (visited[u] == 0) {
ugraph_dfs(g, visited, u);
}
}
}

DirectedGraph* digraph_create(const int v) {
DirectedGraph* g =
malloc(sizeof(DirectedGraph) + sizeof(struct targets*) * v);
assert(g);

g->v = v;
g->e = 0;

for (int i = 0; i < v; ++i) {
g->adjacency_list[i] = malloc(sizeof(struct targets));
assert(g->adjacency_list[i]);

g->adjacency_list[i]->length = 0;
g->adjacency_list[i]->capacity = 0;
}

return g;
}

putchar('\n');
void digraph_destroy(DirectedGraph* g) {
for (int i = 0; i < g->v; ++i) {
free(g->adjacency_list[i]);
}
free(g);
}

void digraph_add_edge(DirectedGraph* g, const int src, const int dest) {
assert(src != dest);
assert(src >= 0 && src < g->v);
assert(dest >= 0 && dest < g->v);

if (g->adjacency_list[src]->length == g->adjacency_list[src]->capacity) {
g->adjacency_list[src]->capacity = g->adjacency_list[src]->capacity * 2 + 1;
g->adjacency_list[src] =
realloc(g->adjacency_list[src],
sizeof(struct targets) +
sizeof(int) * g->adjacency_list[src]->capacity);
}

g->adjacency_list[src]->list[g->adjacency_list[src]->length++] = dest;
g->e++;
}

void digraph_print_dfs(DirectedGraph* g) {
int visited[g->v];
memset(visited, 0, sizeof(visited));

for (int v = 0; v < g->v; ++v) {
digraph_dfs(g, visited, v);
}
}

void digraph_dfs(DirectedGraph* g, int visited[], const int vertex) {
visited[vertex] = 1;

printf("\n%d: ", vertex);

for (int i = 0; i < g->adjacency_list[vertex]->length; ++i) {
int adj_vertex = g->adjacency_list[vertex]->list[i];
if (visited[adj_vertex] == 0) {
printf("\t%d -> %d, ", vertex, adj_vertex);
digraph_dfs(g, visited, adj_vertex);
}
}
}
12 changes: 7 additions & 5 deletions graphs/graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef struct {
int e; /* edge count */

struct neighbors {
int count;
int length;
int capacity;
int list[];
} * adjacency_list[];
Expand All @@ -23,7 +23,7 @@ typedef struct {
int e; /* edge count */

struct targets {
int count;
int length;
int capacity;
int list[];
} * adjacency_list[];
Expand All @@ -38,16 +38,18 @@ void ugraph_add_edge(UndirectedGraph* g, const int src, const int dest);

void _ugraph_append_edge(UndirectedGraph* g, const int src, const int dest);

void ugraph_print_debug(UndirectedGraph* g);
void ugraph_print_dfs(UndirectedGraph* g);

void ugraph_dfs(UndirectedGraph* g, int visited[], const int vertex);

DirectedGraph* digraph_create(int v);
DirectedGraph* digraph_create(const int v);

void digraph_destroy(DirectedGraph* g);

void digraph_add_edge(DirectedGraph* g, const int src, const int dest);

void digraph_print_debug(DirectedGraph* g);
void digraph_print_dfs(DirectedGraph* g);

void digraph_dfs(DirectedGraph* g, int visited[], const int vertex);

#endif // PROJECT_GRAPHS_H
28 changes: 25 additions & 3 deletions graphs/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <stdio.h>
#include "graphs.h"

int main(int argc, char* argv[]) {

const int vertices = 9;
const int u_vertices = 9;
const int d_vertices = 9;

UndirectedGraph* g = ugraph_create(vertices);
UndirectedGraph* g = ugraph_create(u_vertices);

printf("Undirected graph with %d vertices:\n", u_vertices);

ugraph_add_edge(g, 0, 1);
ugraph_add_edge(g, 1, 2);
Expand All @@ -19,9 +23,27 @@ int main(int argc, char* argv[]) {
ugraph_add_edge(g, 6, 8);
ugraph_add_edge(g, 7, 8);

ugraph_print_debug(g);
ugraph_print_dfs(g);

ugraph_destroy(g);

printf("\n\nDirected graph with %d vertices:\n", d_vertices);

DirectedGraph* dg = digraph_create(d_vertices);

digraph_add_edge(dg, 0, 1);
digraph_add_edge(dg, 0, 5);
digraph_add_edge(dg, 1, 2);
digraph_add_edge(dg, 2, 4);
digraph_add_edge(dg, 2, 6);
digraph_add_edge(dg, 3, 2);
digraph_add_edge(dg, 6, 5);
digraph_add_edge(dg, 5, 8);
digraph_add_edge(dg, 7, 5);

digraph_print_dfs(dg);

digraph_destroy(dg);

return 0;
}

0 comments on commit df2a60e

Please sign in to comment.