|
70 | 70 | ones_like(x::AbstractArray, T=eltype(x), sz=size(x)) = fill!(similar(x, T, sz), 1)
|
71 | 71 | ones_like(x::SparseMatrixCSC, T=eltype(x), sz=size(x)) = ones(T, sz)
|
72 | 72 | ones_like(x::CUMAT_T, T=eltype(x), sz=size(x)) = CUDA.ones(T, sz)
|
| 73 | + |
| 74 | + |
| 75 | +# each edge is represented by a number in |
| 76 | +# 1:N^2 |
| 77 | +function edge_encoding(s, t, n; directed=true) |
| 78 | + if directed |
| 79 | + # directed edges and self-loops allowed |
| 80 | + idx = (s .- 1) .* n .+ t |
| 81 | + maxid = n^2 |
| 82 | + else |
| 83 | + # Undirected edges and self-loops allowed |
| 84 | + # In this encoding, each edge has 2 possible encodings (also the self-loops). |
| 85 | + # We return the canonical one given by the upper triangular adj matrix |
| 86 | + maxid = n * (n + 1) ÷ 2 |
| 87 | + mask = s .> t |
| 88 | + # s1, t1 = s[mask], t[mask] |
| 89 | + # t2, s2 = s[.!mask], t[.!mask] |
| 90 | + snew = copy(s) |
| 91 | + tnew = copy(t) |
| 92 | + snew[mask] .= t[mask] |
| 93 | + tnew[mask] .= s[mask] |
| 94 | + s, t = snew, tnew |
| 95 | + |
| 96 | + # idx = ∑_{i',i'<i} ∑_{j',j'>=i'}^n 1 + ∑_{j',i<=j'<=j} 1 |
| 97 | + # = ∑_{i',i'<i} ∑_{j',j'>=i'}^n 1 + j - i + 1 |
| 98 | + # = ∑_{i',i'<i} (n - i' + 1) + (j - i + 1) |
| 99 | + # = (i - 1)*(2*(n+1)-i)÷2 + (j - i + 1) |
| 100 | + idx = @. (s-1)*(2*(n+1)-s)÷2 + (t-s+1) |
| 101 | + end |
| 102 | + return idx, maxid |
| 103 | +end |
| 104 | + |
| 105 | +# each edge is represented by a number in |
| 106 | +# 1:N^2 |
| 107 | +function edge_decoding(idx, n; directed=true) |
| 108 | + # g = remove_self_loops(g) |
| 109 | + s = (idx .- 1) .÷ n .+ 1 |
| 110 | + t = (idx .- 1) .% n .+ 1 |
| 111 | + return s, t |
| 112 | +end |
| 113 | + |
| 114 | +@non_differentiable edge_encoding(x...) |
| 115 | +@non_differentiable edge_decoding(x...) |
| 116 | + |
0 commit comments