-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
163 lines (122 loc) · 2.78 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "graph.h"
#include <stdio.h>
#include <stdlib.h>
#include "hash_table.h"
#include "entries.h"
#include "linked_list.h"
struct graph
{
int id; //panepistimio,atoma,douleia,etc
ht_ptr table;
int size;
};
ptr_graph createGraph(int id,int m,int c)
{
ptr_graph graph;
ht_ptr table;
graph = malloc(sizeof(struct graph));
table = HT_create(m,c); //duo int gia orismata
graph->table = table;
graph->id = id;
graph->size = 0;
return graph;
}
int destroyGraph(ptr_graph graph)
{
HT_destroy(graph->table,destroy_entry); //int epistrofh h HT_destroy
free(graph);
return 0;
}
int insertNode(ptr_graph graph,ptr_entry entry,hash_f h)
{
int key;
HT_insert_node(graph->table,h,(void*) entry,key);
graph->size++;
return 0;
}
int insertEdge(ptr_graph graph,int id,ptr_edge filos,hash_f hash)
{
ptr_entry node;
void *temp;
temp = HT_search(graph->table,id,hash);
node = (ptr_entry) temp;
LL_insert((list_ptr)(node->friends),(void*)filos);
return 0;
}
ptr_entry lookupNode(ptr_graph graph,int id,hash_f hash)
{
ptr_entry node;
void *temp;
temp = HT_search(graph->table,id,hash);
node = (ptr_entry) temp;
return node;
}
int reachNode1( ptr_graph this, int start, int end,hash_f hash )
{
int i, result;
for ( i = 0; i < this->table->size; ++i )
{
if ( IDS( this, start, end, i ) != 0 )
{
return i;
}
}
return -1;
}
int IDS( ptr_graph this, int start, int end, int level,hash_f hash )
{
int i, j;
list_ptr edges;
ptr_edge *array;
if ( level == 0 )
{
return start == end;
}
edges = ( (ptr_entry) HT_search( this->table, start, hash ) )->friends;
array = LL_export( edges );
for ( i = 0; i < this->table->size - level; ++i )
{
for ( j = 0; j < edges->size; ++j )
{
if ( IDS( this, array[j]->id, end, level - 1 ) != 0 )
{
free( array );
return 1;
}
}
}
free( array );
return 0;
}
ResultSet reachNodeN(ptr_graph graph, int start)
{
int i,j,z=0;
struct bucket *init;
int apost;
ResultSet result;
result.pinakas_id = malloc(graph->size * sizeof(int));
result.pinakas_apost = malloc(graph->size * sizeof(int));
for(i=0;i<(graph->table->size);i++)
{
init = graph->table->buckets[i];
while(init != NULL)
{
for(j=0;j<(graph->table->bucket_size);j++)
{
apost = reachNode1(graph,start, ( (ptr_entry) init->records[j]->data )->id);
result.pinakas_apost[z] = apost;
result.pinakas_id[z] = ( (ptr_entry) init->records[j]->data )->id;
z++;
}
init = init->overflow;
}
}
return result;
}
void NEXT(ResultSet result, int *id, int *apostasi)
{
static int i=0;
*id = result.pinakas_id[i];
*apostasi = result.pinakas_apost[i];
i++;
}