Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
s8_ivanov_r
AT2019_distance_throug_cities
Commits
8856d3be
Commit
8856d3be
authored
Oct 09, 2019
by
Roberts Ivanovs
Browse files
Added backtracking to the city_dikstra
parent
84d4ca10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
14 deletions
+32
-14
city_dijkstra.py
city_dijkstra.py
+32
-14
No files found.
city_dijkstra.py
View file @
8856d3be
...
...
@@ -11,14 +11,17 @@ def mans_cels(city_1, city_2, matrix, cities):
visited
=
[
False
]
*
len
(
cities
)
for
iteration
in
range
(
0
,
len
(
cities
)):
# for iteration in range(0, len(sum_dist)):
for
node_to_visit
in
range
(
0
,
len
(
sum_dist
)):
length
=
matrix
[
curr_node
][
node_to_visit
]
node_visited
=
visited
[
node_to_visit
]
visiting_curr
=
node_to_visit
==
curr_node
visiting_curr
=
node_to_visit
==
curr_node
or
length
>=
100
cannot_reach
=
length
==
inf
# Determine wether we're trying to access an invalid city
if
visiting_curr
or
cannot_reach
or
node_visited
:
continue
# Calculate the new length to each of the connected city
if
sum_dist
[
curr_node
]
+
length
<
sum_dist
[
node_to_visit
]:
sum_dist
[
node_to_visit
]
=
sum_dist
[
curr_node
]
+
length
...
...
@@ -26,23 +29,38 @@ def mans_cels(city_1, city_2, matrix, cities):
curr_smallest_len
=
inf
curr_smallest_id
=
0
for
iter_node
in
range
(
0
,
len
(
cities
)):
if
not
visited
[
iter_node
]
and
sum_dist
[
iter_node
]
<
curr_smallest_len
:
curr_smallest_len
=
sum_dist
[
iter_node
]
curr_smallest_id
=
iter_node
# Find a connected node with the least distance
for
i_node
in
range
(
0
,
len
(
cities
)):
if
not
visited
[
i_node
]
and
sum_dist
[
i_node
]
<
curr_smallest_len
:
curr_smallest_len
=
sum_dist
[
i_node
]
curr_smallest_id
=
i_node
# If we're at the end node, trace back the graph
if
visited
[
end_node
]:
route
=
[
cities
[
end_node
]]
pos
=
end_node
while
(
pos
!=
start_node
):
for
i_node
in
range
(
0
,
len
(
sum_dist
)):
if
i_node
!=
pos
and
matrix
[
i_node
][
pos
]
!=
inf
and
sum_dist
[
pos
]
-
\
matrix
[
i_node
][
pos
]
==
sum_dist
[
i_node
]:
route
.
append
(
cities
[
i_node
])
pos
=
i_node
break
route
.
reverse
()
print
(
route
)
break
city_to_visit
=
cities
[
curr_smallest_id
]
print
(
f
"Next city to visit
{
city_to_visit
}
"
)
curr_node
=
curr_smallest_id
if
visited
[
end_node
]:
break
print
(
"sum_dist"
,
sum_dist
)
print
(
"visited"
,
visited
)
if
__name__
==
"__main__"
:
matrix_data
=
generate_matrix
(
50
)
matrix_data
=
generate_matrix
()
cities_string
=
", "
.
join
(
matrix_data
[
'cities'
])
print
(
f
"Availaible cities
{
cities_string
}
"
)
mans_cels
(
"Balvi"
,
"Aluksne"
,
matrix_data
[
"matrix"
],
matrix_data
[
"cities"
])
\ No newline at end of file
mans_cels
(
"Ventspils"
,
"Riga"
,
matrix_data
[
"matrix"
],
matrix_data
[
"cities"
]
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment