FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Basic-Python-Programs/DjikistraAlgorithm.py at main · gitishman/Basic-Python-Programs · GitHub
gitishman
/
Basic-Python-Programs
Public
forked from
souravjain540/Basic-Python-Programs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Basic-Python-Programs
/
DjikistraAlgorithm.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (33 loc) · 1.04 KB
Breadcrumbs
Basic-Python-Programs
/
DjikistraAlgorithm.py
Copy path
File metadata and controls
43 lines (33 loc) · 1.04 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
class
Graph
:
def
__init__
(
self
):
self
.
nodes
=
set
()
self
.
edges
=
defaultdict
(
list
)
self
.
distances
=
{}
def
add_node
(
self
,
value
):
self
.
nodes
.
add
(
value
)
def
add_edge
(
self
,
from_node
,
to_node
,
distance
):
self
.
edges
[
from_node
].
append
(
to_node
)
self
.
edges
[
to_node
].
append
(
from_node
)
self
.
distances
[(
from_node
,
to_node
)]
=
distance
def
dijsktra
(
graph
,
initial
):
visited
=
{
initial
:
0
}
path
=
{}
nodes
=
set
(
graph
.
nodes
)
while
nodes
:
min_node
=
None
for
node
in
nodes
:
if
node
in
visited
:
if
min_node
is
None
:
min_node
=
node
elif
visited
[
node
]
<
visited
[
min_node
]:
min_node
=
node
if
min_node
is
None
:
break
nodes
.
remove
(
min_node
)
current_weight
=
visited
[
min_node
]
for
edge
in
graph
.
edges
[
min_node
]:
weight
=
current_weight
+
graph
.
distance
[(
min_node
,
edge
)]
if
edge
not
in
visited
or
weight
<
visited
[
edge
]:
visited
[
edge
]
=
weight
path
[
edge
]
=
min_node
return
visited
,
path
Back
|
FazBrowse Home
|
New Git URL