📜  如何在Java中使用随机边生成创建随机图?

📅  最后修改于: 2022-05-13 01:54:21.364000             🧑  作者: Mango

如何在Java中使用随机边生成创建随机图?

我们知道图是一种数据结构,它由一组有限的顶点和边(将顶点相互连接)组成。可以是有向的(边有方向)或无向的(边没有方向)。然而,随机图是一种随机生成的图数据结构。随机图模型广泛用于研究复杂网络、社交网络、通信工程甚至生物学(研究细胞内调节网络、激活和抑制生物网络中的连接等)。

在本文中,我们将讨论一些算法来生成各种类型的随机图。

算法 1:

该算法基于随机选择顶点和边的数量,然后随机选择两个顶点在它们之间添加一条边。

  1. 随机选择顶点和边的数量。说,V 是顶点数,E 是边数
  2. 检查所选边数 E 是否与顶点数兼容。对于选定数量的顶点 V,在无向图中最多可以有 (V*(V-1)/2) 条边(为什么是 V*(V – 1)/2 ?稍后讨论)(如果它不包含自循环)。
  3. 运行 for 循环,从 i = 0 到 i < 边数 E,并且在每次迭代期间,随机选择两个顶点并在它们之间创建一条边。
  4. 打印创建的图形。

下面是上述方法的实现:

Java
// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
  
    public int vertices;
    public int edges;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for
        // the number of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
  
        // compute the maximum possible number of edges
        // and randomly choose the number of edges less than
        // or equal to the maximum number of possible edges
        this.edges
            = random.nextInt(computeMaxEdges(vertices)) + 1;
  
        // Creating an adjacency list
        // representation for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < edges; i++) {
            // randomly select two vertices to
            // create an edge between them
            int v = random.nextInt(vertices);
            int w = random.nextInt(vertices);
  
            // add an edge between them
            addEdge(v, w);
        }
    }
  
    // Method to compute the maximum number of possible
    // edges for a given number of vertices
    int computeMaxEdges(int numOfVertices)
    {
        // As it is an undirected graph
        // So, for a given number of vertices
        // there can be at-most v*(v-1)/2 number of edges
        return numOfVertices * ((numOfVertices - 1) / 2);
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is an Undirected graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
  
        // Add v to w's adjacency list
        adjacencyList.get(w).add(v);
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}


Java
// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
  
    public int vertices;
    public int edges;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for the number
        // of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
  
        // compute the maximum possible number of edges
        // and randomly choose the number of edges less than
        // or equal to the maximum number of possible edges
        this.edges
            = random.nextInt(computeMaxEdges(vertices)) + 1;
  
        // Creating an adjacency list representation
        // for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < edges; i++) {
            // randomly select two vertices to
            // create an edge between them
            int v = random.nextInt(vertices);
            int w = random.nextInt(vertices);
  
            // Check if there is already
            // an edge between v and w
            if (adjacencyList.get(v).contains(w)) {
                // Reduce the value of i
                // so that again v and w can be chosen
                // for the same edge count
                i = i - 1;
                continue;
            }
  
            // Add an edge between them if
            // not previously created
            addEdge(v, w);
        }
    }
  
    // Method to compute the maximum number of possible
    // edges for a given number of vertices
    int computeMaxEdges(int numOfVertices)
    {
        // As it is an undirected graph
        // So, for a given number of vertices V
        // there can be at-most V*(V-1)/2 number of edges
        return numOfVertices * ((numOfVertices - 1) / 2);
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is an Undirected graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
  
        // Add v to w's adjacency list
        // if v is not equal to w
        if (v != w)
            adjacencyList.get(w).add(v);
        // The above condition is important
        // If you don't apply the condition then
        // two self-loops will be created if
        // v and w are equal
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}


Java
// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
  
    public int vertices;
    public int edges;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for the
        // number of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
  
        // compute the maximum possible number of edges
        // and randomly choose the number of edges less than
        // or equal to the maximum number of possible edges
        this.edges
            = random.nextInt(computeMaxEdges(vertices)) + 1;
  
        // Creating an adjacency list
        // representation for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < edges; i++) {
            // Randomly select two vertices to
            // create an edge between them
            int v = random.nextInt(vertices);
            int w = random.nextInt(vertices);
  
            // Check if there is already an edge between v
            // and w
            if ((v == w)
                || adjacencyList.get(v).contains(w)) {
                // Reduce the value of i
                // so that again v and w can be chosen
                // for the same edge count
                i = i - 1;
                continue;
            }
  
            // Add an edge between them if
            // not previously created
            addEdge(v, w);
        }
    }
  
    // Method to compute the maximum number of
    // possible edges for a given number of vertices
    int computeMaxEdges(int numOfVertices)
    {
        // As it is a directed graph
        // So, for a given number of vertices
        // there can be at-most v*(v-1) number of edges
        return numOfVertices * (numOfVertices - 1);
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is a directed graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}


Java
// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
    // Number of vertices
    public int vertices;
  
    // p represents the probability
    public float p;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for the
        // number of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
        // p is the probability that there
        // is an edge between any two vertices
        // say, 0.4 is the probability that there
        // is an edge between any two vertices
        this.p = random.nextFloat();
  
        // Print the probability p
        System.out.println(
            "The probability that there is an edge"
            + " between any two vertices is : " + p);
  
        // Creating an adjacency list
        // representation for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < vertices; i++) {
            for (int j = 0; j < vertices; j++) {
                // edgeProbability is a random number
                // between 0.0 and 1.0
                // If the randomly chosen number
                // edgeProbability is less than
                // the probability of an edge p,
                // say, edgeProbability = 0.2 which is less
                // than p = 0.4, then add an edge between the
                // vertex i and the vertex j
                float edgeProbability = random.nextFloat();
                if (edgeProbability < p)
                    addEdge(i, j);
            }
        }
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is a directed graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}


输出
The generated random graph :
0 -> { 7 , 10 , 7 , 2 , 8 , 7 , 1}
1 -> { 5 , 8 , 8 , 3 , 4 , 12 , 8 , 7 , 9 , 0 , 7}
2 -> { 2 , 2 , 7 , 0 , 2 , 2 , 11 , 12 , 3 , 9 , 4 , 2 , 2 , 12 , 5}
3 -> { 6 , 10 , 1 , 12 , 11 , 2 , 10 , 10 , 3 , 3 , 5}
4 -> { 1 , 8 , 6 , 8 , 8 , 2 , 5 , 11}
5 -> { 1 , 5 , 5 , 8 , 4 , 2 , 11 , 3}
6 -> { 3 , 9 , 12 , 4 , 10 , 8 , 9}
7 -> { 0 , 2 , 0 , 12 , 1 , 7 , 7 , 12 , 0 , 8 , 1}
8 -> { 5 , 1 , 1 , 0 , 1 , 4 , 4 , 4 , 6 , 11 , 7}
9 -> { 6 , 12 , 1 , 2 , 9 , 9 , 6 , 9 , 9}
10 -> { 3 , 0 , 3 , 3 , 10 , 10 , 6}
11 -> { 3 , 2 , 12 , 8 , 4 , 5}
12 -> { 7 , 3 , 9 , 1 , 6 , 11 , 2 , 7 , 2}

每次运行上述程序时,您都会得到一个不同的无向图。

2.去除边缘的重复

在运行时检查边是否已经存在。使用这种方法算法 1 的复杂度会增加,但内存中的优化会增加。

下面是上述方法的实现:

Java

// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
  
    public int vertices;
    public int edges;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for the number
        // of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
  
        // compute the maximum possible number of edges
        // and randomly choose the number of edges less than
        // or equal to the maximum number of possible edges
        this.edges
            = random.nextInt(computeMaxEdges(vertices)) + 1;
  
        // Creating an adjacency list representation
        // for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < edges; i++) {
            // randomly select two vertices to
            // create an edge between them
            int v = random.nextInt(vertices);
            int w = random.nextInt(vertices);
  
            // Check if there is already
            // an edge between v and w
            if (adjacencyList.get(v).contains(w)) {
                // Reduce the value of i
                // so that again v and w can be chosen
                // for the same edge count
                i = i - 1;
                continue;
            }
  
            // Add an edge between them if
            // not previously created
            addEdge(v, w);
        }
    }
  
    // Method to compute the maximum number of possible
    // edges for a given number of vertices
    int computeMaxEdges(int numOfVertices)
    {
        // As it is an undirected graph
        // So, for a given number of vertices V
        // there can be at-most V*(V-1)/2 number of edges
        return numOfVertices * ((numOfVertices - 1) / 2);
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is an Undirected graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
  
        // Add v to w's adjacency list
        // if v is not equal to w
        if (v != w)
            adjacencyList.get(w).add(v);
        // The above condition is important
        // If you don't apply the condition then
        // two self-loops will be created if
        // v and w are equal
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}
输出
The generated random graph :
0 -> { 7 , 0 , 9 , 16 , 8 , 3 , 14 , 17 , 10 , 1 , 6 , 11 , 5 , 4 , 12}
1 -> { 17 , 8 , 6 , 12 , 9 , 11 , 13 , 5 , 15 , 0 , 2 , 7 , 16 , 3}
2 -> { 11 , 16 , 9 , 8 , 6 , 13 , 17 , 4 , 2 , 14 , 1 , 7 , 10}
3 -> { 4 , 8 , 13 , 10 , 12 , 17 , 0 , 15 , 16 , 3 , 7 , 6 , 5 , 1}
4 -> { 3 , 17 , 5 , 15 , 16 , 8 , 2 , 7 , 13 , 6 , 9 , 4 , 11 , 14 , 12 , 0}
5 -> { 10 , 17 , 6 , 16 , 4 , 9 , 14 , 13 , 8 , 1 , 3 , 5 , 0 , 15 , 7}
6 -> { 5 , 2 , 8 , 1 , 15 , 16 , 12 , 14 , 4 , 6 , 3 , 0 , 17 , 10 , 7}
7 -> { 9 , 11 , 0 , 15 , 7 , 4 , 10 , 13 , 17 , 16 , 3 , 8 , 1 , 6 , 2 , 5 , 12}
8 -> { 16 , 3 , 2 , 1 , 17 , 6 , 13 , 0 , 15 , 4 , 14 , 5 , 7 , 12 , 9 , 11}
9 -> { 7 , 10 , 2 , 9 , 0 , 1 , 5 , 17 , 16 , 4 , 12 , 11 , 13 , 8}
10 -> { 11 , 5 , 9 , 3 , 12 , 7 , 13 , 10 , 16 , 14 , 17 , 0 , 15 , 6 , 2}
11 -> { 2 , 10 , 17 , 12 , 7 , 15 , 16 , 11 , 1 , 13 , 14 , 4 , 9 , 0 , 8}
12 -> { 12 , 11 , 3 , 1 , 6 , 10 , 16 , 15 , 8 , 9 , 4 , 13 , 0 , 7}
13 -> { 14 , 3 , 17 , 15 , 2 , 8 , 7 , 10 , 5 , 1 , 4 , 11 , 9 , 13 , 12}
14 -> { 13 , 5 , 2 , 8 , 6 , 0 , 10 , 11 , 4 , 17 , 15}
15 -> { 13 , 11 , 17 , 4 , 7 , 6 , 8 , 3 , 1 , 12 , 10 , 16 , 14 , 5}
16 -> { 2 , 8 , 5 , 0 , 4 , 6 , 11 , 12 , 9 , 3 , 10 , 7 , 17 , 15 , 1}
17 -> { 1 , 11 , 5 , 4 , 13 , 8 , 15 , 3 , 2 , 9 , 7 , 0 , 16 , 10 , 14 , 6}

现在输出的无向图在相同顶点之间不包含任何多条边。尽管该图可能包含自循环(但现在每个顶点只有一个)。但是,如果你想生成没有自循环的无向图,那么你可以在上面的代码中添加另一个条件

//Check if there is already an edge between v and w or v and w are equal
if((v == w ) || adjacencyList.get(v).contains(w)) {
    //Reduce the value of i
    //so that again v and w can be chosen  
        //for the same edge count
        i = i - 1;
        continue;
}

现在,生成的无向图中不允许有自环和多条边。

为什么对于无向图中给定数量的顶点 V ,最多可以有 V*((V-1)/2) 条边?

假设,有向图中有 V 个顶点。现在,如果图不包含任何自环和多条边,那么每个顶点可以有 (V-1) 条边与其他 (V-1) 顶点。因此,V 个顶点最多可以有 V*(V – 1) 个顶点。如果图包含自环,则最大可能的边数为 V 2 (没有多条边)。因为,每个顶点也可以与自己有一条边。因此,对于无向图,最大可能的边数是 V*(V – 1)/2,因为边没有任何方向。

到目前为止,我们已经创建了随机无向图,但是,如果您想创建随机有向图,那么我们必须对上面实现的代码进行一些更改——

  1. 对于随机选择的顶点数 V,可能的最大边数现在为 V*(V – 1)(没有多边和自环)。
  2. 对于没有自环的有向图,我们需要检查随机选择的两个顶点是否相等。如果不是,则仅在它们之间创建边缘。
  3. 对于没有多边的有向图,我们需要检查随机选择的顶点之间是否已经存在边。如果不存在这样的边,则仅在它们之间创建边。
  4. 在两个顶点之间创建边时,我们只需要将 w 添加到 v 的邻接表中,而不是将 v 添加到 w 的邻接表中,因为这是一个有向图。

这是创建没有多边和自循环的随机有向图的代码 -

Java

// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
  
    public int vertices;
    public int edges;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for the
        // number of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
  
        // compute the maximum possible number of edges
        // and randomly choose the number of edges less than
        // or equal to the maximum number of possible edges
        this.edges
            = random.nextInt(computeMaxEdges(vertices)) + 1;
  
        // Creating an adjacency list
        // representation for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < edges; i++) {
            // Randomly select two vertices to
            // create an edge between them
            int v = random.nextInt(vertices);
            int w = random.nextInt(vertices);
  
            // Check if there is already an edge between v
            // and w
            if ((v == w)
                || adjacencyList.get(v).contains(w)) {
                // Reduce the value of i
                // so that again v and w can be chosen
                // for the same edge count
                i = i - 1;
                continue;
            }
  
            // Add an edge between them if
            // not previously created
            addEdge(v, w);
        }
    }
  
    // Method to compute the maximum number of
    // possible edges for a given number of vertices
    int computeMaxEdges(int numOfVertices)
    {
        // As it is a directed graph
        // So, for a given number of vertices
        // there can be at-most v*(v-1) number of edges
        return numOfVertices * (numOfVertices - 1);
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is a directed graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}
输出
The generated random graph :
0 -> { 4 , 3 , 5 , 15 , 13}
1 -> { 2 , 6 , 9 , 7 , 12 , 4}
2 -> { 4 , 7 , 13 , 12 , 11 , 9}
3 -> { 5 , 2 , 15 , 10}
4 -> { 2 , 16 , 8 , 7}
5 -> { 7 , 16 , 10 , 0 , 9}
6 -> { 12 , 11 , 14 , 2 , 5 , 16}
7 -> { 8 , 11 , 12 , 3 , 16 , 10 , 13}
8 -> { 6 , 7 , 15 , 12 , 0 , 5 , 9 , 16}
9 -> { 3 , 4 , 16}
10 -> { 9 , 12 , 16 , 6}
11 -> { 10 , 8 , 15 , 9 , 12 , 13}
12 -> { 5 , 7 , 10 , 1}
13 -> { 16 , 2 , 10 , 3 , 1}
14 -> { 3 , 15 , 8 , 12 , 7 , 1}
15 -> { 9 , 2 , 1 , 14 , 8 , 4}
16 -> { 1 , 2 , 9 , 3 , 10 , 7}

上面的输出图是一个没有自环和多边的随机有向图。

算法 1 基于随机选择多个顶点 v 和边 e 并创建包含 v 个顶点和 e 边的图。我们要讨论的第二个算法是基于 Erdos-Renyi G(v,p) 随机图模型。

算法2(Erdos-Renyi G(v,p)模型):

Erdos-Renyi G(v,p) 模型(以 Paul Erdos 和 Alfred Renyi 命名)被认为是最早尝试描述随机网络的模型之一,是最流行的生成随机图的模型之一。该模型生成一个随机图,其中包含 v 个顶点和任意两个顶点之间的边,概率为 p。 p 是任意两个顶点之间存在边的概率。

  1. 随机选择多个顶点和概率 p。 p 的值在 0.0 到 1.0 之间。
  2. 迭代每对顶点并生成一个介于 0.0 和 1.0 之间的随机数。如果随机选择的数字小于概率 p,则在该对的两个顶点之间添加一条边。图中的边数完全取决于概率 p。
  3. 打印图表。

下面是上述方法的实现:

Java

// Create a Random Graph Using
// Random Edge Generation in Java
import java.util.*;
import java.io.*;
  
public class GFGRandomGraph {
    // Number of vertices
    public int vertices;
  
    // p represents the probability
    public float p;
  
    // Set a maximum limit to the vertices
    final int MAX_LIMIT = 20;
  
    // A Random instance to generate random values
    Random random = new Random();
    // An adjacency list to represent a graph
    public List > adjacencyList;
  
    // Creating the constructor
    public GFGRandomGraph()
    {
        // Set a maximum limit for the
        // number of vertices say 20
        this.vertices = random.nextInt(MAX_LIMIT) + 1;
        // p is the probability that there
        // is an edge between any two vertices
        // say, 0.4 is the probability that there
        // is an edge between any two vertices
        this.p = random.nextFloat();
  
        // Print the probability p
        System.out.println(
            "The probability that there is an edge"
            + " between any two vertices is : " + p);
  
        // Creating an adjacency list
        // representation for the random graph
        adjacencyList = new ArrayList<>(vertices);
        for (int i = 0; i < vertices; i++)
            adjacencyList.add(new ArrayList<>());
  
        // A for loop to randomly generate edges
        for (int i = 0; i < vertices; i++) {
            for (int j = 0; j < vertices; j++) {
                // edgeProbability is a random number
                // between 0.0 and 1.0
                // If the randomly chosen number
                // edgeProbability is less than
                // the probability of an edge p,
                // say, edgeProbability = 0.2 which is less
                // than p = 0.4, then add an edge between the
                // vertex i and the vertex j
                float edgeProbability = random.nextFloat();
                if (edgeProbability < p)
                    addEdge(i, j);
            }
        }
    }
  
    // Method to add edges between given vertices
    void addEdge(int v, int w)
    {
        // Note: it is a directed graph
  
        // Add w to v's adjacency list
        adjacencyList.get(v).add(w);
    }
  
    public static void main(String[] args)
    {
        // Create a GFGRandomGraph object
        GFGRandomGraph randomGraph = new GFGRandomGraph();
  
        // Print the graph
        System.out.println("The generated random graph :");
        for (int i = 0;
             i < randomGraph.adjacencyList.size(); i++) {
            System.out.print(i + " -> { ");
  
            List list
                = randomGraph.adjacencyList.get(i);
  
            if (list.isEmpty())
                System.out.print(" No adjacent vertices ");
            else {
                int size = list.size();
                for (int j = 0; j < size; j++) {
  
                    System.out.print(list.get(j));
                    if (j < size - 1)
                        System.out.print(" , ");
                }
            }
  
            System.out.println("}");
        }
    }
}
输出
The probability that there is an edge between any two vertices is : 0.8065328
The generated random graph :
0 -> { 0 , 1 , 2 , 3 , 5 , 6 , 7 , 8 , 11 , 13 , 14 , 15 , 16 , 17}
1 -> { 0 , 2 , 3 , 5 , 6 , 7 , 8 , 9 , 10 , 12 , 14 , 15 , 16 , 17}
2 -> { 0 , 1 , 2 , 3 , 4 , 5 , 7 , 8 , 9 , 10 , 11 , 13 , 15 , 16 , 17}
3 -> { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17}
4 -> { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17}
5 -> { 0 , 1 , 3 , 7 , 9 , 13 , 14 , 15 , 16 , 17}
6 -> { 1 , 2 , 3 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17}
7 -> { 0 , 1 , 2 , 3 , 4 , 5 , 7 , 8 , 9 , 10 , 12 , 13 , 15 , 16 , 17}
8 -> { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17}
9 -> { 0 , 1 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17}
10 -> { 0 , 1 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 17}
11 -> { 0 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 , 10 , 12 , 14 , 15 , 16 , 17}
12 -> { 0 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 11 , 12 , 13 , 14 , 15 , 16 , 17}
13 -> { 0 , 1 , 2 , 3 , 4 , 5 , 7 , 9 , 10 , 12 , 13 , 14 , 15 , 16 , 17}
14 -> { 1 , 2 , 3 , 4 , 6 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 17}
15 -> { 0 , 1 , 3 , 4 , 6 , 7 , 8 , 9 , 10 , 11 , 13 , 14 , 15 , 16}
16 -> { 0 , 1 , 2 , 3 , 5 , 6 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16}
17 -> { 0 , 1 , 2 , 3 , 4 , 5 , 7 , 9 , 10 , 13 , 14 , 15 , 16}

上述程序生成带有自循环的随机有向图。