In [1]:
using Plots

In [6]:
L = 20

M = zeros(Int, L, L);

In [5]:
ρ = 0.1  # density

Out[5]:
0.1
In [9]:
M .= rand(L, L) .< ρ

Out[9]:
20×20 Array{Int64,2}:
0  1  1  0  0  0  1  0  0  0  1  0  0  0  0  0  0  0  0  0
0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  1  0  0  0  1  0  0  0  0  0  0  1  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  1  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0
0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0
0  0  0  0  0  0  1  0  0  0  0  0  1  0  0  0  0  0  0  0
0  0  0  0  0  1  0  0  1  0  0  0  0  0  0  0  0  0  0  0
0  0  1  0  0  0  0  0  0  0  0  1  0  1  0  0  1  0  0  0
0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  1  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0
0  0  0  0  0  1  0  0  0  0  0  0  0  1  0  0  0  0  0  0
1  0  0  0  0  0  0  0  0  0  0  1  0  0  0  1  0  0  0  0
0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
In [11]:
heatmap(M, ratio=1)

Out[11]:
In [13]:
ρ = 0.2  # density
M .= rand(L, L) .< ρ
heatmap(M, ratio=1)

Out[13]:
In [14]:
ρ = 0.45  # density
M .= rand(L, L) .< ρ
heatmap(M, ratio=1)

Out[14]:
In [16]:
ρ = 0.7  # density
M .= rand(L, L) .< ρ
heatmap(M, ratio=1)

Out[16]:

Who are the neighbours of agent 1?

E.g. agent 1 is in contact with agents 2, 7, 73

In [18]:
contacts = Dict()

Out[18]:
Dict{Any,Any} with 0 entries
In [19]:
contacts[1] = [2, 7, 73]

Out[19]:
3-element Array{Int64,1}:
2
7
73
In [23]:
contacts[2] = Set([4, 7, 73])

Out[23]:
Set{Int64} with 3 elements:
7
4
73
In [21]:
3 in contacts[1]

Out[21]:
false
In [25]:
2 ∈ contacts[2]  # \in<TAB>

Out[25]:
false
In [26]:
contacts = [ [2, 7, 73], [4, 7, 73] ]

Out[26]:
2-element Array{Array{Int64,1},1}:
[2, 7, 73]
[4, 7, 73]
In [27]:
contacts[2]

Out[27]:
3-element Array{Int64,1}:
4
7
73

In [31]:
M = rand(0:1, 10, 10)

Out[31]:
10×10 Array{Int64,2}:
1  1  0  0  0  1  0  0  1  0
1  1  0  1  1  0  0  0  0  0
1  1  0  0  0  0  0  0  0  0
1  1  0  1  0  0  0  0  1  1
1  1  1  0  1  0  0  0  0  0
1  0  0  1  1  1  0  1  1  0
1  1  1  1  0  1  1  1  1  0
0  0  1  0  0  1  0  0  0  0
1  1  0  0  1  1  0  0  0  0
1  0  1  1  0  0  1  1  0  1
In [33]:
using LightGraphs

In [34]:
g = Graph()

Out[34]:
{0, 0} undirected simple Int64 graph
In [36]:
add_vertices!(g, 5)

Out[36]:
5
In [37]:
g

Out[37]:
{5, 0} undirected simple Int64 graph
In [42]:
add_edge!(g, 1, 2)

Out[42]:
true
In [43]:
g

Out[43]:
{5, 3} undirected simple Int64 graph
In [44]:
using GraphPlot

In [45]:
gplot(g)

Out[45]:
In [47]:
g = erdos_renyi(30, 0.3)

Out[47]:
{30, 117} undirected simple Int64 graph
In [48]:
gplot(g)

Out[48]:
WARNING: both LightGraphs and Plots export "grid"; uses of it in module Main must be qualified

In [51]:
gplot(LightGraphs.grid((3, 4)))

Out[51]:
In [53]:
neighbors(g, 1)

Out[53]:
7-element Array{Int64,1}:
2
7
10
19
21
28
29
In [54]:
g.fadjlist

Out[54]:
30-element Array{Array{Int64,1},1}:
[2, 7, 10, 19, 21, 28, 29]
[1, 9, 12, 13, 14, 15, 20, 23, 24, 27]
[7, 8, 18, 22, 23, 24, 28, 30]
[14, 15, 16, 19, 20, 23, 29]
[8, 9, 20, 21, 23]
[8, 14, 18, 20, 22, 26, 29]
[1, 3, 10, 11, 15, 18, 25, 26]
[3, 5, 6, 9, 12, 15, 16, 24]
[2, 5, 8, 16, 17, 18, 19, 21, 26]
[1, 7, 11, 12, 21, 23, 25]
[7, 10, 13, 14, 15, 27, 29, 30]
[2, 8, 10, 16, 18, 27, 30]
[2, 11, 15, 18, 21, 24, 28, 29]
⋮
[1, 4, 9, 14, 17, 26]
[2, 4, 5, 6, 17, 18, 21, 23, 24, 28]
[1, 5, 9, 10, 13, 14, 15, 18, 20, 28]
[3, 6, 16]
[2, 3, 4, 5, 10, 20, 25]
[2, 3, 8, 13, 20, 25, 28]
[7, 10, 15, 17, 23, 24, 28]
[6, 7, 9, 14, 19, 27, 29, 30]
[2, 11, 12, 16, 17, 18, 26, 29]
[1, 3, 13, 16, 17, 20, 21, 24, 25, 29, 30]
[1, 4, 6, 11, 13, 26, 27, 28]
[3, 11, 12, 15, 16, 17, 26, 28]
In [ ]: