Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Live Aptitude Training
Live Programming Training
Webinars
About Us

Edit
Reply




Edit

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

Published on 11 Mar 2020

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


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,

check if the graph is a tree or not

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

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".
  • Else, print"The given graph is a tree".


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


C++

Output
Input 1 - Enter the connections of graph 1 1 0 y 0 2 y 0 3 y 3 4 n Output 1 - The given graph is a tree Input 2 - Enter the connections of graph 2 1 2 y 0 1 y 0 2 y 0 3 y 3 4 n Output 2 - The given graph is not a tree


Recommended Programs






If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'c plus plus'
Articles Practice Exercises