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
62c08cfc
Commit
62c08cfc
authored
3 years ago
by
Jacob Theisen
Browse files
Options
Downloads
Patches
Plain Diff
test
parent
1bbe3280
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ov9/program1.py
+29
-22
29 additions, 22 deletions
ov9/program1.py
ov9/test_files/set_test.py
+199
-0
199 additions, 0 deletions
ov9/test_files/set_test.py
with
228 additions
and
22 deletions
ov9/program1.py
+
29
−
22
View file @
62c08cfc
...
...
@@ -75,32 +75,40 @@ def init_table():
for
i
in
range
(
num_of_nodes
):
res_arr
.
append
(
results
(
i
,
max_num
.
time
+
1
,
False
,
None
))
# def rec_func(node, counter, global_fra, end_switch):
# if not end_switch:
# counter += node.time
# if node.node == global_fra:
# return counter
# else:
# node = [a for a in res_arr if a.node == node.prev][0]
# return rec_func(node, counter, global_fra, end_switch)
# elif end_switch:
# counter += node.time
# path.append(node.node)
# if node.node == global_fra:
# return counter, path
# else:
# node = [a for a in res_arr if a.node == node.prev][0]
# return rec_func(node, counter, global_fra, end_switch)
def
rec_func2
(
node
,
counter
,
global_fra
):
path
=
[]
def
rec_func
(
node
,
counter
,
global_fra
,
end_switch
):
if
not
end_switch
:
while
node
.
node
!=
global_fra
:
counter
+=
node
.
time
if
node
.
node
==
global_fra
:
return
counter
else
:
path
.
append
(
node
.
node
)
node
=
[
a
for
a
in
res_arr
if
a
.
node
==
node
.
prev
][
0
]
return
rec_func
(
node
,
counter
,
global_fra
,
end_switch
)
elif
end_switch
:
counter
+=
node
.
time
path
.
append
(
node
.
node
)
if
node
.
node
==
global_fra
:
return
counter
,
path
else
:
node
=
[
a
for
a
in
res_arr
if
a
.
node
==
node
.
prev
][
0
]
return
rec_func
(
node
,
counter
,
global_fra
,
end_switch
)
def
calculate_shortest_path
(
current_node
,
time
,
current_neighbour
,
slutt
):
end_switch
=
False
count_a
=
0
count_a
=
rec_func
(
current_neighbour
,
count_a
,
slutt
,
end_switch
)
count_a
,
_
=
rec_func
2
(
current_neighbour
,
count_a
,
slutt
)
count_b
=
0
count_b
=
rec_func
(
current_node
,
count_b
,
slutt
,
end_switch
)
count_b
,
_
=
rec_func
2
(
current_node
,
count_b
,
slutt
)
count_b
+=
time
if
count_b
<
count_a
:
return
True
...
...
@@ -148,8 +156,7 @@ def dijkstras(slutt, fra, start):
#not finished
end_node
=
[
b
for
b
in
res_arr
if
b
.
node
==
slutt
][
0
]
total_time
=
0
end_switch
=
True
total_time
,
travel_arr
=
rec_func
(
end_node
,
total_time
,
start
,
end_switch
)
total_time
,
travel_arr
=
rec_func2
(
end_node
,
total_time
,
start
)
travel_arr
=
reversed
(
travel_arr
)
print
(
f
'
total time:
{
total_time
}
\n
shortest path:
{
"
->
"
.
join
(
str
(
v
)
for
v
in
travel_arr
)
}
'
)
exit
()
...
...
@@ -171,7 +178,7 @@ def init_things():
if
sys
.
argv
[
1
]
==
'
-d
'
:
#node_id
start
=
20
slutt
=
2
7
slutt
=
2
5
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.
ov9/test_files/set_test.py
0 → 100644
+
199
−
0
View file @
62c08cfc
import
sys
class
int_points
(
object
):
def
__init__
(
self
,
id
,
type
,
name
):
self
.
id
=
id
self
.
type
=
type
self
.
name
=
name
class
nodes
(
object
):
def
__init__
(
self
,
node
,
bredde
,
lengde
):
self
.
node
=
node
self
.
bredde
=
bredde
self
.
lengde
=
lengde
class
edges
(
object
):
def
__init__
(
self
,
fra
,
til
,
time
,
lenght
,
speed
):
self
.
fra
=
fra
self
.
til
=
til
self
.
time
=
time
self
.
lenght
=
lenght
self
.
speed
=
speed
class
results
(
object
):
def
__init__
(
self
,
node
,
time
,
visited
,
prev
):
self
.
node
=
node
self
.
time
=
time
self
.
visited
=
visited
self
.
prev
=
prev
#prep nodes
with
open
(
'
./noder.txt
'
,
'
r
'
)
as
file
:
node_file
=
file
.
read
()
node_file
=
node_file
.
split
()
num_of_nodes
=
int
(
node_file
[
0
])
node_file
.
pop
(
0
)
node_arr
=
set
()
for
i
in
range
(
0
,
len
(
node_file
),
3
):
#nodenr breddegrad lengdegrad
node_arr
.
add
(
nodes
(
int
(
node_file
[
i
]),
float
(
node_file
[
i
+
1
]),
float
(
node_file
[
i
+
2
])))
#prep edges
with
open
(
'
./kanter.txt
'
,
'
r
'
)
as
file
:
edge_file
=
file
.
read
()
edge_file
=
edge_file
.
split
()
num_of_edges
=
int
(
edge_file
[
0
])
edge_file
.
pop
(
0
)
edge_arr
=
set
()
for
i
in
range
(
0
,
len
(
edge_file
),
5
):
#franode tilnode kjøretid lengde fartsgrense
edge_arr
.
add
(
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
:
intresting_file
=
file
.
read
()
intresting_file
=
intresting_file
.
split
(
'
\n
'
)
num_of_intresting
=
int
(
intresting_file
[
0
])
intresting_file
.
pop
(
0
)
intresting_arr
=
set
()
for
i
in
range
(
len
(
intresting_file
)):
#nodenr kode "Navn på stedet"
tmp_values
=
intresting_file
[
i
].
split
()
intresting_arr
.
add
(
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
()
def
init_table
():
for
i
in
range
(
num_of_nodes
):
res_arr
.
add
(
results
(
i
,
max_num
.
time
+
1
,
False
,
None
))
# def rec_func(node, counter, global_fra, end_switch):
# if not end_switch:
# counter += node.time
# if node.node == global_fra:
# return counter
# else:
# node = [a for a in res_arr if a.node == node.prev][0]
# return rec_func(node, counter, global_fra, end_switch)
# elif end_switch:
# counter += node.time
# path.append(node.node)
# if node.node == global_fra:
# return counter, path
# else:
# node = [a for a in res_arr if a.node == node.prev][0]
# return rec_func(node, counter, global_fra, end_switch)
def
rec_func2
(
node
,
counter
,
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
def
calculate_shortest_path
(
current_node
,
time
,
current_neighbour
,
slutt
):
count_a
=
0
count_a
,
_
=
rec_func2
(
current_neighbour
,
count_a
,
slutt
)
count_b
=
0
count_b
,
_
=
rec_func2
(
current_node
,
count_b
,
slutt
)
count_b
+=
time
if
count_b
<
count_a
:
return
True
else
:
return
False
def
update_que
(
que
):
tmp_arr
=
[
a
for
a
in
res_arr
if
a
.
node
in
que
]
tmp_arr
.
sort
(
key
=
lambda
x
:
x
.
time
,
reverse
=
True
)
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
]
for
tmp_edge
in
edge_arr
:
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
if
tmp_edge
.
til
==
slutt
:
current_node
.
visited
=
True
we_soon_to_be_done
=
True
end_que
.
append
(
tmp_edge
.
til
)
else
:
if
tmp_edge
.
fra
==
slutt
:
end_que
.
append
(
tmp_edge
.
til
)
else
:
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
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
=
reversed
(
travel_arr
)
print
(
f
'
total time:
{
total_time
}
\n
shortest path:
{
"
->
"
.
join
(
str
(
v
)
for
v
in
travel_arr
)
}
'
)
exit
()
else
:
if
we_soon_to_be_done
:
current_node
.
visited
=
True
dijkstras
(
slutt
,
end_que
[
0
],
start
)
else
:
queue
.
pop
(
0
)
current_node
.
visited
=
True
queue
=
update_que
(
queue
)
dijkstras
(
slutt
,
queue
[
0
],
start
)
def
init_things
():
if
len
(
sys
.
argv
)
>
1
:
if
sys
.
argv
[
1
]
==
'
-d
'
:
#node_id
start
=
20
slutt
=
30
init_table
()
queue
.
append
(
start
)
start_node
=
[
a
for
a
in
res_arr
if
a
.
node
==
start
][
0
]
start_node
.
time
=
0
dijkstras
(
slutt
,
start
,
start
)
elif
sys
.
argv
[
1
]
==
'
-a
'
:
print
(
'
A* not yet implemented
'
)
else
:
print
(
'
Unknown argument
'
)
else
:
print
(
'
Missing argument
'
)
init_things
()
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