when we were visiting A then you marked flag[A] =1 after exploring all adjacent nodes but after that you were marking flag =1 before exploring that and that may cause ..it will not detect loop on it self means if the path exist to itself please go through it and if i am getting wrong somewhere please tell me
It does, you just have to make sure to run this several times until all nodes have been visited (all nodes have a label of 1 or 0). So if you run this once and you still have nodes with -1 label left, then you restart this algo from the next -1 node.
E is a cheater! Whole this time D kept thinking that E is his adjacent vertex only but E was also the adjacent vertex of B! My condolences for D. Rot in hell B and E!!
Great explaination but for GFG pass will need to take disconnected graph in account public boolean isCycle(int v, ArrayList adj) { // Code here Queue q = new LinkedList(); int f[] = new int[v]; Arrays.fill(f, -1);
Teacher main 12th main ho Maine computer science ke badle biology liya. Hai Kya main 12 ke bad computer engineering Kar sakta hoon aap comment karke bato
Maam does this method work if there is a back edge to the previous node from the current node? I want to mean, say if there are [1,2] and [2,1] or say, [1,1] //here there is an edge from one node to itself. What happens then?
I like this type of concise, to-the-point videos!
i got the concept within 5 minutes and its all because of u. thank you..
u nailed it.
Your algorithm is very succinct and efficient. More importantly, your explanation is extremely intuitive, logical and easy to follow. Thank you!
some test cases are not passing..what is wrong in this code?
bool isCycle(int V, vector adj[]) {
int flag[V];
for(int i=0;i
Mam you are greate teacher (we all like you)
thank you very much Jenny, we love you.
very nice explained, sending thanks from Czech Republic
Absolutely brilliant and concise explanation. Thank you.
Nice idea to use the three colors with a BFS traversal to avoid keeping track of the parent 👍
Thank you mam 🔥🔥🔥
Can you also explain the solution when we have to print all the cycles in an undirected graph?
Helped a lot
Thank you so much maam.
nice explanation ...keep making such videos
great work JENNY
Nice explanation 😘😘😘
thanks ma'am !!
I am paying more attention to this lady's expressions than the lecture...
Is this approach applicable for directed graph ?
well explained
excellent
super explanation mam!!
Samjh v aya aur maja b
can u give the pseudocode for the same ..like upload it in the coment or description it would be very helpful..thank you :)
simply amazing!
If there is a other single egde from a to a will it be a considered a cycle
Keep it up mam
Thnku mam
I need to find the shortest cycle using bfs. how to do so?
when we were visiting A then you marked flag[A] =1 after exploring all adjacent nodes but after that you were marking flag =1 before exploring that and that may cause ..it will not detect loop on it self means if the path exist to itself please go through it and if i am getting wrong somewhere please tell me
does this also work in case of disconnected graphs?
It does, you just have to make sure to run this several times until all nodes have been visited (all nodes have a label of 1 or 0). So if you run this once and you still have nodes with -1 label left, then you restart this algo from the next -1 node.
@@ansismaleckis1296 got it thanks!
So good..studies with beauty ! XD
correct the order of playlist of dsa
Thanks a lot. Is the condition which is if any vertex finds its adjacent vertex with flag 0 then it contains a cycle valid for directed graph?
Excellent. Could you please suggest how to print one such cycle
you can use "DFS method" and keep track parent_map of each node.
when you found cycle then, use the parent_map to print all item in cycle .
Mam, can you please explain the code by making another one video, please
E is a cheater! Whole this time D kept thinking that E is his adjacent vertex only but E was also the adjacent vertex of B! My condolences for D. Rot in hell B and E!!
Lol thank god i have been single my entire life xD.
@@shiwamjaiswal9653 haha
Great explaination but for GFG pass
will need to take disconnected graph in account
public boolean isCycle(int v, ArrayList adj) {
// Code here
Queue q = new LinkedList();
int f[] = new int[v];
Arrays.fill(f, -1);
for(int j = 0; j < v; j++) {
if(f[j] == -1){
q.add(j);
f[j] = 0;
while(!q.isEmpty()){
int curr = q.remove();
f[curr] = 1;
for(int i = 0; i < adj.get(curr).size(); i++){
int vi = adj.get(curr).get(i);
if(f[vi] == 0){
return true;
} else if(f[vi] == -1) {
q.add(vi);
f[vi] = 0;
}
}
}
}
}
return false;
}
How to watch this class infrent of you😍
how u will do it with adjacency matrix and reduce the complexity from o(n)2
Hello ma'am , can you please share the program of the same?
How can you put d and e in queue after c .. it's not possible right .. i will be d,e,c then 😶
Hayeeeeeeeeeeeeeeeee, Thank YOU
can u please explain the code as well in java. I don't able to understand the code SO please help me
Is it work for only connected Graph?
Yes you have to loop over vertix in disconnected graph.
Mam pls tell us the program regarding this concept pls.......mam
I have you give subtitle 😭🔥
why do not you use DFS for detecting cycle in an undirected graph?
same question...
Ok , so where are the disjoint sets
Teacher main 12th main ho Maine computer science ke badle biology liya. Hai Kya main 12 ke bad computer engineering Kar sakta hoon aap comment karke bato
Of course bro NO problem, if you have PCM in 12th then you can do whatever you want in the engineering field.
Day 67
Maam does this method work if there is a back edge to the previous node from the current node? I want to mean, say if there are [1,2] and [2,1] or say, [1,1] //here there is an edge from one node to itself. What happens then?
Back edge concept doesn't count in undirected graph as you already have an edge from a to b and b to a. So back edge doesn't help
why couldnt i see this video before ;-(
This doesn't work for graphs with self Loop
Mam Hindi me bhi video banaiye
this is true for a connected component not for a graph, if a graph has more than one connected component then it may return false result
No it will work u will just have to maintain a for loop and send a bfs whenever u encounter a -1
thx
G 251
If you teach automata theory, plz drop a video and we will appreciate
Also topological sort for undirected graph doesnt make sense
Wowwwww
Who can give me a code this solution
Mam thorha Hindi wallo par bhi dhyan diyaa kariye
Pls stop asking for code! Have some decency! If you cant write your own code then you have to go back to beginners class.