Cycle in undirected graph using DFS and disjoint sets. / tusharroy25 github.com/mission-peace/inte... github.com/mission-peace/inte...
Пікірлер: 84
@fatihsonmez6 жыл бұрын
"yes, i contain a cycle." -that graph
@vaibskinikar8 жыл бұрын
Great job! Thanks for taking out time and making all of these videos. These are really helpful and very well communicated.
@kapilsharma44344 жыл бұрын
5:23 where DFS starts
@johnbaker71023 жыл бұрын
doesn't matter for a connected graph, pick any vertex
@AkshayKumar-fo3dw2 жыл бұрын
@@johnbaker7102 bro you misunderstood 🤣
@shritishaw75102 жыл бұрын
that smile right at the start has made my day. Beautifully explained. Thank you
@AnandPandeyHere4 жыл бұрын
Your videos are really helpful. Keep uploading more such videos on graphs.
@XeCloudeX7 жыл бұрын
Thanks a lot for your videos Tushar. Have a Merry Christmas!
@pawankoranga93324 жыл бұрын
your way of explaining concepts is amazing
@aprasad8654 жыл бұрын
Great Job Tushar . Very clear and crisp
@vyshnavramesh93054 жыл бұрын
7:48 to 8:10 summary of dfs approach to detect cycle in an undirected graph
@fredsladkey9047 жыл бұрын
Great clear and brief explanation. Thank you.
@mclovinf8528 жыл бұрын
Very good! I love your explanation.
@ganeshkhirwadkar41278 жыл бұрын
Appreciable. Simple and understandable teaching.
@DivyanshuShekhar8 жыл бұрын
Impressive...!! So well communicated that the concept looks sooo simple and clear. Thanks Man.
@DivyanshuShekhar8 жыл бұрын
+Divyanshu Shekhar Now I have subscribed to your channel. I wish I could have done it earlier.
@ARYANRAJ-cb4gf4 жыл бұрын
your videos help me a lot, thanks for your great contribution.
@Official-tk3nc4 жыл бұрын
If you are watching this in lockdown believe me you are one of the rarest species on the earth who are willing to acheive something in life. Many students are wasting their time in watching netflix, webseries, playing games, watching movies, ludo, chatting , etc.
@jeevanraajan32384 жыл бұрын
I owe Tushar my Job !! Awesome.Thanks
@KUSHUTRIVEDI9 жыл бұрын
Thank you sir for posting this video..!
@darciogmail8 жыл бұрын
Great job! Thanks for sharing!
@AndrewMelnychuk0seen3 жыл бұрын
This is excellent, thank you!
@202Aziz8 жыл бұрын
great explaining thank you
@shando_tube5 жыл бұрын
Is it possible to implement this algorithm with BFS as well?
@nilanjanroy80238 жыл бұрын
Hi Tushar, Thanks for the great video ! Can i get the nodes which are participating in the cycle from this algorithm ? .what i feel is I have to run union find for every node...pls share your thoughts
@dheerajpatni98109 жыл бұрын
Hi Tushar,Thanks for the video. I wanted to ask if you know any algorithm other than DFS to find the longest cycle in undirected graph? I dont want to use DFS as it is very slow for bigger graph.
@user-jl2tz3rq9f5 жыл бұрын
Very good explanation!Even fool can understand
@OsafMalik8 жыл бұрын
i cant understand how the time complexity turn out to be O(V) when find() and union() operations have time complexity of O(log(V)) and number of both of these operations are O(V) in the worst case.
@TheBugWhisperer16 жыл бұрын
Find() and Union() is not log(n) , it is O(α (n)). In fact it grows so slowly, that it doesn't exceed 4 for all reasonable n (approximately n
@kamalsmusic8 жыл бұрын
Can you make a video about cycles in directed graphs? I think directed is a bit more complex and it'd be nice to see a video on that
@SriramGopalGoli0307925 жыл бұрын
Shouldn't the same algo work for directed graphs as well?
@obinnaubah90455 жыл бұрын
@@SriramGopalGoli030792 I believe it should.
@YogeshDarji995 жыл бұрын
@@SriramGopalGoli030792 This algo is considering all the edges, I believe in directed graphs, you will have to keep track of correct representative because of directions, this algo would require slight modification
@SriramGopalGoli0307925 жыл бұрын
This algo does not work for directed graphs. A simple example below. 1 -> [2,3] 2 -> [3] 3 -> []
@lkez27 жыл бұрын
Hey Tushar, is it possible to also use the DFS method for directed graphs here? Since Undirected graphs are just a special case of directed graph which edges in both directions.
@sidharthanrajendran13875 жыл бұрын
Plain and simple:)
@202Aziz8 жыл бұрын
Do you have the Pseudo-Code for Detecting cycle in Uncorrected graph in BFS algorithm or DFS algorithm and the running time O( m + n )
@lpatrasco5 жыл бұрын
Great Weedeo!
@talaviss1237 жыл бұрын
Thanks Tushar. well explained. Do I have always to use disjoint set to solve this problem?
@chamanjhinga39467 жыл бұрын
Disjoint set is one way to detect cycle. DFS and topological sort are other ways to detect cycle.
@ZiadxKabakibi9 жыл бұрын
thank you very much,can you explain about dijikstra's algorithm please
@Ronakrktanna8 жыл бұрын
As the first step of detecting a cycle in an undirected graph, why can't we just say that a cycle exists in the graph if the number of edges in the graph are V or above? Since it's an undirected graph, if the number of edges are >=V, wouldn't that result in a cycle? If the number of edges < V, a cycle is still possible and then we could go for the Union Find algorithm. Correct me if I'm wrong.
@shando_tube5 жыл бұрын
I had the same thought.
@shashankprashar17245 жыл бұрын
@@shando_tube yes tht is true but tht doesnt mean that less than V wud give no cycle coz may be all nodes mgt not be connected and others might be connected giving cycle in a sub graph formed ex: build graph from edges 0 1 2 3 3 4 4 2 5 nodes 4 edges still cycle exist.
@ShreyanGoswami4 жыл бұрын
Thanks for the video. It really helped. The concept of disjoint sets make it easy to find a cycle in the graph. I do have one question though. I have seen different ways that are used to represent the graph. In particular the adjacency list technique. In that if there is an edge between node 0 and node 1 then the adjacency list will have a pointer from node 0 to node 1 as well as from node 1 to node 0. If we apply the algorithm to such a ds it will say that there is a cycle. Even in the matrix representation form I see this as an issue. I am trying to figure out if there is a way to resolve this.
@GurpreetSingh-nt2os3 жыл бұрын
You can check his GitHub link in the description section.he had implemented an amazing class for representation of graph . Hope it helps
@anshulmishra56337 жыл бұрын
Nice work big man... u convey tricky concepts with ease and clarity.. thnx alot ;)
@kartikv32548 жыл бұрын
Will this work if we have single node loop??
@varun__15534 жыл бұрын
how you are going to apply topological sort on undirected graph for finding cycle..0:30 ???
@abhilakshsharma12754 жыл бұрын
yeah I also was thinking the same. I guess he mistakenly have said that since topological sorting is applicable only to Directed Acyclic Graph (DAG).
@mousagang24772 жыл бұрын
can I implement the first algorithm with union-find by size and path compression and say that the time complexity is O(n * log_star(n) ) where n is the number of edges ??
@11m08 жыл бұрын
09:22 Where is the class DisjointSet defined? Is this a standard java class? Or was it created somewhere?
@achilles1.07 жыл бұрын
He created it separately. There's also a video on it where he explains the code, you might want to check that out. github.com/mission-peace/interview/blob/master/src/com/interview/graph/DisjointSet.java
@pusarlaaishwarya50354 жыл бұрын
Sir...which books are preferred to enhance more knowledge on this concepts
@namansingh86484 жыл бұрын
Can we use white gray black method in undirected graph too???
@arijitjash93756 жыл бұрын
if u uploaded the next lesson for me sir....plz
@kamalsmusic8 жыл бұрын
In the code that you posted for finding a cycle using DFS, you have 2 methods, hasCycleDFS() and hasCycleDFSUtil(). Why do you have to initiate hasCycleDFSUtil() on every vertex in the graph? If we just pick any vertex and run hasCycleDFSUtil(), surely that is enough to figure out if there is a cycle or not?
@kamalsmusic8 жыл бұрын
Ah ok. So in that case, since you have to execute multiple depth first searches, would the runtime be O(|V|(|E|+|V|))? And so, if the graph is connected, starting at any one vertex is sufficient, right?
@kamalsmusic8 жыл бұрын
Wait a second, isnt the runtime still O(|V|+|E|)? We still explore every vertex only once since we keep track of the ones we've visited early.. I think I analyzed it wrong. Can you confirm that the runtime is O(|E|+|V|)?
@SharifKhanitexpart7 жыл бұрын
sir, which mathematics are require for algoritm designer
@ajishgoku8757 жыл бұрын
discrete mathematics
@vivekr53217 жыл бұрын
Hi, Tushar. I have a question in your DFS explanation. You said you're passing the parent so that you know you don't have to go in that direction again. This makes sense, but what if there is another edge from the parent to the same child? In this case, this is a genuine loop. In the diagram below, A-B is itself a genuine loop, so if you pass parent A while exploring B, you will not detect it. Eg: ____ | | A ---- B ----- C | | | F E------D
@treyquattro6 жыл бұрын
Eventually the DFS will return to the second edge A-B and if the B-C-D-E cycle hadn't been detected, the A-B-A would be. B would be already in the visited set.
@MrCnareshkumar6 жыл бұрын
In a directed graph, there exists one path at the maximum from one node to other
@shando_tube5 жыл бұрын
Perhaps this sounds naive, but couldn't we just count the number of edges in our undirected graph? If length(E) > |V|-1, then not only is the graph connected, but a cycle also exists.
@ultra_max_pro5 жыл бұрын
This is clearly not true. Consider the graph with two connected components, one is an isolated vertex, and the other is k_4, (complete graph with 4 vertices, 6 edges). Then the inequality you mention holds, but the graph is not connected. Also, if the graph has, say 100 isolated vertices and one cycle with 3 vertices, the equality does not hold, but the graph has a cycle. Basically, you don't know about how the graph is connected.
@shando_tube5 жыл бұрын
@@ultra_max_pro yea u know what man, I'm just happy I passed the class
@surendrapalSingh7 жыл бұрын
Hi Tushar, Nice explanation. With some help from google I came up with following code to detect cycle in undirected graph using modified DFS. Though I have conceptual clarity but I am failing to understand what does 'else if (temp->dest != parent)' do (marked with ## in code below)? bool hasCycleDFSUtil(int v, bool visited[], int parent, struct Graph* graph) { visited[v] = true; struct AdjListNode* temp=graph->array[v].head; while(temp!=NULL) { if(visited[temp->dest] == false) { if(hasCycleDFSUtil(temp->dest, visited, v, graph)) return true; } else if (temp->dest != parent) // ## return true; temp=temp->next; } return false; } Please help.
@puneetkumar-vv9go2 жыл бұрын
because in the adjacent list of V node, there should be not any other node visited except its parent(because v derived from its parent). if it is , then there is cycle
@vishwanath5019 жыл бұрын
nice explanation!!!!!! but how to find all cycles that exist in graph....
@parasmishra54618 жыл бұрын
pls do a video of directed graphs also thnx in advance
@anandpandey29174 жыл бұрын
kzbin.info/www/bejne/lZS2ZI13rp1od7M
@gauraonandekar58203 жыл бұрын
Graaaafffffffff
@pritamsarkar33713 жыл бұрын
is not "for(Edge edge: graph.getAllEdges())" taking O(E) times ?
@sasipotu98338 жыл бұрын
Hi tushar i need pseudo code for dfs for find in cycle in undirected graph
@sasipotu98338 жыл бұрын
I need using dfs..not disjoints
@sasipotu98338 жыл бұрын
Thank you..
@ashirbadbehera55444 жыл бұрын
thaak about....
@Pariharyash7 жыл бұрын
You evaluated time complexity, have you considered operation findset's and union's time ?
@overclockinggames24194 жыл бұрын
O(1)
@ankushvirmani90397 жыл бұрын
in ur video there is problem in loading
@dibyendubrinto30518 жыл бұрын
all of your code are in java which are difficult to understand for beginner. if you translate this into c++ then it will be easy