• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
September 28, 2023 |420 Views
SDE Sheet - Detect cycle in undirected graph
  Share   Like
Description
Discussion

This video is part of Graphs section under GFG SDE Sheet.

In this video, we are given, an undirected graph with V vertices and E edges, check whether it contains any cycle or not. Graph is in the form of adjacency list where adj[i] contains all the nodes ith node is having edge with.

Example :

Input:  
V = 5, E = 5
adj = {{1}, {0, 2, 4}, {1, 3}, {2, 4}, {1, 3}} 
Output: 1

Explanation:

1->2->3->4->1 is a cycle.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-

Article: https://www.geeksforgeeks.org/detect-cycle-undirected-graph/
Problem: https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More