Introducing the greedy algorithm
I, Introduction to greedy algorithm
In this article, I will introduce an interesting type of algorithm called the greedy algorithm. The solution of this type of algorithm is to always choose the best solution at the current time. This locally optimal solution is in the hope that the result on the global level will be optimized (globally-optimal solution).
II, Advantages and disadvantages
The greedy algorithm has several advantages and disadvantages as follows:
Advantage:
Easy to come up with solutions with greedy algorithmic approach Easy to analyze space and time complexity compared to other techniques (like Divide and Conquer approach)
Disadvantages:
Proving the correctness of an algorithm is more difficult than other techniques.
Example Problem 1 - Tandem Bicycle
Problem: A tandem bicycle is ridden by two riders wearing red and blue shirts. Both riders are pedaling but the speed of the bicycle is equal to the speed of the faster rider. The input is two lists containing the speeds of the riders wearing red and blue shirts. Return the sum of the highest speeds of the pairs of bikers.
Input:
Output:
Solution:
During the Warring States period, Sun Tzu, the grandson of Sun Tzu (author of The Art of War by Sun Tzu), was a brilliant strategist of the Qi state. During Tian Ji's horse race with King Wei, Sun Tzu gave Tian Ji the following brilliant plan:
The solution to this problem is similar to the above: We pair the fastest runners (highest horses) on one team with the slowest runners (lowest horses) on the other team. Since the speed of the bike is equal to the speed of the fastest cyclist, the total speed of the pairs will be the largest. The detailed solution is as follows:
- Sort one list; sort the other list in reverse order.
- Zip sorted list creates pairs consisting of one member in blue shirt and one member in red shirt.
- Calculate the speed of the cars in the pair using the formula max(red_speed, blue_speed).
- Calculate the sum of the speeds of the pairs.
My Python solution:
Space and time complexity analysis
Note: n is the number of input elements
Space complexity is O(1)
- Since the entire algorithm processes the two input lists themselves, the space complexity is constant - O(1)
Time complexity is O(nlogn)
- Time complexity to sort 2 lists is O(nlogn) Time complexity to calculate speed of pairs will be O(n) Therefore total time complexity is O(nlogn)
Note: the log factor in Computer Science defaults to 2, not 10.
Example Problem 2 - Minimum Waiting Time
Problem: Take a list of positive integers representing the time required to perform a task. Only one task can be performed at a time. The waiting time is the time required to wait before performing the next task. For example, if the task is performed second, the waiting time is the time required to perform the first task. Write a function that returns the lowest number of waiting times received for all input tasks.
Input:
Output:
Solution:
To optimize the lowest waiting time, we will take advantage of the time of the job that is closer to the end, the less impact it has. Therefore, we will put the jobs with the longest execution time at the end. The solution is described as follows:
algorithm illustration
Detailed solution:
- First we sort the input list
- We can see that the waiting time example above is the time it takes to perform a task that appears in the waiting time of the following tasks. That means the total waiting time will include (length - i) * n (where length is the length of the list, i is the current index and n is the time it takes to perform the current task)
- Therefore we will use the generalized formula above to calculate the minimum waiting time.
The solution is as follows:
Space and time complexity analysis:
Space complexity is O(1)
Since we are processing on the input list itself, the space complexity is constant - O(1)
Time complexity is O(nlogn)
- Time complexity to sort list is O(nlogn)
- Time complexity to iterate through the list to calculate waiting time is O(n)
- Therefore the total complexity is O(nlogn)
Conclude
Hopefully, this article has given you an overview of the greedy algorithm. If you find this blog interesting, please share it so that more people can know about it. And look forward to more of my blog posts on the topic of Data Structure & Algorithm on TipsMake's tech blog!
You should read it
- Instructions on how to show hidden files in USB correctly
- Dynamic Programming (Dynamic Programming)
- Explore the virtual island, the workplace of 8,000 employees in a company, without geographical restrictions when recruiting
- 9 bad habits should be removed immediately if you don't want to get in trouble
- How to send contacts in the Skype chat window
- How to edit anime style image with Everfilter
- Instructions for transferring data from old iPhone phones to iPhone 7 / iPhone 7 Plus
- MSI L2100 - AMD Athlon Neo laptop running Windows 7
May be interested
- Toshiba's algorithm helps normal computers calculate faster than supercomputersnormal computers will optimize the combination much faster than supercomputers.
- Instagram is committed to adjusting the algorithm to limit the display of 'possibly harmful' content to usersinstagram has just made a commitment to adjust the algorithm to remove as much 'possibly harmful' content as possible on the platform and reach users.
- Insert algorithm (Insertion Sort)sort insertion is a sorting algorithm based on in-place comparison. here, a sub-list is always maintained in sorted form. inserting is inserting an element into the sorted list of children. the element is inserted in the appropriate position so that the list is still in order.
- What determines what YouTube shows on your feed?these videos do not appear by accident; all related to the inner workings of the youtube algorithm.
- Linear search algorithm (Linear Search)linear search is a very basic search algorithm. in this type of search, a continuous search operation takes place through every element. each element is checked and if any connection is found, that particular element is returned; if not found, the search process continues until the data is searched.
- Threads tweaks algorithm to combat Bluesky's risethreads is making changes to the way its algorithm works, revamping the for you feed to ensure more content from people you follow appears.
- Interpolation Search algorithm (Interpolation Search)interpolation search (interpolation search) is an improved variant of binary search (binary search). in order for this search algorithm to work correctly, the data set must be sorted.
- Google is about to update the new search algorithmat the search on 2021 event that just ended this morning, google announced its new search algorithm called the multitasking unified model (mum).
- Algorithm for sharing (divide and conquer)divide and conquer (divide and conquer) is an important method of designing algorithms. the idea of this method is quite simple and very easy to understand.
- Alignment algorithm (Merge Sort)mixing (merge sort) is an arrangement algorithm based on the divide and conquer algorithm. with the worst case time complexity of Ο (n log n), this is one of the algorithms that deserves the most attention.