/
evolving_graph.jl
394 lines (298 loc) · 10.8 KB
/
evolving_graph.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
EvolvingGraph{V,T}(;is_directed=true; is_weighted=true)
EvolvingGraph(;is_directed=true; is_weighted=true)
Construct an evolving graph with node type `V` and timestamp type `T`.
`EvolvingGraph()` constructs a simple evolving graph with integer nodes and timestamps.
# Example
```jldoctest
julia> using EvolvingGraphs
julia> g = EvolvingGraph{Node{String},Int}(is_weighted=false)
Directed EvolvingGraph 0 nodes, 0 static edges, 0 timestamps
julia> add_node!(g, "a")
Node(a)
julia> add_node!(g, "b")
Node(b)
julia> num_nodes(g)
2
julia> add_edge!(g, "a", "b", 2001)
Node(a)->Node(b) at time 2001
julia> add_edge!(g, "a", "c", 2002)
Node(a)->Node(c) at time 2002
julia> timestamps(g)
2-element Array{Int64,1}:
2001
2002
julia> active_nodes(g)
4-element Array{EvolvingGraphs.TimeNode{String,Int64},1}:
TimeNode(a, 2001)
TimeNode(b, 2001)
TimeNode(a, 2002)
TimeNode(c, 2002)
julia> g = EvolvingGraph()
Directed EvolvingGraph 0 nodes, 0 static edges, 0 timestamps
julia> add_edge!(g, 1, 2, 1)
Node(1)-1.0->Node(2) at time 1
julia> add_edge!(g, 2, 3, 2)
Node(2)-1.0->Node(3) at time 2
julia> add_edge!(g, 1, 3, 2)
Node(1)-1.0->Node(3) at time 2
julia> nodes(g)
3-element Array{EvolvingGraphs.Node{Int64},1}:
Node(1)
Node(2)
Node(3)
```
"""
mutable struct EvolvingGraph{V, E, T, KV} <: AbstractEvolvingGraph{V, E, T}
is_directed::Bool
nodes::Vector{V} # a vector of nodes
node_indexof::Dict{KV, Int} # map node keys to indices
edges::Vector{E} # a vector of edges
timestamps::Vector{T} # a vector of timestamps
active_nodes::Vector{TimeNode{KV,T}} # a vector of active nodes
active_node_indexof::Dict{Tuple{KV,T},Int} # mapping active node keys to indices
end
function EvolvingGraph{V,T}(;is_directed::Bool=true, is_weighted::Bool=true) where {V,T}
KV = eltype(V);
E = is_weighted ? WeightedTimeEdge{V,T,Float64} : TimeEdge{V,T};
return EvolvingGraph(is_directed, V[], Dict{KV, Int}(), E[], T[], TimeNode{KV, T}[], Dict{Tuple{KV,T},Int}())
end
EvolvingGraph(;is_directed::Bool=true, is_weighted::Bool=true) = EvolvingGraph{Node{Int},Int}(is_directed=is_directed, is_weighted=is_weighted)
"""
evolving_graph_from_arrays(ils, jls, wls, timestamps; is_directed=true)
evolving_graph_from_arrays(ils, jls, timestamps; is_directed=true)
Generate an EvolvingGraph type object from four input arrays: `ils`, `jls`, `wls` and `timestamps`, such that the ith entry `(ils[i], jls[i], wls[i], timestamps[i])` represents a WeightedTimeEdge from `ils[i]` to `jls[i]` with edge weight `wls[i]` at timestamp `timestamp[i]`. By default, `wls` is a vector of ones.
# Example
```jldoctest
julia> using EvolvingGraphs
julia> g = evolving_graph_from_arrays([1,2,3], [4,5,2], [1,1,2])
Directed EvolvingGraph 5 nodes, 3 static edges, 2 timestamps
julia> nodes(g)
5-element Array{EvolvingGraphs.Node{Int64},1}:
Node(1)
Node(4)
Node(2)
Node(5)
Node(3)
julia> edges(g)
3-element Array{EvolvingGraphs.WeightedTimeEdge{EvolvingGraphs.Node{Int64},Int64,Float64},1}:
Node(1)-1.0->Node(4) at time 1
Node(2)-1.0->Node(5) at time 1
Node(3)-1.0->Node(2) at time 2
julia> g = evolving_graph_from_arrays([1,2], [2, 3], [2.5, 3.8], [1998,2001])
Directed EvolvingGraph 3 nodes, 2 static edges, 2 timestamps
julia> edges(g)
2-element Array{EvolvingGraphs.WeightedTimeEdge{EvolvingGraphs.Node{Int64},Int64,Float64},1}:
Node(1)-2.5->Node(2) at time 1998
Node(2)-3.8->Node(3) at time 2001
```
"""
function evolving_graph_from_arrays{V,T}(ils::Vector{V},
jls::Vector{V}, wls::Vector{<:Real},
timestamps::Vector{T};
is_directed::Bool = true)
n = length(ils)
n == length(jls) == length(timestamps) == length(wls)||
error("4 input vectors must have the same length.")
g = EvolvingGraph{Node{eltype(ils)}, eltype(timestamps)}(is_directed = is_directed)
for i = 1:n
v1 = add_node!(g, ils[i])
v2 = add_node!(g, jls[i])
w = wls[i]
add_edge!(g, v1, v2, timestamps[i], weight = w)
end
g
end
evolving_graph_from_arrays{V,T}(ils::Vector{V}, jls::Vector{V}, timestamps::Vector{T}; is_directed::Bool = true) = evolving_graph_from_arrays(ils, jls, ones(Float64,length(ils)), timestamps, is_directed = is_directed)
deepcopy(g::EvolvingGraph) = EvolvingGraph(is_directed(g),
deepcopy(g.nodes),
deepcopy(g.node_indexof),
deepcopy(g.edges),
deepcopy(g.timestamps),
deepcopy(g.active_nodes),
deepcopy(g.active_node_indexof))
eltype{V,E,T,I}(g::EvolvingGraph{V,E,T,I}) = (V,E,T,I)
is_directed(g::EvolvingGraph) = g.is_directed
nodes(g::EvolvingGraph) = g.nodes
num_nodes(g::EvolvingGraph) = length(nodes(g))
has_node{V}(g::EvolvingGraph{V}, v::V) = v in g.nodes
has_node{V, E, T, KV}(g::EvolvingGraph{V, E, T, KV}, node_key::KV) = node_key in g.node_indexof
unique_timestamps(g::EvolvingGraph) = unique(g.timestamps)
timestamps(g::EvolvingGraph) = g.timestamps
num_timestamps(g::EvolvingGraph) = length(unique_timestamps(g))
active_nodes(g::EvolvingGraph) = g.active_nodes
num_active_nodes(g::EvolvingGraph) = length(g.active_nodes)
has_active_node{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, v::TimeNode{KV,T}) =
v in g.active_nodes
has_active_node{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, key::KV, t::T) =
get(g.active_node_indexof, (key,t), false) != false ? true : false
edges(g::EvolvingGraph) = g.edges
function edges(g::EvolvingGraph, t)
inds = findin(g.timestamps, [t])
if length(inds) == 0
error("unknown timestamp $(t)")
end
return g.edges[inds]
end
num_edges(g::EvolvingGraph) = length(g.edges)
function add_node!{V}(g::EvolvingGraph{V}, v::V)
push!(g.nodes, v)
g.node_indexof[v.key] = node_index(g,v)
v
end
function add_node!{V, E, T, KV}(g::EvolvingGraph{V, E, T, KV}, key::KV)
id = get(g.node_indexof, key, 0)
if id == 0
v = V(g, key)
return add_node!(g, v)
else
return V(id, key)
end
end
function find_node{V, E, T, KV}(g::EvolvingGraph{V, E, T, KV}, key::KV)
try
id = g.node_indexof[v]
return V(id, v)
catch
return false
end
end
find_node{V}(g::EvolvingGraph{V}, v::V) = v in g.nodes ? v : false
function add_edge!{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, v1::KV, v2::KV, t::T; weight::Real = 1.0)
v1 = add_node!(g, v1)
v2 = add_node!(g, v2)
add_edge!(g, v1, v2, t, weight = weight)
end
function add_edge!{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, v1::V, v2::V, t::T; weight::Real = 1.0)
e1 = E <: WeightedTimeEdge? E(v1, v2, weight, t) : E(v1, v2, t)
if !(e1 in edges(g))
# we only add active nodes when we add edges to the graph.
if !((node_key(v1),t) in keys(g.active_node_indexof))
n1 = TimeNode{KV,T}(g, v1.key, t)
push!(g.active_nodes, n1)
g.active_node_indexof[(node_key(v1),t)] = node_index(n1)
end
if !((node_key(v2),t) in keys(g.active_node_indexof))
n2 = TimeNode{KV,T}(g, v2.key, t)
push!(g.active_nodes, n2)
g.active_node_indexof[(node_key(v2),t)] = node_index(n2)
end
push!(g.edges, e1)
push!(g.timestamps, t)
if !(is_directed(g))
push!(g.edges, edge_reverse(e1))
push!(g.timestamps, t)
end
end
return e1
end
function add_bunch_of_edges!(g::EvolvingGraph, ebunch)
for e in ebunch
if length(e) == 3
i,j,t = e
add_edge!(g, i, j, t)
elseif length(e) == 4
i, j, t, w = e
add_edge!(g, i, j, t, weight = w)
else
error("Each edge in ebunch must have 3 or 4 elements")
end
end
return g
end
function adjacency_matrix{V, E, T}(g::EvolvingGraph{V, E, T}, t::T)
n = num_nodes(g)
es = edges(g, t)
A = zeros(Float64, n, n)
for e in es
i = node_index(source(e))
j = node_index(target(e))
v = E <: WeightedTimeEdge ? e.weight : 1.0
A[i,j] = v
end
return A
end
function sparse_adjacency_matrix{V,E,T}(g::EvolvingGraph{V,E,T}, t::T, M::Type = Float64)
n = num_nodes(g)
is = Int[]
js = Int[]
vs = Float64[]
es = edges(g, t)
for e in es
i = node_index(source(e))
j = node_index(target(e))
v = E <: WeightedTimeEdge ? e.weight : 1.0
push!(is, i)
push!(js, j)
push!(vs, v)
end
return sparse(is, js, vs, n, n)
end
function neighbors{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, v::TimeNode{KV,T}; mode::Symbol = :forward)
r = TimeNode{KV,T}[]
switch_mode = Dict(
:forward => (source, target, >),
:backward => (target, source, <)
)
this, that, compare = switch_mode[mode]
if ! has_active_node(g,v)
return r
end
v_t = node_timestamp(v)
v_key = node_key(v)
for e in edges(g)
# forword neighbors at timestamp v_t
if edge_timestamp(e) == v_t
if node_key(this(e)) == v_key
v_key_new = node_key(that(e))
v_index = g.active_node_indexof[(v_key_new,v_t)]
n = TimeNode(v_index, v_key_new, v_t)
if !(n in r)
push!(r, n)
end
end
end
if compare(edge_timestamp(e), v_t)
if node_key(source(e)) == v_key || node_key(target(e)) == v_key
v_t_new = edge_timestamp(e)
v_index = g.active_node_indexof[(v_key, v_t_new)]
n = TimeNode(v_index, v_key, v_t_new)
if !(n in r)
push!(r, n)
end
end
end
end
return r
end
forward_neighbors{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, v::TimeNode{KV,T}) = neighbors(g, v, mode = :forward)
backward_neighbors{V,E,T,KV}(g::EvolvingGraph{V,E,T,KV}, v::TimeNode{KV,T}) = neighbors(g, v, mode = :backward)
"""
add_graph!(eg, g, t)
Add a static graph `g` at timestamp `t` to an evolving graph `eg`.
# Examples
```jldoctest
julia> using EvolvingGraphs
julia> g = EvolvingGraph()
Directed EvolvingGraph 0 nodes, 0 static edges, 0 timestamps
julia> sg = DiGraph()
DiGraph 0 nodes, 0 edges
julia> add_edge!(sg, 1, 2)
Node(1)->Node(2)
julia> add_edge!(sg, 2, 3)
Node(2)->Node(3)
julia> add_graph!(g, sg, 1)
Directed EvolvingGraph 3 nodes, 2 static edges, 1 timestamps
julia> edges(g)
2-element Array{EvolvingGraphs.WeightedTimeEdge{EvolvingGraphs.Node{Int64},Int64,Float64},1}:
Node(1)-1.0->Node(2) at time 1
Node(2)-1.0->Node(3) at time 1
```
"""
function add_graph!{V, E, T}(eg::EvolvingGraph{V, E, T}, g::AbstractStaticGraph, t::T)
es = edges(g)
for e in es
add_edge!(eg, node_key(source(e)), node_key(target(e)), t)
end
eg
end