-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchFunctions.c
147 lines (142 loc) · 3.89 KB
/
SearchFunctions.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
#include <stdio.h>
#include <stdlib.h>
#include "BaseFunctions.h"
// The recursive search parts of the BaseFunctions
void triangle_search(Graph *graph, visitors *vis, double limit) {
// If the graph has already been visited, skip it
if (graph->visited) {
return;
}
tnode *current = graph->sent;
tnode *temp = graph->sent;
if (current == NULL) {
return;
}
// When exactly 3 ids are into the array, search
// if the last account is pointing at the beggining
if (vis->visits == 2) {
while (temp != NULL) {
if ((temp->amount >= limit) && (graph_getID(temp->transaction_acc) == vis->found[0])) {
printf("Success: triangle(%d,%f)=\n", vis->found[0], limit);
printf("(%d , %d , %d)\n", vis->found[0], vis->found[1], graph->id);
return;
}
temp = temp->next;
}
return;
}
// Add the account into the array
vis->found[vis->visits] = graph->id;
vis->visits++;
graph->visited = 1;
// Perform a recursive search
while (current != NULL) {
if (current->amount >= limit) {
triangle_search(get_graph(current), vis, limit);
}
current = current->next;
}
vis->visits--;
graph_unvisited(graph);
}
int conn_search(Graph *graph, visitors *vis, int id_end) {
if (graph->visited) {
return 0;
}
// If a path to the id_end was found, print it and exit
if (graph->id == id_end) {
int i;
printf("success: conn(%d,%d) = (", vis->found[0], id_end);
for (i = 0; i < vis->visits; i++) {
printf("%d , ", vis->found[i]);
}
printf("%d)\n", id_end);
return 1;
}
tnode *current = graph->sent;
if (current == NULL) {
return 0;
}
// If there is not enough space in the array, double it
if (vis->visits == vis->array_size) {
vis->found = realloc(vis->found, (2*(vis->array_size))*sizeof(int));
vis->array_size *= 2;
}
vis->found[vis->visits] = graph->id;
vis->visits++;
graph->visited = 1;
while (current != NULL) {
// If a connection has been found, stop searching
if (conn_search(get_graph(current), vis, id_end)) {
graph_unvisited(graph);
return 1;
}
current = current->next;
}
vis->visits--;
graph_unvisited(graph);
return 0;
}
void allcycles_search(Graph *graph, visitors *vis) {
// If there is at minimum a triangle formed, print it
if ((graph->id == vis->found[0]) && (vis->visits > 2)) {
int i;
printf("success: cycles %d = \n(", vis->found[0]);
for (i = 0; i < vis->visits - 1; i++) {
printf("%d , ", vis->found[i]);
}
printf("%d)\n", vis->found[i]);
return;
}
if (graph->visited) {
return;
}
tnode *current = graph->sent;
if (current == NULL) {
return;
}
if (vis->visits == vis->array_size) {
vis->found = realloc(vis->found, (2*(vis->array_size))*sizeof(int));
vis->array_size *= 2;
}
vis->found[vis->visits] = graph->id;
vis->visits++;
graph->visited = 1;
while (current != NULL) {
allcycles_search(get_graph(current), vis);
current = current->next;
}
vis->visits--;
graph_unvisited(graph);
}
void traceflow_search(Graph *graph, visitors *vis, int depth, double *total) {
if (graph->visited) {
return;
}
if (vis->visits == depth) {
int i;
// If the depth is reached, print the path and the total money sent
printf("success: traceflow(%d,%d) = \n(", vis->found[0], depth);
for (i = 0; i < vis->visits; i++) {
printf("%d , ", vis->found[i]);
}
printf("%d, %f)\n", graph->id, *total);
return;
}
tnode *current = graph->sent;
if (current == NULL) {
return;
}
vis->found[vis->visits] = graph->id;
vis->visits++;
graph->visited = 1;
while (current != NULL) {
*total += current->amount;
traceflow_search(get_graph(current), vis, depth, total);
// Remove the money sent after the printing or searching;
*total -= current->amount;
current = current->next;
}
vis->visits--;
graph_unvisited(graph);
}