FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-examples/cpp-algorithm/src/graph/dijkstra.cpp at main · codejsha/algorithm-examples · GitHub
codejsha
/
algorithm-examples
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
graph
/
dijkstra.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (46 loc) · 1.4 KB
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
graph
/
dijkstra.cpp
Copy path
File metadata and controls
53 lines (46 loc) · 1.4 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
#
include
"
dijkstra.h
"
void
Dijkstra::Graph::DijkstraAlgorithm
(Vertex& source)
{
std::priority_queue<Vertex*, std::vector<Vertex*>, MinComparator> min_queue;
for
(
auto
v : vertices)
{
min_queue.
push
(v);
}
while
(!min_queue.
empty
())
{
const
auto
u = min_queue.
top
();
min_queue.
pop
();
for
(
const
auto
v : u->
neighbors
)
{
//
Relaxation
const
auto
weight_uv = weight_list.
at
(
std::make_pair
(u->
id
, v->
id
));
if
(v->
distance
> (u->
distance
+ weight_uv))
{
v->
distance
= u->
distance
+ weight_uv;
v->
predecessor
= u;
}
ReorderQueue
(min_queue);
}
}
}
void
Dijkstra::Graph::ReorderQueue
(std::priority_queue<Vertex*, std::vector<Vertex*>, MinComparator>& min_queue)
{
auto
queue = std::priority_queue<Vertex*, std::vector<Vertex*>, MinComparator>{};
const
int
min_queue_size =
static_cast
<
int
>(min_queue.
size
());
for
(
int
i =
0
; i < min_queue_size; ++i)
{
queue.
push
(min_queue.
top
());
min_queue.
pop
();
}
min_queue =
std::move
(queue);
}
void
Dijkstra::Graph::AddVertex
(Vertex& v)
{
vertices.
push_back
(&v);
}
void
Dijkstra::Graph::AddEdge
(Vertex& u, Vertex& v,
int
weight)
{
adjacency_list.
emplace_back
(&u, &v);
weight_list.
insert
(
std::make_pair
(
std::make_pair
(u.
id
, v.
id
), weight));
u.
neighbors
.
insert
(&v);
}
Back
|
FazBrowse Home
|
New Git URL