Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞 Do follow me on Instagram: striver_79
@GirishJain Жыл бұрын
FYI - for someone thinking why visited array is not used here Visited array is not required in this case because we are only inserting the node into the queue whenever the degree of that node becomes 0 and this will only happen once. Suppose like in the above example 4 was trying to visit 0 but it did not insert 0 into the queue for the first time as the indegree was not 0. and that;s why when 5 was trying to insert 0 , it did not have to check if 0 was visited earlier
@stl8857 Жыл бұрын
that means once it becomes 0 next time it will go negative??thats why it will only get into the que once
@codingalley6229 Жыл бұрын
@@stl8857 nah bro, once its zero it mean incoming edges to it is 0 so how can it even reduce further?
@stl8857 Жыл бұрын
@@codingalley6229 okier
@Moch117 Жыл бұрын
Correct. I realized that too after running through the algorithm. In a sense, a visited array here is the inDegree check
@aadharjain313 Жыл бұрын
in simple language , an element's indegree will become 0 only when all the parent nodes(nodes pointing to it) are withdrawn that is put into the topo list. Since every possible node (which could have that element as a neighbour) is already taken out of the queue and put into the topo list , so our neighbour node won't be touched again. And for not touching an already touched node , visited array is required , and it purpose is already solved in kahn's algorithm itslef , so no vis list is required here !!
@Amankumar-sb4jn Жыл бұрын
Sir, I only respect a few people for their way of teaching people, and you are among them for sure. Hats off to you and YES "UNDERSTOOD".
@Moch117 Жыл бұрын
His graph series is impeccable
@sayandey2492 Жыл бұрын
Note: Componentwise checking not required here because every component has at least one node with indeg=0 and hence atleast one node of every component will get included in the queue initially
@RajeevCanDev Жыл бұрын
But to find those nodes we need to traverse the graph first
@namesurname2223 Жыл бұрын
@@RajeevCanDev we are already having a loop (V) in starting while checking the inDegree
@RajeevCanDev Жыл бұрын
@@namesurname2223 yes that's what I'm saying.. to find those nodes we first need to traverse all nodes for finding the indegree
@Stumbleguys-qk6uzКүн бұрын
right
@visheshagrawal8676 Жыл бұрын
understood !! ........... Hey he is amazing I learned a new algo (kahn's).... in just 15 mins with its code because of him .... he is a gem guy!!.......
@hellothere473211 күн бұрын
Your explanation is great and helpful. Wishing your channels great success!
@vijayanks17146 ай бұрын
FAQ 1: why we don't use boolean[] visited in Kahn's algo? The standard bfs algo is generic if it is cycle or acyclic or directed or undirected work well the Kahn's only apply on DAG so no cycle, no need to worry about infinite loop condition then what about getting same node twice(it won't happy bcz we reach 0 only one time) so the need to visited not require we process each node once by using indegree array
@jaishriharivishnu5 ай бұрын
since, we are only putting in queue when indegree become zero........ so it is not possible for any node later on , that it visit an already visited node again.
@gayatrichaudhary5803 ай бұрын
We are very lucky because we have this amazing playlist of graph . All thanks to you brother.❤❤
@VarshaSingh-hi2sb2 ай бұрын
Why calculating indegree takes O(N) and not O(N^2) ?
@FooBar-lb5wf4 ай бұрын
FYI - Kahn's algorithm works well even if you replace the queue with a stack. It just produces a different topological sort sequence. Here's an excerpt from Wikipedia article. "...Reflecting the non-uniqueness of the resulting sort, the structure S can be simply a set or a queue or a stack. Depending on the order that nodes n are removed from set S, a different solution is created..."
@CodeMode9313 Жыл бұрын
Thanks habibi tum acha kaam karti ...understood
@tanaysingh53482 жыл бұрын
thanks for making the graph series even better
@stith_pragya11 ай бұрын
Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@saibunny12537 ай бұрын
striver thank you ! i understood this kahn's algorithm using a slightly different bfs
@sahllsaharn4664 Жыл бұрын
you've reallly worked on your explanation a great one sir it is a good change and a great teacher
@jaishriharivishnu5 ай бұрын
only similarity between Kahn's Algorithm and BFS is using queue Data Structure.... BFS is when we search in level, but here we are searching when indegree is getting zero. So, we can't relate BFS and Kahn's Algo. ~ Kahn
@googleit2490 Жыл бұрын
Revision: Forgot everything about indegree, was trying to use bfs the same as dfs and ans.push_back(q.front()); Sep'6, 2023 03:52 pm
@dhruvdangi89233 күн бұрын
love your videos sir
@jatinkumar44107 ай бұрын
Amazing explanation 🙌
@cinime2 жыл бұрын
Understood! Extremely wonderful explanation as always, thank you very much!!
@DevashishJose9 ай бұрын
Understood, Thank you so much .
@bablushaw6856 Жыл бұрын
My laziness raised 1 question that keeps my head scratched: "When easier one algo is there (about DFS) then why this algo exists?". I believe I will get answers and uses in next couple of videos in the series.
@decepticonWave Жыл бұрын
The problem with using the dfs approach is that if you are given a directed graph that contains a cycle in the solution. The algorithm wont be able to detect, it will spill out an ordering which is wrong. But when using khans algorithm, you will be able to detect whether top. sort was possible or not. You can do this by checking if the ordering has the same length as the number of nodes. if it is true then top sort was possible. So you have to know this just in case your interviewer adds such an edge case
@TheOfficialAyush Жыл бұрын
@@decepticonWave you are absolutely correct ! Just in case anyone Tends to use DFS and still wanna detect CYCLE u must see How to detect Cycle in direted graph using DFS of Striver u just need to add one array which stores Self visited[ ] and boom problem is solved in one line
@shivarajpatil8699 Жыл бұрын
@@decepticonWave true.
@whateverittakes7929 Жыл бұрын
because if u use dfs u hv to do cycle detection also
@therangeplus6 ай бұрын
Bro it is amazing how you explain things so clearly. I learned sm more from this video than others tysm!!
@sripriyapotnuru58392 жыл бұрын
Thank you, Striver 🙂
@ashutosh.. Жыл бұрын
At the end of the day... I understood it well :-)
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@kaichang81862 ай бұрын
understood, thanks for the effort
@AtharvaRao01042 ай бұрын
Everything else is fine. But for someone who watches this video will not understand why we need the vertices in this order. It is not enough to say topological sort gives an ordering of vertices .. we should also know why we need that ordering .. what problems it solves if we process the graph nodes in this ordering.. these basics are missing .. I still feel William Fiset is the best or reading books like Sedgewick will give a very good idea of applications
@atifhu Жыл бұрын
when did top changed to topo
@UECAshutoshKumar Жыл бұрын
Thank you sir 😁
@karthiklv292 жыл бұрын
understood, and thank you for the clear cut explanation and the series
@Piyush-yp2po Жыл бұрын
Isn't calculating indegree is O(V*E)??
@loudcapricorn5513 Жыл бұрын
UNDERSTOOD 🤞🙌
@vaalarivan_p Жыл бұрын
4:00 - 8:30
@kulvindersingh576421 күн бұрын
very helpful!
@ow_aqua8875 Жыл бұрын
im a bit confused on the assumed form of this adjacency matrix. are we assuming a 2d array where arr[i][j] = 1 if there is a directed edge from i to j?
@kamikaze92452 жыл бұрын
Understood bhaiya 🔥
@sohailulislamalvi8656 Жыл бұрын
insanely good!
@Learnprogramming-q7f6 ай бұрын
Thank you bhaiya
@pranayjain._ Жыл бұрын
Understood Sir!!
@ayushsinghchauhan6917 Жыл бұрын
u are just amazing
@TON-1082 ай бұрын
Understood ✨✨
@ventacode4 ай бұрын
thanks striver it helped
@santhoshs7028 Жыл бұрын
understood, thank you
@ujjalsaha4282 жыл бұрын
Understood ❤️
@The_Shubham_Soni Жыл бұрын
UNDERSTOOD.
@ishan5033 Жыл бұрын
Can we say a Directed graph whose none of the indegree is zero has a cycle?
@rahulmehndiratta3856 Жыл бұрын
No , That means that is a not connected graph think huh?
@mriduljain6809 Жыл бұрын
Understood Bhaiya
@pawandeepchor89 Жыл бұрын
Top class content, thanks for all these videos.
@mansisethi812710 ай бұрын
can we not do a thing that sort the indices in ascending order of indegrees?
@shlokaggarwal7714 Жыл бұрын
We dont even need a stack, we can directly add index whose value is zero.
@avadhut325 Жыл бұрын
Does kahn's algorithm work for Iterative DFS or does it only work for BFS ?
@UECSoumyaRay Жыл бұрын
Sometimes I just want to give up, cuz I feel that remembering so many intuitions is impossible for me! And remembering all of these for a lifetime! I am seriously thinking of shifting to Data Analytics. Salary kam milegi, lekin ek acche company ka tag to mil jayga na!!!!
@SanidhyaPatel-q9s4 ай бұрын
Which app do u use for teaching DSA in iPad?
@harshdeepsinghlongia6040 Жыл бұрын
we can use stack also in this implementation instead of a queue.. why doesnt it make a difference
@rushidesai2836 Жыл бұрын
Kahn's is Topo sort with BFS, that's why.
@_hulk748 Жыл бұрын
Understood sir❤🙏🙇♂
@anishraj66 Жыл бұрын
What will be the time req to find indegree??
@adebisisheriff15910 ай бұрын
Thanks so much!!!
@TheSpiritualOne401 Жыл бұрын
Striver how do you remember all this?
@shyren_more2 жыл бұрын
understood, thanks!
@Kiruthika_vlogs2 ай бұрын
where are you working bro
@princenagar16864 ай бұрын
Yeah understood
@sohammukherjee1392 Жыл бұрын
What if the graph has multiple components???
@ujwalgupta4508 Жыл бұрын
then in the beginning, the node will in-degree 0 from each component will be pushed in the queue
@UmeshYadav-qx4hr11 ай бұрын
file not found for -- C++/Java/Codes and Notes Link
@shivyanshgarg264111 ай бұрын
First question in which visited array is not used . And it makes complete sense. But still it feels weird and i can't digest it.
@RohitSharma-yc8lm2 жыл бұрын
UNDERSTOOD
@gangsta_coder_12 Жыл бұрын
Understood 😁😁
@chandantaneja63882 жыл бұрын
Understood 👍
@Chandraprakash-kx4ic Жыл бұрын
understood sir❤
@kushagramishra56382 жыл бұрын
"Understood"
@AnmolSharma-br2eg2 жыл бұрын
Understood!
@ishangujarathi10 Жыл бұрын
amazingg intuition
@subee128 Жыл бұрын
Thanks
@oqant04242 жыл бұрын
Understood !!!!!!!!!!!!!
@LoayThePhrygian6 ай бұрын
Understood^^
@sayakghosh51042 жыл бұрын
understood!!
@cypher75362 жыл бұрын
understood ❤️❤️
@AnkitKumar-ko5tr2 жыл бұрын
Understood
@sannareddymonesh75982 жыл бұрын
Understood!!
@shreyanshkumarsahu65342 жыл бұрын
Understand 🥰🥰
@dreamyme5432 жыл бұрын
Understood:)
@rayyansyed2998 Жыл бұрын
"understood"
@snehiltiwari28 Жыл бұрын
understood :)
@teetanrobotics53632 жыл бұрын
understood
@rickk3300 Жыл бұрын
Why didn't we take a visited array in this code?
@GirishJain Жыл бұрын
I was also thinking the same but I got the reason now. Visited array is not required in this case because we are only inserting the node into the queue whenever the degree of that node becomes 0 and this will only happen once. Suppose like in the above example 4 was trying to visit 0 but it did not insert 0 into the queue for the first time as the indegree was not 0. and that;s why when 5 was trying to insert 0 , it did not have to check if 0 was visited earlier
@shivamnegi7552 Жыл бұрын
whats the intuition
@pratyakshhhhhhhhhhhhhhhhhhhhh Жыл бұрын
🎉
@codingalley6229 Жыл бұрын
🐐
@akashkumarsingh8369 Жыл бұрын
understood lo bhai kr diya comment ye emoji(😞😞😞) mt kra kro yarr
@aniketambat6334 Жыл бұрын
understood++
@vaibhavjangid31123 ай бұрын
:beatin💋💋💋
@soicooc35007 ай бұрын
4:00
@antassachan17822 жыл бұрын
leetcode link for the question?
@deepakjain44819 ай бұрын
//here is the code both by bfs and dfs #include using namespace std; void dfs(vector & adj, vector & visited,int node,stack & st); vector sol(vector adj, vector & visited){ stack st; for(int i=0;i
@cypher75362 жыл бұрын
0:19 kya kaha 🤣🤣...?
@aryankumar3018 Жыл бұрын
Understood ntr
@mdsameer5332 жыл бұрын
us
@bemypro86022 жыл бұрын
what happened to ur neck
@Harsh-jc2bz2 күн бұрын
0:19 gand?
@pranavkhairnar1290 Жыл бұрын
Hey can anyone explain why he did not used visited array in this problem??????
@shivamjain9968 Жыл бұрын
because here we are storing the no. of incoming edges in the node so it can be 2,3 and so on but visited array is used only to check if the node has been visited or not and it store only 0 & 1.