|
| 1 | +using UnPack |
| 2 | +using SparseArrays |
| 3 | +using LightGraphs |
| 4 | +using Setfield |
| 5 | + |
| 6 | +### |
| 7 | +### Edges & Vertex |
| 8 | +### |
| 9 | +@enum VertType SRC DST ALL |
| 10 | + |
| 11 | +struct BipartiteEdge{I<:Integer} <: LightGraphs.AbstractEdge{I} |
| 12 | + src::I |
| 13 | + dst::I |
| 14 | + function BipartiteEdge(src::I, dst::V) where {I,V} |
| 15 | + T = promote_type(I, V) |
| 16 | + new{T}(T(src), T(dst)) |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +LightGraphs.src(edge::BipartiteEdge) = edge.src |
| 21 | +LightGraphs.dst(edge::BipartiteEdge) = edge.dst |
| 22 | + |
| 23 | +function Base.show(io::IO, edge::BipartiteEdge) |
| 24 | + @unpack src, dst = edge |
| 25 | + print(io, "[src: ", src, "] => [dst: ", dst, "]") |
| 26 | +end |
| 27 | + |
| 28 | +Base.:(==)(a::BipartiteEdge, b::BipartiteEdge) = src(a) == src(b) && dst(a) == dst(b) |
| 29 | + |
| 30 | +### |
| 31 | +### Graph |
| 32 | +### |
| 33 | +""" |
| 34 | +$(TYPEDEF) |
| 35 | +
|
| 36 | +A bipartite graph representation between two, possibly distinct, sets of vertices |
| 37 | +(source and dependencies). Maps source vertices, labelled `1:N₁`, to vertices |
| 38 | +on which they depend (labelled `1:N₂`). |
| 39 | +
|
| 40 | +# Fields |
| 41 | +$(FIELDS) |
| 42 | +
|
| 43 | +# Example |
| 44 | +```julia |
| 45 | +using ModelingToolkit |
| 46 | +
|
| 47 | +ne = 4 |
| 48 | +srcverts = 1:4 |
| 49 | +depverts = 1:2 |
| 50 | +
|
| 51 | +# six source vertices |
| 52 | +fadjlist = [[1],[1],[2],[2],[1],[1,2]] |
| 53 | +
|
| 54 | +# two vertices they depend on |
| 55 | +badjlist = [[1,2,5,6],[3,4,6]] |
| 56 | +
|
| 57 | +bg = BipartiteGraph(7, fadjlist, badjlist) |
| 58 | +``` |
| 59 | +""" |
| 60 | +mutable struct BipartiteGraph{I<:Integer} <: LightGraphs.AbstractGraph{I} |
| 61 | + ne::Int |
| 62 | + fadjlist::Vector{Vector{I}} # `fadjlist[src] => dsts` |
| 63 | + badjlist::Vector{Vector{I}} # `badjlist[dst] => srcs` |
| 64 | +end |
| 65 | + |
| 66 | +""" |
| 67 | +```julia |
| 68 | +Base.isequal(bg1::BipartiteGraph{T}, bg2::BipartiteGraph{T}) where {T<:Integer} |
| 69 | +``` |
| 70 | +
|
| 71 | +Test whether two [`BipartiteGraph`](@ref)s are equal. |
| 72 | +""" |
| 73 | +function Base.isequal(bg1::BipartiteGraph{T}, bg2::BipartiteGraph{T}) where {T<:Integer} |
| 74 | + iseq = (bg1.ne == bg2.ne) |
| 75 | + iseq &= (bg1.fadjlist == bg2.fadjlist) |
| 76 | + iseq &= (bg1.badjlist == bg2.badjlist) |
| 77 | + iseq |
| 78 | +end |
| 79 | + |
| 80 | +""" |
| 81 | +$(SIGNATURES) |
| 82 | +
|
| 83 | +Build an empty `BipartiteGraph` with `nsrcs` sources and `ndsts` destinations. |
| 84 | +""" |
| 85 | +BipartiteGraph(nsrcs::T, ndsts::T) where T = BipartiteGraph(0, map(_->T[], 1:nsrcs), map(_->T[], 1:ndsts)) |
| 86 | + |
| 87 | +Base.eltype(::Type{BipartiteGraph{I}}) where I = I |
| 88 | +Base.empty!(g::BipartiteGraph) = (foreach(empty!, g.fadjlist); foreach(empty!, g.badjlist); g.ne = 0; g) |
| 89 | +Base.length(::BipartiteGraph) = error("length is not well defined! Use `ne` or `nv`.") |
| 90 | + |
| 91 | +if isdefined(LightGraphs, :has_contiguous_vertices) |
| 92 | + LightGraphs.has_contiguous_vertices(::Type{<:BipartiteGraph}) = false |
| 93 | +end |
| 94 | +LightGraphs.is_directed(::Type{<:BipartiteGraph}) = false |
| 95 | +LightGraphs.vertices(g::BipartiteGraph) = (𝑠vertices(g), 𝑑vertices(g)) |
| 96 | +𝑠vertices(g::BipartiteGraph) = axes(g.fadjlist, 1) |
| 97 | +𝑑vertices(g::BipartiteGraph) = axes(g.badjlist, 1) |
| 98 | +has_𝑠vertex(g::BipartiteGraph, v::Integer) = v in 𝑠vertices(g) |
| 99 | +has_𝑑vertex(g::BipartiteGraph, v::Integer) = v in 𝑑vertices(g) |
| 100 | +𝑠neighbors(g::BipartiteGraph, i::Integer) = g.fadjlist[i] |
| 101 | +𝑑neighbors(g::BipartiteGraph, i::Integer) = g.badjlist[i] |
| 102 | +LightGraphs.ne(g::BipartiteGraph) = g.ne |
| 103 | +LightGraphs.nv(g::BipartiteGraph) = sum(length, vertices(g)) |
| 104 | +LightGraphs.edgetype(g::BipartiteGraph{I}) where I = BipartiteEdge{I} |
| 105 | + |
| 106 | +nsrcs(g::BipartiteGraph) = length(𝑠vertices(g)) |
| 107 | +ndsts(g::BipartiteGraph) = length(𝑑vertices(g)) |
| 108 | + |
| 109 | +function LightGraphs.has_edge(g::BipartiteGraph, edge::BipartiteEdge) |
| 110 | + @unpack src, dst = edge |
| 111 | + (src in 𝑠vertices(g) && dst in 𝑑vertices(g)) || return false # edge out of bounds |
| 112 | + insorted(𝑠neighbors(src), dst) |
| 113 | +end |
| 114 | + |
| 115 | +### |
| 116 | +### Populate |
| 117 | +### |
| 118 | +LightGraphs.add_edge!(g::BipartiteGraph, i::Integer, j::Integer) = add_edge!(g, BipartiteEdge(i, j)) |
| 119 | +function LightGraphs.add_edge!(g::BipartiteGraph, edge::BipartiteEdge) |
| 120 | + @unpack fadjlist, badjlist = g |
| 121 | + verts = vertices(g) |
| 122 | + s, d = src(edge), dst(edge) |
| 123 | + (has_𝑠vertex(g, s) && has_𝑑vertex(g, d)) || error("edge ($edge) out of range.") |
| 124 | + @inbounds list = fadjlist[s] |
| 125 | + index = searchsortedfirst(list, d) |
| 126 | + @inbounds (index <= length(list) && list[index] == d) && return false # edge already in graph |
| 127 | + insert!(list, index, d) |
| 128 | + |
| 129 | + g.ne += 1 |
| 130 | + @inbounds list = badjlist[d] |
| 131 | + index = searchsortedfirst(list, s) |
| 132 | + insert!(list, index, s) |
| 133 | + return true # edge successfully added |
| 134 | +end |
| 135 | + |
| 136 | +function LightGraphs.add_vertex!(g::BipartiteGraph{T}, type::VertType) where T |
| 137 | + if type === DST |
| 138 | + push!(g.badjlist, T[]) |
| 139 | + elseif type === SRC |
| 140 | + push!(g.fadjlist, T[]) |
| 141 | + else |
| 142 | + error("type ($type) must be either `DST` or `SRC`") |
| 143 | + end |
| 144 | + return true # vertex successfully added |
| 145 | +end |
| 146 | + |
| 147 | +### |
| 148 | +### Edges iteration |
| 149 | +### |
| 150 | +LightGraphs.edges(g::BipartiteGraph) = BipartiteEdgeIter(g, Val(ALL)) |
| 151 | +𝑠edges(g::BipartiteGraph) = BipartiteEdgeIter(g, Val(SRC)) |
| 152 | +𝑑edges(g::BipartiteGraph) = BipartiteEdgeIter(g, Val(DST)) |
| 153 | + |
| 154 | +struct BipartiteEdgeIter{T,G} <: LightGraphs.AbstractEdgeIter |
| 155 | + g::G |
| 156 | + type::Val{T} |
| 157 | +end |
| 158 | + |
| 159 | +Base.length(it::BipartiteEdgeIter) = ne(it.g) |
| 160 | +Base.length(it::BipartiteEdgeIter{ALL}) = 2ne(it.g) |
| 161 | + |
| 162 | +Base.eltype(it::BipartiteEdgeIter) = edgetype(it.g) |
| 163 | + |
| 164 | +function Base.iterate(it::BipartiteEdgeIter{SRC,BipartiteGraph{T}}, state=(1, 1, SRC)) where T |
| 165 | + @unpack g = it |
| 166 | + neqs = nsrcs(g) |
| 167 | + neqs == 0 && return nothing |
| 168 | + eq, jvar = state |
| 169 | + |
| 170 | + while eq <= neqs |
| 171 | + eq′ = eq |
| 172 | + vars = 𝑠neighbors(g, eq′) |
| 173 | + if jvar > length(vars) |
| 174 | + eq += 1 |
| 175 | + jvar = 1 |
| 176 | + continue |
| 177 | + end |
| 178 | + edge = BipartiteEdge(eq′, vars[jvar]) |
| 179 | + state = (eq, jvar + 1, SRC) |
| 180 | + return edge, state |
| 181 | + end |
| 182 | + return nothing |
| 183 | +end |
| 184 | + |
| 185 | +function Base.iterate(it::BipartiteEdgeIter{DST,BipartiteGraph{T}}, state=(1, 1, DST)) where T |
| 186 | + @unpack g = it |
| 187 | + nvars = ndsts(g) |
| 188 | + nvars == 0 && return nothing |
| 189 | + ieq, jvar = state |
| 190 | + |
| 191 | + while jvar <= nvars |
| 192 | + eqs = 𝑑neighbors(g, jvar) |
| 193 | + if ieq > length(eqs) |
| 194 | + ieq = 1 |
| 195 | + jvar += 1 |
| 196 | + continue |
| 197 | + end |
| 198 | + edge = BipartiteEdge(eqs[ieq], jvar) |
| 199 | + state = (ieq + 1, jvar, DST) |
| 200 | + return edge, state |
| 201 | + end |
| 202 | + return nothing |
| 203 | +end |
| 204 | + |
| 205 | +function Base.iterate(it::BipartiteEdgeIter{ALL,<:BipartiteGraph}, state=nothing) |
| 206 | + if state === nothing |
| 207 | + ss = iterate((@set it.type = Val(SRC))) |
| 208 | + elseif state[3] === SRC |
| 209 | + ss = iterate((@set it.type = Val(SRC)), state) |
| 210 | + elseif state[3] == DST |
| 211 | + ss = iterate((@set it.type = Val(DST)), state) |
| 212 | + end |
| 213 | + if ss === nothing && state[3] == SRC |
| 214 | + return iterate((@set it.type = Val(DST))) |
| 215 | + else |
| 216 | + return ss |
| 217 | + end |
| 218 | +end |
| 219 | + |
| 220 | +### |
| 221 | +### Utils |
| 222 | +### |
| 223 | +function LightGraphs.incidence_matrix(g::BipartiteGraph, val=true) |
| 224 | + I = Int[] |
| 225 | + J = Int[] |
| 226 | + for i in 𝑠vertices(g), n in 𝑠neighbors(g, i) |
| 227 | + push!(I, i) |
| 228 | + push!(J, n) |
| 229 | + end |
| 230 | + S = sparse(I, J, val, nsrcs(g), ndsts(g)) |
| 231 | +end |
0 commit comments