Skip to content
Snippets Groups Projects
Commit a787943a authored by Julien Siebert's avatar Julien Siebert
Browse files

Merge branch 'graph_generators' into 'master'

functions to generate graphs with fixed degree sequence

See merge request siebert/curveball!18
parents 2cc1289a 9bfccf8a
Branches
Tags java-version-1.2
No related merge requests found
package de.kl.uni.cs.aalab.experiments.curvball;
import de.kl.uni.cs.aalab.curveball.adjacencyListImpl.bipartite.thread.ThreadImpl;
import de.kl.uni.cs.aalab.utilities.LoadAdjacencyListFile;
import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import static de.kl.uni.cs.aalab.utilities.GraphGenerator.powerLaw;
/**
* Generates random bipartite graphs following a powerlaw distribution
*/
public class GeneratePowerLawRandomGraphs {
private static String usage = "This program first generate a dummy bipartite graph following with a power law degree distribution, then calls the global curveball algorithm to generate random graphs from that degree distribution \n" +
"USAGE:\n" +
"This program requires 6 command line arguments, namely\n" +
"base\n" +
"exponent\n" +
"the random seed\n" +
"number of threads\n" +
"number of random graphs to generate\n" +
"number of steps of the curveball algorithm before generating a new random graph\n";
public static void main(String[] args){
// checks arguments
if (args.length != 6) {
System.err.println(usage);
System.exit(1);
}
// parse arguments
int base = Integer.parseInt(args[0]);
int exponent = Integer.parseInt(args[1]);
long seed = Long.parseLong(args[2]);
int nbThreads = Integer.parseInt(args[3]);
int nbGraphs = Integer.parseInt(args[4]);
int nbSteps = Integer.parseInt(args[5]);
// generate adjacency matrix
HashSet<Integer>[] adjacencyList = powerLaw(base,exponent);
int[] nodesIndices = new int[adjacencyList.length];
for (int i = 0; i < adjacencyList.length; i++) {
nodesIndices[i] = i;
}
Random rnd = new Random(seed);
ThreadImpl curveball = new ThreadImpl(nbThreads, nodesIndices, adjacencyList, rnd);
for (int g = 0; g < nbGraphs; g++) {
for (int s = 0; s < nbSteps; s++) {
try {
curveball.step();
} catch (InterruptedException e) {
e.printStackTrace();
curveball.shutdown();
System.exit(1);
}
// save
try {
LoadAdjacencyListFile.save(adjacencyList, String.format("adj_list_graph%d.csv", g), ' ');
} catch (IOException e) {
e.printStackTrace();
curveball.shutdown();
System.exit(1);
}
}
}
curveball.shutdown();
}
}
package de.kl.uni.cs.aalab.utilities;
import java.util.HashSet;
/**
* Utilities to generate graphs with specific degree distributions
*/
public class GraphGenerator {
/**
* @param base
* @param exponent
* @return the adjacency list of a bipartite graph with n = base^exponent nodes with a degree distribution following a power law
*/
public static HashSet<Integer>[] powerLaw(int base, int exponent) {
int n = (int) Math.pow(base, exponent);
HashSet<Integer>[] adjacencyList = new HashSet[n];
for (int i = 0; i < n; i++) {
adjacencyList[i] = new HashSet<Integer>();
}
for (int i = 0; i < exponent; i++) {
for (int j = 0; j < n / Math.pow(base, i); j++) {
adjacencyList[j].add(i + j);
}
}
return adjacencyList;
}
/**
* @param n
* @return the adjacency list of a bipartite graph with n nodes where the degree distribution of each side is {n,n-1,n-2,...,3,2,1}
*/
public static HashSet<Integer>[] linear(int n) {
HashSet<Integer>[] adjacencyList = new HashSet[n];
for (int i = 0; i < n; i++) {
adjacencyList[i] = new HashSet<Integer>(n - 1, 1);
for (int j = 0; j < n - i; j++) {
adjacencyList[i].add(j);
}
}
return adjacencyList;
}
}
package de.kl.uni.cs.aalab.utilities;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashSet;
import static de.kl.uni.cs.aalab.utilities.GraphGenerator.linear;
import static de.kl.uni.cs.aalab.utilities.GraphGenerator.powerLaw;
import static org.junit.jupiter.api.Assertions.*;
class GraphGeneratorTest {
@Test
@DisplayName("generates a graph following a power law")
void testPowerLawGenerator() {
HashSet<Integer>[] expected = new HashSet[8];
for (int i = 0; i < 8; i++) {
expected[i] = new HashSet<Integer>();
}
expected[0].addAll(Arrays.asList(0,1,2));
expected[1].addAll(Arrays.asList(1,2,3));
expected[2].addAll(Arrays.asList(2,3));
expected[3].addAll(Arrays.asList(3,4));
expected[4].addAll(Arrays.asList(4));
expected[5].addAll(Arrays.asList(5));
expected[6].addAll(Arrays.asList(6));
expected[7].addAll(Arrays.asList(7));
HashSet<Integer>[] adjacencyList = powerLaw(2,3);
for (int i = 0; i < 8; i++) {
assertEquals(expected[i],adjacencyList[i]);
}
}
@Test
void testLinear() {
HashSet<Integer>[] expected = new HashSet[4];
for (int i = 0; i < 4; i++) {
expected[i] = new HashSet<Integer>();
}
expected[0].addAll(Arrays.asList(0,1,2,3));
expected[1].addAll(Arrays.asList(0,1,2));
expected[2].addAll(Arrays.asList(0,1));
expected[3].addAll(Arrays.asList(0));
HashSet<Integer>[] adjacencyList = linear(4);
for (int i = 0; i < 4; i++) {
assertEquals(expected[i],adjacencyList[i]);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment