@@ -141,21 +141,54 @@ function ParametricTree(nv::Int, src::Vector{Int}, dst::Vector{Int}, w::Vector{T
141141 zeros (Int, 2 ne), zeros (Int, 2 ne), IndexedHeap {T} (ne))
142142end
143143
144- function _child_append! (S:: ParametricTree , p:: Int , c:: Int )
144+ # The parametric shortest-path tree of a dense digraph, with its arcs held in
145+ # matrices. Parallel arcs are merged into one of largest weight: they share the
146+ # coefficient of `λ` in their reduced lengths, so that one becomes tight first.
147+ # Between live nodes `p ≠ q` there is then at most one arc `p → q`, of weight
148+ # `win[p, q] == wout[q, p]`, or `-Inf` if there is none; column `q` of `win`
149+ # holds the arcs entering `q`, and column `p` of `wout` the arcs leaving `p`.
150+ #
151+ # - `live` lists the live nodes, and `islive` marks them.
152+ # - `a`, `b`, and the lists of children are as for `ParametricTree`;
153+ # `parent[v]` is the tail of the tree arc entering `v`, or 0 at the root.
154+ # - The candidate arcs are the non-tree arcs `p → q` with `a[p] + 1 > a[q]`,
155+ # keyed by the `λ` at which they become tight. `heap` holds each live node `q`
156+ # that a candidate arc enters, keyed by an upper bound on the keys of those
157+ # arcs; unless `dirty[q]`, the bound is the key of the arc from `best[q]`.
158+ struct DenseParametricTree{T}
159+ win:: Matrix{T}
160+ wout:: Matrix{T}
161+ live:: Vector{Int}
162+ islive:: BitVector
163+ a:: Vector{Int}
164+ b:: Vector{T}
165+ parent:: Vector{Int}
166+ fchild:: Vector{Int}
167+ lchild:: Vector{Int}
168+ nsib:: Vector{Int}
169+ psib:: Vector{Int}
170+ heap:: IndexedHeap{T}
171+ best:: Vector{Int}
172+ dirty:: BitVector
173+ end
174+
175+ const ParametricTrees = Union{ParametricTree,DenseParametricTree}
176+
177+ function _child_append! (S:: ParametricTrees , p:: Int , c:: Int )
145178 S. psib[c] = S. lchild[p]
146179 S. nsib[c] = 0
147180 S. lchild[p] == 0 ? (S. fchild[p] = c) : (S. nsib[S. lchild[p]] = c)
148181 S. lchild[p] = c
149182 return nothing
150183end
151- function _child_remove! (S:: ParametricTree , p:: Int , c:: Int )
184+ function _child_remove! (S:: ParametricTrees , p:: Int , c:: Int )
152185 pr, nx = S. psib[c], S. nsib[c]
153186 pr == 0 ? (S. fchild[p] = nx) : (S. nsib[pr] = nx)
154187 nx == 0 ? (S. lchild[p] = pr) : (S. psib[nx] = pr)
155188 return nothing
156189end
157190# Moves the children of `z` to the end of the children of `p`.
158- function _child_concat! (S:: ParametricTree , p:: Int , z:: Int )
191+ function _child_concat! (S:: ParametricTrees , p:: Int , z:: Int )
159192 S. fchild[z] == 0 && return nothing
160193 if S. lchild[p] == 0
161194 S. fchild[p] = S. fchild[z]
@@ -235,7 +268,7 @@ function _shift_subtree!(S::ParametricTree, sub::Vector{Int}, Δa::Int, Δb, sta
235268end
236269
237270# Appends the descendants of `v` to `sub`, with `stack` as workspace.
238- function _subtree! (sub:: Vector{Int} , S:: ParametricTree , v:: Int , stack:: Vector{Int} )
271+ function _subtree! (sub:: Vector{Int} , S:: ParametricTrees , v:: Int , stack:: Vector{Int} )
239272 empty! (stack)
240273 c = S. fchild[v]
241274 while c != 0
@@ -277,35 +310,20 @@ end
277310# largest weight. Each time an arc becomes tight, the work is proportional to
278311# the size of the subtrees whose depths change plus the number of arcs incident
279312# to them: `O(nm + n² log n)` in all, and far less when the tree stays shallow.
280- function _maxbalance (nv:: Int , src:: Vector{Int} , dst:: Vector{Int} , w:: Vector{T} ) where {T}
313+ #
314+ # On a dense digraph, with `dense`, the tree is held by `DenseParametricTree`:
315+ # each node whose depth changes then costs a pass over the live nodes rather
316+ # than a heap update for each of its arcs.
317+ function _maxbalance (nv:: Int , src:: Vector{Int} , dst:: Vector{Int} , w:: Vector{T} ;
318+ dense:: Bool = 8 * length (w) > nv^ 2 ) where {T}
281319 x = zeros (T, nv)
282320 nv == 1 && return x
321+ dense && return _maxbalance_dense (nv, src, dst, w)
283322 ne = length (w)
284323 S = ParametricTree (nv, src, dst, w)
285324 a, b, tarc, heap = S. a, S. b, S. tarc, S. heap
286325 csrc, cdst, cw = S. src, S. dst, S. w
287- fill! (a, - 1 )
288- ptr, adj = _out_arcs (nv, src, e -> S. alive[e])
289- queue = Int[1 ]
290- a[1 ] = 0
291- i = 1
292- while i <= length (queue)
293- v = queue[i]
294- i += 1
295- for s in ptr[v]: ptr[v+ 1 ]- 1
296- e = adj[s]
297- q = dst[e]
298- if a[q] == - 1
299- a[q] = a[v] + 1
300- push! (queue, q)
301- end
302- if a[q] == a[v] + 1 && (tarc[q] == 0 || b[v] + w[e] > b[q])
303- b[q] = b[v] + w[e]
304- tarc[q] = e
305- end
306- end
307- end
308- length (queue) == nv || error (" internal error: the digraph is not strongly connected" )
326+ _initial_tree! (a, b, tarc, src, dst, w)
309327 for q in 2 : nv
310328 _child_append! (S, src[tarc[q]], q)
311329 end
@@ -414,9 +432,47 @@ function _maxbalance(nv::Int, src::Vector{Int}, dst::Vector{Int}, w::Vector{T})
414432 nlive -= length (path)
415433 end
416434 end
417- # Sum `uΔ` along each vertex's chain of contractions, compressing the
418- # chains as they are summed.
419- for z in 1 : nv
435+ return _chain_sums! (x, uf, uΔ, stack)
436+ end
437+
438+ # The shortest-path tree for `λ → ∞` on the strongly connected digraph on
439+ # `1:length(a)` with arcs `src[e] → dst[e]` of weight `w[e]`, rooted at vertex
440+ # 1: fewest arcs, then largest weight. Sets the depths `a`, the path weights
441+ # `b`, and the tree arcs `tarc`, with `tarc[1] == 0`.
442+ function _initial_tree! (a:: Vector{Int} , b:: Vector , tarc:: Vector{Int} , src:: Vector{Int} , dst:: Vector{Int} , w:: Vector )
443+ nv = length (a)
444+ fill! (a, - 1 )
445+ fill! (b, zero (eltype (b)))
446+ fill! (tarc, 0 )
447+ ptr, adj = _out_arcs (nv, src, e -> src[e] != dst[e])
448+ queue = Int[1 ]
449+ a[1 ] = 0
450+ i = 1
451+ while i <= length (queue)
452+ v = queue[i]
453+ i += 1
454+ for s in ptr[v]: ptr[v+ 1 ]- 1
455+ e = adj[s]
456+ q = dst[e]
457+ if a[q] == - 1
458+ a[q] = a[v] + 1
459+ push! (queue, q)
460+ end
461+ if a[q] == a[v] + 1 && (tarc[q] == 0 || b[v] + w[e] > b[q])
462+ b[q] = b[v] + w[e]
463+ tarc[q] = e
464+ end
465+ end
466+ end
467+ length (queue) == nv || error (" internal error: the digraph is not strongly connected" )
468+ return nothing
469+ end
470+
471+ # The shift `x` of each vertex `z`, as the sum of `uΔ` along its chain of
472+ # contractions `z`, `uf[z]`, `uf[uf[z]]`, …, with `x[1] == 0`; the chains are
473+ # compressed as they are summed, with `stack` as workspace.
474+ function _chain_sums! (x:: Vector{T} , uf:: Vector{Int} , uΔ:: Vector{T} , stack:: Vector{Int} ) where {T}
475+ for z in eachindex (uf)
420476 empty! (stack)
421477 y = z
422478 while uf[y] != y
@@ -437,6 +493,210 @@ function _maxbalance(nv::Int, src::Vector{Int}, dst::Vector{Int}, w::Vector{T})
437493 return x
438494end
439495
496+ # The `λ` at which the arc `p → q` of weight `wpq` between live nodes becomes
497+ # tight, as for `_key!`, or `-Inf` if it is not a candidate arc.
498+ function _dense_key (S:: DenseParametricTree{T} , p:: Int , q:: Int , wpq:: T ) where {T}
499+ c = S. a[p] + 1 - S. a[q]
500+ (c <= 0 || S. parent[q] == p) && return - T (Inf )
501+ return (S. b[p] + wpq - S. b[q]) / c
502+ end
503+
504+ # Sets `best[q]` to `p` and the key of `q` to `k`, or removes `q` from the heap
505+ # if `p == 0`.
506+ function _dense_set_best! (S:: DenseParametricTree , q:: Int , p:: Int , k)
507+ S. best[q] = p
508+ S. dirty[q] = false
509+ p == 0 ? _heap_remove! (S. heap, q) : _heap_set! (S. heap, q, k)
510+ return nothing
511+ end
512+
513+ # Sets `best[q]` to the tail of the candidate arc entering `q` with the
514+ # largest key.
515+ function _dense_best! (S:: DenseParametricTree{T} , q:: Int ) where {T}
516+ f, kf = 0 , - T (Inf )
517+ for p in S. live
518+ k = _dense_key (S, p, q, S. win[p, q])
519+ if k > kf
520+ f, kf = p, k
521+ end
522+ end
523+ _dense_set_best! (S, q, f, kf)
524+ return nothing
525+ end
526+
527+ # Updates `q` after the key of the arc `p → q` of weight `wpq` has changed: the
528+ # arc from `p` becomes `best[q]` if its key exceeds the key of `q`, and `q`
529+ # becomes dirty if `best[q] == p` and the key fell.
530+ function _dense_offer! (S:: DenseParametricTree{T} , p:: Int , q:: Int , wpq:: T ) where {T}
531+ k = _dense_key (S, p, q, wpq)
532+ h = S. heap
533+ if k > - T (Inf ) && (h. pos[q] == 0 || k > h. key[q])
534+ S. best[q] = p
535+ S. dirty[q] = false
536+ _heap_set! (h, q, k)
537+ elseif S. best[q] == p && k != h. key[q]
538+ S. dirty[q] = true
539+ end
540+ return nothing
541+ end
542+
543+ # Adds `Δa` to the depth and `Δb` to the path weight of each node `y` of the
544+ # subtree listed in `sub`, marks it with `stamp[y] = st`, and updates `best` at
545+ # the subtree and at the heads of the arcs leaving it. Arcs within the subtree
546+ # keep their keys, since both ends shift alike.
547+ function _dense_shift! (S:: DenseParametricTree{T} , sub:: Vector{Int} , Δa:: Int , Δb, stamp:: Vector{Int} ,
548+ st:: Int ) where {T}
549+ for y in sub
550+ S. a[y] += Δa
551+ S. b[y] += Δb
552+ stamp[y] = st
553+ end
554+ for y in sub
555+ f, kf = 0 , - T (Inf )
556+ for p in S. live
557+ k = _dense_key (S, p, y, S. win[p, y])
558+ if k > kf
559+ f, kf = p, k
560+ end
561+ wyp = S. wout[p, y]
562+ stamp[p] == st || wyp == - T (Inf ) || _dense_offer! (S, y, p, wyp)
563+ end
564+ _dense_set_best! (S, y, f, kf)
565+ end
566+ return nothing
567+ end
568+
569+ # `_maxbalance` with the tree held by `DenseParametricTree`.
570+ function _maxbalance_dense (nv:: Int , src:: Vector{Int} , dst:: Vector{Int} , w:: Vector{T} ) where {T}
571+ all (isfinite, w) || error (" internal error: the weights must be finite" )
572+ tarc = zeros (Int, nv)
573+ S = DenseParametricTree {T} (fill (- T (Inf ), nv, nv), fill (- T (Inf ), nv, nv), collect (1 : nv), trues (nv),
574+ zeros (Int, nv), zeros (T, nv), zeros (Int, nv), zeros (Int, nv), zeros (Int, nv),
575+ zeros (Int, nv), zeros (Int, nv), IndexedHeap {T} (nv), zeros (Int, nv), falses (nv))
576+ a, b, parent, win, wout, heap = S. a, S. b, S. parent, S. win, S. wout, S. heap
577+ for e in eachindex (w)
578+ p, q = src[e], dst[e]
579+ if p != q && w[e] > win[p, q]
580+ win[p, q] = wout[q, p] = w[e]
581+ end
582+ end
583+ # The tree arc chosen among parallel arcs has the largest weight, so
584+ # `b[q] == b[parent[q]] + win[parent[q], q]`.
585+ _initial_tree! (a, b, tarc, src, dst, w)
586+ for q in 2 : nv
587+ parent[q] = src[tarc[q]]
588+ _child_append! (S, parent[q], q)
589+ end
590+ for q in 1 : nv
591+ _dense_best! (S, q)
592+ end
593+ uf = collect (1 : nv)
594+ uΔ = zeros (T, nv)
595+ stamp = zeros (Int, nv)
596+ st = 0
597+ sub = Int[]
598+ stack = Int[]
599+ path = Int[]
600+ while length (S. live) > 1
601+ isempty (heap. heap) && error (" internal error: no arc can become tight on a strongly connected digraph" )
602+ v = heap. heap[1 ]
603+ while S. dirty[v]
604+ _dense_best! (S, v)
605+ isempty (heap. heap) && error (" internal error: no arc can become tight on a strongly connected digraph" )
606+ v = heap. heap[1 ]
607+ end
608+ u = S. best[v]
609+ λ = heap. key[v]
610+ # Decide whether `u` is a descendant of `v` as in `_maxbalance`.
611+ cycle = false
612+ z = u
613+ empty! (sub)
614+ empty! (stack)
615+ push! (stack, v)
616+ while true
617+ if z != 0
618+ z == v && (cycle = true ; break )
619+ z = parent[z]
620+ end
621+ isempty (stack) && break
622+ y = pop! (stack)
623+ y == v || push! (sub, y)
624+ y == u && (cycle = true ; break )
625+ c = S. fchild[y]
626+ while c != 0
627+ push! (stack, c)
628+ c = S. nsib[c]
629+ end
630+ end
631+ if ! cycle
632+ # The arc `u → v` replaces the tree arc into `v`: the subtree of `v`
633+ # moves under `u` and becomes deeper.
634+ Δa = a[u] + 1 - a[v]
635+ Δb = b[u] + win[u, v] - b[v]
636+ _child_remove! (S, parent[v], v)
637+ _child_append! (S, u, v)
638+ parent[v] = u
639+ push! (sub, v)
640+ _dense_shift! (S, sub, Δa, Δb, stamp, st += 1 )
641+ else
642+ # Contract the cycle `v → ⋯ → u → v` into `v`.
643+ empty! (path)
644+ z = u
645+ while z != v
646+ push! (path, z)
647+ z = parent[z]
648+ end
649+ for z in path
650+ uf[z] = v
651+ uΔ[z] = (a[z] - a[v]) * λ - (b[z] - b[v])
652+ _child_remove! (S, parent[z], z)
653+ S. islive[z] = false
654+ end
655+ filter! (y -> S. islive[y], S. live)
656+ for z in path
657+ # The arcs between `z` and the live nodes join `v`, merged with
658+ # the parallel arcs there; a tree arc keeps its weight.
659+ δ = uΔ[z]
660+ for p in S. live
661+ p == v && continue
662+ wi = win[p, z]
663+ if wi != - T (Inf ) && parent[v] != p
664+ wv = wi + δ
665+ wv > win[p, v] && (win[p, v] = wout[v, p] = wv)
666+ end
667+ wo = wout[p, z]
668+ if wo != - T (Inf ) && parent[p] != v
669+ wv = wo - δ
670+ (parent[p] == z || wv > wout[p, v]) && (wout[p, v] = win[v, p] = wv)
671+ end
672+ end
673+ # The children of `z` move under `v`, which is shallower, and
674+ # their subtrees rise by `a[z] - a[v]`.
675+ c = S. fchild[z]
676+ while c != 0
677+ parent[c] = v
678+ c = S. nsib[c]
679+ end
680+ Δa = a[v] - a[z]
681+ _dense_shift! (S, _subtree! (empty! (sub), S, z, stack), Δa, Δa * λ, stamp, st += 1 )
682+ _child_concat! (S, v, z)
683+ end
684+ for z in path
685+ _dense_set_best! (S, z, 0 , λ)
686+ end
687+ for q in S. live
688+ q == v && continue
689+ f = S. best[q]
690+ f == 0 || S. islive[f] || (S. dirty[q] = true )
691+ wvq = wout[q, v]
692+ wvq == - T (Inf ) || _dense_offer! (S, v, q, wvq)
693+ end
694+ _dense_best! (S, v)
695+ end
696+ end
697+ return _chain_sums! (zeros (T, nv), uf, uΔ, stack)
698+ end
699+
440700# The scales `y` of the difference system `sys` shifted by the max-balancing
441701# shift of each strongly connected component, `scc` and `nscc` as returned by
442702# `_strong_components`. The shift of each component is fixed by making its
0 commit comments