Design & Analysis of Algorithms Greedy Algorithms Presented By 22,24,25,26,27,28,29,31,32,34,37,38,40 BSCS-E122
What Are Greedy Techqniques?
Greedy Techniques Definition: Greedy algorithms make locally optimal choices at each step, aiming to reach a global optimum solution. In simple words, a greedy algorithm is a simple technique that solves a problem by selecting the best option at each step without considering long-term implication. .
Characteristics of Greedy Algorithm Locally optimal choices : Decisions made based on immediate information. No backtracking: Once a decision is made, it's never reconsidered. Greedy choice property: Optimal solution can be reached by making a series of greedy choices.
Greedy Choice Property – (Core of G.A) Explanation of the Greedy Choice Property : At each step, the optimal choice is made without considering future consequences . This choice doesn't depend on previous decisions . “It means Greedy algorithm always selecting the best option available at each phase, even if the final decision is wrong.” Also, it does not guarantee the best overall solution.
Real Life Example One real-life example of a greedy algorithm is the process of making change . Vending Machine Change Dispensing: “Vending Machine Change Dispensing is the process by which a vending machine provides the correct amount of change”
Working of Vending Machine Problem Statement : Scenario : Making change for 73 rupees. Coin available : 1 rupee, 5 rupees, 10 rupees, 25 rupees. Objective: Minimize the number of coins issued while providing correct change.
Greedy Algorithm Approach: Prioritize dispensing the highest denomination coin that is smaller than or equal to the required change amount. 25 25 1 5 10 Coins 25 25 = 50
Fractional Knapsack The basic idea of the greedy approach is to calculate the ratio profit/weight for each item and sort the item on the basis of this ratio. Then take the item with the highest ratio and add them as much as we can (can be the whole element or a fraction of it). This will always give the maximum profit because, in each step it adds an element such that this is the maximum possible profit for that much weight. Problem Given the weights and profits of N items, in the form of {profit, weight} put these items in a knapsack of capacity W to get the maximum total profit in the knapsack. In Fractional Knapsack, we can break items for maximizing the total value of the knapsack. For Example, in this example we have to determine the best combination we can take to maximize both profit and wieght whereas the max load of the knapsack is 20. Keep this in mind we are to make such a combination which allows us to optimize our solution.
Dijkstra’s algorithm Dijkstra's algorithm is a graph search algorithm that finds the shortest path between nodes in a weighted graph. It starts from a source node and iteratively explores the neighboring nodes, updating the shortest distance to each node as it goes. 1 2 3 20 10 40
Dijkstra’s algorithm The real-life examples of the Dijkstra Algorithm are Google Maps, Social networking, and DNA mapping. All these things are represented in the form of graphs using Nodes and edges. To reach from one single source to multiple sources ( Nodes ) using the shortest path, then we use Dijkstra Algorithm. It is also known as the minimization algorithm and it uses a greedy approach and provides optimal solution. Relaxation: If d(u) + d(u+v) < d(v) d(v) = d(u) + c(u+v)
Huffman Coding Huffman coding is a method used in computer science and information theory to compress data efficiently. It works by assigning variable-length codes to input characters based on their frequency of occurrence in the data. Steps Of Huffman Algorithm are following. Frequency Count : Calculate the frequency of each character in the input data. Priority Queue: Create a priority queue or min-heap based on the frequencies, where nodes with lower frequencies have higher priority. Build Huffman Tree: While there is more than one node in the queue: Remove the two nodes with the lowest frequency from the queue. Create a new internal node with a frequency equal to the sum of the frequencies of the two nodes. Set the two removed nodes as children of the new node. Add the new node back to the queue. The remaining node in the queue is the root of the Huffman tree.
Huffman Coding Cont 4.Generate Huffman Codes: Traverse the Huffman tree from the root. Assign '0' to the left child edge and '1' to the right child edge. When reaching a leaf node (character node), the path from the root to that node represents the Huffman code for that character. 5.Encode Data: Replace each input character with its corresponding Huffman code to create the compressed data. 6.Decoding: Start from the root of the Huffman tree. Read each bit from the compressed data. Traverse down the tree following the '0' path for 'left' and '1' path for 'right' until reaching a leaf node. Output the character represented by the leaf node and go back to the root to continue decoding the next bits.
Huffman Coding Cont 2 a=50, c=30, b=10, d=5, e=3, f=2 2 f e 3 5 5 d 10 10 b 20 30 50 100 c M is the message and consist of 100 characters with the alphabets given above. We know messages are coded in 8-bit ASCII codes. For 100 characters the code will be of 7*100 bits = 700 bits. Our Goal is to minimize this using huffman coding. Solution a = 0 1*50 = 50 b=100 3*10 = 30 c=11 2*30 = 60 d=1010 4*5 = 20 e=10111 5*3 = 15 f=10110 5*2 = 10 TOTAL 185bits 50 a 1 1 1 1 1
Kruskal’s ALgorithm : Construct Min Heap with edges . Take one by one edge and add in spinning tree . ( Cycle should not be created ) Best Case ( n - 1 ) edges Worst Case ‘ e ’ edges A B C D F G E 5 4 3 6 6 6 5 6 2 4 4 3 Example
Solution : A B C D F G E 4 3 5 2 4 3 21 n - 1 => 7 - 1 => 6 Time Complexity of Kruskal’s ALgorithm : In Best Case , ( n - 1 ) log e . In Worst Case , e log e .
Real Life Applications Airline Ticket Booking Systems: Systems used by airlines to optimize seat allocation and pricing often use greedy algorithms. For example, when allocating seats, the system may prioritize filling higher-priced seats first before moving on to lower-priced ones. Data Compression: Greedy algorithms play a role in data compression techniques such as Huffman coding. This technique assigns shorter codes to more frequent symbols, reducing the overall size of the data. Google Maps: Google Maps uses greedy algorithms, particularly Dijkstra's algorithm, to find the shortest routes between locations. This is crucial for providing accurate and efficient navigation directions to users.