using Plots
L = 20
M = zeros(Int, L, L);
ρ = 0.1 # density
0.1
M .= rand(L, L) .< ρ
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
heatmap(M, ratio=1)
ρ = 0.2 # density
M .= rand(L, L) .< ρ
heatmap(M, ratio=1)
ρ = 0.45 # density
M .= rand(L, L) .< ρ
heatmap(M, ratio=1)
ρ = 0.7 # density
M .= rand(L, L) .< ρ
heatmap(M, ratio=1)
Who are the neighbours of agent 1?
E.g. agent 1 is in contact with agents 2, 7, 73
contacts = Dict()
Dict{Any,Any} with 0 entries
contacts[1] = [2, 7, 73]
3-element Array{Int64,1}: 2 7 73
contacts[2] = Set([4, 7, 73])
Set{Int64} with 3 elements: 7 4 73
3 in contacts[1]
false
2 ∈ contacts[2] # \in<TAB>
false
contacts = [ [2, 7, 73], [4, 7, 73] ]
2-element Array{Array{Int64,1},1}: [2, 7, 73] [4, 7, 73]
contacts[2]
3-element Array{Int64,1}: 4 7 73
Called an adjacency list
Adjacency matrix:
M = rand(0:1, 10, 10)
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
using LightGraphs
g = Graph()
{0, 0} undirected simple Int64 graph
add_vertices!(g, 5)
5
g
{5, 0} undirected simple Int64 graph
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 1, 3)
true
g
{5, 3} undirected simple Int64 graph
using GraphPlot
gplot(g)
g = erdos_renyi(30, 0.3)
{30, 117} undirected simple Int64 graph
gplot(g)
WARNING: both LightGraphs and Plots export "grid"; uses of it in module Main must be qualified
gplot(LightGraphs.grid((3, 4)))
neighbors(g, 1)
7-element Array{Int64,1}: 2 7 10 19 21 28 29
g.fadjlist
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]