-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph_environment.jl
73 lines (62 loc) · 2 KB
/
graph_environment.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
using LightGraphs, MetaGraphs, Compose
using DataFrames, GraphPlot, Colors
"""
read_graph
Reads a comma delimitted text file where each line represents: `source, destination, weight` for each edge.
"""
function read_graph(filename)
f = open(filename)
s = readlines(f)
srcs = Vector{Int}(undef, length(s))
dsts = Vector{Int}(undef, length(s))
wgts = Vector{Float64}(undef, length(s))
for (i, line) in enumerate(s)
spl = split(line, ",")
srcs[i] = parse(Int64, spl[1])
dsts[i] = parse(Int64, spl[2])
wgts[i] = parse(Float64, spl[3])
end
return construct_graph(srcs, dsts, wgts)
end
"""
construct_graph(src, dst, wgt)
Takes in three vectors (sources, destinations, weights) and constructs a MetaGraph out of them.
"""
function construct_graph(srcs, dsts, wgts)
# the maximal value in sources and destinations is the number of nodes
n = maximum([srcs; dsts])
G = MetaGraph(SimpleGraph(n))
defaultweight!(G, 0.0)
# For each src=>dst, wgt, create a weighted edge for it
for (s,d,w) in zip(srcs, dsts, wgts)
# ignore zero weight edges, since the default is 0 anyway
if w > 0
add_edge!(G, s, d)
set_prop!(G, s, d, :weight, w)
end
end
return G
end
"""
plot_graph(G, fn)
Save the graph `G` as an SVG in the local directory with the filename `fn`.
Uses the default `spring_layout` layout.
"""
function plot_graph(G, fn)
mn, mx = extrema(weights(G))
line_colors = map(edges(G)) do ε
src, dst = Tuple(ε)
weighted_color_mean(weights(G)[src, dst], colorant"white", colorant"black")
end
nodelabel = 1:nv(G)
p = gplot(G, edgestrokec = line_colors, nodelabel = nodelabel)
draw(SVG(fn, 10cm, 10cm), p)
end
"""
main
I don't believe in the concept of "main" functions, but whatever.
"""
function main()
G = read_graph("weightsSmall.csv")
plot_graph(G, "test-graph.svg")
end