「無向グラフが木であるかどうか」を決定するアルゴリズムを書いた
前提:グラフ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)ですか?
コメントseviyor ..のためのThnx私はまた、M [j] = 1であるが、 "edgecount <= n-1"という条件を変更する必要があると思う...しかし、私は見ていない実際のアルゴリズムが失敗した場合カウンタの例を与えることができます – rakesh
まあ、出力は、(たとえツリーでなくても)接続されている入力グラフに当てはまると思います。 – seviyor
hmmm ...はいあなたは正しいです... thnx – rakesh