-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
60 additions
and
132 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
dag_longest_path(g, distmx=weights(g); topological_order=topological_sort_by_dfs(g)) | ||
Return a longest path within the directed acyclic graph `g`, with distance matrix `distmx` and using `topological_order` to iterate on vertices. | ||
""" | ||
function dag_longest_path end | ||
|
||
@traitfn function dag_longest_path( | ||
g::::IsDirected, | ||
distmx::AbstractMatrix=weights(g); | ||
topological_order=topological_sort_by_dfs(g), | ||
) | ||
U = eltype(g) | ||
T = eltype(distmx) | ||
|
||
dists = zeros(T, nv(g)) | ||
parents = zeros(U, nv(g)) | ||
|
||
for v in topological_order | ||
for u in inneighbors(g, v) | ||
newdist = dists[u] + distmx[u, v] | ||
if newdist > dists[v] | ||
dists[v] = newdist | ||
parents[v] = u | ||
end | ||
end | ||
end | ||
|
||
if isempty(dists) | ||
return U[] | ||
else | ||
v = argmax(dists) | ||
return path_from_parents(v, parents) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function path_from_parents(target::Integer, parents::AbstractVector) | ||
v = target | ||
path = [v] | ||
while parents[v] != v && parents[v] != zero(v) | ||
v = parents[v] | ||
pushfirst!(path, v) | ||
end | ||
return path | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@testset "Path from parents" begin | ||
using Graphs: path_from_parents | ||
parents = [3, 0, 2, 5, 5] | ||
@test path_from_parents(1, parents) == [2, 3, 1] | ||
@test path_from_parents(2, parents) == [2] | ||
@test path_from_parents(3, parents) == [2, 3] | ||
@test path_from_parents(4, parents) == [5, 4] | ||
@test path_from_parents(5, parents) == [5] | ||
end |