-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.jl
81 lines (74 loc) · 2.22 KB
/
utilities.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
using Graphs
using SimpleGraphs
function save_json(arg,fname::ASCIIString,gname::ASCIIString)
d = JSON.json(arg)
f = open(@sprintf("%s",joinpath(fname,gname)),"w")
JSON.print(f,d)
close(f)
end
function plot_to_file(g::GenericGraph,fname::ASCIIString,gname::ASCIIString)
write_to_file(g,fname,gname)
pname = joinpath(fname,gname)
run(`neato -Tpng $pname".dot" -o $pname".png"`)
end
function write_to_file(g::GenericGraph,fname::ASCIIString,gname::ASCIIString)
if !ispath(fname)
mkdir(fname)
end
f = open(@sprintf("%s.dot",joinpath(fname,gname)),"w")
write(f,to_dot(g))
close(f)
end
function plot_to_file(g::SimpleGraphs.SimpleGraph,fname::ASCIIString,gname::ASCIIString)
(G,d,dinv) = convert_simple(g)
plot_to_file(G,fname,gname)
end
function write_to_file(g::SimpleGraphs.SimpleGraph,fname::ASCIIString,gname::ASCIIString)
(G,d,dinv) = convert_simple(g)
write_to_file(G,fname,gname)
end
function simple_NE_sgraph(n::Int,e::Int)
g = SimpleGraphs.SimpleGraph()
for i = 1:e
pts = rand(collect(1:n),2)
add!(g,pts[1],pts[2])
end
if NE(g) != e
# add in missing edges
for i = 1:(e-NE(g))
ns = nt = 1
while ns == nt || has(g,ns,nt)
ns = rand(1:NV(g))
nt = rand(1:NV(g))
end
add!(g,ns,nt)
end
end
return g
end
function simple_NE_graph(N,E;parallel_edges=false,self_loops=false)
g = simple_graph(N,is_directed=false)
for i = 1:E
if !parallel_edges
edge_placed = false
while !edge_placed
pts = rand(collect(1:N),2)
if !(pts[2] in collect(Graphs.out_neighbors(pts[1],g)))
if !self_loops && (pts[2] != pts[1])
add_edge!(g,pts[1],pts[2])
edge_placed = true
elseif self_loops
add_edge!(g,pts[1],pts[2])
edge_placed = true
end
else
continue
end
end
else
pts = rand(collect(1:N),2)
add_edge!(g,pts[1],pts[2])
end
end
return g
end