我有一个问题,我正在研究Kruskal算法的实现,我完成了它,并且它正在工作,但是有一个问题,我使用了“静态”数组2D,(由用户输入,但是限于100)。 如何在动态分配或使用向量转换它? 我更喜欢矢量,但我不知道如何在矢量矢量中“修改和插入”项目。 有人可以帮帮我吗? 谢谢, 再见
有代码:
class kruskal
{
private:
int n; //no of nodes
int noe; //no edges in the graph
int graph_edge[100][4];
int tree[10][10];
int sets[100][10];
int top[100];
public:
int read_graph();
void initialize_span_t();
void sort_edges();
void algorithm();
int find_node(int );
void print_min_span_t();
};
int kruskal::read_graph()
{
cout<<"This program implements the kruskal algorithm\n";
cout<<"Enter the no. of nodes in the undirected weighted graph";
cin>>n;
noe=0;
cout<<"Enter the weights for the following edges ::\n";
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout<<i<<" , "<<j;
int w;
cin>>w;
if(w!=0)
{
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
}
}
}
// print the graph edges
cout<<"\n\nThe edges in the given graph are::\n";
for(int i=1;i<=noe;i++)
{
cout<<" < "<<graph_edge[i][1]
<<" , "<<graph_edge[i][2]
<<" > "<<graph_edge[i][3]<<endl;
}
}
void kruskal::sort_edges()
{
/**** Sort the edges using bubble sort in increasing order**************/
for(int i=1;i<=noe-1;i++)
{
for(int j=1;j<=noe-i;j++)
{
if(graph_edge[j][3]>graph_edge[j+1][3])
{
int t=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
t=graph_edge[j][2];
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
t=graph_edge[j][3];
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
}
}
}
// print the graph edges
cout<<"\n\nAfter sorting the edges in the given graph are::\n";
for(int i=1;i<=noe;i++)
cout<<""<< graph_edge[i][1]
<<" , "<<graph_edge[i][2]
<<" > ::"<<graph_edge[i][3]<<endl;
}
void kruskal::algorithm()
{
// ->make a set for each node
for(int i=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
cout<<"\nThe algorithm starts ::\n\n";
for(int i=1;i<=noe;i++)
{
int p1=find_node(graph_edge[i][1]);
int p2=find_node(graph_edge[i][2]);
if(p1!=p2)
{
cout<<"The edge included in the tree is ::"
<<" < "<<graph_edge[i][1]<<" , "
<<graph_edge[i][2]<<" > "<<endl<<endl;
tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
// Mix the two sets
for(int j=1;j<=top[p2];j++)
{
top[p1]++;
sets[p1][top[p1]]=sets[p2][j];
}
top[p2]=0;
}
else
{
cout<<"Inclusion of the edge "
<<" < "<<graph_edge[i][1]<<" , "
<<graph_edge[i][2]<<" > "<<"forms a cycle so it is removed\n\n";
}
}
}
int kruskal::find_node(int n)
{
for(int i=1;i<=noe;i++)
{
for(int j=1;j<=top[i];j++)
{
if(n==sets[i][j])
return i;
}
}
return -1;
}
void kruskal::print_min_span_t()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<tree[i][j]<<"\t";
cout<<endl;
}
}
答案 0 :(得分:2)
您可以创建结构或类边;
struct Edge {
int v, u, w;
Edge(int v, int u, int w) : v(v), u(u), w(w) {}
bool operator < (const Edge& c) const{
if (w != c.w)
return w < c.w;
if (v != c.v)
return v < c.v;
return u < c.u;
}
}
然后你可以将图形存储为边缘矢量
vector<edge> es;
....
cin >> v >> u >> w;
if (w != 0) {
es.push_back(Edge(v, u, w));
}
另外,因为运营商&lt;在Edge类中定义,您可以使用内置的C ++排序算法
对边进行排序#include <algorithm>
....
std::sort(es.begin(), es.end());
答案 1 :(得分:0)
像
这样的东西std::vector<std::vector<int>> vvi;
vvi.push_back(std::vector<int>()); // Push back a new vector, as vvi[0]
vvi[0].push_back(123); // Push back a new integer, as vvi[0][0]
std::cout << "vvi[0][0] = " << vvi[0][0] << '\n'; // Prints 123