-
Notifications
You must be signed in to change notification settings - Fork 6
/
graph.c
111 lines (80 loc) · 2.14 KB
/
graph.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdlib.h>
#include <stdio.h>
#include "graph.h"
struct graph *create_graph()
{
struct graph *created = malloc(sizeof(struct graph));
created->vertices = NULL;
created->vertex_index = 1;
return created;
}
void free_graph(struct graph *graph)
{
struct vertex *next_vertex = graph->vertices;
while (next_vertex != NULL) {
struct vertex *current = next_vertex;
next_vertex = next_vertex->next;
struct edge *next_edge = current->edges;
while (next_edge != NULL) {
struct edge *current_edge = next_edge;
next_edge = next_edge->next;
free(current_edge);
}
free(current);
}
free(graph);
}
struct vertex *add_vertex(struct graph *graph, int num_possible, int value)
{
struct vertex *next = malloc(sizeof(struct vertex));
next->index = graph->vertex_index++;
next->current_value = value;
next->num_possible = num_possible;
next->edges = NULL;
next->next = graph->vertices;
graph->vertices = next;
return next;
}
void add_edge(struct vertex *vertex1, struct vertex *vertex2)
{
struct edge *next = malloc(sizeof(struct edge));
next->connects_to = vertex2;
next->next = vertex1->edges;
vertex1->edges = next;
}
void print_graph(struct graph *graph)
{
int edge_count = 0;
struct vertex *last_vertex = graph->vertices;
while (last_vertex != NULL) {
printf("Vertex %d[%d] connects to vertices: ", last_vertex->index, last_vertex->current_value);
struct edge *last_edge = last_vertex->edges;
while (last_edge != NULL) {
if (last_edge->connects_to != NULL) {
edge_count++;
printf("%d[%d], ", last_edge->connects_to->index,
last_edge->connects_to->current_value);
}
last_edge = last_edge->next;
}
printf("\n");
last_vertex = last_vertex->next;
}
printf("Total edge count: %d\n", edge_count);
}
void print_sudoku(struct graph *graph, int size)
{
int *output = malloc(sizeof(int) * (size * size));
struct vertex *last_vertex = graph->vertices;
int index = 0;
while (last_vertex != NULL) {
output[index] = last_vertex->current_value;
last_vertex = last_vertex->next;
index++;
}
index--;
for (; index >= 0; index--)
printf("%d", output[index]);
printf("\n");
free(output);
}