library(wext)
library(tidygraph)
library(tibble)
library(dplyr)
library(bench)

This is just a quick comparison of the edge swapping algorithms used in this package.

The first step is to create a bipartite graph for the test.

This graph has 1001 nodes and 9799 edges.

The first bipartite edge swapping method is bipartite_edge_swap() which relies on the ‘igraph’ and ‘tidygraph’ APIs for handling all of the data and logically manipulates a tidygraph graph object. This was the easiest implementation, though I expected that the manipulation of high-level data types would be a time-suck. This first test runs the edge swapping algorithm to swap 100 edges 20 times.

The second edge swapping algorithm implementation is in bipartite_edge_swap2(). This method took more work on my end, though its use of vector manipulations rapidly increased the speed of the algorithm. (As it is much faster, I now run 50 iterations for the benchmark.)

This first test ran 100 edge swaps on the graph bgr. The first method took 7.38 s on average (a median of 20 iterations) while the second took 625 ms on average. Thus, the second method is much faster. Another striking difference is the amount of memory each method was allocated - the first method took about 2 GB of RAM! It also called the garbage collector an order of magnitude more frequently.

The function bipartite_edge_swap3() implements the same algorithm used in method 2 in C++.

This is the fastest method, requiring 457 ms on average with even less RAM required.

To specifically test everything expect for the edge swapping, I re-ran the same test but only conducting 1 edge swap in each iteration.

Interestingly, the first method (based around the ‘igraph’ and ‘tidygraph’ APIs) is substantially faster to do all of the other steps in the edge swapping process. This is likely because the second method must first transform the bipartite graph bgr into the list of vectors while the first method works directly on the tidygraph object (it only does this once per call to bipartite_edge_swap2). Still, it may be worth poking around on the setup steps in the second method to improve its efficiency.

To further test this hypothesis, the difference should shrink if I use a smaller bipartite graph.

This smaller graph has 51 nodes and 95 edges. I now re-run the test with only 1 edge swap.

Now, the second method is actually faster because it was easier to change to the vector manipulation than lug around the relatively “heavy” tidygraph object (see the mem_alloc columns). You can see that the garbage collection intervened a few times (in the gc/sec and n_gc column).


For comparison, here is the same test using the heavily optimized edge swapping algorithm in ‘igraph’. While fast, this test does not preserve the bipartite nature of the graph, thus I cannot use it for ‘WExT’.