[英]Generating random Graph
Have someone input an integer N as the number of vertices in the graph.让某人输入一个整数 N 作为图中的顶点数。
#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
.零对应于
i
和j
之间没有连接。 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.