1
prim的Algo的朴素实现应该给出O(mn)的运行时间。但是我在Prim函数中有3个循环(使得运行时间立方体)。我哪里错了?Prim算法的以下代码的运行时间
void Graph::Prim (const int src) //Nodes start from 1 to n-1
{ // n = Nodes+1.
NodeExplored[src] = true;
for(int i=1; i<n-1;++i) //n-2 iterations
{
int minIndex;
int minEW = 10000;
int svIndex;
for(int sv =1;sv < n;sv++) //To check for Explored nodes
{
if(NodeExplored[sv]==true)
{
for(auto i = G[sv].begin();i!= G[sv].end();++i)
{ //To scan through the edges from sv.
int dv = i->first; //Destination Vertex
int ew = i->second; //Edge Weight b/w sv & dv.
if(NodeExplored[dv]==false && ew < minEW)
{
minEW = ew;
minIndex = dv;
svIndex = sv;
}
}
}
}
NodeExplored[minIndex] = true;
MST[svIndex].push_back(make_pair(minIndex,minEW));
MST[minIndex].push_back(make_pair(svIndex,minEW));
}
取而代之的是队列,将向量(将存储在“探索节点”)工作吗?因此,不是扫描所有顶点[for(int sv = 1; sv