Generating random Graph

Have someone input an integer N as the number of vertices in the graph.

  • Assign random weights on each edges ranging from 1 to 10. Not all possible edges are present though! As in the above example, represented an absent edge by an X.
  • Return a pair (M,L), with M and L respectively being the matrix and list representation of the (same) random graph you are generating.
  • Use non-digit characters as vertex names, to avoid confusion with edge weights.
  • #include <iostream>
    #include <stdlib.h>
    using namespace std;
    void gen_random_graph(int n)
    {
        int adj_matrix[n][n];
        for(int u = 0; u < n; u++)
        {
            for (int v = 0; v < n; v++)
            {
                if(adj_matrix[u][v]==adj_matrix[v][u])
                {
                    adj_matrix[u][v] = rand() % 10 + 1;
                    cout << adj_matrix[u][v] << endl;
                }
            }
        }
    
    }
    
    int main()
    {
        int N;
        cout << "enter number of vertices" << endl;
        cin >> N;
        gen_random_graph(N);
    
        return 0;
    }
    

    THis is my code so far. Is it generateing the weights ? and what does it mean i have to return a pair?


    A graph can be represented as an N x N adjacency matrix (as you have already set up), where the value in matrix[i][j] corresponds to the weight of the edge connecting vertex i to vertex j . A zero corresponds to there not being a connection between i and j . If matrix[i][j] == matrix[j][i] , then you have an undirected graph. Furthermore, a random graph would have random values as the edges between vertices.

    In the case where every edge either exists or doesn't exist (ie weights are either 0 or 1), you could have the following:

    无向图

    This image is stolen from the internet, so I take no credit for it. Note that you can easily confirm that the first graph is undirected because the adjacency matrix is symmetric. Likewise, the second graph is directed because the matrix is NOT symmetric.

    链接地址: http://www.djcxy.com/p/37314.html

    上一篇: 在特定条件下生成边的组合

    下一篇: 生成随机图