Skip to content

Commit

Permalink
Tentative balanced implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vigna committed Feb 20, 2021
1 parent de93f41 commit eddd21d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public abstract class AbstractSuccinctUndirectedGraph<E>
extends
AbstractSuccinctGraph<E>
{
/** 2<sup>32</sup> &middot; &phi;, &phi; = (&#x221A;5 &minus; 1)/2. */
protected static final int INT_PHI = 0x9E3779B9;

public AbstractSuccinctUndirectedGraph(final int n, final int m)
{
Expand Down Expand Up @@ -100,13 +102,14 @@ public boolean hasNext()
int d = 0;
for (final E e : succ.apply(x)) {
final int y = Graphs.getOppositeVertex(graph, e, x);

if (sorted) {
if (x <= y) {
if (x * INT_PHI <= y * INT_PHI) {
s = IntArrays.grow(s, d + 1);
s[d++] = y;
}
} else {
if (x >= y) {
if (x * INT_PHI >= y * INT_PHI) {
s = IntArrays.grow(s, d + 1);
s[d++] = y;
}
Expand Down Expand Up @@ -178,11 +181,11 @@ public long nextLong()
int d = 0;
if (sorted) {
for (final E e : succ.apply(x))
if (x <= Graphs.getOppositeVertex(graph, e, x))
if (x * INT_PHI <= Graphs.getOppositeVertex(graph, e, x) * INT_PHI)
d++;
} else {
for (final E e : succ.apply(x))
if (x >= Graphs.getOppositeVertex(graph, e, x))
if (x * INT_PHI >= Graphs.getOppositeVertex(graph, e, x) * INT_PHI)
d++;
}
x++;
Expand All @@ -205,7 +208,7 @@ public int outDegreeOf(final Integer vertex)
protected boolean containsEdge(
final EliasFanoIndexedMonotoneLongBigList successors, int x, int y)
{
if (x > y) {
if (x * INT_PHI > y * INT_PHI) {
final int t = x;
x = y;
y = t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public SuccinctUndirectedGraph(
@Override
public boolean containsEdge(final IntIntSortedPair e)
{
int x = e.firstInt();
int y = e.secondInt();
if (x * INT_PHI > y * INT_PHI) {
final int t = x;
x = y;
y = t;
}
return successors.indexOfUnsafe(((long) e.firstInt() << sourceShift) + e.secondInt()) != -1;
}

Expand Down Expand Up @@ -216,7 +223,14 @@ public int getIndexFromEdge(final IntIntSortedPair e)
final int target = e.secondInt();
if (source < 0 || source >= n || target < 0 || target >= n)
throw new IllegalArgumentException();
return (int) successors.indexOfUnsafe(((long) source << sourceShift) + target);
int x = source;
int y = target;
if (x * INT_PHI > y * INT_PHI) {
final int t = x;
x = y;
y = t;
}
return (int) successors.indexOfUnsafe(((long) x << sourceShift) + y);
}

/**
Expand All @@ -239,7 +253,7 @@ public IntIntSortedPair getEdge(final Integer sourceVertex, final Integer target
{
int x = sourceVertex;
int y = targetVertex;
if (x > y) {
if (x * INT_PHI > y * INT_PHI) {
final int t = x;
x = y;
y = t;
Expand Down

0 comments on commit eddd21d

Please sign in to comment.