Skip to content

Commit 8486473

Browse files
committed
free graph when we're done, valgrind says everything is cool. updated readme.
1 parent fb3db25 commit 8486473

File tree

7 files changed

+58
-24
lines changed

7 files changed

+58
-24
lines changed

README

-1
This file was deleted.

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Sudoku Solver
2+
=============
3+
4+
Graph coloring based sudoku solver. Yes, I could just bruteforce it in a simpler way, but this is cooler.
5+
6+
Accepts input from stdin, puzzles should be one line with 0s to mark blank cells, like this:
7+
8+
530070000600195000098000060800060003400803001700020006060000280000419005000080079
9+
10+
You can run the examples by building the program with make, then something like:
11+
12+
cat sample_easy | ./solver
13+
14+
It will return the solved puzzle in the same format, or error out saying it cannot color the graph. Currently, this solver only supports 9x9 puzzles and will expect 81 characters to be read from stdin.
15+
16+
17+

graph.c

+28-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ struct graph *create_graph()
1111
return created;
1212
}
1313

14+
15+
void free_graph(struct graph *graph)
16+
{
17+
struct vertex *next_vertex = graph->vertices;
18+
19+
while (next_vertex != NULL) {
20+
struct vertex *current = next_vertex;
21+
next_vertex = next_vertex->next;
22+
23+
struct edge *next_edge = current->edges;
24+
while (next_edge != NULL) {
25+
struct edge *current_edge = next_edge;
26+
next_edge = next_edge->next;
27+
28+
free(current_edge);
29+
}
30+
31+
free(current);
32+
}
33+
34+
free(graph);
35+
}
36+
37+
1438
struct vertex *add_vertex(struct graph *graph, int num_possible, int value)
1539
{
1640
struct vertex *next = malloc(sizeof(struct vertex));
@@ -24,6 +48,7 @@ struct vertex *add_vertex(struct graph *graph, int num_possible, int value)
2448
return next;
2549
}
2650

51+
2752
void add_edge(struct vertex *vertex1, struct vertex *vertex2)
2853
{
2954
struct edge *next = malloc(sizeof(struct edge));
@@ -32,6 +57,7 @@ void add_edge(struct vertex *vertex1, struct vertex *vertex2)
3257
vertex1->edges = next;
3358
}
3459

60+
3561
void print_graph(struct graph *graph)
3662
{
3763
int edge_count = 0;
@@ -79,10 +105,7 @@ void print_sudoku(struct graph *graph, int size)
79105
printf("%d", output[index]);
80106

81107
printf("\n");
82-
}
83108

84-
85-
86-
87-
109+
free(output);
110+
}
88111

graph.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ struct graph {
55
int vertex_index;
66
};
77

8+
89
struct vertex {
910
int current_value;
1011
int num_possible;
@@ -22,8 +23,11 @@ struct edge {
2223

2324

2425
struct graph *create_graph();
26+
void free_graph(struct graph *graph);
27+
2528
struct vertex *add_vertex(struct graph *graph, int num_possible, int value);
2629
void add_edge(struct vertex *vertex1, struct vertex *vertex2);
30+
2731
void print_graph(struct graph *graph);
2832
void print_sudoku(struct graph *graph, int size);
2933

graph_color.c

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ bool color_graph(struct vertex *vertex, int num_colors)
7272
int original_value = vertex->current_value;
7373
int original_num = vertex->num_possible;
7474

75-
7675
if (original_num == 1)
7776
return color_graph(vertex->next, num_colors);
7877

sample_easy

-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
11
530070000600195000098000060800060003400803001700020006060000280000419005000080079
22

3-
4-
5-
530070000
6-
600195000
7-
098000060
8-
800060003
9-
400803001
10-
700020006
11-
060000280
12-
000419005
13-
000080079
14-

solver.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ bool index_in_group(int *group, int size, int index)
2323
return false;
2424
}
2525

26+
2627
// link a vertex at index with every other vertex in the group
2728
void link_vertex_with_group(int *group, int size, int index, struct vertex **vertices)
2829
{
@@ -61,6 +62,7 @@ void link_three_groups(struct vertex **vertices)
6162
}
6263
}
6364

65+
6466
// link rows and columns together
6567
void link_cols_and_rows(struct vertex **vertices)
6668
{
@@ -84,6 +86,7 @@ void link_cols_and_rows(struct vertex **vertices)
8486
link_vertex_with_group(current_col, 9, j, vertices);
8587
}
8688
}
89+
8790

8891
// create graph, load up vertices and link them together in the sudoku pattern
8992
struct graph *load_initial()
@@ -96,7 +99,6 @@ struct graph *load_initial()
9699
char *buffer = malloc(sizeof(char) * num_chars);
97100
fgets(buffer, num_chars, stdin);
98101

99-
100102
struct graph *graph = create_graph();
101103
struct vertex **vertices = malloc(sizeof(void *) * num_boxes);
102104

@@ -113,10 +115,12 @@ struct graph *load_initial()
113115
}
114116
}
115117

116-
117118
link_three_groups(vertices);
118119
link_cols_and_rows(vertices);
119120

121+
free(buffer);
122+
free(vertices);
123+
120124
return graph;
121125
}
122126

@@ -128,11 +132,11 @@ int main(int argc, char **argv)
128132
color_graph(graph->vertices, 9);
129133

130134
if (graph_colored(graph))
131-
printf("Graph is colored.\n");
135+
print_sudoku(graph, 9);
132136
else
133-
printf("Graph is not colored.\n");
137+
printf("Unable to color sudoku graph.\n");
134138

135-
print_sudoku(graph, 9);
139+
free_graph(graph);
136140

137141
return 0;
138142
}

0 commit comments

Comments
 (0)