Assignment 2 (Greedy)
Assignment 2 (Greedy)
To minimize total cutting costs in the cutting board problem, a greedy strategy selects cuts based on their cost, preferring the least expensive cut available at each step and prioritizing cuts that affect more segments initially. This minimizes the multiplication factor of additional segments in costs for subsequent cuts, thereby reducing the total cutting cost .
Deadlines define the latest possible time by which each job must be completed to earn profit in the job sequencing problem. By prioritizing jobs with the highest profit and considering deadlines, the greedy algorithm schedules jobs such that each job is completed within its deadline, maximizing overall profit. Jobs are sorted by descending profit, and the greedy strategy seeks to fill available time slots before their respective deadlines .
Adjusting k based on the string's length ensures the removal process is dynamically adapted, either halving or doubling k relative to whether the string's length is a power of two. This adjusted k influences which characters are removed to achieve the lexicographically smallest string, as the removal algorithm focuses on maintaining a balanced character presence throughout the modified k removals .
A Huffman Tree minimizes the total bits required by assigning shorter codes to more frequent characters and longer codes to less frequent ones. This is achieved using a priority queue to build a binary tree from the bottom up, merging two least frequent nodes iteratively. The resulting paths from root to leaves provide variable-length codes that reduce the overall encoding size .
The relationship between train arrival and departure times determines the number of platforms required by considering overlapping intervals. At each train's arrival time, a platform must be available, and it is freed at the train's departure. The algorithm maintains a count of maximum overlapping intervals (trains needing platforms simultaneously) at any point, determining the peak number of platforms required .
Choosing the starting station carefully ensures the accumulated gas never falls short relative to the cost of reaching subsequent stations. The greedy approach looks for a station where the remaining gas is consistently non-negative as the car moves. If any gas supply deficit is encountered, testing must resume from the following station, ensuring enough initial gas supply is available for the entire route .
The greedy approach prioritizes seating customers by earliest departure times within their preferred compartments, allowing the maximum number of customers to be served. By seating customers whose preferred compartments and arrival times coincide, or immediately after a previous customer vacates, the algorithm maximizes the occupancy of each compartment without overlap .
Taking fractions of items in the fractional knapsack problem allows for optimal use of the knapsack's capacity, as it can accommodate the most valuable parts of different items, compared to the exclusive, all-or-nothing nature of the 0/1 knapsack approach. This leads to a higher total value since it maximizes value per unit of weight by including the most valuable portions of items .
Designing study plans requires aligning course timeframes with available study days to maximize course completion. Proper scheduling ensures no overlap occurs in attended days, leveraging the full range of the given plan to include entire course durations fitting within the plan's start and end. Each study plan is evaluated independently, selecting courses that fit entirely within that plan's span .
The greedy strategy for the activity selection problem focuses on selecting the activity that finishes the earliest among the available compatible activities, allowing for the maximum number of non-overlapping activities. By sorting the activities based on their finish times, the algorithm iteratively selects activities that start after the last selected activity has finished, ensuring maximum utilization of time .