Тёмный

Breadth-First Search (BFS) | Traversal Technique in Graphs 

take U forward
Подписаться 699 тыс.
Просмотров 176 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 237   
@takeUforward
@takeUforward 3 года назад
I will be posting a video every Sunday on the other channel, so bell DABAAAAA dena: ru-vid.com/show-UCvEKHATlVq84hm1jduTYm8g
@krishnabhagat2716
@krishnabhagat2716 3 года назад
Your teaching is really awesome bhai, I have taken GFG dsa course but i unable to understand graphs concepts property but after watching your graph's video series now i can say i can any basic graph, Thank you for your quality content.
@omkarlavangad1434
@omkarlavangad1434 2 года назад
On GFG, their test cases have disconnected graphs but they expect the answer only to be the bfs traversal of component containing node numbered 0. Don't waste time. Just run your code without the loop. It will pass the test cases
@Shivanai_h
@Shivanai_h 2 года назад
Yes exactly i was doing the same using loop but then i dry run and got to know that they only want for vertex 0
@youcantgetme1126
@youcantgetme1126 2 года назад
Exactly 💯💯 thanks man....
@tanishsharma5440
@tanishsharma5440 2 года назад
Thanks buddy
@tanishsharma5440
@tanishsharma5440 2 года назад
Can you please tell me, what will be the difference between the code of the BFS of directed and undirected graph? They are same or not?
@omkarlavangad1434
@omkarlavangad1434 2 года назад
@@tanishsharma5440 yes directed or undirected doesn't matter for a normal bfs traversal
@deveshyadav7830
@deveshyadav7830 2 года назад
Striver, your explanation is always so good that after watching just 3-4 mins of your explanation I'm able to code and think the rest part on my own. Gives me a lot of confidence .Crystal clear concepts.
@Shourya_performs
@Shourya_performs 2 года назад
CLEAR CLEAN QUALITY CONTENT. U are providing a strong foundation for graph. Thank You
@rajeshg3570
@rajeshg3570 2 года назад
This is nice explanation and really helped me to grasp the template to write a BFS traversal code for a give graph. I've a suggestion though, whenever you are referring using 'Queue', try to draw it with a horizontal rectangular box, this vertical box would typically used to represent 'Stack'
@dhivyashri6554
@dhivyashri6554 3 года назад
Out of all the videos online, yours is the only one that made me understand the algorithm. Thank you! Commenting for the algorithm!
@j3ck390
@j3ck390 2 года назад
TC & SC: 13:26 Java code: 14:16 C++ solution: 17:12
@tusharnain6652
@tusharnain6652 2 года назад
for visited array, we can use visited arary of type bool instead of type int, this won't change the TC of SC at all, but it will use some lesser space.
@SDE_FACT
@SDE_FACT 3 года назад
Please please please make a series on tree🙏🙏🙏 The way you explain the topics are fantastic... Please help us in trees also
@lakshitajoshi9181
@lakshitajoshi9181 3 года назад
Can't believe that this topic was hard for me before i watched the video....but after watching the video it's like the most easy thing ..thank u so much sir ....this video was very helpful 🤩😇
@archanakumari1482
@archanakumari1482 3 года назад
Is it your teaching or my understanding level have raised...haha....Thanks bro, you made it simple.
@madhabkafle8898
@madhabkafle8898 2 года назад
Anyone explain this please!! in the beginning we put 1 in the queue and mark it as visited , then inside that for loop we also mark adjacency of node 1 , that is node 2 as visited , now in the second iteration of first for loop it will check if the node 2 is visited or not and found that 2 is already visited during first iteration where we checked adjacency nodes of 1 and found 2 their and marked it as visited ,then it will ignore the if condition and it will check for 3rd iteration.... so how do we find adjancency nodes of node 2 ? please anyone clear my doubt.
@priyanshugupta812
@priyanshugupta812 2 года назад
@@madhabkafle8898 check on 2's visit wont be performed immediately after 1's adjacent nodes are put inside the queue
@rakeshcharpe4478
@rakeshcharpe4478 3 года назад
This graph is so useful bhaiya Thank you , please make more series like this of different topics too, it's really helpful ...👍🏻
@arka6302
@arka6302 3 года назад
Quality at its peak as usual ! ♥️
@parthsalat
@parthsalat 2 года назад
This is the best channel for dsa cp on youtube!
@computernetworking6061
@computernetworking6061 3 года назад
These are some really good lectures 👍, one suggestion/request put timestamp in videos.
@sahil1053
@sahil1053 2 года назад
this is the best playlist to learn graphs
@ayeshasumaiya8280
@ayeshasumaiya8280 2 года назад
Before watching this video I was confused why we used that visited array The concept is crystal clear now
@degavathanandnayak9692
@degavathanandnayak9692 3 года назад
Great explanation! It is easily understandable with your video. Thank you so much!
@prashantdubey4112
@prashantdubey4112 2 года назад
bhai itna sahi padhate ho aapke video dekhta main hu place mera padosi ho gaya hai!
@akashkanni4622
@akashkanni4622 3 года назад
The outer for loop should iterate from 1 to n ..(not from 1 to v) , and the size of vis (visited) vector should also be equal to n ( not v) Correct me if i am wrong .........much needed series striver bhaiyya ...thank you :-)
@forgotten_menace
@forgotten_menace 3 года назад
n and v are the same thing(no. of nodes) here lmao
@mrjava18
@mrjava18 2 года назад
Problem Link : practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1/#
@DharmveerSingh-ct1um
@DharmveerSingh-ct1um 3 года назад
0 Dislikes. Power of Honesty.
@harpic949
@harpic949 3 года назад
Now there are 9🙂
@dhruvgupta4254
@dhruvgupta4254 2 года назад
you are really doing a great work sir ... thank you so much for this playlist !
@mansimishra7089
@mansimishra7089 2 года назад
I wish I could have watched this video before my Google STEP Intern interview😖. Thankyou bhaiya for amazing explanation.
@ashishchopra7491
@ashishchopra7491 2 года назад
Amazing explanation! This topic always went above my head before this video.
@vaibhavdeshmukh7402
@vaibhavdeshmukh7402 Год назад
Very helpful. Thanks for your efforts!
@chandanbanerjee3721
@chandanbanerjee3721 3 года назад
this is excellent...can you do the same series for binary tree and linked lists
@channelname4394
@channelname4394 2 года назад
Best explanation. understood , hope this channel reaches more people
@madhabkafle8898
@madhabkafle8898 2 года назад
Anyone explain this please!! in the beginning we put 1 in the queue and mark it as visited , then inside that for loop we also mark adjacency of node 1 , that is node 2 as visited , now in the second iteration of first for loop it will check if the node 2 is visited or not and found that 2 is already visited during first iteration where we checked adjacency nodes of 1 and found 2 their and marked it as visited ,then it will ignore the if condition and it will check for 3rd iteration.... so how do we find adjancency nodes of node 2 ? please anyone clear my doubt.
@prakharagarwal4933
@prakharagarwal4933 3 года назад
bhaiya after completeing this can you suggest problems we should try ??
@vedantagarwal22
@vedantagarwal22 3 года назад
yes
@kaushikramabhotla4635
@kaushikramabhotla4635 3 года назад
try hackerrank graph theory sums
@pranjwalsingh7615
@pranjwalsingh7615 3 года назад
What a video series just before internship drive in my college
@sayantaniguha8519
@sayantaniguha8519 2 года назад
For *visited* array, Why take 'V+1' nodes and start from index 1? Why not 'V' nodes and start from index 0?
@yadneshkhode3091
@yadneshkhode3091 2 года назад
V will also suffice if starting node is 0 else we will have to use V+1
@adityabalodi1185
@adityabalodi1185 2 года назад
Great video. 1 thing, I was checking out your java solution, it doesn't cover disconnected components.
@jayadubey_22
@jayadubey_22 3 года назад
Thanks for the series bhaiya🙌
@cinime
@cinime 2 года назад
Understood! Thank you so much as always!!
@shahzebkhan1770
@shahzebkhan1770 3 года назад
You are doing Great,Good explanation.
@hemrajjeure2210
@hemrajjeure2210 3 года назад
What changes should we need to make if the graph starts from 0, In for loop if we write i=0;i
@takeUforward
@takeUforward 3 года назад
The code works at gfg.
@glint695
@glint695 3 года назад
Check the code given in description as well
@aditya14-02
@aditya14-02 3 года назад
you are crrt it doesnt work with for loop
@aditya14-02
@aditya14-02 3 года назад
because the for loop is for multiple components where as in gfg the test case is for single component only
@hemrajjeure2210
@hemrajjeure2210 3 года назад
@@aditya14-02 yup you are right
@PrashantSingh-jy6zp
@PrashantSingh-jy6zp 3 года назад
for skip promotion go to 4:22
@suhitgupta9429
@suhitgupta9429 2 года назад
Great Explanation btw for loop logic is not working on gfg why is it so ????
@aniketbasu3865
@aniketbasu3865 2 года назад
yes its not working
@RAJENDRASHARMA-xt8uu
@RAJENDRASHARMA-xt8uu 2 года назад
very great explanation
@rishavkavlog
@rishavkavlog 2 года назад
code explained and provided in description box is different , I understood but it creates confusion. Anyway loving watch and learn
@rishavkavlog
@rishavkavlog 2 года назад
Code provided is for single component and code explained in video is for multiple component
@shreyabagati8020
@shreyabagati8020 2 года назад
What changes do we need to make if the source node is not 1 but some other node?
@arjunkurariya2235
@arjunkurariya2235 3 года назад
1st comment. Bhaiya U are great ❤️.pls make video on CHEAT sheet on CP for Coding Round pls🙏🙏
@takeUforward
@takeUforward 3 года назад
Cp sheet striver youtube me search kro
@arjunkurariya2235
@arjunkurariya2235 3 года назад
@@takeUforward Mill gya Bhaiya video. Now I will follow it. Thanks bhaiya❤️❤️
@gauravupreti4398
@gauravupreti4398 2 года назад
Crystal Clear
@vikashsharma5777
@vikashsharma5777 2 года назад
Thank you Raj bro. However could u pl help me understand why you are telling time complixty is O(n) ? you have outer for loop then while loop and then for loop inside. so time complicity is l*m*n of not n^3 please let me know
@amarjeetkumarsingh733
@amarjeetkumarsingh733 2 года назад
Have you got the answer?
@pratikthorat2314
@pratikthorat2314 2 года назад
Great Explanation.
@someshsangwan5302
@someshsangwan5302 2 года назад
Bhaiya your code is not working actually you take while loop inside if condition . /*correct code*/ vector bfsOfGraph(int V, vector adj[]) { vectorv; vectorvisited(V+1,0); queueq; q.push(0); visited[0]=1; while(!q.empty()){ int node=q.front(); q.pop(); v.push_back(node); for(auto it:adj[node]){ if(visited[it]==0){ q.push(it); visited[it]=1; } } } return v; }
@afnanhasan4223
@afnanhasan4223 2 года назад
Concepts are crystal clear but would have been better if a BFS traversal for adjacency matrix was also covered
@purushottamkumar3140
@purushottamkumar3140 2 года назад
I think we can apply similar by creating a adjacency list from the adjacency matrix
@mohdhammadsiddiqui7598
@mohdhammadsiddiqui7598 2 года назад
Why the time complexity is 0(N+E) but not 0(N+NE) because for reaching 1 node it will take 0(1) and in the worst case every node is connected with every node so accessing all adjacent nodes for 1 node will take 0(E) if we repeat the task for N node dont we get 0(1)+0(1)+.......n => 0(N) for all N nodes and for all adjacent nodes it will be N *0(E) so total will be 0(N+NE) ?
@dillitimalsina6003
@dillitimalsina6003 2 года назад
No because here we used visited array if adjacent node is already visited then we dont visit it twice so i guess t.c should be o(n) but also don't know why t.c=o(n+e)
@mithileshkumarsingh222
@mithileshkumarsingh222 2 года назад
TAKE A BOW MAN🙇‍♀🙇‍♀🙇‍♀🙇‍♀🙇‍♀
@karanudayandas1608
@karanudayandas1608 3 года назад
19:25 the c++ code
@vaibhavtiwari6540
@vaibhavtiwari6540 3 года назад
Constructive criticism: Striver if you explained how the frontier kind of ripples out I would have made sense quicker to me.
@rohinianekar61
@rohinianekar61 2 года назад
nice explanation , thanks
@sayantaniguha8519
@sayantaniguha8519 2 года назад
The value of 'i' is a constant incase the starting point is mentioned from earlier. Right?
@ekanshsanger8356
@ekanshsanger8356 2 года назад
adjacency list just doesn't print edges in increasing order. WILL IT AFFEct the PATTERN OF TESTCASES IN ANY PROBLEM OF GRAPHS?? Pls answer...
@bhaveshkumar6842
@bhaveshkumar6842 2 года назад
Thank you, Striver!!!
@pasitopasito1967
@pasitopasito1967 3 года назад
Nice explanation
@saurabhpatle8744
@saurabhpatle8744 2 года назад
Well explained!!
@rahulsati5819
@rahulsati5819 2 года назад
amazing brother
@r.apuroop4034
@r.apuroop4034 2 года назад
Understood Brother!
@ghoshdipan
@ghoshdipan 3 года назад
It makes sense to use the for loop. Why is that gfg accepts the solution without the for loop?
@takeUforward
@takeUforward 3 года назад
Does not have a test case with multiple components
@lavishgarg5090
@lavishgarg5090 3 года назад
@@takeUforward bhaiya after completeing this can you suggest problems we should try ??
@ankitdwivedi4213
@ankitdwivedi4213 2 года назад
@@takeUforward No! It has a Note: One can move from node u to node v only if there's an edge from u to v and find the BFS traversal of the graph starting from the 0th vertex, from left to right according to the graph. Also, you should only take nodes directly or indirectly connected from Node 0 in consideration.
@ankitdwivedi4213
@ankitdwivedi4213 2 года назад
so we will just do it without for loop
@surajmaity6194
@surajmaity6194 2 года назад
Thank you
@dancodes887
@dancodes887 2 года назад
The size of the visited array should be (the value of the vertice having maximum value in the graph among all vertices+1) not (number of vertices+1) in the code
@SuperWhatusername
@SuperWhatusername 2 года назад
understood Thanks
@shivamtiwari9985
@shivamtiwari9985 2 года назад
Can we initialise the queue before for loop??
@devanshsingh6897
@devanshsingh6897 2 года назад
Is there any problem in taking the queue just before the start of the first for loop?
@radhe1335
@radhe1335 3 года назад
Mauj kardi Bhai 😁
@anshumansharma4580
@anshumansharma4580 3 года назад
Can anyone please help me in this: when instead of auto, I wrote- vector:: iterator it, then I need to replace 'it' in the loop with *it, so I want to ask that if we use auto, does that mean that we can simply use the variable as index? that is 'it' is no longer pointing to the cells of the vector, rather is working as indices?
@takeUforward
@takeUforward 3 года назад
Yes..it is the value itself..
@chetanraghavv
@chetanraghavv 2 года назад
when iterator is used, you need *it to access value because iterator is just like pointers in C++ for example: suppose we need to print values from key-value pairs in a map //auto can be changed to map::iterator for (auto it = mp.begin(), it!= mp.end(), it++) cout
@anshumansharma4580
@anshumansharma4580 2 года назад
@@chetanraghavv It is very informative. Thanks for sharing.
@sachinupreti7159
@sachinupreti7159 Год назад
didn't get this line i.e auto it:adj[x]. what is the another way of writing this ?
@aman_sahu
@aman_sahu 3 года назад
Heree it goes
@rashmi_gaur
@rashmi_gaur 3 года назад
Really great explanation!👍👏👏
@yadneshkhode3091
@yadneshkhode3091 2 года назад
thanks mate
@snehilmishra755
@snehilmishra755 3 года назад
I am taking the input in adjacency list in main function and also making the vis array in main function but when i am passing them in my find bfs function ,it is giving an error.Should I pass the reference??
@krishnabhagat2716
@krishnabhagat2716 3 года назад
I think you need to use arr.get(i).get (j) to access the every elements of array list.
@ram_pande8039
@ram_pande8039 3 года назад
thanks a lot lot lot... bhaiya.
@ggk656
@ggk656 3 года назад
Thanks a lot bhaiya ♥️✨
@sameera2426
@sameera2426 2 года назад
i implemented this on gfg, they are not considering multiple components of the graph
@letsinevolve6230
@letsinevolve6230 2 года назад
Plz help me with this doubt. I've always found it difficult to understand why Time and Space complexity is O(V + E) in both BFS as well as DFS? Why not O(V*E) or O(Max(V, E)), etc?
@dillitimalsina6003
@dillitimalsina6003 2 года назад
I am confused why not only o(n) why subtitle showed o(n+e),clearly it should be o(n) na
@letsinevolve6230
@letsinevolve6230 2 года назад
@@dillitimalsina6003 You need to specify what's 'n' when describing time and space complexity. Are you taking about number of vertices or edges?
@dillitimalsina6003
@dillitimalsina6003 2 года назад
@@letsinevolve6230 ​ @Let's InEvolve no of vertices, but you too didn't mentioned what Vand E are(just kidding), oviously it is vertices yr bcz we are travelling all vertices via edges and also visited array makes sure we won't travel same vertices twice,isn't it>
@letsinevolve6230
@letsinevolve6230 2 года назад
@@dillitimalsina6003 My bad😅. I got more confused now! You're right, we're not re-visiting any vertex.. so the complexity could be O(V). But I guess cuz we're looping within a recursion, the complexity certainly can't be just O(V) ...right?
@mfhussain29
@mfhussain29 3 года назад
I'm a beginner can some one clear my doubt, I feel this bfs in not universal😅 What if the starting index is 0 What if we want another starting vertex
@ayushtyagi4240
@ayushtyagi4240 2 года назад
you can start with any vertex .
@hitansupatra8383
@hitansupatra8383 3 года назад
plz make more series on other data structures like tree & linkedlist
@muktadirkhan6674
@muktadirkhan6674 3 года назад
C++ code at 19:26
@purellajajisaipavankumar651
@purellajajisaipavankumar651 3 года назад
What Happens if the values of nodes in graph doesnt start from 1,what if it starts from 10 or 20??
@varsha9094
@varsha9094 3 года назад
Even I got the same doubt...you got the answer??
@photoshopscreen3011
@photoshopscreen3011 3 года назад
if it starts from 10 then you make vector adj[N+11] and now just traverse from 10 to N+10 like this for(int i=10;i
@saipavanpurella9253
@saipavanpurella9253 3 года назад
@@varsha9094 It is fixed that values in graph starts from either 0 or 1!! or else we can subtract the value for example if we want to put the values like a,b,c by traversing the graph we get( Integer)(currchar-'a').
@sasikumarvm5172
@sasikumarvm5172 3 года назад
Instead of using array we could use map and insert all the vertices to map and iterate the map and when we vist the vertices make it true in map, so that we can acces it in constant time and TC and SC also will not exceed, also it will work if graph contains character
@sans.creates
@sans.creates 3 года назад
Thank you!!
@prantikofficial
@prantikofficial 3 года назад
If i complete this series does that mean that i have covered all the topics for graphs that are required for Placements and stuff?
@supratimbhattacharjee5324
@supratimbhattacharjee5324 3 года назад
Yes
@dillitimalsina6003
@dillitimalsina6003 2 года назад
100% true,but 100 to 150 etra question practice would be required to crack good company beside sde sheet
@piyushkumarsingh7628
@piyushkumarsingh7628 3 года назад
Start a series on LLD System Design
@ketandoshi5641
@ketandoshi5641 3 года назад
cannot open your course where u teach pls give me the link
@sumitmukharjee5816
@sumitmukharjee5816 3 года назад
bhaiayya, I have a question weather implementation with Adjacency list is good or with vector matrix??
@takeUforward
@takeUforward 3 года назад
Analyse complexity u will understand
@jackwalker2947
@jackwalker2947 3 года назад
See the 2nd video man ....don't skip any otherwise the base will not build up.
@sumitmukharjee5816
@sumitmukharjee5816 3 года назад
@@jackwalker2947 yeah it happened exactly I did not se sequence of the videos
@hitansupatra8383
@hitansupatra8383 3 года назад
@@sumitmukharjee5816 Adjacency list
@gauravshukla6675
@gauravshukla6675 3 года назад
itna achha bfs kisi ne nahi samjhaya aaj tak
@apurba_debnath
@apurba_debnath 2 года назад
Bhaiya, I have tried to implement bfs recursively: vector adj[n + 1]; int vis[n + 1] = {0}; queue que; void bfs(int i) { vis[i] = 1; cout
@Manish_Sahu
@Manish_Sahu 3 года назад
superb
@AB-tp9eg
@AB-tp9eg 3 года назад
bhaiya, i have written the code same to same as you had explain in this video, but i found this code is giving segmentation fault. But the code that you have gave us in github that was working. Why the code that was explained is not working? plzz explain
@takeUforward
@takeUforward 3 года назад
Wo gfg m 0-1 based indexing ka chakkar h. Wo dekh lena ho jaega
@yadneshkhode3091
@yadneshkhode3091 2 года назад
sir how to prevent stack overflow for huge data in recursion ?
@AbhishekKumar-fp7bm
@AbhishekKumar-fp7bm 3 года назад
where is the question link?
@sushantpandey2127
@sushantpandey2127 2 года назад
c++ code shown in this video is not working. why???
@joshrak3412
@joshrak3412 2 года назад
Can we do without queue.. I used recursion and recursion itself does the work what queue does here... void bfs(ll node, vector *arr, ll *visited){ if(!visited[node]){ cout
@shrutiagarwal6909
@shrutiagarwal6909 3 года назад
Can u please let me know that why u have used Integer node and not int ? Will it make any difference?
@takeUforward
@takeUforward 3 года назад
No.
@eshagusain9718
@eshagusain9718 3 года назад
thank you sir
@ketandoshi5641
@ketandoshi5641 3 года назад
what is that tc in cpp code .what does it mean MULTIPLE GRAPH.
@oqant0424
@oqant0424 3 года назад
for(auto it: adj[node]) could not understand this line.....can anyone help me out
@takeUforward
@takeUforward 3 года назад
Its for each loop. Since adj[node] means a vector hence it will iterate in it, and it will be iterating on every integer
@godgamer555
@godgamer555 3 года назад
First Class content
@aditya_jackdaw
@aditya_jackdaw 2 года назад
this is not working for directed graph
Далее
Cycle Detection in Undirected Graph using BFS
25:23
Просмотров 156 тыс.
Breadth First Search | Word Ladder | LeetCode 127.
16:25
Viral Video of a Man's Crazy Job Interview
16:02
Просмотров 1,5 млн
Chapter 1 | The Beauty of Graph Theory
45:23
Просмотров 86 тыс.
A Breakthrough in Graph Theory - Numberphile
24:57
Просмотров 996 тыс.
Intro to Binary Trees and Breadth First Traversal
21:40