0

I am trying to create a randomly generated graph with the that takes 5 input parameters, n1, n2, p1, p2 and p3 and generates the random graph denoted G(n1, n2, p1, p2, p3) which has n1+n2 vertices partitioned into two sets V1, V2 such that |V1| = n1 and |V2| = n2.

p1, p2 and p3 are probabilities and therefore in the range 0 through 1. For every pair of vertices u, v ∈ V1, add an edge connecting u and v with probability p1. For every pair 1 of vertices u, v ∈ V2, add an edge connecting u and v with probability p2. For every pair of vertices u ∈ V1 and v ∈ V2, add an edge connecting u and v with probability p3.

Currently I have it making a random graph

private static ArrayList<LinkedList<Integer>> RandomGraph(int n1, int n2, double p1, double p2, double p3) {
int V = n1 + n2;
int[] verticies = new int[V];
// Fills up the verticies array
for (int i = 0; i < V; i++) {
    verticies[i] = i;
}
// Shuffles the array
Random rand = new Random();
for (int i = 0; i < V; i++) {
    int pos = i + rand.nextInt(V - i);
    int temp = verticies[i];
    verticies[i] = verticies[pos];
    verticies[pos] = temp;
}
// System.out.println(Arrays.toString(verticies));
// V1
ArrayList<Edge> a = new ArrayList<>();
int i;
int j;
// for every pair so double for loop for each
for (i = 0; i < n1; i++) {
    for (j = i + 1; j <= rand.nextInt(n1 - i) + i; j++) {
        if(rand.nextDouble()<p1)
        {
            a.add(new Edge(verticies[i], verticies[j], p1));
            a.add(new Edge(verticies[j], verticies[i], p1));
        }
    }
}

// V2
for (j = n1 + 1; j < V; j++) {
    for (int k = j + 1; j <= rand.nextInt(V - k) + k; k++) {
        if(rand.nextDouble<p2)
        {
             a.add(new Edge(verticies[j], verticies[k], p2));
             a.add(new Edge(verticies[k], verticies[j], p2));
        }

    }
}
// V3
for (j = 0; j < n1; j++) {
    for (int k = n1 + 1; k < V; k++) {
        if(rand.nextDouble()<p3)
        {
        a.add(new Edge(verticies[j], verticies[k], p3));
        a.add(new Edge(verticies[k], verticies[j], p3));
        }
    }
}
ArrayList<LinkedList<Integer>> Alist = new ArrayList<>();
for (j = 0; j < V; j++) {
    Alist.add(new LinkedList<Integer>());
}
for (j = 0; j < a.size(); j++) {

    Alist.get(a.get(j).start).add(a.get(j).end);
}

return Alist;

}

but I am not sure where the probabilities come into play. From what I have seen they come into when computing cost but I am not sure how to implement it into the graph generation.

Edit: Looked into Erdos-Renyi Model, but it doesnt really help when it comes to cost analysis? Added the implementation of ERM

Thank you for any help

Georgrio
  • 91
  • 2
  • 14

1 Answers1

0

I think you'd be on the right track using an ER model. Something like:

List<List<Integer>> adjacencyList = new ArrayList<>();
for (ArrayList<Integer> list : adjacencyList) {
  list = new ArrayList<>();
}

Random random = new Random();
// Randomly add edges for V1.
for (int u = 0; u < n1; u++) {
  for (int v = u + 1; v < n1; v++) {
    if (random.nextDouble() < p1) {
      adjacencyList.get(u).add(v);
      adjacencyList.get(v).add(u);
    }
  }
}

// Randomly add edges for V2.
for (int u = n1; u < n1 + n2; u++) {
  for (int v = u + 1; v < n1 + n2; v++) {
    if (random.nextDouble() < p2) {
      adjacencyList.get(u).add(v);
      adjacencyList.get(v).add(u);
    }
  }
}

// Randomly add edges between V1 and V2.
for (int u = 0; u < n1; u++) {
  for (int v = n1; v < n1 + n2; v++) {
    if (random.nextDouble() < p3) {
      adjacencyList.get(u).add(v);
      adjacencyList.get(v).add(u);
    }
  }
}

If the graphs are small, you may be better implementing the adjacency list as an adjacency matrix:

boolean[][] adjacencyMatrix = new boolean[n1 + n2][n1 + n2];

Then, if an edge exists between two vertices, set the corresponding element in the matrix to true:

adjacencyMatrix[u][v] = true;

If you need to add weights to the edges, replace the boolean values with double values or whatever you are using to represent weight.

junglie85
  • 1,243
  • 10
  • 30