Detect cycle in an undirected graph | Graph coloring method

  Рет қаралды 49,540

Techdose

Techdose

Күн бұрын

Пікірлер: 95
@prasannakumarz
@prasannakumarz 4 жыл бұрын
This channel will rock in future for the stuff it has.........!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@vrushalijadhav2062
@vrushalijadhav2062 2 жыл бұрын
Everything that I have learned about graphs so far is from your videos. Concise and thorough. Thank you!
@sujalhansda8285
@sujalhansda8285 2 жыл бұрын
nice dp you are an inspiration to young girls
@rahulchudasama9363
@rahulchudasama9363 4 жыл бұрын
Many thank you sir for teaching. I have tried to understand this algo but it took long time to implement it in code(Java) and i gave up. Convert back 2 to 1 I found that is difficult for me. I find BFS with graph coloring is the best to approach this.
@techdose4u
@techdose4u 4 жыл бұрын
👍
@tanishksharma7339
@tanishksharma7339 4 жыл бұрын
Hey can you please help me, I am trying in Java and getting error ide.geeksforgeeks.org/H9cOiEmow6
@ayushminanayak5352
@ayushminanayak5352 4 жыл бұрын
At 3:01 when the value of [0] changes from 2 to 1 I think the value of [1] will also Change from 1 to 0
@harshpatel1385
@harshpatel1385 4 жыл бұрын
I am in 2nd year sir your video's are very helpful to me. It will help me to crack interview. Good bless you sir
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Most simplest and unique technique. Thank you sir
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@mahipalsingh-yo4jt
@mahipalsingh-yo4jt 4 жыл бұрын
your videos helped me a lot in developing thought process....
@techdose4u
@techdose4u 4 жыл бұрын
Nice to know this :)
@sarveshshirishvhawal7986
@sarveshshirishvhawal7986 4 жыл бұрын
Simplest, understandable code and explanation ever!!!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@ayushmohan7747
@ayushmohan7747 4 жыл бұрын
Just Awesome bro, You made Graph DS easy for me . Thanks, God Bless You :)
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@manojg4451
@manojg4451 3 жыл бұрын
Another trick we can use is that a graph with out any cycle is considered a tree and it has n-1 edges, So if the graph has more than n-1 edges it has a cycle
@kaushikgopu6639
@kaushikgopu6639 2 жыл бұрын
this will only work if graph is connected.
@RahulChauhan-wi4jv
@RahulChauhan-wi4jv 4 жыл бұрын
what changes would you make to the iscyclicUtil function if the visited array was passed by reference?
@saifmohammed1481
@saifmohammed1481 4 жыл бұрын
In my opinion we just need 2 colours for visited i.e 0 and 1 denoting unvisited and visited. If a node encounters visited value 1 and that vertex is not it's caller in DFS, then it's a cycle . In directed graphs though it makes sense to have 3 colours. Don't you think ??
@Rahul-sx5zo
@Rahul-sx5zo 4 жыл бұрын
at 2:57 after all the nodes for 1 have been processed and we return back to node 0. in visited[0] is changed to 1 but you forgot to change visited[1] back to 0
@techdose4u
@techdose4u 4 жыл бұрын
Yes I might have 😅
@Rahul-sx5zo
@Rahul-sx5zo 4 жыл бұрын
@@techdose4u can you verify my code comment i made, please?
@vankshubansal6495
@vankshubansal6495 3 жыл бұрын
We can also pass the visited vector by reference. We just need to make sure that before calling other children, visited[curr] is set to 1. Attaching the accepted code for reference. bool dfs2(int V, vector adj[], int sv, vector& visited) { for(auto neigh: adj[sv]) { visited[neigh]++; if(visited[neigh] == 3) return true; else if(visited[neigh] == 1) { if(dfs2(V, adj, neigh, visited)) return true; } visited[sv] = 1; } return false; } bool isCycle(int V, vectoradj[]) { vector visited(V, 0); for(int i = 0; i < V; i++) { if(visited[i] == 0) { visited[i] = 1; if(dfs2(V, adj, i, visited) == true) return true; } } return false; }
@devcodes6495
@devcodes6495 2 жыл бұрын
Can you elaborate a little on why we did that??
@lifecodesher5818
@lifecodesher5818 4 жыл бұрын
Thanks so much for such clear explanation!! sending lots of good wishes to you...
@AlanSchooll
@AlanSchooll 2 жыл бұрын
simple solution is keep track of previous node and you cannot move to nodes previous node.
@sambhumahato8320
@sambhumahato8320 4 жыл бұрын
U Rocked Sir..💖
@techdose4u
@techdose4u 4 жыл бұрын
Thanks shambhu :)
@thechristchild4625
@thechristchild4625 4 жыл бұрын
thank so much sir.u are like a god for us:
@techdose4u
@techdose4u 4 жыл бұрын
😅
@rangapavankumar79
@rangapavankumar79 4 жыл бұрын
Truee
@shravanreddy2557
@shravanreddy2557 4 жыл бұрын
Sir please make a video on finding number of islands in the given 2d matrix
@techdose4u
@techdose4u 4 жыл бұрын
Yea....i will add it in my to-do queue list. Okay.
@shravanreddy2557
@shravanreddy2557 4 жыл бұрын
@@techdose4u Thank you :)
@falakchhikara7230
@falakchhikara7230 4 жыл бұрын
I think this coloring method is for directed and visited method is for undirected
@abhireddy8164
@abhireddy8164 4 жыл бұрын
Please add to your queue sir.find words in a dictionary from 2d character array recursively moving 8 directions.i.e word boggle problem
@techdose4u
@techdose4u 4 жыл бұрын
Okay
@sandeepnallala48
@sandeepnallala48 3 жыл бұрын
sir same method we can use for directed graphs also sir ?? by changing the condition from if(vis[u] == 2) to if(vis[u] == 1) return true; and removing the if condition in util fun for loop ?
@ajithpalani5139
@ajithpalani5139 4 жыл бұрын
I think there is no need of outer for loop in isCyclic function. If I'm wrong please correct me . Thank you in advance.
@Sky-nt1hy
@Sky-nt1hy 3 жыл бұрын
making the node from 1 to 2 is actually for taking care of the "adjacent" node so that two adjacent nodes are not counted as a cycle right?
@BaljeetSingh-bx8yj
@BaljeetSingh-bx8yj 4 жыл бұрын
Amazing explanation !!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks 😊
@impatientgaming9868
@impatientgaming9868 11 ай бұрын
Gooo one
@mahesh_kok
@mahesh_kok 2 жыл бұрын
better approach is to use prev variable: Python code for that is def detect_cycle_for_undirected(graph): visited = set() def dfs(vertex, prev): if vertex in visited: print("found cycle for vertex: ", vertex) return False visited.add(vertex) for neighbor in graph[vertex]: if neighbor == prev: continue if not dfs(neighbor, vertex): return False return True return "No cycle detected" if dfs(0, -1) else "Cycle detected" cycle_leetcode_data = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]] no_cycle_leetcode_data = [[0, 1], [0, 2], [0, 3], [1, 4]] graph = defaultdict(list) for src, dest in cycle_leetcode_data: graph[src].append(dest) graph[dest].append(src) print(detect_cycle_for_undirected(graph))
@sumitghewade2002
@sumitghewade2002 4 жыл бұрын
Hi Surya, I tried to implement the code but it's returning true for the graph which doesn't have a cycle. Can you tell what might be going wrong and in the GitHub link also some people have mentioned that the answer is wrong for graphs that don't have a cycle.
@techdose4u
@techdose4u 4 жыл бұрын
I will check it once and maybe I will update the code.
@Sky-nt1hy
@Sky-nt1hy 3 жыл бұрын
Sir!!!! Thanks for your video! I have one question. If I pass by value the visit array, then it gives me TLE on any judge site. Is this just for the sake of knowledge? Or is there any other way (global visit array) to make this work?
@techdose4u
@techdose4u 3 жыл бұрын
You need to pass by reference so that a new copy is not created. Passing variables by value in recursion call consumes a lot of time.
@Sky-nt1hy
@Sky-nt1hy 3 жыл бұрын
@@techdose4u I've been working on how to do by pass by reference for hours but keep failing...since it is 3 colorable, it's really confusing at where in the function I should decrement or set 0 for visit[curr]
@Rahul-sx5zo
@Rahul-sx5zo 4 жыл бұрын
bool isCyclic(int V, vector adj[]){ vector vis(V,0); for(int i = 0; i < V; i++){ if(isCyclicUtil(adj,vis,i)){ return true; } } return false; } since we mark visited for every node as 1 before calling the isCyclicUtil function on its adjacent nodes, can this be an alternative to that? where we pass the node itself to isCyclicUtil?
@noobninja4882
@noobninja4882 4 жыл бұрын
awesome explanation sir
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@aditikhandelwal135
@aditikhandelwal135 3 жыл бұрын
Just amazing!!!
@mvkisshore
@mvkisshore 3 жыл бұрын
Thanks for the nice content. Can someone share with me what is the name of the software which used to write on the screen with a mouse?
@ghoshdipan
@ghoshdipan 4 жыл бұрын
Please do try to explain the code more thoroughly
@techdose4u
@techdose4u 4 жыл бұрын
Sure. Doing it now.
@akshatsuwalka5759
@akshatsuwalka5759 4 жыл бұрын
TLE in InterviewBit below is the same code bool isCyclic_util(vector adj[], vector visited, int curr) { if(visited[curr]==2) return true; visited[curr] = 1; bool FLAG = false; for(int i=0;i
@MSCSJhabar
@MSCSJhabar 4 жыл бұрын
Sir can't we detect cycle by simply traversing using DFS or BFS ?
@techdose4u
@techdose4u 4 жыл бұрын
Yes. We can
@MSCSJhabar
@MSCSJhabar 4 жыл бұрын
@@techdose4u okay sir, thanks for the reply 🙂
@saileshswaminathan9818
@saileshswaminathan9818 2 жыл бұрын
Can this also be used in a directed graph?
@sunilkumar-re7qj
@sunilkumar-re7qj 3 жыл бұрын
Why this method is not working in java..... i have converted the same code to java....even then it is not accepting
@ravikumarkamble6403
@ravikumarkamble6403 3 жыл бұрын
why we use this method ? previous one not?
@techdose4u
@techdose4u 3 жыл бұрын
That means visited our unvisited I think.
@ravikumarkamble6403
@ravikumarkamble6403 3 жыл бұрын
@@techdose4u previous one means detect cycle in directed graph
@ashutoshshrivastava6521
@ashutoshshrivastava6521 3 жыл бұрын
this algorithm is not working for test case B : [ [1, 2] [1, 3] [2, 3] ]
@sunnychy86
@sunnychy86 4 жыл бұрын
Sir, where did you apply "pass by value" method in the code?
@swappy4515
@swappy4515 3 жыл бұрын
I guess it's worked internally in recursive call I might be wrong .
@yitingg7942
@yitingg7942 3 жыл бұрын
Can I ask which leetcode question is this ? I can't find it.
@techdose4u
@techdose4u 3 жыл бұрын
Search for some cycle detection problem Most probably you will get this. But I took this from geeksforgeeks
@yitingg7942
@yitingg7942 3 жыл бұрын
@@techdose4u Oh I see. I couldn't find good problems that are closely related to this.
@lifecodesher5818
@lifecodesher5818 4 жыл бұрын
This is using dfs too right? asking because in my assignment I have to solve this question using both dfs and bfs
@ayushgarg5929
@ayushgarg5929 4 жыл бұрын
Yes , it is DFS
@ritikyadav926
@ritikyadav926 4 жыл бұрын
can we use it for directed graph?
@techdose4u
@techdose4u 4 жыл бұрын
Yes. Try it.
@ritikyadav926
@ritikyadav926 4 жыл бұрын
@@techdose4u Yeh got it
@rahulbhati3079
@rahulbhati3079 4 жыл бұрын
is there need of that loop of j in "isCyclic" bcoz we can get in above loop also
@ayushme53
@ayushme53 4 жыл бұрын
Awesome.
@antwanwimberly1729
@antwanwimberly1729 11 ай бұрын
Kewl!
@ajaygoswami36
@ajaygoswami36 3 жыл бұрын
What's to take input
@saitejdandge2315
@saitejdandge2315 3 жыл бұрын
Why don't we apply the dfs function directly on the nodes, instead of their neighbors ? iterate every node and see if this dfs(node) doesnt return true; private static boolean dfs(Integer curr, int[] visited, Map graph) { if (visited[curr] == 2) return true; visited[curr] = 1; if (graph.get(curr) != null) for (Integer n : graph.get(curr)) { if (visited[n] == 1) { visited[n] = 2; } else if (dfs(n, visited, graph)) return true; //marking current to 1 again visited[curr] = 1; } visited[curr] = 0; return false; }
@amar229832
@amar229832 2 жыл бұрын
Even I have this question. Did you find solution to it ?
@RakeshSingh-yw1bi
@RakeshSingh-yw1bi 2 жыл бұрын
please explain with diagram, not by narrating it.
@prodiptamondal1758
@prodiptamondal1758 4 жыл бұрын
Here is the simpler version code: ideone.com/zWqcWq
@ayushgarg5929
@ayushgarg5929 4 жыл бұрын
Not so efficient...could have easily understood by just passing its parent node in every iteration..and then check whether the children is matching with parent or not...if so , then we won't include that node...rather we will move to another neighbor if it is present...
@techdose4u
@techdose4u 4 жыл бұрын
There are other methods to solve but Coloring is also efficient.
@cedarpan5305
@cedarpan5305 4 жыл бұрын
​@@techdose4u I also think the presented solution is bit more complicated than the "passing parent" approach. I have also heard there is another approach using disjoint sets. If possible, can you do a video to compare all different solutions for this problem in the future?
@techdose4u
@techdose4u 4 жыл бұрын
@@cedarpan5305 I have already explained cycle detection in disjoint set. Please watch that. When I come back to graph playlist again then I will add some more videos.
@cedarpan5305
@cedarpan5305 4 жыл бұрын
@@techdose4u Thank you! I will check out the disjoint set video.
@spetsnaz_2
@spetsnaz_2 3 жыл бұрын
Code Link : techdose.co.in/detect-cycle-in-an-undirected-graph/
Number of islands | Leetcode #200
12:44
Techdose
Рет қаралды 183 М.
G-19. Detect cycle in a directed graph using DFS | Java | C++
17:22
take U forward
Рет қаралды 251 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 12 МЛН
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 112 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 31 МЛН
Minimum edit distance | Dynamic programming | Backtracking
28:52
G-12. Detect a Cycle in an Undirected Graph using DFS | C++ | Java
19:10
Detect Cycle in a Directed Graph | GeeksforGeeks
7:14
GeeksforGeeks
Рет қаралды 190 М.
Kosaraju Algorithm | Strongly connected components in a graph
24:30
Introduction to Graph Theory: A Computer Science Perspective
16:26
The hidden beauty of the A* algorithm
19:22
Polylog
Рет қаралды 898 М.
G-11. Detect a Cycle in an Undirected Graph using BFS | C++ | Java
20:19