2017-02-20 117 views
0

我想创建一个随机生成的图,其中需要5个输入参数,n1,n2,p1,p2和p3和 生成随机图G(n1,n2,p1,p2 ,p3),其具有划分为 的n1 + n2个顶点成为两个集合V1,V2,使得| V1 | = n1和| V2 | = n2。带概率的随机图生成

p1,p2和p3是概率,因此在0到1的范围内。对于顶点u,v∈V1的每对 ,添加一个连接u和v的边,其概率为p1。对于顶点u,v∈V2的每一对,添加以概率p2连接u和v的边。对于顶点u∈V1和v∈V2的每一对 ,添加连接u和v的概率为p3的边。

目前,我有它做一个随机图

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; 

}

,但我不知道在哪里的概率开始发挥作用。从我看到他们进入计算成本时,但我不知道如何将其实现到图生成中。

编辑:看着鄂尔多斯 - 仁义模型,但它在成本分析方面并没有真正的帮助?加入ERM

实施感谢您的帮助

回答

0

我想你会使用ER模型在正确的轨道上。喜欢的东西:

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); 
    } 
    } 
} 

如果图形是小,你可能会更好地执行邻接表作为邻接矩阵:

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

然后,如果两个顶点之间存在一条边,设置相应的元素在矩阵为true:

adjacencyMatrix[u][v] = true; 

如果需要权重加入到边,双值或任何你正在使用,以代表重替换布尔值。