Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
svf_project
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
Container Registry
Model registry
Operate
Environments
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Magnus.Ersdal
svf_project
Commits
e974f076
Commit
e974f076
authored
7 years ago
by
Magnus.Ersdal
Browse files
Options
Downloads
Patches
Plain Diff
sped up simplegraph.py with 10-30%
parent
881269d2
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
simplegraph.py
+56
-4
56 additions, 4 deletions
simplegraph.py
with
56 additions
and
4 deletions
simplegraph.py
+
56
−
4
View file @
e974f076
...
@@ -37,7 +37,7 @@ def find_all_paths(graph, start, end, path=[]):
...
@@ -37,7 +37,7 @@ def find_all_paths(graph, start, end, path=[]):
paths
.
append
(
newpath
)
paths
.
append
(
newpath
)
return
paths
return
paths
"""
def find_shortest_path(graph, start, end, path=[], level=0):
def find_shortest_path(graph, start, end, path=[], level=0):
#print(
"
start:
"
, start,
"
end:
"
,end)
#print(
"
start:
"
, start,
"
end:
"
,end)
path = path + [start]
path = path + [start]
...
@@ -69,17 +69,69 @@ def find_shortest_path(graph, start, end, path=[], level=0):
...
@@ -69,17 +69,69 @@ def find_shortest_path(graph, start, end, path=[], level=0):
shortest = b
shortest = b
path = [start] + shortest
path = [start] + shortest
return path
return path
"""
def
find_shortest_path
(
graph
,
start
,
end
,
path
=
[]):
"""
this is called first, fsp_post is called second
"""
#print("start:", start, "end:",end)
path
=
path
+
[
start
]
# print("PATH :",path, "start",start,"end",end)
if
start
==
end
and
end
not
in
graph
[
start
]:
# from the next node, which is the fastest way to the end?
alt0
,
alt1
=
graph
[
start
]
# get the two alternatives
first
=
find_shortest_path
(
graph
,
alt0
,
end
,
[])
second
=
find_shortest_path
(
graph
,
alt1
,
end
,
[])
shortest
=
fsp_select_shortest
(
first
,
second
)
path
=
[
start
]
+
shortest
return
path
elif
start
==
end
:
return
path
if
not
start
in
graph
.
keys
():
# print("error, start node not in graph")
return
None
shortest
=
None
for
node
in
graph
[
start
]:
if
node
not
in
path
:
newpath
=
fsp_post
(
graph
,
node
,
end
,
path
)
if
newpath
:
if
not
shortest
or
len
(
newpath
)
<
len
(
shortest
):
shortest
=
newpath
return
shortest
def
fsp_select_shortest
(
first
,
second
):
mini
=
min
(
len
(
first
),
len
(
second
))
if
len
(
first
)
==
len
(
second
):
# lengths are equal.
if
'
IDLE
'
in
first
:
shortest
=
first
if
'
IDLE
'
in
second
:
shortest
=
second
else
:
shortest
=
first
else
:
if
mini
==
len
(
first
):
shortest
=
first
else
:
shortest
=
second
return
shortest
# elif start == end:
def
fsp_post
(
graph
,
start
,
end
,
path
=
[]):
# path = find_path_back(graph, start, path)
"""
faster version of find_shortest_path, because of checks in first call
"""
#print("start:", start, "end:",end)
path
=
path
+
[
start
]
# print("PATH :",path, "start",start,"end",end)
if
start
==
end
:
return
path
if
not
start
in
graph
.
keys
():
if
not
start
in
graph
.
keys
():
# print("error, start node not in graph")
# print("error, start node not in graph")
return
None
return
None
shortest
=
None
shortest
=
None
for
node
in
graph
[
start
]:
for
node
in
graph
[
start
]:
if
node
not
in
path
:
if
node
not
in
path
:
newpath
=
f
ind_shortest_path
(
graph
,
node
,
end
,
path
,
level
=
level
+
1
)
newpath
=
f
sp_post
(
graph
,
node
,
end
,
path
)
if
newpath
:
if
newpath
:
if
not
shortest
or
len
(
newpath
)
<
len
(
shortest
):
if
not
shortest
or
len
(
newpath
)
<
len
(
shortest
):
shortest
=
newpath
shortest
=
newpath
return
shortest
return
shortest
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