Greedy Algorithms Explained

Поделиться
HTML-код
  • Опубликовано: 11 июл 2024
  • Welcome to another video! In this video, I am going to cover greedy algorithms. Specifically, what a greedy algorithm is and how to create a greedy algorithm. I'll also show you some examples of greedy algorithms.
    💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" algoexpert.io/techwithtim
    📄 Resources 📄
    Definitions From: brilliant.org/wiki/greedy-alg...
    ⭐️ Timestamps ⭐️
    00:00 | Overview
    01:28 | What Are Greedy Algorithms?
    04:02 | Greedy Algorithm Properties
    05:42 | Fractional Knapsack Problem
    15:03 | Knapsack Problem
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    💰 Courses & Merch 💰
    💻 The Fundamentals of Programming w/ Python: tech-with-tim.teachable.com/p...
    👕 Merchandise: teespring.com/stores/tech-wit...
    🔗 Social Medias 🔗
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My RUclips Gear 🎬
    🎥 Main Camera (EOS Canon 90D): amzn.to/3cY23y9
    🎥 Secondary Camera (Panasonic Lumix G7): amzn.to/3fl2iEV
    📹 Main Lens (EFS 24mm f/2.8): amzn.to/2Yuol5r
    🕹 Tripod: amzn.to/3hpSprv
    🎤 Main Microphone (Rode NT1): amzn.to/2HrZxXc
    🎤 Secondary Microphone (Synco Wireless Lapel System): amzn.to/3e07Swl
    🎤 Third Microphone (Rode NTG4+): amzn.to/3oi0v8Z
    ☀️ Lights: amzn.to/2ApeiXr
    ⌨ Keyboard (Daskeyboard 4Q): amzn.to/2YpN5vm
    🖱 Mouse (Logitech MX Master): amzn.to/2HsmRDN
    📸 Webcam (Logitech 1080p Pro): amzn.to/2B2IXcQ
    📢 Speaker (Beats Pill): amzn.to/2XYc5ef
    🎧 Headphones (Bose Quiet Comfort 35): amzn.to/2MWbl3e
    🌞 Lamp (BenQ E-reading Lamp): amzn.to/3e0UCr8
    🌞 Secondary Lamp (BenQ Screenbar Plus): amzn.to/30Dtafi
    💻 Monitor (BenQ EX2780Q): amzn.to/2HsmUPZ
    💻 Monitor (LG Ultrawide 34WN750): amzn.to/3dSD7tS
    🎙 Mic Boom Arm (Rode PSA 1): amzn.to/30EZw9m
    🎚 Audio Interface (Focusrite Scarlet 4i4): amzn.to/2TjXsih
    💸 Donations 💸
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    - Tech With Tim
    - Greedy Algorithm
    - Examples
    - Knapsack Problem
    - Fractional Knapsack Problem
    - Algoexpert
    ⭐️ Hashtags ⭐️
    #TechWithTim #GreedyAlgorithms

Комментарии • 108

  • @evgiz0r
    @evgiz0r 2 года назад +385

    transforming the 0 into 1 was too greedy :)

  • @ofiryaffe8223
    @ofiryaffe8223 2 года назад +3

    Nice video, also a good example because I actually understood on my own the greedy algorithm of the fractional knapsack problem. Keep making videos about algorithms !

  • @ashleydean9350
    @ashleydean9350 2 года назад +3

    Hello .
    I am new to this topic - however with your explanation and some maths background this is easy topic to understand.
    Excellent explanation.
    Tim you are doing a superb job- keep it up.

  • @kian180
    @kian180 2 года назад

    Love the content. Fair play to you keep it coming

  • @r125l6
    @r125l6 2 года назад

    Tons of thanks.. I hope you will continue to more algorithms

  • @dominator1699
    @dominator1699 2 года назад +2

    You always upload a video on the topic I wanna learn haha. (btw idk if you knew this but fresca in arabic means a type of sweet snacks :D )

  • @Jkindelb
    @Jkindelb 2 года назад

    Awesome ty for the explanation! Any chance on going over a flow (vector) field in the future?

  • @yvanbrunel9734
    @yvanbrunel9734 2 года назад

    Thank you very much!!
    I'll try implementing this in a c++ program.

  • @whyisknight6taken
    @whyisknight6taken 2 года назад +23

    why isn't it 4,3,2,0 for a sum of 9? at 3:12

    • @woolfool
      @woolfool 2 года назад +2

      It also confuses me. Where does the 1 come from?

    • @daverobertson8399
      @daverobertson8399 2 года назад

      @@shdwshdw-mu6vo If 4 = 4 and 3 = 3, then 0 does not = 1

    • @sukhmandersingh4306
      @sukhmandersingh4306 2 года назад +2

      @@shdwshdw-mu6vo no that's not true, 0 and 1 are completely different. Values other than 0 maybe treated as 1 in some cases but i have never seen 0 treated as 1. Tim most likely just made a mistake there.

    • @antoine_9667
      @antoine_9667 2 года назад +4

      it shouldn t he messed up :)

    • @samcarter9111
      @samcarter9111 Год назад +3

      I'm sure it was a mistake

  • @ananthramvijayaraj4554
    @ananthramvijayaraj4554 2 года назад +2

    I loved this video. Please continue this algorithms and data structures series

  • @sumukhjagirdar5860
    @sumukhjagirdar5860 2 года назад +55

    Hi Tim. Today is my birthday. I love your content

  • @pedrorequio5515
    @pedrorequio5515 2 года назад

    Do a series on Metaheuristics, the video simplifies alot what I know is a very difficult subject, not all problems are small enough to solve like this in code or have simple heuristics to solve. I am interested in more videos on this kind of algorithms of optimization.

  • @Rebeljah
    @Rebeljah 2 года назад +24

    I already had the answer to optimal solution but only because this is EXACTLY what you do in your head when you are comparing prices at a grocery store. Maximizing the value/size ratio to stay within a certain limit (your grocery budget).

    • @radicalpotato666
      @radicalpotato666 Год назад

      The grocery budget is typically a range, so I think the greedy algorithm may be quite helpful.

    • @mintlatte1376
      @mintlatte1376 5 месяцев назад +1

      well, in my head I don't have to write a code to make it work, every one walks through the logic and no one shows how on earth I should code all this(

  • @736939
    @736939 2 года назад +75

    Tim, please more videos like this. 20-30 videos of Greedy algorithms then 20-30 videos of Dynamic programming, then Divide and Conquer - this is the real programming: Django, Flask, React everyone can learn fast, but Algorithms are more important.

    • @dylanwilliams9860
      @dylanwilliams9860 2 года назад +11

      Do you realise how much work goes into even a single video? The research and fact checking, development, storyboarding, editing and all the steps I didnt mention mean a 20 minute video for us takes 2-4 days each to make. 20 videos is way way way overkill for just about anything you could want to learn. You would actually want like 3-5 videos on it as thats an hour and a half of lectures, is more than enough to explain a topic, and won't take him a full year to get out.

    • @rortox1539
      @rortox1539 2 года назад +5

      @@dylanwilliams9860 This. Most people dont realize that they get literal days of work for absolutely free on RUclips

    • @736939
      @736939 2 года назад

      @@dylanwilliams9860 Yes, I realize, but anyone can spend 15-20 minutes for creating video to discuss only one competitive programming problem.

    • @dylanwilliams9860
      @dylanwilliams9860 2 года назад +3

      @@736939 first off, competitive programming is fun. But its full of bad practices that should never be within 300 light-years of production code. Secondly, I just said the videos take days to create and check. Most Vlogs even take several hours worth of takes for big channels. The only time something takes "20 minutes" is in your head or on stream.

    • @joshk9328
      @joshk9328 2 года назад +3

      Or maybe you could create your own youtube channel and let people upload the content that they want?

  • @Raghav1205
    @Raghav1205 2 года назад +10

    Hi Tim can we expect more questions on algoexpert?

  • @khimleshgajuddhur6892
    @khimleshgajuddhur6892 2 года назад +24

    3:12
    you said: we select 0 but how can you get 1 then 😕😁😢

    • @RobertPorterNZ
      @RobertPorterNZ 2 года назад +1

      Yeh I was thinking the same... max total would be 9?

    • @khimleshgajuddhur6892
      @khimleshgajuddhur6892 2 года назад

      @@RobertPorterNZ yes🙄

    • @rbrojas2040
      @rbrojas2040 2 года назад

      I think it's an error, you should bring down 0, which would make the sum 9, like @Robert Porter said.

  • @judedavis92
    @judedavis92 2 года назад

    Now this is more like it!! 🔥

  • @aravindhang1264
    @aravindhang1264 2 года назад +1

    I would like to see contents on dynamic programming too tim

  • @ajaswanth6607
    @ajaswanth6607 2 года назад +15

    Why 1 instead 0

  • @kietphamhoanganh5641
    @kietphamhoanganh5641 2 года назад

    thank you so much!

  • @noobypro4560
    @noobypro4560 2 года назад

    yeaaaaaa boii tim made an algorithm video
    this gonna be amazing

  • @sanduchicu7545
    @sanduchicu7545 2 года назад +1

    love how you explain

  • @pythonenthusiast9292
    @pythonenthusiast9292 2 года назад +2

    Can we expect a playlist on greedy or DP?

  • @futhedude4848
    @futhedude4848 10 месяцев назад

    this is just explain for "Fractional Knapsach Problem" solved by "Greedy Algorithms", there are many more Problems we can use "Greedy" to resolve.
    but this clip is good at explaining so well done.

  • @CodeWithJoe
    @CodeWithJoe 2 года назад

    Good video dude, how would you implemnt this algo in python, can you do a practical example

  • @evantoomey6712
    @evantoomey6712 2 года назад

    Great video. I have this exact problem in Skyrim.

  • @Cookie-mv2hg
    @Cookie-mv2hg 2 года назад +3

    I'm all in for an algorithm series from Tim ! I'll even pay for that!!

  • @codeaperture
    @codeaperture 2 года назад +3

    We're greedy more algorithms in future 🔥

  • @kasyapdharanikota8570
    @kasyapdharanikota8570 2 года назад

    please more videos like this

  • @shawnbeans7389
    @shawnbeans7389 2 года назад

    tim when are you doing more podcasts? pls reply

  • @PP-tc1zp
    @PP-tc1zp 2 года назад

    Hi Tim
    Can You make tutorial about implemate ai to some game(like birds example) in python? It was super.

  • @justins7796
    @justins7796 2 года назад +3

    Algorithm: *Laughs* *in* *Mr* *Krabs*

  • @demonslayer8502
    @demonslayer8502 2 года назад

    Awesome 😎

  • @cuteflygon
    @cuteflygon 15 дней назад

    funny thing is 18 is also the optimal solution for non-fractional units, isn't it? A lucky example :p

  • @ys98110
    @ys98110 2 года назад +1

    ??? If you can get fractions of item, why not just select the best value/size item for all? What's the point in just getting one of it? Really don't understand how this derives any optimal solution.

  • @DaigoMatsuoka
    @DaigoMatsuoka 2 года назад

    And what about the code how they look like?

  • @korsik4559
    @korsik4559 2 года назад +1

    def algorithm(items, capacity):
    ratio = sorted([(size, value, value / size)
    for size, value in items], key=lambda tup: tup[2], reverse=True)
    total_size = total_value = 0
    for size, value, _ in ratio:
    new_size = total_size + size
    if new_size > capacity:
    break
    total_size = new_size
    total_value += value
    else:
    return total_value
    return total_value + (capacity - total_size) / size * value
    items = ((22, 19), (10, 9), (9, 9), (7, 6))
    capacity = 25
    print(algorithm(items, capacity))

  • @heh2k
    @heh2k 2 года назад +1

    Greedy means the solution is self-similar - it works at smaller scales (subsets) within the same input.

  • @alihusham1560
    @alihusham1560 2 года назад

    I would solve it with recursion just like the coins exchange problem

  • @sunitmody6064
    @sunitmody6064 2 года назад +1

    This is really useful Tim, but you might want to be slightly more careful with the numbers you say and write on the screen lol. A couple of times you misspoke or mis-wrote.

  • @vishwakamps
    @vishwakamps 2 года назад

    the handwriting is not so bad man.
    Its better than mine 😆

  • @ajaswanth6607
    @ajaswanth6607 2 года назад +4

    Expecting more ds and algo

  • @unknownman5296
    @unknownman5296 2 года назад +1

    please make a algoithm and data structures tutorial

  • @harshit.chitkara
    @harshit.chitkara 2 года назад

    Wassup Fresca? How's life on Tim's keyboard?

  • @mohitjain4943
    @mohitjain4943 2 года назад +4

    NEED MORE DS AND ALGO!❤❤❤❤

  • @fojifojigurjar2461
    @fojifojigurjar2461 2 года назад

    Brother Mico graddy solution please

  • @HuanYinKoh1995
    @HuanYinKoh1995 2 года назад +1

    Will Dynamic Programming (DP) be explained in the video? hahaha

  • @thenewelegance7772
    @thenewelegance7772 2 года назад

    my hash rate is 0.053mh/s why ;-; rx 470 4gb binance

  • @TheKiryu.
    @TheKiryu. 2 года назад +1

    I've never thought that a fractional knapsack problem could be much easier to solve than a original DP knapsack problem 😂

  • @pv9060
    @pv9060 2 года назад +4

    Fun Fact: Texting robots on Mars use python to send images to the earth. It
    uses request module to communicate with the API on mars.

  • @mrolivernone4040
    @mrolivernone4040 2 года назад

    Hey tim! what's up!

  • @Ben-jw7yw
    @Ben-jw7yw 2 года назад

    Are we just suppose to ignore his cat? 0:09

  • @flyte9844
    @flyte9844 2 года назад

    that's a backpack of holding (for the tibia player 🤪)

  • @Knuddelfell
    @Knuddelfell 2 года назад

    hey there 😃

  • @lonelyboy8640
    @lonelyboy8640 2 года назад

    can you put indonesian text translite plz🥺

  • @raghunathmahakud3725
    @raghunathmahakud3725 2 года назад +1

    not clearly able to understand

  • @criss5404
    @criss5404 2 года назад +2

    Like for the cat:3

  • @GenericInternetter
    @GenericInternetter 2 года назад

    Hi Tim. Today is my bathday. I like your

  • @scrutinyng3018
    @scrutinyng3018 2 года назад

    Bruteforce

  • @mastersrikavipriyan280
    @mastersrikavipriyan280 2 года назад

    9 mins ago.

  • @AMIN-yn5nl
    @AMIN-yn5nl 2 года назад

    😍😍😍♥♥

  • @pshr2447
    @pshr2447 2 года назад

    KITTY.

  • @nickchourchoulis
    @nickchourchoulis 2 года назад

    Yayyy cattt :)

  • @skyricq
    @skyricq 2 года назад

    KITTY

  • @dydigaming
    @dydigaming 2 года назад +3

    First

  • @emonymph6911
    @emonymph6911 2 года назад +1

    Tim, with all due respect. Pretty please invest in a digital inkpad your handwriting is worse then a busy doctors... Writing with the mouse is hard but I think you will be fine with a e-pad.