Skip to content

Commit cd93be7

Browse files
committed
drop Graphs.jl dependency
1 parent 7883dc5 commit cd93be7

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

Project.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
99
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1010
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
1111
GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb"
12-
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1312
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"
1413
JuliaVariables = "b14d175d-62b4-44ba-8fb7-3064adc8c3ec"
1514
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -43,9 +42,8 @@ TupleVectors = "615932cf-77b6-4358-adcd-5b7eba981d7e"
4342
ArrayInterface = "3"
4443
DiffResults = "1"
4544
Distributions = "0.23, 0.24, 0.25"
46-
FillArrays = "0.9, 0.10, 0.11, 0.12"
45+
FillArrays = "0.10, 0.11, 0.12"
4746
GeneralizedGenerated = "0.3"
48-
Graphs = "0.10"
4947
IRTools = "0.4"
5048
JuliaVariables = "0.2"
5149
MLStyle = "0.3,0.4"
@@ -67,7 +65,7 @@ SpecialFunctions = "0.9, 0.10, 1"
6765
StatsBase = "0.33"
6866
StatsFuns = "0.9"
6967
SymbolicCodegen = "0.2"
70-
SymbolicUtils = "0.11, 0.12, 0.13"
68+
SymbolicUtils = "0.14, 0.15, 0.16"
7169
TransformVariables = "0.4"
7270
TupleVectors = "0.1"
7371
julia = "1.5"

src/core/toposort.jl

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
import SimplePosets
22
using SimpleGraphs: SimpleGraph, AbstractSimpleGraph, SimpleDigraph, eltype, NV, elist, in_neighbors, add_edges!, vertex2idx
33

4-
import Graphs
5-
import Graphs.simple_graph, Graphs.add_edge!, Graphs.topological_sort_by_dfs
6-
export convert_simple
4+
export toposort
5+
function toposort(m::Model)
6+
names = toposort(poset(m).D)
7+
setdiff(names, arguments(m))
8+
end
79

8-
# From https://github.com/scheinerman/SimpleGraphs.jl/blob/1396758729f95912d7f245f9c70957f4993be417/src/simple_converters.jl
9-
function convert_simple(G::AbstractSimpleGraph)
10-
T = eltype(G)
11-
n = NV(G)
12-
has_dir = isa(G,SimpleDigraph)
10+
# Thanks to @CameronBieganek for this implementation:
11+
# https://discourse.julialang.org/t/lightgraphs-jl-transition/69526/52?u=cscherrer
12+
function toposort(g::SimpleDigraph{T}) where {T}
13+
g = deepcopy(g)
14+
order = T[]
15+
s = collect(filter(v -> SimpleGraphs.in_deg(g, v) == 0, vlist(g)))
1316

17+
while !isempty(s)
18+
u = pop!(s)
19+
push!(order, u)
1420

15-
d = vertex2idx(G)
16-
dinv = Dict{Int,T}()
17-
for k in keys(d)
18-
v = d[k]
19-
dinv[v] = k
21+
for v in out_neighbors(g, u)
22+
delete!(g, u, v)
23+
if SimpleGraphs.in_deg(g, v) == 0
24+
push!(s, v)
25+
end
26+
end
2027
end
2128

22-
H = simple_graph(n,is_directed=has_dir)
23-
24-
EE = elist(G)
25-
for e in EE
26-
u = d[e[1]]
27-
v = d[e[2]]
28-
add_edge!(H,u,v)
29+
if SimpleGraphs.NE(g) > 0
30+
error("Graph contains cycles.")
2931
end
30-
return (H,d,dinv)
31-
end
3232

33-
export toposort
34-
function toposort(m::Model)
35-
(g, _, names) = poset(m).D |> convert_simple
36-
setdiff(map(v -> names[v], Graphs.topological_sort_by_dfs(g)), arguments(m))
33+
order
3734
end

0 commit comments

Comments
 (0)