• 3 days ago
Transcript
00:00Hi everyone, in this chapter, we are going to talk about Greedy Approach.
00:04If you listen to Greedy in English, this word means LALACH KERNA or LALCHI.
00:11In other words, LALACH KERNA, GREEDY HUNA LIFE is never good in life, we don't need to be greedy.
00:16But in Data Structures and Algorithms, Coders are specially an approach, a series of algorithms
00:22which we give a name of Greedy, and we solve all the problems with Greedy Approach.
00:28So, first of all, we have to think that Greedy is actually going to happen in Java or in programming.
00:35Greedy means that if you have a problem and you have to solve it in small steps,
00:41and you have to solve it in small steps, you have to find a final solution.
00:47So, in every small step, what will you do?
00:50A Greedy choice.
00:53Greedy choice means that if you have a choice,
00:56that if I can pick so many things in the solution,
00:59then I will choose the best choice,
01:02or to choose the best choice,
01:04that we will be greedy.
01:05Either we will chase the maximum value,
01:08or we will chase the minimum value.
01:10In this way, in every step,
01:12what will I do?
01:13I will take a Greedy choice.
01:14It will take a practical example.
01:17This is a practical example.
01:18We will see a real life example.
01:19What will it be?
01:19Greedy choice.
01:20We will think of a company.
01:23We will think of a company.
01:24We will recruit a HR company.
01:27What do you do usually?
01:29First of all,
01:29you will take an online test.
01:31So, round one,
01:33you can take a lot of students' online test.
01:37Let's say,
01:38it will take 1000 students' online test.
01:40After that, my round two comes.
01:42In round two,
01:43let's say,
01:44let's say,
01:44let's say,
01:46there are some 100 candidates.
01:48Shortlist.
01:49After that,
01:50let's say,
01:50there was a round three.
01:52There was another interview.
01:55And this interview,
01:56you know,
01:56HR round,
01:57which he called 20 people.
01:59And finally,
02:00what did he do?
02:01Finally,
02:0110 people hired.
02:04So,
02:04some kind of scenario,
02:06in real life,
02:06there is such a hiring.
02:08If you talk about on-campus hiring,
02:09off-campus hiring.
02:11Usually,
02:12we have to go first,
02:12coding test.
02:13There are many candidates.
02:15Then,
02:16the company will filter something.
02:17One hundred candidates will take some.
02:19we will filter something.
02:19Then,
02:20Then,
02:2020 candidates will take.
02:21Then,
02:22we will hire 10 people.
02:23Now,
02:24logically,
02:24we can think.
02:25That may happen,
02:26if someone's online test is bad.
02:27But,
02:28as opposed to the interview,
02:29they have a good skill.
02:31So,
02:31why does a company not do that?
02:33The people who have 1000 candidates,
02:34who have 10 people have online test,
02:36they have 1000,
02:37they have 1000,
02:38they have 1000,
02:39they have 1000,
02:39and they have 1000.
02:40You should have 1000 opportunities.
02:43We need to participate in every round, so if we think that if we always go with fair things,
02:51then we need to participate in every type of round, because the company is a little greedy.
02:59But this is the second approach in which all candidates are participating in all rounds,
03:04in this approach there will be a lot of time, effort will be a lot of people,
03:08and the resources will be a lot of people.
03:12There will be a lot of online coding tests for 1000 kids,
03:14but there will be a lot of HR in the round,
03:18so there will be a lot of constraints here,
03:21because of the constraints, because of the limitation,
03:23we can't explore all possible solutions.
03:28So what does the company do?
03:29The company has a greedy approach,
03:32it means that it has released the best solution in the online test,
03:34the best solution,
03:36which are my top 100 kids.
03:38They will be shortlisted,
03:39and they will have the best solution in the interview,
03:41which are my top 20 students.
03:43After that, they will have the best solution in the interview,
03:45and then they will have the best approach in the interview,
03:46which is my top 10 kids,
03:48I will take them.
03:49So in every step,
03:51what did the company do?
03:52The company has picked a solution,
03:55and this is the solution,
03:56we call them a local solution.
03:58Local means,
03:58this means,
03:59here are the local ones,
04:01here are the 100 students,
04:02that will be the local solution.
04:05Round 2,
04:06where I will get 20 good students,
04:08that will be the local solution.
04:10Round 3,
04:11where I will get 10 good kids,
04:13that will be the local solution.
04:15So,
04:15we get the local solution,
04:17and we get the global solution,
04:19that is my final solution.
04:23You got the better solution.
04:24the company,
04:25in this case,
04:25a greedy approach is like this.
04:27Be ready,
04:29that's all,
04:30this is a new one year,
04:31where the best solution Pass.
04:35In this case,
04:35when thedagel has found their best solution,
04:36the company's best solution in the interview,
04:36that they will have at least 10 candidate.
04:39This will always have a well.
04:41You will tend to win.
04:42Both candidates already have one of the best candidates
04:46but the company seems to be fixed for the statement.
04:5110 hires आएंगे, यह best candidates होंगे
04:54तो एक practical example है कि
04:56greedy algorithm काम कैसे करता है
04:57greedy algorithm को इसलिए use करते हैं
04:59क्योंकि कुछ-कुछ cases में सारे solutions
05:01explore करने से अच्छा है
05:03कि एक हम छोटा सा shortcut ले ले
05:05और उस shortcut की मदद से
05:07हम हमेशा अपने locally optimum solution को
05:10pick करें, यानि locally best solution को
05:12pick करें, local best को pick करने का
05:13मतलब है हर stage of solution में
05:15थोड़ा सा greedy हो जाना, अब greedy approach
05:17के बारे में थोड़ी सी चीजें हम और पढ़ते हैं
05:20जो greedy approach होती है
05:21generally optimization problems के लिए
05:23use होती है, optimization problem
05:26उस तरीकी की problem है, जिसमें
05:28हमें या तो किसी भी problem
05:29का minimum value निकालना है, यानि
05:31final solution कुछ minimum होना चाहिए
05:33या फिर final solution कुछ maximum होना चाहिए
05:36usually most of the
05:38greedy algorithms होती है
05:39या जो solution greedy algorithm को
05:41मिलकर बनते हैं, उनके अंदर
05:43हम sorting का इस्तमाल करते हैं
05:45यानि पहले मुझे अगर कोई data दिया होता है
05:48तो उस data को मुझे sort करना होता है
05:50उसके बाद उस पर कोई में code
05:51apply कर सकती हूँ
05:52तीसरी चीज़ जो greedy algorithms के बारे में
05:55पता होने चाहिए कि उनका कोई fixed rule नहीं होता
05:58जैसे merge sort हमने
06:00एक algorithm पड़ी थी
06:01उसके अलावा हमने quick sort
06:03algorithm पड़ी थी, उसके अलावा हमने
06:05binary search पड़ा हुआ है, linear search पड़ा हुआ है
06:08आफी सारी algorithms हम अभी तक पड़ते आए है
06:10इन सबका एक fixed approach होती है
06:12एक fixed rule होता है
06:14जिस हिसाब से चलते है
06:15merge sort के अंदर मुझे पता ये कि मैं अपने array को
06:17दो parts में divide करने वाली हूँ
06:19तो जो मेरा left part होगा
06:20and मेरा right part होगा
06:22फिर मुझे इन दोनों को individually sort करके
06:24merge करना है
06:25ये fixed rule होता है
06:27जो हमेशा merge sort के अंदर
06:28मुझे follow करना होता है
06:29but greedy algorithms के अंदर
06:31ऐसा कोई fixed rule नहीं होता
06:33इसलिए greedy algorithms एक तरीके से
06:35ad hoc basis पर चलती है
06:37यानि जिस तरीके की problem है
06:38वैसा साही हम solution निकालेंगे
06:40तो there is no fixed rule
06:42चाथी चीज जो हमें greedy algorithms
06:44के बारे में पता हो नीचे
06:45वह यह है कि कभी-कभी ऐसा हो सकता है
06:47कि आज तक हमने बहुत सारे greedy algorithms
06:49किये हो पहले वाले data structures के अंदर
06:52पहले वाले chapters के अंदर
06:53और हो सकता है
06:54इस chapter के बाद भी हम बहुत सारे
06:56ऐसे problems करें
06:56जिनको हम greedy की मदब से solve करेंगे
06:58पर हो सकता है हम वहाँ पर realize ना करें
07:01कि मैंने greedy apply किया है या नहीं किया
07:03ऐसा नहीं है कि greedy algorithms को
07:05हम सिर्फ इस chapter के अंदर कर रहे होंगे
07:07इस chapter के अंदर हम basically greedy का मतलब समझेंगे
07:10और बहुत सारे problems को greedy algorithms
07:13या greedy approach की मदद से solve करेंगे
07:15पर हो सकता है आने वाले chapters के अंदर भी
07:18हम बहुत सारे ऐसे problems करें
07:20जिनमें हम greedy approach का यूज करेंगे
07:22तो greedy एक ऐसी चीज है जिसको
07:24हम usually कभी में भी करते हुए
07:25realize नहीं करते हैं
07:26इसलिए best way to identify कि
07:28कि किसी भी problem को हम greedy approach की
07:30मदद से solve कर रहे है या नहीं करें
07:31कि हम जादा से जादा questions को
07:34practice करें जितना जादा हम
07:36approach को एक बार समझ लेंगे उसके
07:37classical questions कर लेंगे assignments कर लेंगे
07:39उसके बाद जैसे-जैसे हम
07:41जादा questions को अपनी तरफ से practice करेंगे
07:43तो थोड़े बाद time बाद हमें
07:45हमें खुद से ही realize होना शुरू हो जाएगा
07:47अगर हर एक step पर मैं अपने लिए
07:50best solution pick कर रहा हूं तो हाँ
07:52there is a major possibility कि
07:53यहां पर greedy approach use हो रही है
07:55तो इसलिए greedy को use करने
07:57का greedy को और जादा familiarize होने
07:59का सबसे बैस तरीका है कि हम
08:01इस approach को और जादा questions के अंदर
08:03practice करें अब एक बार formal
08:05definition की बात कर लेते हैं
08:07greedy की definition यह कहती है कि
08:09greedy algorithm is the problem solving technique
08:11where we make the locally optimum
08:13choice at each stage इसका मतलब
08:15यह है कि अगर मेरे पास कोई question आया
08:17और मुझे उसका solution निकालना है
08:19तो solution को निकालने के लिए
08:20जो different different मेरे पास steps होंगे
08:22हर एक step पर मुझे अपना
08:25locally optimum choice लेना है
08:27optimum कहने का मतलब होता है
08:28English के अंदर best choice
08:32तो अगर solution निकालने की तरफ में
08:34कदम बढ़ रही हूं तो हर एक कदम पर
08:36अगर मैं best solution निकालूं
08:38तो मैं यह hope करती हूं कि जो मेरे steps
08:40हैं हर बार मेने best step लेने की कोशिश
08:42करी है तो final जो end result
08:44जो मेरा final solution आएगा
08:45वो भी एक best step होगा
08:47यानि वो भी मेरा global optimum solution होगा
08:50यह हमारी hope होती है
08:52तो हम greedy की मदद से हर एक step पर
08:54locally best solution चूज करते है
08:56और hope करते है final जो output आएगा
08:58वो मेरा global optimum यानि
08:59global best solution होगा पर जैसे
09:01life में हमने कही बार क्या देखा है कि life
09:03में हम क्या करते है हर step पर
09:05अगर कोई व्यक्ती greedy होने की कोशिश करता है
09:07greedy होने की कोशिश करता है
09:09तो कभी-कभी life के अंदर end result
09:11लालच का हमेशा अच्छा नहीं होता
09:13वैसे ही greedy अगर
09:15हम algorithms के अंदर apply करें
09:17coding में तो वहाँ पर भी locally
09:19optimum solution लेने के बाद भी
09:20ऐसा बहुत सारे problems में होता है
09:23कि final solution हमें optimum
09:24ना मिले तो वहाँ पर हम parallel
09:27techniques लगाते हैं वहाँ पर हम dynamic
09:28programming का use करेंगे branch and bound
09:30का use करेंगे इनको हम आगे पढ़ रहे होंगे
09:32पर अभी हम focus कर रहे हैं greedy approaches पर
09:35तो greedy जो है maximum
09:37problems के लिए greedy approach fail हो जाता है
09:39बहुत ही specific cases होते हैं
09:41जिनके अंदर greedy approach मुझे best result
09:42लाकर देगा और उन cases को
09:44मुझे practice के साथ ही जादा
09:46realization आएगी कि वो case कौन से है
09:48अब pros and cons की बात करूँ तो
09:50greedy जो है बहुत ही simple and easy to
09:52understand होता है यानि आप हर
09:54solution जो question हम discuss कर रहे होंगे
09:56उनमें notice करेंगे हमें क्या करना होता है
09:59data को वह sort कर दो और
10:00उससे locally optimum को build करते
10:12इसका सबसे बड़ा con सबसे बड़ी
10:14problem greedy approach के साथ यह है
10:17कि lots of times मेरा
10:18global optimum है वो achieve ही नहीं होता
10:21यानि हर बार में greedy होकर
10:22सोचू हर बार में लालश के साथ सोचू
10:24तो ऐसा बहुत बार होता है कि
10:26मेरा final solution सही नहीं आता
10:28इसलिए greedy approach को हम बहुत ही particular
10:30cases में apply कर पाते हैं