Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Algoritmer og datastrukturer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jacob Theisen
Algoritmer og datastrukturer
Commits
076e8d20
Commit
076e8d20
authored
3 years ago
by
Jacob Theisen
Browse files
Options
Downloads
Patches
Plain Diff
!!
parent
62c08cfc
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ov9/test_files/set_test.py
+22
-23
22 additions, 23 deletions
ov9/test_files/set_test.py
with
22 additions
and
23 deletions
ov9/test_files/set_test.py
+
22
−
23
View file @
076e8d20
...
...
@@ -21,9 +21,9 @@ class edges (object):
self
.
speed
=
speed
class
results
(
object
):
def
__init__
(
self
,
node
,
time
,
visited
,
prev
):
def
__init__
(
self
,
node
,
total_
time
,
visited
,
prev
):
self
.
node
=
node
self
.
t
ime
=
time
self
.
t
otal_time
=
total_
time
self
.
visited
=
visited
self
.
prev
=
prev
...
...
@@ -36,10 +36,10 @@ node_file = node_file.split()
num_of_nodes
=
int
(
node_file
[
0
])
node_file
.
pop
(
0
)
node_arr
=
set
()
node_arr
=
[]
for
i
in
range
(
0
,
len
(
node_file
),
3
):
#nodenr breddegrad lengdegrad
node_arr
.
a
d
d
(
nodes
(
int
(
node_file
[
i
]),
float
(
node_file
[
i
+
1
]),
float
(
node_file
[
i
+
2
])))
node_arr
.
a
ppen
d
(
nodes
(
int
(
node_file
[
i
]),
float
(
node_file
[
i
+
1
]),
float
(
node_file
[
i
+
2
])))
#prep edges
with
open
(
'
./kanter.txt
'
,
'
r
'
)
as
file
:
...
...
@@ -49,10 +49,10 @@ edge_file = edge_file.split()
num_of_edges
=
int
(
edge_file
[
0
])
edge_file
.
pop
(
0
)
edge_arr
=
set
()
edge_arr
=
[]
for
i
in
range
(
0
,
len
(
edge_file
),
5
):
#franode tilnode kjøretid lengde fartsgrense
edge_arr
.
a
d
d
(
edges
(
int
(
edge_file
[
i
]),
int
(
edge_file
[
i
+
1
]),
int
(
edge_file
[
i
+
2
]),
int
(
edge_file
[
i
+
3
]),
int
(
edge_file
[
i
+
4
])))
edge_arr
.
a
ppen
d
(
edges
(
int
(
edge_file
[
i
]),
int
(
edge_file
[
i
+
1
]),
int
(
edge_file
[
i
+
2
]),
int
(
edge_file
[
i
+
3
]),
int
(
edge_file
[
i
+
4
])))
#prep intresting
with
open
(
'
./interessepkt.txt
'
,
'
r
'
)
as
file
:
...
...
@@ -62,18 +62,18 @@ intresting_file = intresting_file.split('\n')
num_of_intresting
=
int
(
intresting_file
[
0
])
intresting_file
.
pop
(
0
)
intresting_arr
=
set
()
intresting_arr
=
[]
for
i
in
range
(
len
(
intresting_file
)):
#nodenr kode "Navn på stedet"
tmp_values
=
intresting_file
[
i
].
split
()
intresting_arr
.
a
d
d
(
int_points
(
int
(
tmp_values
[
0
]),
int
(
tmp_values
[
1
]),
'
'
.
join
(
tmp_values
[
2
:])))
intresting_arr
.
a
ppen
d
(
int_points
(
int
(
tmp_values
[
0
]),
int
(
tmp_values
[
1
]),
'
'
.
join
(
tmp_values
[
2
:])))
max_num
=
max
(
edge_arr
,
key
=
lambda
item
:
item
.
time
)
res_arr
=
set
()
res_arr
=
[]
def
init_table
():
for
i
in
range
(
num_of_nodes
):
res_arr
.
a
d
d
(
results
(
i
,
max_num
.
time
+
1
,
False
,
None
))
res_arr
.
a
ppen
d
(
results
(
i
,
max_num
.
time
+
1
,
False
,
None
,
))
# def rec_func(node, counter, global_fra, end_switch):
# if not end_switch:
...
...
@@ -93,14 +93,13 @@ def init_table():
# return rec_func(node, counter, global_fra, end_switch)
def
rec_func2
(
node
,
counter
,
global_fra
):
def
rec_func2
(
node
,
global_fra
):
path
=
[]
while
node
.
node
!=
global_fra
:
counter
+=
node
.
time
path
.
append
(
node
.
node
)
node
=
[
a
for
a
in
res_arr
if
a
.
node
==
node
.
prev
][
0
]
path
.
append
(
node
.
node
)
return
counter
,
path
return
path
...
...
@@ -123,13 +122,14 @@ def update_que(que):
que
=
[
a
.
node
for
a
in
tmp_arr
if
a
in
tmp_arr
]
return
que
end_que
=
[]
queue
=
[]
we_soon_to_be_done
=
False
def
dijkstras
(
slutt
,
fra
,
start
):
global
we_soon_to_be_done
global
queue
if
we_soon_to_be_done
:
end_que
.
pop
(
0
)
current_node
=
[
b
for
b
in
res_arr
if
b
.
node
==
fra
][
0
]
...
...
@@ -137,8 +137,8 @@ def dijkstras(slutt, fra, start):
if
tmp_edge
.
fra
==
fra
:
current_neighbour
=
[
a
for
a
in
res_arr
if
a
.
node
==
tmp_edge
.
til
][
0
]
if
current_neighbour
.
visited
==
False
and
current_neighbour
.
node
not
in
queue
:
current_neighbour
.
time
=
tmp_edge
.
time
current_neighbour
.
prev
=
fra
current_neighbour
.
time
=
current_node
.
time
+
tmp_edge
.
time
current_neighbour
.
prev
=
current_node
.
node
if
tmp_edge
.
til
==
slutt
:
current_node
.
visited
=
True
we_soon_to_be_done
=
True
...
...
@@ -150,16 +150,15 @@ def dijkstras(slutt, fra, start):
queue
.
append
(
tmp_edge
.
til
)
elif
current_neighbour
.
node
!=
start
:
if
calculate_shortest_path
(
current_node
,
tmp_edge
.
time
,
current_neighbour
,
start
)
:
current_neighbour
.
time
=
tmp_edge
.
time
current_neighbour
.
prev
=
tmp_edge
.
fra
if
current_node
.
time
+
tmp_edge
.
time
<
current_neighbour
.
time
:
current_neighbour
.
time
=
tmp_edge
.
time
+
current_node
.
time
current_neighbour
.
prev
=
current_node
.
node
if
we_soon_to_be_done
and
not
len
(
end_que
):
#not finished
end_node
=
[
b
for
b
in
res_arr
if
b
.
node
==
slutt
][
0
]
total_time
=
0
total_time
,
travel_arr
=
rec_func2
(
end_node
,
total_time
,
start
)
travel_arr
=
rec_func2
(
end_node
,
start
)
travel_arr
=
reversed
(
travel_arr
)
print
(
f
'
total time:
{
total_
time
}
\n
shortest path:
{
"
->
"
.
join
(
str
(
v
)
for
v
in
travel_arr
)
}
'
)
print
(
f
'
total time:
{
end_node
.
time
}
\n
shortest path:
{
"
->
"
.
join
(
str
(
v
)
for
v
in
travel_arr
)
}
'
)
exit
()
else
:
if
we_soon_to_be_done
:
...
...
@@ -179,7 +178,7 @@ def init_things():
if
sys
.
argv
[
1
]
==
'
-d
'
:
#node_id
start
=
20
slutt
=
3
0
slutt
=
400
0
init_table
()
queue
.
append
(
start
)
start_node
=
[
a
for
a
in
res_arr
if
a
.
node
==
start
][
0
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment