Greedy Algorithms - Algorithms & Data Structures #8

NeuralNine · Intermediate ·⚡ Algorithms & Data Structures ·5y ago

Key Takeaways

The video discusses greedy algorithms, a category of algorithms, and is part of the Algorithms & Data Structures series by NeuralNine.

Full Transcript

[Music] what is going on guys welcome back to the algorithms and data structures tutorial series in today's video we're going to talk about greedy algorithms and greedy algorithms are essentially just a category of algorithms that have certain characteristics and in today's video we're going to briefly mention them we're not going to talk about them too much in detail i just want you to have heard of them which is why i'm making this video at this point in the series i want you to know about greedy algorithms what they are what they look like we're going to look at an example today so that you can see how greedy algorithms look so that you can recognize them in future videos we're going to use some of them in the future and i also want you to see that sometimes they're very efficient or you should use them in some cases and sometimes they don't produce optimal results so let's get right into it so in order to explain greedy algorithms i'm going to use a very simple example uh let's say we have three types of coins so we have one cent we have five cents and we have 10 cents uh these are the types of coins that we can use and we have an unlimited amount of those uh so now the question is if i'm given a certain amount of cents for example 47 cents how can i find the optimal solution when the goal is to get to that amount to give the person this amount of sense with the least amount of coins possible with the smallest amount of coins possible so of course if someone says i want 47 cents and i have an unlimited amount of all these coins i can just go ahead and say okay i'll give you one cent 47 times but then i end up with 47 coins which is not an optimal result and the question is can we find an optimal solution now to this problem the optimal solution would be 10 cents times 4 five cents times one and one cent times two so we end up with seven coins and a total of 47 cents is a very simple solution optimal solution and the question now is not just how can you solve this for a particular number for example 127 would be another number that we can choose uh the question is can you find an algorithm and we don't need to write down the pseudocode just by logical thinking can you find an algorithm that always gives you the optimal solution and it's not obvious that any algorithm that seems intuitive will work here so now what we did in the first example here with 47 cents for example is we just looked at okay what is the largest coin here 10 cents okay how many times can i use 10 cents in order to get to 47 okay i can use it four times so i end up with seven cents left and then i can use the five cents then i have two cents left so i can just go ahead one cent times two okay now what we're doing here is applying a so-called greedy algorithm and a greedy algorithm is defined by always picking the solution the local solution that seems the most beneficial right now so you're not looking long-term you're thinking okay what decreases the value of the um of the price here so we we have 47 cents decreasing 47 cents the most with one step is is done by subtracting 10 cents right it doesn't have to be the case that subtracting 10 cents is necessarily the best solution long term maybe if i subtract 10 cents now it's going to be a problem later on now i can spoil the surprise for you in this case in this constellation the greedy algorithm will always give us the best result so uh actually doing this and picking the largest possible number will give us the the best results in this case but this is what a greedy algorithm does it looks at the value 51 for example and asks okay how can i decrease this value the most with my next step it doesn't ask the question of how can i decrease it in in the long run uh with the least amount of coins possible it looks okay which is the next value that i can choose to decrease this number the most and the answer is the largest coin and we do this as long as the largest coin still fits into the value and then we choose the other coins however it is not always necessarily the case that a greedy algorithm gives you the best result possible for example let's pick a different uh set of coins so we have one cent 5 cents and 10 cents again but then we can also add 20 cents and 25 cents now notice i'm not picking any prime numbers or something i'm not picking any uh super crazy numbers here these are ordinary numbers and the goal now is to uh find optimal solutions now of course if i have something like 30 cents i can go ahead and say okay 25 cents plus 5 cents is 30 cents so i have two coins as the optimal solution so sometimes it still works but uh not necessarily every time for example let's look at 40 cents and 40 cents is an example where this would not work because the greedy algorithm asks what is the coin that i can use to decrease this value the most right now the greedy algorithm is interested in the best solution locally we want to decrease the value to zero okay the best solution to do that right now as far as i can see is subtracting 25 cents so okay what we do is we end up with 15 cents then i go ahead subtract 10 cents end up with five cents and then we go ahead and subtract the five cents and we end up with zero since using three coins however this is not the optimal solution because the optimal solution is not a greedy solution the optimal solution is just picking 20 cents two times two coins and we still end up with 40 cents so this is as you can see a proof that greedy algorithms are not always especially not in general but also not for this example where sometimes in some constellation a greedy algorithm always gives you the optimal result if you only use these three coins here a greedy algorithm will always give you the optimal result however sometimes in different constellations it's not going to be the case so you have to be careful and i just want you to know what greedy algorithms are that sometimes they work sometimes they don't work and now you should know about this and we'll see a lot of greedy algorithms in the future so that's it for today's video thanks for watching i hope you enjoyed it was a very short video but i just wanted to mention greedy algorithms in a separate video here because it is an important topic you should know about them i just wanted to have it mentioned here i hope you enjoyed it in the future videos we're going to talk more about uh sorting algorithms now we're going to start analyzing them we're going to talk about divide and conquer sorting algorithms runtime complexities of bubble sword insertion sword selection sword merge sword and so on we're going to analyze all of them and uh yeah that's it for today's video i hope you enjoyed it leave a like and leave a comment in the comment section down below if you enjoyed this video subscribe to this channel to see more future videos for free and thanks for watching see you next video and bye [Music] you

Original Description

Today we talk about a category of algorithms called greedy algorithms. Programming Books: https://www.neuralnine.com/books/ Website: https://www.neuralnine.com/ Instagram: https://www.instagram.com/neuralnine Twitter: https://twitter.com/neuralnine GitHub: https://github.com/NeuralNine Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches the basics of greedy algorithms, a fundamental concept in algorithms and data structures, and provides examples of how to apply them to solve problems. By watching this video, viewers can learn how to implement greedy algorithms and analyze their complexity. Greedy algorithms are a crucial part of algorithm design and can be used to solve a wide range of problems.

Key Takeaways
  1. Define the problem to be solved
  2. Choose a greedy approach
  3. Implement the greedy algorithm
  4. Test and evaluate the algorithm
  5. Optimize the algorithm for better performance
💡 Greedy algorithms can be an efficient and effective way to solve complex problems, but they require careful consideration of the problem constraints and the algorithm's limitations.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →