G-22. Kahn's Algorithm | Topological Sort Algorithm | BFS

  Рет қаралды 266,406

take U forward

take U forward

Күн бұрын

Пікірлер: 194
@takeUforward
@takeUforward 2 жыл бұрын
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
@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
@stl8857 Жыл бұрын
that means once it becomes 0 next time it will go negative??thats why it will only get into the que once
@codingalley6229
@codingalley6229 Жыл бұрын
@@stl8857 nah bro, once its zero it mean incoming edges to it is 0 so how can it even reduce further?
@stl8857
@stl8857 Жыл бұрын
@@codingalley6229 okier
@Moch117
@Moch117 Жыл бұрын
Correct. I realized that too after running through the algorithm. In a sense, a visited array here is the inDegree check
@aadharjain313
@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
@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
@Moch117 Жыл бұрын
His graph series is impeccable
@sayandey2492
@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
@RajeevCanDev Жыл бұрын
But to find those nodes we need to traverse the graph first
@namesurname2223
@namesurname2223 Жыл бұрын
@@RajeevCanDev we are already having a loop (V) in starting while checking the inDegree
@RajeevCanDev
@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
@Stumbleguys-qk6uz Күн бұрын
right
@visheshagrawal8676
@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!!.......
@hellothere4732
@hellothere4732 11 күн бұрын
Your explanation is great and helpful. Wishing your channels great success!
@vijayanks1714
@vijayanks1714 6 ай бұрын
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
@jaishriharivishnu
@jaishriharivishnu 5 ай бұрын
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.
@gayatrichaudhary580
@gayatrichaudhary580 3 ай бұрын
We are very lucky because we have this amazing playlist of graph . All thanks to you brother.❤❤
@VarshaSingh-hi2sb
@VarshaSingh-hi2sb 2 ай бұрын
Why calculating indegree takes O(N) and not O(N^2) ?
@FooBar-lb5wf
@FooBar-lb5wf 4 ай бұрын
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
@CodeMode9313 Жыл бұрын
Thanks habibi tum acha kaam karti ...understood
@tanaysingh5348
@tanaysingh5348 2 жыл бұрын
thanks for making the graph series even better
@stith_pragya
@stith_pragya 11 ай бұрын
Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@saibunny1253
@saibunny1253 7 ай бұрын
striver thank you ! i understood this kahn's algorithm using a slightly different bfs
@sahllsaharn4664
@sahllsaharn4664 Жыл бұрын
you've reallly worked on your explanation a great one sir it is a good change and a great teacher
@jaishriharivishnu
@jaishriharivishnu 5 ай бұрын
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
@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
@dhruvdangi8923
@dhruvdangi8923 3 күн бұрын
love your videos sir
@jatinkumar4410
@jatinkumar4410 7 ай бұрын
Amazing explanation 🙌
@cinime
@cinime 2 жыл бұрын
Understood! Extremely wonderful explanation as always, thank you very much!!
@DevashishJose
@DevashishJose 9 ай бұрын
Understood, Thank you so much .
@bablushaw6856
@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
@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
@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
@shivarajpatil8699 Жыл бұрын
@@decepticonWave true.
@whateverittakes7929
@whateverittakes7929 Жыл бұрын
because if u use dfs u hv to do cycle detection also
@therangeplus
@therangeplus 6 ай бұрын
Bro it is amazing how you explain things so clearly. I learned sm more from this video than others tysm!!
@sripriyapotnuru5839
@sripriyapotnuru5839 2 жыл бұрын
Thank you, Striver 🙂
@ashutosh..
@ashutosh.. Жыл бұрын
At the end of the day... I understood it well :-)
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@kaichang8186
@kaichang8186 2 ай бұрын
understood, thanks for the effort
@AtharvaRao0104
@AtharvaRao0104 2 ай бұрын
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
@atifhu Жыл бұрын
when did top changed to topo
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir 😁
@karthiklv29
@karthiklv29 2 жыл бұрын
understood, and thank you for the clear cut explanation and the series
@Piyush-yp2po
@Piyush-yp2po Жыл бұрын
Isn't calculating indegree is O(V*E)??
@loudcapricorn5513
@loudcapricorn5513 Жыл бұрын
UNDERSTOOD 🤞🙌
@vaalarivan_p
@vaalarivan_p Жыл бұрын
4:00 - 8:30
@kulvindersingh5764
@kulvindersingh5764 21 күн бұрын
very helpful!
@ow_aqua8875
@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?
@kamikaze9245
@kamikaze9245 2 жыл бұрын
Understood bhaiya 🔥
@sohailulislamalvi8656
@sohailulislamalvi8656 Жыл бұрын
insanely good!
@Learnprogramming-q7f
@Learnprogramming-q7f 6 ай бұрын
Thank you bhaiya
@pranayjain._
@pranayjain._ Жыл бұрын
Understood Sir!!
@ayushsinghchauhan6917
@ayushsinghchauhan6917 Жыл бұрын
u are just amazing
@TON-108
@TON-108 2 ай бұрын
Understood ✨✨
@ventacode
@ventacode 4 ай бұрын
thanks striver it helped
@santhoshs7028
@santhoshs7028 Жыл бұрын
understood, thank you
@ujjalsaha428
@ujjalsaha428 2 жыл бұрын
Understood ❤️
@The_Shubham_Soni
@The_Shubham_Soni Жыл бұрын
UNDERSTOOD.
@ishan5033
@ishan5033 Жыл бұрын
Can we say a Directed graph whose none of the indegree is zero has a cycle?
@rahulmehndiratta3856
@rahulmehndiratta3856 Жыл бұрын
No , That means that is a not connected graph think huh?
@mriduljain6809
@mriduljain6809 Жыл бұрын
Understood Bhaiya
@pawandeepchor89
@pawandeepchor89 Жыл бұрын
Top class content, thanks for all these videos.
@mansisethi8127
@mansisethi8127 10 ай бұрын
can we not do a thing that sort the indices in ascending order of indegrees?
@shlokaggarwal7714
@shlokaggarwal7714 Жыл бұрын
We dont even need a stack, we can directly add index whose value is zero.
@avadhut325
@avadhut325 Жыл бұрын
Does kahn's algorithm work for Iterative DFS or does it only work for BFS ?
@UECSoumyaRay
@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-q9s
@SanidhyaPatel-q9s 4 ай бұрын
Which app do u use for teaching DSA in iPad?
@harshdeepsinghlongia6040
@harshdeepsinghlongia6040 Жыл бұрын
we can use stack also in this implementation instead of a queue.. why doesnt it make a difference
@rushidesai2836
@rushidesai2836 Жыл бұрын
Kahn's is Topo sort with BFS, that's why.
@_hulk748
@_hulk748 Жыл бұрын
Understood sir❤🙏🙇‍♂
@anishraj66
@anishraj66 Жыл бұрын
What will be the time req to find indegree??
@adebisisheriff159
@adebisisheriff159 10 ай бұрын
Thanks so much!!!
@TheSpiritualOne401
@TheSpiritualOne401 Жыл бұрын
Striver how do you remember all this?
@shyren_more
@shyren_more 2 жыл бұрын
understood, thanks!
@Kiruthika_vlogs
@Kiruthika_vlogs 2 ай бұрын
where are you working bro
@princenagar1686
@princenagar1686 4 ай бұрын
Yeah understood
@sohammukherjee1392
@sohammukherjee1392 Жыл бұрын
What if the graph has multiple components???
@ujwalgupta4508
@ujwalgupta4508 Жыл бұрын
then in the beginning, the node will in-degree 0 from each component will be pushed in the queue
@UmeshYadav-qx4hr
@UmeshYadav-qx4hr 11 ай бұрын
file not found for -- C++/Java/Codes and Notes Link
@shivyanshgarg2641
@shivyanshgarg2641 11 ай бұрын
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-yc8lm
@RohitSharma-yc8lm 2 жыл бұрын
UNDERSTOOD
@gangsta_coder_12
@gangsta_coder_12 Жыл бұрын
Understood 😁😁
@chandantaneja6388
@chandantaneja6388 2 жыл бұрын
Understood 👍
@Chandraprakash-kx4ic
@Chandraprakash-kx4ic Жыл бұрын
understood sir❤
@kushagramishra5638
@kushagramishra5638 2 жыл бұрын
"Understood"
@AnmolSharma-br2eg
@AnmolSharma-br2eg 2 жыл бұрын
Understood!
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
amazingg intuition
@subee128
@subee128 Жыл бұрын
Thanks
@oqant0424
@oqant0424 2 жыл бұрын
Understood !!!!!!!!!!!!!
@LoayThePhrygian
@LoayThePhrygian 6 ай бұрын
Understood^^
@sayakghosh5104
@sayakghosh5104 2 жыл бұрын
understood!!
@cypher7536
@cypher7536 2 жыл бұрын
understood ❤️❤️
@AnkitKumar-ko5tr
@AnkitKumar-ko5tr 2 жыл бұрын
Understood
@sannareddymonesh7598
@sannareddymonesh7598 2 жыл бұрын
Understood!!
@shreyanshkumarsahu6534
@shreyanshkumarsahu6534 2 жыл бұрын
Understand 🥰🥰
@dreamyme543
@dreamyme543 2 жыл бұрын
Understood:)
@rayyansyed2998
@rayyansyed2998 Жыл бұрын
"understood"
@snehiltiwari28
@snehiltiwari28 Жыл бұрын
understood :)
@teetanrobotics5363
@teetanrobotics5363 2 жыл бұрын
understood
@rickk3300
@rickk3300 Жыл бұрын
Why didn't we take a visited array in this code?
@GirishJain
@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
@shivamnegi7552 Жыл бұрын
whats the intuition
@pratyakshhhhhhhhhhhhhhhhhhhhh
@pratyakshhhhhhhhhhhhhhhhhhhhh Жыл бұрын
🎉
@codingalley6229
@codingalley6229 Жыл бұрын
🐐
@akashkumarsingh8369
@akashkumarsingh8369 Жыл бұрын
understood lo bhai kr diya comment ye emoji(😞😞😞) mt kra kro yarr
@aniketambat6334
@aniketambat6334 Жыл бұрын
understood++
@vaibhavjangid3112
@vaibhavjangid3112 3 ай бұрын
:beatin💋💋💋
@soicooc3500
@soicooc3500 7 ай бұрын
4:00
@antassachan1782
@antassachan1782 2 жыл бұрын
leetcode link for the question?
@deepakjain4481
@deepakjain4481 9 ай бұрын
//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
@cypher7536
@cypher7536 2 жыл бұрын
0:19 kya kaha 🤣🤣...?
@aryankumar3018
@aryankumar3018 Жыл бұрын
Understood ntr
@mdsameer533
@mdsameer533 2 жыл бұрын
us
@bemypro8602
@bemypro8602 2 жыл бұрын
what happened to ur neck
@Harsh-jc2bz
@Harsh-jc2bz 2 күн бұрын
0:19 gand?
@pranavkhairnar1290
@pranavkhairnar1290 Жыл бұрын
Hey can anyone explain why he did not used visited array in this problem??????
@shivamjain9968
@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.
@lonersunited
@lonersunited 9 ай бұрын
Understood 😊
G-21. Topological Sort Algorithm | DFS
13:30
take U forward
Рет қаралды 330 М.
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 9 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 157 МЛН
G-24. Course Schedule I and II | Pre-requisite Tasks | Topological Sort
11:32
G-27. Shortest Path in Directed Acyclic Graph - Topological Sort
26:36
take U forward
Рет қаралды 241 М.
Topological Sort Algorithm | Graph Theory
14:09
WilliamFiset
Рет қаралды 472 М.
G-41. Bellman Ford Algorithm
27:43
take U forward
Рет қаралды 233 М.
Topological Sort | Kahn's Algorithm | Graph Theory
13:32
WilliamFiset
Рет қаралды 128 М.
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 9 МЛН