FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppvisgraph/src/cppvisgraph/shortest_path.hpp at master · emranemon/cppvisgraph · GitHub
emranemon
/
cppvisgraph
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
4
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppvisgraph
/
src
/
cppvisgraph
/
shortest_path.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
162 lines (147 loc) · 5.58 KB
Breadcrumbs
cppvisgraph
/
src
/
cppvisgraph
/
shortest_path.hpp
Copy path
File metadata and controls
162 lines (147 loc) · 5.58 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
//
The MIT License (MIT)
//
Copyright (c) 2023 MIEA MD EMON
//
https://github.com/emranemon
//
Permission is hereby granted, free of charge, to any person obtaining a copy
//
of this software and associated documentation files (the "Software"), to deal
//
in the Software without restriction, including without limitation the rights
//
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//
copies of the Software, and to permit persons to whom the Software is
//
furnished to do so, subject to the following conditions:
//
The above copyright notice and this permission notice shall be included in all
//
copies or substantial portions of the Software.
//
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//
SOFTWARE.
#
ifndef
CPPVISGRAPH_SHORTEST_PATH_HPP
#
define
CPPVISGRAPH_SHORTEST_PATH_HPP
#
include
"
graph.hpp
"
#
include
"
visible_vertices.hpp
"
#
include
<
unordered_map
>
#
include
<
vector
>
#
include
<
algorithm
>
#
include
<
functional
>
#
include
<
utility
>
#
include
<
queue
>
#
include
<
string
>
namespace
cppvisgraph
{
/*
*
* @brief Hashmap that can be used as a priority queue.
* This is a replica of priority_dict(dict) implemented in python
*
* Keys of the hashmap are items to be put into the queue, and values
* are their respective priorities. All hashmap methods work as expected.
* The advantage over a standard heapq-based priority queue is that priorities
* of items can be efficiently updated (amortized O(1)) using code as
* 'themap.push(item, new_priority).'
*
* Note that this is a modified version of
* https://gist.github.com/matteodellamico/4451520
*/
template
<
typename
Key,
typename
Value,
typename
Hash = std::hash<Key>>
class
priority_dict
:
public
std
::unordered_map<Key, Value, Hash>
{
private:
typedef
std::pair<Value, Key> ValueKeyPair;
struct
Greater
{
bool
operator
()(
const
ValueKeyPair& lhs,
const
ValueKeyPair& rhs)
{
return
lhs.
first
> rhs.
first
;
//
Greater than for min-heap, Smaller than for max-heap
}
};
std::priority_queue<ValueKeyPair, std::vector<ValueKeyPair>, Greater> stl_heap;
public:
void
push
(Key key, Value value)
{
auto
result =
this
->
emplace
(key, value);
if
(!result.
second
)
{
result.
first
->
second
= value;
}
stl_heap.
push
(
std::make_pair
(value, key));
}
std::pair<Key, Value>
pop
()
{
auto
vk = stl_heap.
top
();
auto
v = vk.
first
;
auto
k = vk.
second
;
stl_heap.
pop
();
while
(
this
->
count
(k)==
0
||
this
->
at
(k) != v)
{
vk = stl_heap.
top
();
v = vk.
first
;
k = vk.
second
;
stl_heap.
pop
();
}
this
->
erase
(k);
return
std::pair<Key, Value>(k, v);
}
};
//
class priority_dict
class
ShortestPath
{
private:
static
std::pair<std::unordered_map<Point,
double
, PointHash>, std::unordered_map<Point, Point, PointHash>>
dijkstra
(
const
Graph& graph,
const
Point& origin,
const
Point& destination,
const
Graph& add_to_visgraph)
{
std::unordered_map<Point,
double
, PointHash> D;
std::unordered_map<Point, Point, PointHash> P;
priority_dict<Point,
double
, PointHash> Q;
Q.
push
(origin,
0
);
while
(!Q.
empty
())
{
auto
top = Q.
pop
();
Point v = top.
first
;
D[v] = top.
second
;
if
(v == destination)
break
;
std::unordered_set<Edge, EdgeHash> edges = graph[v];
if
(add_to_visgraph.
contains
(v) && !add_to_visgraph[v].
empty
())
{
edges.
insert
(add_to_visgraph[v].
begin
(), add_to_visgraph[v].
end
());
}
for
(
const
Edge& e : edges)
{
Point w = e.
get_adjacent
(v);
double
elength = D.
at
(v) +
PolygonUtils::edge_distance
(v, w);
if
(D.
find
(w) != D.
end
())
{
if
(elength < D.
at
(w))
{
throw
std::runtime_error
(
"
Shorter path found, contradicting the assumption of shortest distances.
"
);
}
}
else
if
(Q.
find
(w) == Q.
end
() || elength < Q.
at
(w))
{
Q.
push
(w, elength);
auto
p = P.
emplace
(w, v);
if
(!p.
second
) { p.
first
->
second
= v; }
}
}
}
return
std::make_pair
(D, P);
}
public:
static
std::vector<Point>
get
(
const
Graph& graph,
const
Point& origin,
const
Point& destination,
const
Graph& add_to_visgraph = Graph())
{
auto
dp =
ShortestPath::dijkstra
(graph, origin, destination, add_to_visgraph);
std::unordered_map<Point,
double
, PointHash> D = dp.
first
;
std::unordered_map<Point, Point, PointHash> P = dp.
second
;
std::vector<Point> path;
Point back_to_origin = destination;
while
(
true
)
{
path.
push_back
(back_to_origin);
if
(back_to_origin == origin)
break
;
back_to_origin = P.
at
(back_to_origin);
}
std::reverse
(path.
begin
(), path.
end
());
return
path;
}
};
//
class ShortestPath
}
//
namespace cppvisgraph
#
endif
//
CPPVISGRAPH_SHORTEST_PATH_HPP
Back
|
FazBrowse Home
|
New Git URL