【发布时间】:2013-01-18 13:35:19
【问题描述】:
我编写了一个算法来确定“无向图是否是树”
假设:图 G 表示为邻接表,其中我们已经知道顶点数为 n
Is_graph_a_tree(G,1,n) /* using BFS */
{
-->Q={1} //is a Queue
-->An array M[1:n], such that for all i, M[i]=0 /* to mark visited vertices*/
-->M[1]=1
-->edgecount=0 // to determine the number of edges visited
-->While( (Q is not empty) and (edgecount<=n-1) )
{
-->i=dequeue(Q)
-->for each edge (i,j) and M[j] =0 and edgecount<=n-1
{
-->M[j]=1
-->Q=Q U {j}
-->edgecount++
}
}
If(edgecount != n-1)
--> print “G is not a tree”
Else
{
-->If there exists i such that M[i]==0
Print “ G is not a tree”
Else
Print “G is tree”
}
}
对吗??
这个算法的时间复杂度是Big0h(n)吗??
【问题讨论】:
标签: performance time-complexity graph-algorithm