Q.) Write a ALGORITHM to show whether the GRAPH is Connected or Not.
Sol)
# Comp( Graph )
{
For each vertex v belongs to Vertices[Graph]
{
make-set(v)
}
For each edge (u,v) belongs to Edges[Graph]
{
if(find-set(u) not equal to find-set(v)
{
Union(u,v)
}
}
}
# Connected-Comp(u,v)
{
if (find-set(u)=find-set(v))
{
print "CONNECTED"
}
else
print "NOT CONNECTED"
}
No comments:
Post a Comment