【发布时间】:2016-04-24 18:57:20
【问题描述】:
基本上我生成了一个 adj_matrix,我想从 adj_matrix 中创建一个 adj_list...但是我一直收到一个错误消息,说“与呼叫不匹配...” 我在没有 aPair 的情况下尝试了它我仍然得到同样的错误我似乎无法弄清楚我的问题是什么。谁能告诉我为什么列表不起作用?列表在代码的最后
int **gen_random_graph(int n)
{
srand(time(0));
int **adj_matrix = new int*[n];
for(int i = 0; i < n; i++)
{
for (int j = i; j < n; j++) //generating a N x N matrix based on the # of vertex input
{
adj_matrix[i] = new int[n];
}
}
for(int u = 0; u < n; u++)
{
for (int v = u; v < n; v++)
{
bool edgeOrNot = rand() % 2; //decide whether it has an edge or not
adj_matrix[u][v] = adj_matrix[v][u] = edgeOrNot;
if(adj_matrix[u][v] == true)
{
adj_matrix[v][u] = true;
if(u == v) //We can't have i = j in an undirected graph so we set it to false
{
adj_matrix[u][v] = -1;
}
}
else //if adj_matrix[u][v] is false set the symmetry to be false
{
adj_matrix[v][u] = adj_matrix[u][v] = -1;
}
}
}
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 weight = rand() % 10 + 1;
adj_matrix[i][j] = adj_matrix[j][i] = weight;
cout << " ( " << i << "," << j << " ) " << "weight: " << adj_matrix[i][j] << endl;
}
}
}
for(int i = 0; i < n; i++)
{
vector<int> adj_list;
for(int j = i; j < n; j++)
{
if(adj_matrix[i][j] > 0)
{
int weight = adj_matrix[i][j];
adj_list.push_back(j);
cout << adj_list[i] << " " << endl;
}
}
}
print(n,adj_matrix);
return (adj_matrix);
}
【问题讨论】:
-
可以调用adj_list吗?
-
嗯,我没有为 adj_list 创建函数,所以不,我认为它不能被调用。基本上我生成了一个 adj_matrix,现在我必须从 adj 矩阵创建 adj_list
-
hmmmm 但我只需要来自 adj_matrix[i][j] 的权重有没有办法解决这个问题?
-
请提供完整的错误信息。
标签: c++ adjacency-list