Program to check if the given graph is a tree or not | faceprep

Program to check if the given graph is a tree or not | faceprep

The program to check if the given graph is a tree or not is discussed here.

Program to check if the given graph is a tree or not


An undirected graph is given as input,

The given graph is said to be a tree

  • The graph is connected.
  • When there are no cycles in the graph.

For example:


Program to check if the given graph is a tree or not

The above-given graph is not a tree because it has a cycle.



Program to check if the given graph is a tree or not

Algorithm to check if the given graph is a tree or not

  • Input the graph.
  • Check if the graph has any cycles in it.
  • If it has a cycle, then print “The given graph is not a tree”.
  • Otherwise, print”The given graph is a tree”.


face prep pro ad bannerClick here to learn more about FACE Prep PRO


Program to check if the given graph is a tree or not

@@coding::1@@


Recommended Programs


Program to check if the given graph is a tree or not

c