Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Mar 5, 2024
1 parent 432b163 commit 989b819
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 132 deletions.
20 changes: 0 additions & 20 deletions docs/src/algorithms/longestpaths.md

This file was deleted.

3 changes: 2 additions & 1 deletion docs/src/algorithms/shortestpaths.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Shortest paths

*Graphs.jl* includes standard algorithms for [shortest paths](https://en.wikipedia.org/wiki/Shortest_path_problem).
*Graphs.jl* includes standard algorithms for [shortest paths](https://en.wikipedia.org/wiki/Shortest_path_problem) and longest paths.

## Index

Expand All @@ -19,6 +19,7 @@ Pages = [
"shortestpaths/dijkstra.jl",
"shortestpaths/floyd-warshall.jl",
"shortestpaths/johnson.jl",
"shortestpaths/longest_path.jl",
"shortestpaths/spfa.jl",
"shortestpaths/yen.jl",
]
Expand Down
3 changes: 2 additions & 1 deletion src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ include("traversals/eulerian.jl")
include("connectivity.jl")
include("distance.jl")
include("editdist.jl")
include("shortestpaths/utils.jl")
include("shortestpaths/astar.jl")
include("shortestpaths/bellman-ford.jl")
include("shortestpaths/dijkstra.jl")
Expand All @@ -507,6 +508,7 @@ include("shortestpaths/desopo-pape.jl")
include("shortestpaths/floyd-warshall.jl")
include("shortestpaths/yen.jl")
include("shortestpaths/spfa.jl")
include("shortestpaths/longest_path.jl")
include("linalg/LinAlg.jl")
include("operators.jl")
include("persistence/common.jl")
Expand Down Expand Up @@ -543,7 +545,6 @@ include("independentset/degree_ind_set.jl")
include("independentset/maximal_ind_set.jl")
include("vertexcover/degree_vertex_cover.jl")
include("vertexcover/random_vertex_cover.jl")
include("longestpaths/longest_path.jl")
include("Experimental/Experimental.jl")
include("Parallel/Parallel.jl")
include("Test/Test.jl")
Expand Down
99 changes: 0 additions & 99 deletions src/longestpaths/longest_path.jl

This file was deleted.

35 changes: 35 additions & 0 deletions src/shortestpaths/longest_path.jl
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
9 changes: 9 additions & 0 deletions src/shortestpaths/utils.jl
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
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ tests = [
"edit_distance",
"connectivity",
"persistence/persistence",
"shortestpaths/utils",
"shortestpaths/astar",
"shortestpaths/bellman-ford",
"shortestpaths/desopo-pape",
Expand All @@ -111,6 +112,7 @@ tests = [
"shortestpaths/floyd-warshall",
"shortestpaths/yen",
"shortestpaths/spfa",
"shortestpaths/longest_path",
"traversals/bfs",
"traversals/bipartition",
"traversals/greedy_color",
Expand Down Expand Up @@ -152,7 +154,6 @@ tests = [
"independentset/maximal_ind_set",
"vertexcover/degree_vertex_cover",
"vertexcover/random_vertex_cover",
"longestpaths/longest_path",
"trees/prufer",
"experimental/experimental",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
@test dag_longest_path(g) == Int[]

# unweighted DAG
g = DiGraph(7)
A = [(1, 2), (2, 3), (2, 4), (3, 5), (5, 6), (3, 7)]
for (i, j) in A
add_edge!(g, i, j)
end
g = SimpleDiGraphFromIterator(Edge.([(1, 2), (2, 3), (2, 4), (3, 5), (5, 6), (3, 7)]))
@test dag_longest_path(g) == [1, 2, 3, 5, 6]

@test dag_longest_path(g; topological_order = topological_sort_by_dfs(g)) == [1, 2, 3, 5, 6]

# weighted DAG
n = 6
g = DiGraph(n)
Expand All @@ -23,7 +17,4 @@
distmx[i, j] = dist
end
@test dag_longest_path(g, distmx) == [2, 3, 5]

@test dag_longest_path(g, distmx; topological_order = topological_sort_by_dfs(g)) == [2, 3, 5]

end
9 changes: 9 additions & 0 deletions test/shortestpaths/utils.jl
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

0 comments on commit 989b819

Please sign in to comment.