Detect cycle in a Directed Graph | Using DFS | Graph Data Structure | DSA-One Course #78

  Рет қаралды 34,320

Anuj Bhaiya

Anuj Bhaiya

Күн бұрын

Hey guys, In this video, We're going to solve an interesting problem in Graphs known as Detect Cycle in a Directed Graph.
Pre-requisites:
DFS Video: • DFS Algorithm | Depth ...
Detect cycle in an Undirected Graph: • Detect Cycle in Undire...
Practice here: practice.geeks...
💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
🥳 Join our Telegram Community:
Telegram channel: telegram.me/re...
Telegram group: telegram.me/ds...
🚀 Follow me on:
Instagram: / anuj.kumar.sharma
Linkedin: / sharma-kumar-anuj
Twitter: / realanujbhaiya
📚 Complete DSA Playlist: • DSA-One Course - The C...
Complete Android Development Playlist: • Android Development Tu...
Hashtags:
#anujbhaiya #dsaone
Tags:
detect cycle in a directed graph
detect cycle in directed graph
cycle in directed graph
anuj bhaiya
cycle detection in directed graph
cycle detection in graph
graph data structure
detect cycle in graph
detect cycle in undirected graph
directed graph
graph dsa
cycle detection
finding cycle in a graph
anuj bhaiya java
cycle in graph
find cycle in graph
graph
detect cycle in a undirected graph
dfs graph
flattening a linked list
kangal vs pitbull+18
cycle detection dfs
cycle in a directed graph
cycle in a graph
detect a cycle in directed graph
directed acyclic graph
dsa
graph cycle detection
graph in dsa
graphs dsa
java anuj bhaiya
subarray with given sum
anuj bhai
anuj kumar sharma
bfs and dfs algorithm
connected graph
cycle detection in undirected graph
cycle in undirected graph
data structure and algorithm
data structures and algorithms
detect cycle in an undirected graph
dfs
dfs algorithm
dfs directed graph
dfs in graph
dfs in java
direct acyclic graph
dsa course
dsa one course
educative
graph data structure in java
graph dfs
graph problems
graph tutorial
graphs in java
java by anuj bhaiya
java graph
jeet hogi meri jeet yo yo honey singh
sliding window maximum
striver
tarjan algorithm
topological sort
topological sorting in graph
zig zag traversal

Пікірлер: 34
@prathmeshadsod629
@prathmeshadsod629 2 жыл бұрын
One of the best teacher I have ever seen
@rahulsahay19
@rahulsahay19 2 жыл бұрын
Good. My graph understanding with your videos are getting much clearer and robust. Thanks.
@Mani_OnFire
@Mani_OnFire 5 ай бұрын
Superb explanation sir❤🎉
@_sf_editz1870
@_sf_editz1870 2 ай бұрын
Simple clear and crisp
@Miss__Nikita_K
@Miss__Nikita_K Күн бұрын
only explanation I could grasp!
@designwithrehana
@designwithrehana 2 жыл бұрын
You are doing amazing. Keep doing👌👌
@AnujBhaiya
@AnujBhaiya 2 жыл бұрын
Thank you 😊
@vivekchaudhary3794
@vivekchaudhary3794 29 күн бұрын
Very clear explanation 🎉❤❤❤
@aparnakanchi
@aparnakanchi Жыл бұрын
What an explanation bhaiyaa!!!😇😇Truly Amazing
@_SOHAMSAMANTA
@_SOHAMSAMANTA 2 жыл бұрын
@Anuj Bhaiya how many more videos left on Graph ? Please reply!
@anjaliyadav9360
@anjaliyadav9360 2 жыл бұрын
Awesome Explanation!
@suar_pilla
@suar_pilla 7 ай бұрын
Using DFS Iterative... Simple: public boolean hasCycleDirectedGraph(T src) { Set vertices = adjList.keySet(); //This will contain all the vertices that are processed/visited previously Set processedVertices = new HashSet(); //Check all paths even the disconnected ones for (T vertex : vertices) { //If the vertex is visited/processed previously then ignore if (!processedVertices.contains(vertex)) { HashSet visited = new HashSet(); boolean foundCycle = hasCycleDFSDirectedGraph(vertex, visited); if (foundCycle == false) { processedVertices.addAll(visited); //if no cycle found so far continue, with other paths if available hasCycleDFSDirectedGraph(src, visited); } else return true; } } //No cycle found so far after checking all vertices return false; } private boolean hasCycleDFSDirectedGraph(T src, Set visited) { Stack stack = new Stack(); stack.add(src); visited.add(src); while (!stack.isEmpty()) { T pop = stack.pop(); List neighbors = adjList.getOrDefault(pop, Collections.emptyList()); for (T neighbor : neighbors) { if (!visited.contains(neighbor)) { visited.add(neighbor); stack.add(neighbor); } else // if any path is leading to a previously visited node, that means it has a cycle return true; } } return false; }
@veerendrashukla
@veerendrashukla 2 жыл бұрын
Very nice explanation, Anuj!
@saurabhmina8671
@saurabhmina8671 2 жыл бұрын
showing TLE
@chandnibhatia1211
@chandnibhatia1211 2 жыл бұрын
One more alternative solution : push the elements in stack ..if there is a cycle ...the stack wil contain same element stack.contains(i) will return true ..if thats the case ..there is a cycle or else continue till the array length exhausts and if stack doesnt contain any i element ...that means no cycle
@tanaykamath1415
@tanaykamath1415 2 жыл бұрын
contains will take O(n) which is unnecessary
@jayaswalrajp
@jayaswalrajp 2 жыл бұрын
very nice explanation.. thank you so much
@user-ej2df1ub3s
@user-ej2df1ub3s 10 ай бұрын
thanks alot buhat bariya
@mindbehaviour9195
@mindbehaviour9195 2 жыл бұрын
Thnx bhaiya ji 🙏
@satyajeet7633
@satyajeet7633 2 жыл бұрын
Thank u Anuj bhaiya.
@Ajay-wv1jh
@Ajay-wv1jh Жыл бұрын
Thanks a lott❤❤
@souhardyahalder3903
@souhardyahalder3903 2 жыл бұрын
Worked for java But showing TLE in cpp
@zainabmalik7858
@zainabmalik7858 Жыл бұрын
excellent explanation!
@malayasabar4108
@malayasabar4108 Жыл бұрын
Boom 💥 💥
@gautamsajwan111
@gautamsajwan111 2 жыл бұрын
Bhiya y course complete kb hone wla h 😅
@karansinghnegi9384
@karansinghnegi9384 2 жыл бұрын
*2050*
@meetsoni1938
@meetsoni1938 2 жыл бұрын
Moj karadi bhaiya 😃😃💗
@yashyadav7927
@yashyadav7927 Жыл бұрын
bhaiya plz make 1 more video for directed graph bfs and dfs.. plz bhaiya
@tech_wizard9315
@tech_wizard9315 2 жыл бұрын
Please make a 60day roadmap for DSA beginner to crack tech giant's like Microsoft Amazon level companies in 60days
@dillirajtimalsina
@dillirajtimalsina 2 жыл бұрын
haah benchod meah 1 saal se padraha huu DSA full time ab tuje 60d roadmap chaiye ,chal bsdk gand merwale
@harshubhaigaming7882
@harshubhaigaming7882 Жыл бұрын
Which is best anuj sir OR apna college 🤔🤔🤔🤔🤔🤔
@akshayroy3869
@akshayroy3869 2 жыл бұрын
Sir mera ek question h ki Mera 10th me 46% hai aur 12th me 56% to kya mujhe placement mil jaegi mca se
@niloynath8458
@niloynath8458 2 жыл бұрын
❤️❤️❤️
@sanyamkothari4469
@sanyamkothari4469 2 жыл бұрын
Hello bhaiya, amazon november me hire karti hai interns , uska prepration kaise karu 3 mhine me??
Bend The Impossible Bar Win $1,000
00:57
Stokes Twins
Рет қаралды 43 МЛН
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 43 МЛН
Detect Cycle in Directed Graph Algorithm
10:43
Tushar Roy - Coding Made Simple
Рет қаралды 193 М.
Detect cycle in a Undirected Graph | Graph | Love Babbar DSA Sheet [Explaination + CODE]🔥
10:25
Graph Data Structure 4. Dijkstra’s Shortest Path Algorithm
10:52
Computer Science
Рет қаралды 1,5 МЛН
How to: Work at Google - Example Coding/Engineering Interview
24:02
Life at Google
Рет қаралды 7 МЛН