@@ -81,20 +81,17 @@ function edge_encoding(s, t, n; directed=true)
81
81
maxid = n^ 2
82
82
else
83
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
84
maxid = n * (n + 1 ) ÷ 2
85
+
87
86
mask = s .> t
88
- # s1, t1 = s[mask], t[mask]
89
- # t2, s2 = s[.!mask], t[.!mask]
90
87
snew = copy (s)
91
88
tnew = copy (t)
92
89
snew[mask] .= t[mask]
93
90
tnew[mask] .= s[mask]
94
91
s, t = snew, tnew
95
-
92
+
96
93
# 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
94
+ # = ∑_{i',i'<i} ∑_{j',j'>=i'}^n 1 + ( j - i + 1)
98
95
# = ∑_{i',i'<i} (n - i' + 1) + (j - i + 1)
99
96
# = (i - 1)*(2*(n+1)-i)÷2 + (j - i + 1)
100
97
idx = @. (s- 1 )* (2 * (n+ 1 )- s)÷ 2 + (t- s+ 1 )
105
102
# each edge is represented by a number in
106
103
# 1:N^2
107
104
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
105
+ if directed
106
+ # g = remove_self_loops(g)
107
+ s = (idx .- 1 ) .÷ n .+ 1
108
+ t = (idx .- 1 ) .% n .+ 1
109
+ else
110
+ # We replace j=n in
111
+ # idx = (i - 1)*(2*(n+1)-i)÷2 + (j - i + 1)
112
+ # and obtain
113
+ # idx = (i - 1)*(2*(n+1)-i)÷2 + (n - i + 1)
114
+
115
+ # OR We replace j=i and obtain??
116
+ # idx = (i - 1)*(2*(n+1)-i)÷2 + 1
117
+
118
+ # inverting we have
119
+ s = @. ceil (Int, - sqrt ((n + 1 / 2 )^ 2 - 2 * idx) + n + 1 / 2 )
120
+ t = @. idx - (s- 1 )* (2 * (n+ 1 )- s)÷ 2 - 1 + s
121
+ # t = (idx .- 1) .% n .+ 1
122
+ end
111
123
return s, t
112
124
end
113
125
0 commit comments