Skip to content

Commit 2bba233

Browse files
committed
graph / prim / implementation
1 parent 46d4ed3 commit 2bba233

File tree

7 files changed

+666
-0
lines changed

7 files changed

+666
-0
lines changed

graph/prim/implementation/graph.c

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* graph.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: jko <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/04/22 20:57:47 by jko #+# #+# */
9+
/* Updated: 2020/04/25 16:42:05 by jko ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "graph.h"
14+
15+
t_graph *graph_init(uint size)
16+
{
17+
t_graph *graph;
18+
uint i;
19+
20+
if (size < 1 || !(graph = malloc(sizeof(t_graph))))
21+
return (0);
22+
if (!(graph->data = malloc(sizeof(void *) * size)))
23+
{
24+
free(graph);
25+
return (0);
26+
}
27+
if (!(graph->list = malloc(sizeof(t_node *) * size)))
28+
{
29+
free(graph->data);
30+
free(graph);
31+
return (0);
32+
}
33+
i = 0;
34+
while (i < size)
35+
{
36+
graph->data[i] = 0;
37+
graph->list[i] = 0;
38+
i++;
39+
}
40+
graph->size = size;
41+
return (graph);
42+
}
43+
44+
bool graph_set_data(t_graph *graph, uint vertex, void *data)
45+
{
46+
if (!graph || vertex >= graph->size)
47+
return (false);
48+
graph->data[vertex] = data;
49+
return (true);
50+
}
51+
52+
void *graph_get_data(t_graph *graph, uint vertex)
53+
{
54+
if (!graph || vertex >= graph->size)
55+
return (0);
56+
return (graph->data[vertex]);
57+
}
58+
59+
static t_node *create_elem(uint vertex, int cost)
60+
{
61+
t_node *node;
62+
63+
if (!(node = malloc(sizeof(t_node))))
64+
return (0);
65+
node->vertex = vertex;
66+
node->next = 0;
67+
node->cost = cost;
68+
return (node);
69+
}
70+
71+
static bool add_list(t_node **head, t_node *new)
72+
{
73+
t_node *prev;
74+
t_node *curr;
75+
76+
if (!head || !new)
77+
return (false);
78+
if (*head == 0)
79+
{
80+
*head = new;
81+
return (true);
82+
}
83+
prev = 0;
84+
curr = *head;
85+
while (curr && new->vertex > curr->vertex)
86+
{
87+
prev = curr;
88+
curr = curr->next;
89+
}
90+
if (!prev)
91+
*head = new;
92+
else
93+
prev->next = new;
94+
new->next = curr;
95+
return (true);
96+
}
97+
98+
static bool find_list(t_node *head, uint vertex, int *cost)
99+
{
100+
t_node *curr;
101+
102+
if (!head)
103+
return (false);
104+
curr = head;
105+
while (curr)
106+
{
107+
if (curr->vertex == vertex)
108+
{
109+
if (cost)
110+
*cost = curr->cost;
111+
return (true);
112+
}
113+
curr = curr->next;
114+
}
115+
return (false);
116+
}
117+
118+
static bool remove_list(t_node **head, uint vertex)
119+
{
120+
t_node *prev;
121+
t_node *curr;
122+
123+
if (!head)
124+
return (false);
125+
if (!(*head))
126+
return (true);
127+
prev = 0;
128+
curr = *head;
129+
while (curr)
130+
{
131+
if (curr->vertex == vertex)
132+
break ;
133+
prev = curr;
134+
curr = curr->next;
135+
}
136+
if (!curr)
137+
return (true);
138+
if (!prev)
139+
*head = curr->next;
140+
else
141+
prev->next = curr->next;
142+
free(curr);
143+
return (true);
144+
}
145+
146+
bool graph_set_edge(
147+
t_graph *graph, uint vertex1, uint vertex2, bool state, int cost)
148+
{
149+
t_node *new1;
150+
t_node *new2;
151+
152+
if (!graph || vertex1 >= graph->size || vertex2 >= graph->size)
153+
return (false);
154+
if (find_list(graph->list[vertex1], vertex2, 0) == state)
155+
return (true);
156+
if (!state)
157+
{
158+
if (remove_list(&(graph->list[vertex1]), vertex2)
159+
&& remove_list(&(graph->list[vertex2]), vertex1))
160+
return (true);
161+
else
162+
return (false);
163+
}
164+
if (!(new1 = create_elem(vertex2, cost)))
165+
return (false);
166+
if (!(new2 = create_elem(vertex1, cost)))
167+
{
168+
free(new1);
169+
return (false);
170+
}
171+
if (!add_list(&(graph->list[vertex1]), new1)
172+
|| !add_list(&(graph->list[vertex2]), new2))
173+
{
174+
free(new1);
175+
free(new2);
176+
return (false);
177+
}
178+
return (true);
179+
}
180+
181+
bool graph_get_edge(t_graph *graph, uint vertex1, uint vertex2, int *cost)
182+
{
183+
if (!graph || vertex1 >= graph->size || vertex2 >= graph->size || !cost)
184+
return (false);
185+
return (find_list(graph->list[vertex1], vertex2, cost));
186+
}
187+
188+
static void free_list(t_node *curr)
189+
{
190+
t_node *temp;
191+
192+
while (curr)
193+
{
194+
temp = curr;
195+
curr = curr->next;
196+
free(temp);
197+
}
198+
}
199+
200+
void free_graph(t_graph *graph, void (*free_data)(void *))
201+
{
202+
uint i;
203+
204+
if (!graph || !free_data)
205+
return ;
206+
i = 0;
207+
while (i < graph->size)
208+
{
209+
free_data(graph->data[i]);
210+
free_list(graph->list[i]);
211+
i++;
212+
}
213+
free(graph->data);
214+
free(graph->list);
215+
free(graph);
216+
}

graph/prim/implementation/graph.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* graph.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: jko <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/04/22 20:52:43 by jko #+# #+# */
9+
/* Updated: 2020/04/25 13:31:13 by jko ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef GRAPH_H
14+
# define GRAPH_H
15+
16+
# include <stdlib.h>
17+
# include <stdbool.h>
18+
19+
typedef unsigned int uint;
20+
21+
typedef struct s_node
22+
{
23+
unsigned int vertex;
24+
int cost;
25+
struct s_node *next;
26+
} t_node;
27+
28+
typedef struct s_graph
29+
{
30+
unsigned int size;
31+
void **data;
32+
t_node **list;
33+
} t_graph;
34+
35+
t_graph *graph_init(uint size);
36+
bool graph_set_data(
37+
t_graph *graph, uint vertex, void *data);
38+
bool graph_set_edge(t_graph *graph,
39+
uint vertex1, uint vertex2, bool state, int cost);
40+
void *graph_get_data(t_graph *graph, uint vertex);
41+
bool graph_get_edge(t_graph *graph,
42+
uint vertex1, uint vertex2, int *cost);
43+
void free_graph(t_graph *graph, void (*free_data)(void *));
44+
45+
#endif

graph/prim/implementation/heap.c

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* heap.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: jko <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/04/09 15:49:29 by jko #+# #+# */
9+
/* Updated: 2020/04/11 00:03:44 by jko ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "heap.h"
14+
15+
t_heap *heap_init(unsigned int max_size, int (*cmp)(void *, void *))
16+
{
17+
t_heap *heap;
18+
19+
if (max_size < 1 || !cmp)
20+
return (0);
21+
if (!(heap = malloc(sizeof(t_heap))))
22+
return (0);
23+
if (!(heap->data = malloc(sizeof(void *) * (max_size + 1))))
24+
{
25+
free(heap);
26+
return (0);
27+
}
28+
heap->max_size = max_size;
29+
heap->cmp = cmp;
30+
heap->size = 0;
31+
return (heap);
32+
}
33+
34+
int heap_push(t_heap *heap, void *data)
35+
{
36+
unsigned int i;
37+
void *temp;
38+
39+
if (!heap || heap->size >= heap->max_size)
40+
return (0);
41+
heap->size++;
42+
i = heap->size;
43+
heap->data[i] = data;
44+
while (i / 2 > 0 && heap->cmp(heap->data[i / 2], heap->data[i]) > 0)
45+
{
46+
temp = heap->data[i / 2];
47+
heap->data[i / 2] = heap->data[i];
48+
heap->data[i] = temp;
49+
i /= 2;
50+
}
51+
return (1);
52+
}
53+
54+
void *heap_peek(t_heap *heap)
55+
{
56+
if (!heap || heap->size < 1)
57+
return (0);
58+
return (heap->data[1]);
59+
}
60+
61+
static void fill_node(t_heap *heap)
62+
{
63+
unsigned int i;
64+
unsigned int j;
65+
unsigned int k;
66+
void *temp;
67+
68+
j = 1;
69+
while ((i = j))
70+
{
71+
j = i * 2 <= heap->size &&
72+
heap->cmp(heap->data[i], heap->data[i * 2]) > 0;
73+
k = i * 2 + 1 <= heap->size
74+
&& heap->cmp(heap->data[i], heap->data[i * 2 + 1]) > 0;
75+
if (j && k)
76+
j = heap->cmp(heap->data[i * 2], heap->data[i * 2 + 1])
77+
< 0 ? i * 2 : i * 2 + 1;
78+
else if (j)
79+
j = i * 2;
80+
else if (k)
81+
j = i * 2 + 1;
82+
else
83+
break;
84+
temp = heap->data[i];
85+
heap->data[i] = heap->data[j];
86+
heap->data[j] = temp;
87+
}
88+
}
89+
90+
void *heap_pop(t_heap *heap)
91+
{
92+
void *result;
93+
94+
if (!heap || heap->size < 1)
95+
return (0);
96+
result = heap->data[1];
97+
heap->data[1] = heap->data[heap->size--];
98+
fill_node(heap);
99+
return (result);
100+
}
101+
102+
void free_heap(t_heap *heap, void (*free_data)(void *))
103+
{
104+
if (!heap || !free_data)
105+
return ;
106+
while (heap->size > 0)
107+
{
108+
free_data(heap->data[heap->size]);
109+
heap->size--;
110+
}
111+
free(heap->data);
112+
free(heap);
113+
}

0 commit comments

Comments
 (0)