Undirected graph is a tree or not?

https://ide.codingblocks.com/s/447834,

bhaiya code check karna kya dikkat aa rhi hai ?

output sahi nhi aa rhi

hello @CODER_JATIN

bro output shai hai,
tumhara graph me cycle ban rahi hai , isiliye wo aise output de raha hai.

0-1-2-3 - 0 ye cycle ban rahi hai , dhyan se dekho

bhaiya ye input deke dekho

graph g(5);

g.addEdge(0,1);

g.addEdge(1,2);

g.addEdge(2,3);

g.addEdge(3,0);

g.addEdge(3,4);

ha wahi diya hai
ss
ye graph ban raha hai, isme dhyan se dekho cyle ban rahi hai ,isilye output aisa aa raha hai

hanji hanji bhaiya , cycle bn rhi hai

graph g(3);

g.addEdge(0,1);

g.addEdge(1,2);

g.addEdge(2,3);

bhaiya agr ham ye input de to tree bnna chahiye , lekin seg fault aa rha hai

4 vertex hai na total(0,1,2,3) ,
to g(4) hona chaiye na

okay okay ,
lekin ab bhi (galat output aari bhaiya )

ok mai check karta hun

ab dekho ->

tum parent[node] ki jagah pe parent[nbr] check kar raHE THE

bhaiya parent[nbr] hini aana chahiye?

HUM abhi node pe hai aur nbr jo hai wo neigbhour hai.

tu hame ye check karna hai , ki node ka jo parent hai wo nbr to nai,
agar nbr parent hai ,iska matlab node pe hum nbr ke through aaye hai , to ye cyle nahi bana sakta .

agar aisa nahi hai to iska matlab ki cycle hai, kyunki nbr pe koi pehle hi visit kar chuka hai

bhaiya jese maanlo
(0,1) (1,2) (2,3) (3,0) hai
to agar mai 0 se start karta hu to o ke neighbour hue 1 or 3 , in dono ko mene visited mark kar diya or parent[1] mai bhi or parent[3] mai bhi dono mai mene 0 daal diya ,

ab next mai 1 ke neighbours pe jaunga , vo hai 0 or 2 , to ab mai ye check krunga naa ki parent[1] 0 ya 2 hai to matlab cycle nahi bna rha

jab tum 1 pe jaoege tab, uske do neighbour hai 0 aur 2.

0 already visited , to hame ye check karna hai ki ye 0 jo hai wo 1 ka parent hai ya nahi, agar parenrt hai tab to koi dikkat nahi aage badho otherwise agar ye parent nahi hai aur phir bhi visited hai to cycle hai.

Hanji Hanji thik hai bhaiya ,
smjh gya
Thank you so Much :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.