FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python/graphs/bellman_ford.py at master · wcfylcf/Python · GitHub
wcfylcf
/
Python
Public
forked from
TheAlgorithms/Python
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
Python
/
graphs
/
bellman_ford.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (41 loc) · 1.22 KB
Breadcrumbs
Python
/
graphs
/
bellman_ford.py
Copy path
File metadata and controls
54 lines (41 loc) · 1.22 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
from
__future__
import
print_function
def
printDist
(
dist
,
V
):
print
(
"
\n
Vertex Distance"
)
for
i
in
range
(
V
):
if
dist
[
i
]
!=
float
(
'inf'
) :
print
(
i
,
"
\t
"
,
int
(
dist
[
i
]),
end
=
"
\t
"
)
else
:
print
(
i
,
"
\t
"
,
"INF"
,
end
=
"
\t
"
)
print
()
def
BellmanFord
(
graph
,
V
,
E
,
src
):
mdist
=
[
float
(
'inf'
)
for
i
in
range
(
V
)]
mdist
[
src
]
=
0.0
for
i
in
range
(
V
-
1
):
for
j
in
range
(
V
):
u
=
graph
[
j
][
"src"
]
v
=
graph
[
j
][
"dst"
]
w
=
graph
[
j
][
"weight"
]
if
mdist
[
u
]
!=
float
(
'inf'
)
and
mdist
[
u
]
+
w
<
mdist
[
v
]:
mdist
[
v
]
=
mdist
[
u
]
+
w
for
j
in
range
(
V
):
u
=
graph
[
j
][
"src"
]
v
=
graph
[
j
][
"dst"
]
w
=
graph
[
j
][
"weight"
]
if
mdist
[
u
]
!=
float
(
'inf'
)
and
mdist
[
u
]
+
w
<
mdist
[
v
]:
print
(
"Negative cycle found. Solution not possible."
)
return
printDist
(
mdist
,
V
)
#MAIN
V
=
int
(
input
(
"Enter number of vertices: "
))
E
=
int
(
input
(
"Enter number of edges: "
))
graph
=
[
dict
()
for
j
in
range
(
E
)]
for
i
in
range
(
V
):
graph
[
i
][
i
]
=
0.0
for
i
in
range
(
E
):
print
(
"
\n
Edge "
,
i
+
1
)
src
=
int
(
input
(
"Enter source:"
))
dst
=
int
(
input
(
"Enter destination:"
))
weight
=
float
(
input
(
"Enter weight:"
))
graph
[
i
]
=
{
"src"
:
src
,
"dst"
:
dst
,
"weight"
:
weight
}
gsrc
=
int
(
input
(
"
\n
Enter shortest path source:"
))
BellmanFord
(
graph
,
V
,
E
,
gsrc
)
Back
|
FazBrowse Home
|
New Git URL