您的位置:首页 > 理论基础 > 数据结构算法

数据结构之图-无权图的最小生成树(7)

2013-03-11 13:15 561 查看
1 Unweighted Graphs 

For unweighted Graphs, a minimum  spanning tree(MST) means that a graph with minimum number of edges to connect all the vertices.

the number of edges E in a minimum spanning tree is always one less than the number of vertices V: E = V – 1.

The path of the
DFS(depth-first search) through the graph must be a minimum spanning tree.




1.1 Java implement 

public void mst()  // minimum spanning tree (depth first)
{                                  // start at 0
vertexList[0].wasVisited = true;   // mark it
theStack.push(0);                  // push it

while( !theStack.isEmpty() )       // until stack empty
{                               // get stack top
int currentVertex = theStack.peek();
// get next unvisited neighbor
int v = getAdjUnvisitedVertex(currentVertex);
if(v == -1)                     // if no more neighbors
theStack.pop();              //    pop it away
else                            // got a neighbor
{
vertexList[v].wasVisited = true;  // mark it
theStack.push(v);                 // push it
// display edge
displayVertex(currentVertex);     // from currentV
displayVertex(v);                 // to v
System.out.print(" ");
}
}  // end while(stack not empty)

// stack is empty, so we're done
for(int j=0; j<nVerts; j++)          // reset flags
vertexList[j].wasVisited = false;
}  // end mst()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: