[英]generating random graph undirected
I need help whenever i try to make the adj_matrix[u][v] == adj_matrix[v][u] for the same weight it doesn't apply and can anyone tell me if i am on the correct path ?每当我尝试使 adj_matrix[u][v] == adj_matrix[v][u] 的重量相同时,我都需要帮助,它不适用,谁能告诉我我是否在正确的道路上?
and is there a way to set the matrix without edge to an X instead of a 0?有没有办法将没有边缘的矩阵设置为 X 而不是 0?
int gen_random_graph(int n)
{
srand(time(0));
int adj_matrix[n][n];
for(int u = 0; u < n; u++)
{
for (int v = u; v < n; v++) //generating a N x N matrix based on the # of vertex input
{
bool edgeOrNot = rand() % 2; //decide whether it has an edge or not
adj_matrix[u][v] = adj_matrix[v][u] = edgeOrNot;
cout << u << " " << v << " " << adj_matrix[u][v] << endl;
if(adj_matrix[u][v] == true)
{
adj_matrix[v][u] = true;
if(u == v) //We can't have i = j in an undirected graph
{
adj_matrix[u][v] = -1;
}
cout << u << " " << v << " " << adj_matrix[u][v] << endl;
}
else
{
adj_matrix[v][u] = -1;
cout << u << " " << v << " " << adj_matrix[u][v] << "else" << endl;
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++) //create the N x N with edges and sets the weight between the edge randomly
{
if(adj_matrix[i][j] == true)
{
int weight1 = rand()%10 + 1;
adj_matrix[i][j] = adj_matrix[j][i] = weight1;
cout << " ( " << i << "," << j << " ) " << "weight: " << adj_matrix[i][j] << endl;
}
}
}
}
int main()
{
int N;
cout << "Enter number of vertices" << endl;
cin >> N;
gen_random_graph(N);
return 0;
}
Instead 'X' I recommend use -1 value.我建议使用 -1 值代替 'X'。 There's a sample:
有一个示例:
void gen_random_graph(int n) {
srand(time(0));
int adj_matrix[n][n];
for(int u = 0; u < n; u++) {
for (int v = u; v < n; v++) { //you don't need to calculate weight twice so loop starts from u
if(v == u) {
adj_matrix[u][v] = -1;
}
else {
int weight = rand() % 10 - 1;
adj_matrix[u][v] = adj_matrix[v][u] = weight;
}
}
}
}
Now you can check value of edge.现在您可以检查边缘的值。 For -1 it doesn't exist.
对于 -1 它不存在。
I am not sure of your algorithm among other things.我不确定你的算法等等。 It's one thing to use
int adj_matrix
to store bool
s, but storing 'X'
, a char
is something I would avoid.使用
int adj_matrix
存储bool
是int adj_matrix
,但存储'X'
,我会避免使用char
。
For storing the same weights I would try:为了存储相同的权重,我会尝试:
const int weight = rand() % 10 + 1;
adj_matrix[u][v] = weight;
adj_matrix[v][u] = weight;
The compiler may try to invoke rand()
twice, once for each statement.编译器可能会尝试调用
rand()
两次,每个语句调用一次。 In the above you invoke rand()
only once.在上面你只调用
rand()
一次。
In general I would use STL containers such as std::vector<int>
, and std::vector<std::vector<int>>
, although the latter is not ideal from memory footprint point of view.一般来说,我会使用 STL 容器,例如
std::vector<int>
和std::vector<std::vector<int>>
,尽管后者从内存占用的角度来看并不理想。
Instead of 'X'
I would use std::numeric_limits<int>::min
or max
, which may not be equal to rand() % 10 + 1
.我会使用
std::numeric_limits<int>::min
或max
代替'X'
,它们可能不等于rand() % 10 + 1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.