@@ -81,20 +81,17 @@ function edge_encoding(s, t, n; directed=true)
8181 maxid = n^ 2
8282 else
8383 # 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
8684 maxid = n * (n + 1 ) ÷ 2
85+
8786 mask = s .> t
88- # s1, t1 = s[mask], t[mask]
89- # t2, s2 = s[.!mask], t[.!mask]
9087 snew = copy (s)
9188 tnew = copy (t)
9289 snew[mask] .= t[mask]
9390 tnew[mask] .= s[mask]
9491 s, t = snew, tnew
95-
92+
9693 # 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)
9895 # = ∑_{i',i'<i} (n - i' + 1) + (j - i + 1)
9996 # = (i - 1)*(2*(n+1)-i)÷2 + (j - i + 1)
10097 idx = @. (s- 1 )* (2 * (n+ 1 )- s)÷ 2 + (t- s+ 1 )
105102# each edge is represented by a number in
106103# 1:N^2
107104function 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
111123 return s, t
112124end
113125
0 commit comments