FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mage/cpp/algo_module/algorithm/algo.hpp at main · memgraph/mage · GitHub
This repository was archived by the owner on Jan 23, 2026. It is now read-only.
memgraph
/
mage
Public archive
Notifications
You must be signed in to change notification settings
Fork
35
Star
331
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mage
/
cpp
/
algo_module
/
algorithm
/
algo.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
270 lines (232 loc) · 9.48 KB
Breadcrumbs
mage
/
cpp
/
algo_module
/
algorithm
/
algo.hpp
Copy path
File metadata and controls
270 lines (232 loc) · 9.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#
pragma
once
#
include
<
cmath
>
#
include
<
memory
>
#
include
<
mgp.hpp
>
#
include
<
queue
>
#
include
<
unordered_map
>
#
include
<
unordered_set
>
#
include
<
vector
>
namespace
Algo
{
enum
class
RelDirection
{
kNone
= -
1
,
kAny
=
0
,
kIncoming
=
1
,
kOutgoing
=
2
,
kBoth
=
3
};
class
PathFinder
{
public:
PathFinder
(
const
mgp::Node &start_node,
const
mgp::Node &end_node,
int64_t
max_length,
const
mgp::List &rel_types,
const
mgp::RecordFactory &record_factory);
RelDirection
GetDirection
(
const
std::string &rel_type)
const
;
void
UpdateRelationshipDirection
(
const
mgp::List &relationship_types);
void
DFS
(
const
mgp::Node &curr_node, mgp::Path &curr_path, std::unordered_set<
int64_t
> &visited);
void
FindAllPaths
();
private:
const
mgp::Node start_node_;
const
mgp::Id end_node_id_;
const
int64_t
max_length_;
bool
any_incoming_;
bool
any_outgoing_;
bool
all_incoming_;
bool
all_outgoing_;
std::unordered_map<std::string_view, RelDirection> rel_direction_;
const
mgp::RecordFactory &record_factory_;
};
/*
all_simple_paths constants
*/
constexpr
const
std::string_view
kProcedureAllSimplePaths
=
"
all_simple_paths
"
;
constexpr
const
std::string_view
kAllSimplePathsArg1
=
"
start_node
"
;
constexpr
const
std::string_view
kAllSimplePathsArg2
=
"
end_node
"
;
constexpr
const
std::string_view
kAllSimplePathsArg3
=
"
relationship_types
"
;
constexpr
const
std::string_view
kAllSimplePathsArg4
=
"
max_length
"
;
constexpr
const
std::string_view
kResultAllSimplePaths
=
"
path
"
;
/*
cover constants
*/
constexpr
std::string_view
kProcedureCover
=
"
cover
"
;
constexpr
std::string_view
kCoverArg1
=
"
nodes
"
;
constexpr
std::string_view
kCoverRet1
=
"
rel
"
;
void
AllSimplePaths
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void
Cover
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
/*
from_nodes constants
*/
constexpr
const
std::string_view
kProcedureAStar
=
"
astar
"
;
constexpr
const
std::string_view
kAStarStart
=
"
start
"
;
constexpr
const
std::string_view
kAStarTarget
=
"
target
"
;
constexpr
const
std::string_view
kAStarConfig
=
"
config
"
;
constexpr
const
std::string_view
kAStarPath
=
"
path
"
;
constexpr
const
std::string_view
kAStarWeight
=
"
weight
"
;
const
std::string
kDefaultHeuristic
=
"
"
;
const
std::string
kDefaultDistance
=
"
distance
"
;
const
std::string
kDefaultLatitude
=
"
lat
"
;
const
std::string
kDefaultLongitude
=
"
lon
"
;
enum
class
RelationshipType
{
IN
,
OUT
};
struct
NodeObject
{
public:
//
heuristic distance of the node
double
heuristic_distance;
//
total distance of the path to the node
double
total_distance;
//
the node the object represents
mgp::Node node;
//
path relationship that leads into the node
mgp::Relationship rel;
//
previous node object
std::shared_ptr<NodeObject> prev;
NodeObject
(
const
double
heuristic_distance,
const
double
total_distance,
const
mgp::Node &node,
const
mgp::Relationship &rel,
const
std::shared_ptr<NodeObject> &prev)
: heuristic_distance(heuristic_distance), total_distance(total_distance), node(node), rel(rel), prev(prev) {}
struct
Hash
{
size_t
operator
()(
const
std::shared_ptr<NodeObject> &nodeObj)
const
{
return
std::hash<
int64_t
>()(nodeObj->
node
.
Id
().
AsInt
());
}
};
struct
Comp
{
bool
operator
()(
const
std::shared_ptr<NodeObject> &nodeObj,
const
std::shared_ptr<NodeObject> &nodeObj2) {
return
nodeObj->
total_distance
+ nodeObj->
heuristic_distance
>
nodeObj2->
total_distance
+ nodeObj2->
heuristic_distance
;
}
};
struct
Equal
{
bool
operator
()(
const
std::shared_ptr<NodeObject> &nodeObj,
const
std::shared_ptr<NodeObject> &nodeObj2)
const
{
return
nodeObj->
node
.
Id
() == nodeObj2->
node
.
Id
();
}
};
};
class
Open
{
public:
/*
since C++ pq doesnt enable to do std::find or loop and find element in pq,
and for A* we need to see if elements already exist in pq, I created a class open, which
uses a pq normally for A*, but also has a set which checks if the value is
already in pq, and if it has lesser path distance to it
*/
std::priority_queue<std::shared_ptr<NodeObject>, std::vector<std::shared_ptr<NodeObject>>, NodeObject::Comp> pq;
std::unordered_map<mgp::Id,
double
> set;
bool
Empty
() {
return
set.
empty
(); }
const
std::shared_ptr<NodeObject>
Top
() {
while
(set.
find
(pq.
top
()->
node
.
Id
()) == set.
end
()) {
//
this is to make sure duplicates are ignored
pq.
pop
();
}
return
pq.
top
();
}
void
Pop
() {
set.
erase
(pq.
top
()->
node
.
Id
());
pq.
pop
();
}
void
InsertOrUpdate
(
const
std::shared_ptr<NodeObject> &elem) {
auto
it = set.
find
(elem->
node
.
Id
());
if
(it != set.
end
()) {
if
(elem->
total_distance
< it->
second
) {
it->
second
= elem->
total_distance
;
pq.
push
(elem);
}
return
;
}
pq.
push
(elem);
set.
insert
({elem->
node
.
Id
(), elem->
total_distance
});
}
Open
() =
default
;
};
class
Closed
{
public:
std::unordered_set<std::shared_ptr<NodeObject>, NodeObject::Hash, NodeObject::Equal> closed;
bool
Empty
() {
return
closed.
empty
(); }
void
Erase
(
const
std::shared_ptr<NodeObject> &obj) { closed.
erase
(obj); }
bool
FindAndCompare
(
const
std::shared_ptr<NodeObject> &obj) {
auto
it = closed.
find
(obj);
if
(it != closed.
end
()) {
bool
erase = (*it)->
total_distance
> obj->
total_distance
;
if
(erase) {
closed.
erase
(it);
}
return
erase;
}
return
true
;
}
void
Insert
(
const
std::shared_ptr<NodeObject> &obj) { closed.
insert
(obj); }
Closed
() =
default
;
};
class
Config
{
public:
bool
unweighted =
false
;
double
epsilon =
1.0
;
std::string distance_prop =
kDefaultDistance
;
std::string heuristic_name =
kDefaultHeuristic
;
std::string latitude_name =
kDefaultLatitude
;
std::string longitude_name =
kDefaultLongitude
;
std::unordered_set<std::string> whitelist;
std::unordered_set<std::string> blacklist;
std::unordered_set<std::string> in_rels;
std::unordered_set<std::string> out_rels;
bool
duration =
false
;
Config
(
const
mgp::Map &map) {
if
(!map.
At
(
"
unweighted
"
).
IsNull
()) {
unweighted = map.
At
(
"
unweighted
"
).
ValueBool
();
}
if
(!map.
At
(
"
epsilon
"
).
IsNull
()) {
epsilon = map.
At
(
"
epsilon
"
).
ValueNumeric
();
}
if
(!map.
At
(
"
distance_prop
"
).
IsNull
()) {
distance_prop = map.
At
(
"
distance_prop
"
).
ValueString
();
}
if
(!map.
At
(
"
heuristic_name
"
).
IsNull
()) {
heuristic_name = map.
At
(
"
heuristic_name
"
).
ValueString
();
}
if
(!map.
At
(
"
latitude_name
"
).
IsNull
()) {
latitude_name = map.
At
(
"
latitude_name
"
).
ValueString
();
}
if
(!map.
At
(
"
longitude_name
"
).
IsNull
()) {
longitude_name = map.
At
(
"
longitude_name
"
).
ValueString
();
}
if
(!map.
At
(
"
whitelisted_labels
"
).
IsNull
()) {
auto
list = map.
At
(
"
whitelisted_labels
"
).
ValueList
();
for
(
const
auto
value : list) {
whitelist.
insert
(
std::string
(value.
ValueString
()));
}
}
if
(!map.
At
(
"
blacklisted_labels
"
).
IsNull
()) {
auto
list = map.
At
(
"
blacklisted_labels
"
).
ValueList
();
for
(
const
auto
value : list) {
blacklist.
insert
(
std::string
(value.
ValueString
()));
}
}
if
(!map.
At
(
"
relationships_filter
"
).
IsNull
()) {
auto
list = map.
At
(
"
relationships_filter
"
).
ValueList
();
for
(
const
auto
value : list) {
auto
rel_type =
std::string
(value.
ValueString
());
const
size_t
size = rel_type.
size
();
const
char
first_elem = rel_type[
0
];
const
char
last_elem = rel_type[size -
1
];
if
(first_elem ==
'
<
'
&& size !=
1
) {
in_rels.
insert
(rel_type.
erase
(
0
,
1
));
//
only specified incoming relationships are allowed
}
else
if
(last_elem ==
'
>
'
&& size !=
1
) {
rel_type.
pop_back
();
out_rels.
insert
(rel_type);
//
only specifed outgoing relationships are allowed
}
else
{
//
if not specified, a relationship goes both ways
in_rels.
insert
(rel_type);
out_rels.
insert
(rel_type);
}
}
}
if
(!map.
At
(
"
duration
"
).
IsNull
()) {
duration = map.
At
(
"
duration
"
).
ValueBool
();
}
}
};
struct
GoalNodes
{
const
mgp::Node start;
const
mgp::Node target;
std::pair<
double
,
double
> lat_lon;
GoalNodes
(
const
mgp::Node &start,
const
mgp::Node &target,
const
std::pair<
double
,
double
> lat_lon)
: start(start), target(target), lat_lon(lat_lon) {}
GoalNodes
(
const
mgp::Node &start,
const
mgp::Node &target) : start(start), target(target) {}
};
struct
TrackingLists
{
Open open;
Closed closed;
};
double
GetHaversineDistance
(
double
lat1,
double
lon1,
double
lat2,
double
lon2);
double
GetRadians
(
double
degrees);
void
AStar
(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
bool
RelOk
(
const
mgp::Relationship &rel,
const
Config &config,
const
RelationshipType rel_type);
bool
IsLabelOk
(
const
mgp::Node &node,
const
Config &config);
std::pair<mgp::Path,
double
>
BuildResult
(std::shared_ptr<NodeObject> final_node,
const
mgp::Node &start);
std::shared_ptr<NodeObject>
InitializeStart
(
const
mgp::Node &start);
std::pair<mgp::Path,
double
>
HelperAstar
(
const
GoalNodes &nodes,
const
Config &config);
void
ExpandRelationships
(
const
std::shared_ptr<NodeObject> &prev,
const
RelationshipType rel_type,
const
GoalNodes &nodes, TrackingLists &lists,
const
Config &config);
double
CalculateHeuristic
(
const
Config &config,
const
mgp::Node &node,
const
GoalNodes &nodes);
std::pair<
double
,
double
>
GetLatLon
(
const
mgp::Node &target,
const
Config &config);
double
CalculateDistance
(
const
Config &config,
const
mgp::Relationship &rel);
void
CheckConfigTypes
(
const
mgp::Map &map);
}
//
namespace Algo
Back
|
FazBrowse Home
|
New Git URL