简体   繁体   English

生成随机图

[英]Generating random Graph

Have someone input an integer N as the number of vertices in the graph.让某人输入一个整数 N 作为图中的顶点数。

  • Assign random weights on each edges ranging from 1 to 10. Not all possible edges are present though!在每条边上分配从 1 到 10 的随机权重。虽然并非所有可能的边都存在! As in the above example, represented an absent edge by an X.如上例所示,用 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.返回一对 (M,L),其中 M 和 L 分别是您生成的(相同)随机图的矩阵和列表表示。
  • 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 .图可以表示为 N x N 邻接矩阵(正如您已经设置的那样),其中matrix[i][j]中的值对应于将顶点i连接到顶点j的边的权重。 A zero corresponds to there not being a connection between i and j .零对应于ij之间没有连接。 If matrix[i][j] == matrix[j][i] , then you have an undirected graph.如果matrix[i][j] == matrix[j][i] ,那么你有一个无向图。 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:在每条边存在或不存在的情况下(即权重为 0 或 1),您可以具有以下内容:

无向图

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.同样,第二个图是有向的,因为矩阵不是对称的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM