Paths API Reference
This page documents the temporal path and reachability functions in TSNA.jl.
Path Types
TSNA.TemporalPath — Type
TemporalPath{T, Time}A time-respecting path through a dynamic network: times[k] is the instant edge edges[k] is traversed, and times are non-decreasing. Returned by temporal_path.
Example
using TSNA
p = TemporalPath([1, 2, 3], [0.0, 4.0], [(1, 2), (2, 3)])
length(p), path_duration(p) # (2, 4.0)TSNA.path_duration — Function
path_duration(p::TemporalPath) -> Time differenceElapsed time between the first and last traversal of the path (zero for a path with no edges).
Example
using TSNA
path_duration(TemporalPath([1, 2, 3], [1.0, 6.5], [(1, 2), (2, 3)])) # 5.5TSNA.Contact — Type
Contact{T, Time}A single contact (one contiguous edge activity) in a temporal network: source, target, onset time and duration.
Example
using TSNA
c = Contact{Int, Float64}(1, 2, 3.0, 2.5)
c.time, c.duration # (3.0, 2.5)TSNA.ContactSequence — Type
ContactSequence{T, Time}A sequence of contacts ordered by onset time, with the vertex count and directedness of the network they came from. Built by as_contact_sequence; iterable.
Example
using TSNA
cs = ContactSequence([Contact{Int, Float64}(2, 3, 5.0, 1.0),
Contact{Int, Float64}(1, 2, 1.0, 2.0)], 3)
[c.time for c in cs] # [1.0, 5.0]Earliest Arrival and Path Finding
TSNA.earliest_arrival — Function
earliest_arrival(dnet, source, start_time; end_time=window end or Inf,
target=nothing, missing=:error) -> (arrival::Dict, parent::Dict)Earliest-arrival times from source to every vertex, starting at start_time, under interval semantics: an edge spell [onset, terminus) is traversable at any instant in it (boarding mid-spell is allowed; spells that began before start_time but are still active count). Edges with no spell record are traversable at any time (R's active.default); vertex activity does not restrict paths, as in tsna::tPath. Implemented as a heap-based Dijkstra label-setting search over a memoized per-network contact index, so chains of simultaneous (equal-onset) spells are handled correctly and repeated queries do not rebuild the index. end_time (exclusive) defaults to the end of the observation window (get_observation_period) and, when no window is set, to Inf (no horizon), as tsna::tPath's end does. (tsna's tPath ignores a window; here a window bounds the observation, so paths stop at its end.)
With target set, the search stops as soon as that vertex is settled (its arrival time is already final); the returned dictionaries then only cover the explored part of the network.
Returns the arrival-time dictionary (vertices absent = unreachable) and the parent map (vertex => (predecessor, boarding time)) for path reconstruction. Agrees with tsna::tPath(direction = "fwd") on randomised networks with and without a window (the test suite's golden fixture).
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
arrival, parent = earliest_arrival(dnet, 1, 0.0)
arrival[3] # 5.0TSNA.temporal_distance — Function
temporal_distance(dnet, source, target, start_time; end_time=window end or Inf, missing=:error)
-> elapsed time or nothingElapsed time of the earliest time-respecting path from source to target departing at start_time (arrival − start). Returns nothing when no path exists before end_time.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
temporal_distance(dnet, 1, 3, 0.0) # 5.0
temporal_distance(dnet, 3, 1, 0.0) # nothingTSNA.temporal_path — Function
temporal_path(dnet, source, target, start_time; end_time=window end or Inf, missing=:error)
-> Union{TemporalPath, Nothing}The earliest-arrival (foremost) time-respecting path from source to target departing at start_time — not necessarily the path with the fewest hops or the shortest elapsed time. Returns nothing when no path exists.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
p = temporal_path(dnet, 1, 3, 0.0)
p.vertices, p.times # ([1, 2, 3], [1.0, 5.0])Reachability
TSNA.forward_reachable_set — Function
forward_reachable_set(dnet, source, start_time; end_time=window end or Inf, missing=:error) -> VectorVertices reachable from source by a time-respecting path departing at or after start_time and arriving before end_time (source included). The sorted vertex set of tsna::tPath(nd, v, direction = "fwd").
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(4; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
forward_reachable_set(dnet, 1, 0.0) # [1, 2, 3]TSNA.backward_reachable_set — Function
backward_reachable_set(dnet, target, end_time; start_time=window start or -Inf, missing=:error) -> VectorVertices from which target can be reached by a time-respecting path in [start_time, end_time) — the exact dual of forward_reachable_set (computed by forward searches that stop as soon as target is settled, so both use identical traversal semantics). tsna::tPath(direction = "bkwd") does not chain same-instant point spells although its forward search does; this function stays the exact dual of the forward search.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(4; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
backward_reachable_set(dnet, 3, 10.0) # [1, 2, 3]Batch and In-Place Computation
All-pairs routines and the reusable workspace behind them. earliest_arrival! writes into a caller-supplied TemporalPathWorkspace so that a sweep over many sources allocates once rather than once per source.
TSNA.TemporalPathWorkspace — Type
TemporalPathWorkspace{T, Time}()Reusable scratch space for the earliest-arrival search: the arrival and parent maps, the settled set, and the heap.
A single-source search allocates all four containers. An all-source analysis (temporal closeness, betweenness, backward_reachable_set, a reachability matrix) runs one search per vertex, so it pays that allocation nv(dnet) times over — even though the searches are independent and none of the scratch outlives its own search. A workspace is filled, read, and emptied once per source instead.
Pass one to earliest_arrival!, or use the batch entry points (earliest_arrival_all, temporal_distance_matrix, reachability_matrix), which manage it for you.
The memoized contact index is shared across searches regardless (it is cached on the network); the workspace is about the per-search containers.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
ws = TemporalPathWorkspace{Int, Float64}()
arrival, _ = earliest_arrival!(ws, dnet, 1, 0.0)
arrival[2] # 1.0TSNA.earliest_arrival! — Function
earliest_arrival!(ws::TemporalPathWorkspace, dnet, source, start_time;
end_time=window end or Inf, target=nothing, missing=:error)
-> (arrival::Dict, parent::Dict)In-place earliest_arrival: runs the same search, reusing ws's containers instead of allocating fresh ones.
The returned dictionaries alias ws and are overwritten by the next search on the same workspace. Copy what you need to keep, or use earliest_arrival_all, which does that for you.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 2.0, 4.0; edge=(1, 2))
ws = TemporalPathWorkspace{Int, Float64}()
first_run = copy(earliest_arrival!(ws, dnet, 1, 0.0)[1]) # copy: ws is reused
first_run[2] # 2.0TSNA.earliest_arrival_all — Function
earliest_arrival_all(dnet, start_time; sources=all vertices,
end_time=window end or Inf, missing=:error) -> Dict{T, Dict{T, Time}}Earliest-arrival times from every source in one pass, reusing a single TemporalPathWorkspace across the searches.
The searches are independent, so the result is identical to calling earliest_arrival per source — but the scratch containers are allocated once rather than once per source, and the memoized contact index is built at most once. This is the entry point for any all-source analysis (temporal closeness, betweenness, reachability); running the single-source function in a loop is the pattern this exists to replace.
Each source's arrival map is copied out of the workspace before the next search overwrites it, so the returned dictionaries are independent and safe to keep.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
all_arr = earliest_arrival_all(dnet, 0.0)
all_arr[1][3], haskey(all_arr[3], 1) # (5.0, false)TSNA.temporal_distance_matrix — Function
temporal_distance_matrix(dnet, start_time; end_time=window end or Inf, missing=:error)
-> Matrix{Union{Duration, Nothing}}All-pairs temporal distances (elapsed time of the earliest time-respecting path, arrival − start_time), in one batched pass. nothing where no path exists before end_time; zero on the diagonal.
Computed with a single reused workspace via earliest_arrival_all rather than nv(dnet)² independent temporal_distance calls.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 1.0, 3.0; edge=(1, 2))
activate!(dnet, 5.0, 6.0; edge=(2, 3))
D = temporal_distance_matrix(dnet, 0.0)
D[1, 3], D[3, 1] # (5.0, nothing)TSNA.reachability_matrix — Function
reachability_matrix(dnet, start_time; end_time=window end or Inf, missing=:error) -> BitMatrixR[i, j] is true when a time-respecting path runs from i to j within the window (the diagonal is true). One batched pass; see earliest_arrival_all.
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 5.0, 6.0; edge=(1, 2))
activate!(dnet, 1.0, 3.0; edge=(2, 3)) # closes before 1 reaches 2
R = reachability_matrix(dnet, 0.0)
R[1, 2], R[1, 3] # (true, false)Contact Sequence Conversion
TSNA.as_contact_sequence — Function
as_contact_sequence(dnet::DynamicNetwork; missing=:error, report=false) -> ContactSequenceConvert a dynamic network's edge activity to a contact sequence: one Contact per contiguous activity of an edge (its spells merged, so adjacent or overlapping spells are one contact, as R's activate.edges stores them), carrying its onset and duration (DynamicNetworks' spell_duration). A point spell [t,t) becomes a contact of zero duration.
The contacts are the stored activity over the whole time axis; they are not clipped to an observation window. A spell with no finite start (onset -Inf, or the axis minimum on integer and calendar axes; see DynamicNetworks.unbounded_spell) has no instant at which to place a contact, so it is dropped and reported, like an edge with no spell record. A spell with a finite start and no finite end becomes a contact whose duration is the axis's unbounded duration (Inf on a floating-point axis, typemax on an integer axis, Millisecond(typemax(Int64)) on a DateTime axis), as spell_duration gives it; the report counts these contacts too.
Conversion invariants
Preserved: the vertex count, directedness, and the onset and duration of every (merged) edge spell, so the activity is reconstructable.
A ContactSequence has no slot for the rest of the dynamic network, so the conversion is lossy by nature: spell censoring flags, vertex activity spells (actor presence/composition), static and time-varying attributes, and the observation window are dropped. Base-network edges with no spell record (active throughout, by default) and spells with no finite onset are dropped too, and contacts with no finite end are counted. Pass report=true for (cs, ::NetworkCore.ConversionReport) naming each.
A Contact cannot record that a dyad is unobserved, so a network with a missing-dyad mask is rejected by default (missing=:error) rather than being flattened into contacts that read as observed. Pass missing=:face to convert the recorded face values anyway (the mask is then dropped, and the report says so).
Example
using TSNA, DynamicNetworks
dnet = DynamicNetwork(3; observation_start=0.0, observation_end=10.0)
activate!(dnet, 0.0, 4.0; edge=(1, 2))
activate!(dnet, 7.0, 7.0; edge=(2, 3)) # instantaneous contact
cs = as_contact_sequence(dnet)
[(c.source, c.target, c.time, c.duration) for c in cs]
# [(1, 2, 0.0, 4.0), (2, 3, 7.0, 0.0)]