BootcampHackathonHiring ChallengeAll Events
JobsCompanies
Greedy Algorithm

Greedy Algorithm

These powerful mathematical tools will amaze you with their ability to solve complex problems and add a whole new dimension to your creativity!
Techcareer.net
Techcareer.net
31.05.2023

Greedy Algorithm

The Greedy Algorithm, is the name given to simple, easy-to-implement, and intuitive algorithms used to eliminate problems encountered during optimizations. Although they are primarily used for optimization problems, they are generally seen as a design technique. Therefore, they hold significant importance. The basic technique used in solving problems with greedy algorithms involves creating a solution for a very small subset of the problem and turning this solution into a general solution. The choices made to obtain the solution in the algorithm may initially seem correct, but over time, they can become unfavorable and incorrect. This is how the question "What does greedy mean?" can be answered. Based on this explanation, various techniques are utilized in the Greedy algorithm. The most commonly preferred greedy algorithms are listed below:

  • Knapsack Problem
  • Kruskal Algorithm
  • Prim's Algorithm
  • Huffman Coding
  • Dijkstra Algorithm

The question 'What does the greedy algorithm mean?' can be summarized as follows. So, how is the greedy algorithm created? In this section, it is crucial to have knowledge about how the algorithms work. Otherwise, it is not possible to make correct decisions on which path to follow. Examining examples of algorithms, understanding how they function, knowing their advantages and disadvantages, and understanding how they are implemented enable a more professional progression. This way, the desired maximum efficiency can be achieved in a shorter period of time, leading to maximum efficiency.

How to Create a Greedy Algorithm?

To create a greedy algorithm, the first step is to focus on the problem for which you want to find a solution. Once you have identified the desired solution point, the available set is sorted. Through this sorting process, the problem is addressed, and a repeated process is carried out until reaching the finest detail by considering the overall set.

For example, if we consider filling a bag to maximize profit, the values of the available items are evaluated. After the evaluation, the values are sorted from highest to lowest, and the bag is filled according to its capacity. If the sorting is to be done based on activity, sorting by the end time of the activities becomes important. In this case, it is recommended to prioritize activities that will end earlier and sort them according to the longer duration. The sorting is done considering the greedy approach, and the operations are performed accordingly.

Based on this, an example can be given where a textile factory models product distribution for maximum efficiency, or a dairy factory operates with the logic of maximizing milk production. Examples of greedy algorithm can be listed as follows. In this way, progress can be made based on the obtained examples.

Examples of Greedy Algorithms

The most preferred types of greedy algorithms can be listed as follows: Knapsack, Huffman, Activity Selection, Dijkstra, Prims, and Kruskal algorithms are among the most preferred greedy algorithms. Learning these algorithms in detail provides many advantages in terms of their usage.

Firstly, it is necessary to mention the greedy approach algorithm developed for the activity selection problem. Although it may seem that only one activity can be selected, it allows for the possibility of creating the maximum activity. Just as the greedy approach or Greedy Algorithm requires, when the closest timed activity is removed from the available set, the remaining modified part needs to be searched for an optimized solution. In other words, let's assume we have a set S, activity a with the earliest finishing time, and B as our optimized solution. Thus, S - a creates the set S', and the optimized solution of S is B, which is also the solution of S' + a. According to this, it is seen that the set S - a is also solved simultaneously. Progress is made until no activities remain, and our optimized solution can be interpreted.

The interpretation of the pseudocode, which is a draft code, is also among the examples that can be provided. For example, comparing the start and finish times of activities obtained through traversal within any loop allows for making changes to the available data to model all activities to maximize them. In the second step, the greedy approach can be examined according to the Knapsack problem. Considering the Knapsack problem is of great importance for a clearer perception of examples of the greedy approach. 

When looking at dynamic programming, a binary (0-1) Knapsack model is found, while in the greedy approach, operations can be performed with fractional objects. An example of this is trying to fill a bag with gold dust and similar substances. To be able to add the maximum number of objects to the bag, new methods need to be found. In this comparison, the greedy approach is significantly highlighted. Each object is compared based on its weight and size, and the answer to the question 'What is a greedy algorithm?' can be given at this stage. In the greedy approach, where objects are sorted and compared, it is checked whether there is enough capacity for the objects, and the if condition is calculated. As products are added to the bag, the profit and capacity are evaluated, and necessary revisions are made. This stage is crucial for the greedy approach. After the necessary revisions are made, if the bag's capacity becomes insufficient, an algorithm is formed where the maximum profit can be obtained based on the order obtained from the data.

Another example that can be given among greedy algorithm examples is Huffman. This example, called Huffman coding, is seen as an algorithm performed to reduce data representation and maintain efficient data after this reduction. In normal cases, binary calculations are performed to represent numbers, but in Huffman coding, bit calculations are done based on usage frequency. This calculation process is carried out as follows:

For example, consider a file in which the character A appears at least 45 thousand times, the character B appears 13 thousand times, the character C appears 9 thousand times, the character D appears 16 thousand times, the character E appears 5 thousand times, and the character F appears 12 thousand times. Normally, it is observed that a minimum of 3 bits is required to add these 6 characters to the binary calculation.

(2^3 = 8 numbers can be represented, 000, 001, 010...). In this case, (13 thousand + 12 thousand + 9 thousand + 5 thousand + 45 thousand + 16 thousand) x 3 bits = 300 bits are obtained as a result. However, when looking at Huffman coding and using this code, the numbers 45, 13, 12, 16, 9, and 5 are matched and the bit numbers are determined using Huffman code. When this example is taken into account, when a ranking is made from the most frequent to the least frequent, it becomes 0, 111, 101, 100, 1101, 1100. It is observed that these results occupy less than 300 bits when multiplied by their frequencies. Therefore, these examples are the answer to the question of what the greedy parameter is.

What are the Advantages of the Greedy Algorithm?

The Greedy algorithm is one of the approach algorithms and it comes with several advantages. It allows users to achieve maximum efficiency regardless of the field in which they use the algorithm. Thus, maximum efficiency can be attained in various areas based on the given data. Being a mathematical algorithm also helps prevent potential drawbacks and minimizes risks. The typical representative of the Greedy Algorithm is also significant in this stage.

One of the advantages of this algorithm is that it minimizes the error rate. By paying attention to the key points of the algorithm, the process can be carried out smoothly, and progress can be made especially through examples.

Since the algorithm is based on a mathematical foundation, it becomes possible to achieve maximum efficiency in all kinds of fields, regardless of the domain. For example, while calculating the maximum efficiency of milk production in a dairy factory can be done with this algorithm, the maximum efficiency of a textile factory can also be calculated using the same algorithm. In short, it can be applied in various fields and sectors, irrespective of the domain.

Another advantage of the Greedy algorithm is undoubtedly its cost-effectiveness. Since calculations can be easily performed, there is no need to pay extra fees for this application. It can be individually computed, and a path can be followed based on this algorithm.

Another advantage of Greedy algorithms is the provision of optimal solutions. Let's say you are in a tight spot with limited time. Using the Greedy algorithm in this limited time frame can be a significant advantage in finding an instant solution and resolving the problem. Although it may seem like a simple solution, thanks to this algorithm, a solution that starts in a small area can turn into a general solution. However, it should be noted that what appears to be a solution in a small area may prove insufficient over time. Therefore, careful consideration is required when proceeding.

Comparison of Greedy Algorithm and Dynamic Programming

When comparing the Greedy algorithm and Dynamic Programming, it can be observed that both algorithms are used to obtain optimal solutions for problems. However, each algorithm has distinct features, and users need to pay attention to these features and implement the appropriate approach based on their understanding of the differences.

The first difference between these two commonly used algorithms lies in the Greedy approach's retention of the selection element, while Dynamic Programming does not involve any selection element. The selected element represents the decision change that may occur in the optimal solution. By examining the variable and making a selection, solutions are generated by starting from subproblems. In the case of Dynamic Programming, it is observed that a selection is made at each step, and optimal solutions to subproblems are added based on these selections.

However, when looking at the Greedy approach, it is observed that unlike dynamic programming, there is no selection made based on future steps, and solutions are not generated accordingly. Essentially, this design is not implemented, and a solution is not produced at each step. In dynamic programming, it is seen that a memoization-based conversation, in other words, a bottom-up strategy, is used. On the other hand, the Greedy approach works in the opposite direction with a top-down strategy. In the Greedy strategy, the change is usually increased at each step, and calculations are performed through reductions. However, in dynamic programming, it is observed that the opposite is applied. In summary, the Greedy approach follows a top-down strategy, while dynamic programming follows a bottom-up strategy. Individuals who will use these programming methods need to understand the difference between the two programming approaches and act accordingly.


More Stories

What is .NET Core? What is it used for?

What is .NET Core? What is it used for?

In this blog post, we explained how to develop effective software on every platform with .NET Core.
29.03.2024
9 Minutes

TECHCAREER

About Us
techcareer.net
Türkiye’nin teknoloji kariyeri platformu

SOCIAL MEDIA

TwitterInstagramLinkedinYoutubeFacebook

tr


en

All rights reserved
© Copyright 2024
support@techcareer.net
İşkur logo

Kariyer.net Elektronik Yayıncılık ve İletişim Hizmetleri A.Ş. Özel İstihdam Bürosu Olarak 31/08/2021-30/08/2024 tarihleri arasında faaliyette bulunmak üzere, Türkiye İş Kurumu tarafından 17/08/2021 tarih ve 9317662 sayılı karar uyarınca 170 nolu belge ile faaliyet göstermektedir. 4904 sayılı kanun uyarınca iş arayanlardan ücret alınmayacak ve menfaat temin edilmeyecektir. Şikayetleriniz için aşağıdaki telefon numaralarına başvurabilirsiniz. Türkiye İş Kurumu İstanbul İl Müdürlüğü: 0212 249 29 87 Türkiye iş Kurumu İstanbul Çalışma ve İş Kurumu Ümraniye Hizmet Merkezi : 0216 523 90 26