|
1 | | -#= # Some custom extensions of Graphs.jl and SimpleWeightedGraphs.jl |
2 | | -""" |
3 | | - SimpleWeightedGraph(n_vertices::Integer, n_edges::Integer; T::Type = Int64, U::Type = Float64) |
4 | | -
|
5 | | -Random SimpleWeightedGraph with `n_vertices` vertices and `n_edges` edges, vertex type `T` and adjacency matrix eltype `U`. Edge weights are uniformly extracted between 0 and 1. |
6 | | -""" |
7 | | -function SimpleWeightedGraphs.SimpleWeightedGraph( |
8 | | - n_vertices::Integer, n_edges::Integer; T::Type=Int64, U::Type=Float64 |
9 | | -) |
10 | | - adjm = zeros(U, n_vertices, n_vertices) |
11 | | - rand_nnz_cart_idxs = rand(CartesianIndices(adjm), n_edges) |
12 | | -
|
13 | | - for cart_idx in rand_nnz_cart_idxs |
14 | | - adjm[cart_idx] = U(rand()) |
15 | | - end |
16 | | -
|
17 | | - adjm .= (adjm .+ adjm') ./ U(2) |
18 | | -
|
19 | | - return SimpleWeightedGraph{T,U}(adjm) |
20 | | -end |
21 | | -
|
22 | | -""" |
23 | | - SimpleWeightedGraph{T}(n_vertices::Integer, n_edges::Integer; U::Type = Float64) where { T } |
24 | | -
|
25 | | -Random SimpleWeightedGraph with `n_vertices` vertices and `n_edges` edges, vertex type `T` and adjacency matrix eltype `U`. Edge weights are uniformly extracted between 0 and 1. |
26 | | -""" |
27 | | -function SimpleWeightedGraphs.SimpleWeightedGraph{T}( |
28 | | - n_vertices::Integer, n_edges::Integer; U::Type=Float64 |
29 | | -) where {T} |
30 | | - adjm = zeros(U, n_vertices, n_vertices) |
31 | | - rand_nnz_cart_idxs = rand(CartesianIndices(adjm), n_edges) |
32 | | -
|
33 | | - for cart_idx in rand_nnz_cart_idxs |
34 | | - adjm[cart_idx] = U(rand()) |
35 | | - end |
36 | | -
|
37 | | - adjm .= (adjm .+ adjm') ./ U(2) |
38 | | -
|
39 | | - return SimpleWeightedGraph{T,U}(adjm) |
40 | | -end |
41 | | -
|
42 | | -""" |
43 | | - SimpleWeightedGraph{T,U}(n_vertices::Integer, n_edges::Integer) where { T, U } |
44 | | -
|
45 | | -Random SimpleWeightedGraph with `n_vertices` vertices and `n_edges` edges, vertex type `T` and adjacency matrix eltype `U`. Edge weights are uniformly extracted between 0 and 1. |
46 | | -""" |
47 | | -function SimpleWeightedGraphs.SimpleWeightedGraph{T,U}( |
48 | | - n_vertices::Integer, n_edges::Integer; weight_range::Tuple{U,U} = (zero(U),one(U)), |
49 | | -) where {T,U} |
50 | | - adjm = zeros(U, n_vertices, n_vertices) |
51 | | - rand_nnz_cart_idxs = rand(CartesianIndices(adjm), n_edges) |
52 | | -
|
53 | | - for cart_idx in rand_nnz_cart_idxs |
54 | | - if U <: Integer |
55 | | - adjm[cart_idx] = U(rand(weight_range[1]:weight_range[2])) |
56 | | - elseif U <: Real |
57 | | - adjm[cart_idx] = U(rand(Uniform(weight_range[1],weight_range[2]))) |
58 | | - else |
59 | | - throw(ErrorException("The rand function suited for U = $U has not yet been implemented. Please file an issue.")) |
60 | | - end |
61 | | - end |
62 | | -
|
63 | | - adjm .= (adjm .+ adjm') ./ U(2) |
64 | | -
|
65 | | - return SimpleWeightedGraph{T,U}(adjm) |
66 | | -end |
67 | | -
|
68 | | -""" |
69 | | - SimpleWeightedDiGraph(n_vertices::Integer, n_edges::Integer; T::Type = Int64, U::Type = Float64) |
70 | | -
|
71 | | -Random SimpleWeightedDiGraph with `n_vertices` vertices and `n_edges` edges, vertex type `T` and adjacency matrix eltype `U`. Edge weights are uniformly extracted between 0 and 1. |
72 | | -""" |
73 | | -function SimpleWeightedGraphs.SimpleWeightedDiGraph( |
74 | | - n_vertices::Integer, n_edges::Integer; T::Type=Int64, U::Type=Float64 |
75 | | -) |
76 | | - adjm = zeros(U, n_vertices, n_vertices) |
77 | | - rand_nnz_cart_idxs = rand(CartesianIndices(adjm), n_edges) |
78 | | -
|
79 | | - for cart_idx in rand_nnz_cart_idxs |
80 | | - adjm[cart_idx] = U(rand()) |
81 | | - end |
82 | | -
|
83 | | - return SimpleWeightedDiGraph{T,U}(adjm) |
84 | | -end |
85 | | -
|
86 | | -""" |
87 | | - SimpleWeightedGraphs.SimpleWeightedDiGraph{T}(n_vertices::Integer, n_edges::Integer; U::Type = Float64) where {T} |
88 | | -
|
89 | | -Random SimpleWeightedDiGraph with `n_vertices` vertices and `n_edges` edges, vertex type `T` and adjacency matrix eltype `U`. Edge weights are uniformly extracted between 0 and 1. |
90 | | -""" |
91 | | -function SimpleWeightedGraphs.SimpleWeightedDiGraph{T}( |
92 | | - n_vertices::Integer, n_edges::Integer; U::Type=Float64 |
93 | | -) where {T} |
94 | | - adjm = zeros(U, n_vertices, n_vertices) |
95 | | - rand_nnz_cart_idxs = rand(CartesianIndices(adjm), n_edges) |
96 | | -
|
97 | | - for cart_idx in rand_nnz_cart_idxs |
98 | | - adjm[cart_idx] = U(rand()) |
99 | | - end |
100 | | -
|
101 | | - return SimpleWeightedDiGraph{T,U}(adjm) |
102 | | -end |
103 | | -
|
104 | | -""" |
105 | | - SimpleWeightedDiGraph{T,U}(n_vertices::Integer, n_edges::Integer) |
106 | | -
|
107 | | -Random SimpleWeightedDiGraph with `n_vertices` vertices and `n_edges` edges, vertex type `T` and adjacency matrix eltype `U`. Edge weights are uniformly extracted between 0 and 1. |
108 | | -""" |
109 | | -function SimpleWeightedGraphs.SimpleWeightedDiGraph{T,U}( |
110 | | - n_vertices::Integer, n_edges::Integer, weight_range::Tuple{U,U} = (zero(U),one(U)) |
111 | | -) where {T,U} |
112 | | - adjm = zeros(U, n_vertices, n_vertices) |
113 | | - rand_nnz_cart_idxs = rand(CartesianIndices(adjm), n_edges) |
114 | | -
|
115 | | - for cart_idx in rand_nnz_cart_idxs |
116 | | - if U <: Integer |
117 | | - adjm[cart_idx] = U(rand(weight_range[1]:weight_range[2])) |
118 | | - elseif U <: Real |
119 | | - adjm[cart_idx] = U(rand(Uniform(weight_range[1],weight_range[2]))) |
120 | | - else |
121 | | - throw(ErrorException("The rand function suited for U = $U has not yet been implemented. Please file an issue.")) |
122 | | - end |
123 | | - end |
124 | | - return SimpleWeightedDiGraph{T,U}(adjm) |
125 | | -end =# |
126 | | - |
127 | 1 | Graphs.weights(g::G) where {T, G <: AbstractSimpleWeightedGraph{T}} = Graphs.weights(g) |
128 | 2 |
|
129 | 3 |
|
|
0 commit comments