Docsity

Gunadarma Greedy Algorithm Study Guide, Study Guides, Projects, Research of Data Structures and Algorithms

Partial preview of the text

Download Gunadarma Greedy Algorithm Study Guide and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

Greedy Algorithm

Data Structure

Informatics Engineering

Gunadarma University

Introduction to Greedy Algorithms: A greedy algorithm is an approach for solving optimization problems by making a sequence of choices, each of which looks best at the moment. The key idea is to pick the locally optimal solution with the hope that these local solutions will lead to a globally optimal solution. Key Concepts in Greedy Algorithms

  1. Greedy Choice Property: o A problem exhibits the greedy choice property if a globally optimal solution can be arrived at by making a locally optimal choice.
  2. Optimal Substructure: o A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to its subproblems. This is similar to dynamic programming, but here, only one subproblem is solved at a time, and no recombination of subproblem solutions is necessary.
  3. Feasibility: o At every step, ensure the choice is feasible, i.e., it satisfies the problem’s constraints.
  4. Local Optimality: o The algorithm selects the best choice at the current step.
  5. Irrevocability: o Once a choice is made, it cannot be changed in future steps. Examples of Greedy Algorithms Problem 1: Activity Selection Problem Description: Given a set of activities with start and finish times, select the maximum number of activities that can be performed by a single person, assuming that a person can only work on one activity at a time.
  1. The remaining node is the root of the Huffman tree. Steps to Solve a Problem Using Greedy Algorithm
  2. Understand the Problem: o Carefully read and understand the problem statement and constraints.
  3. Identify the Greedy Choice Property: o Determine if a locally optimal choice can be made that will lead to a globally optimal solution.
  4. Define the Objective: o Clearly define what needs to be optimized (e.g., maximum value, minimum cost).
  5. Prove the Greedy Choice Property: o Provide a proof or rationale for why the greedy choice property holds for the problem.
  6. Implement the Algorithm: o Write the code to implement the greedy strategy, ensuring it follows the greedy choice and is feasible.

Practice Exercises

  1. Minimum Number of Coins: o Given a set of coin denominations and an amount, find the minimum number of coins needed to make the amount (assuming an infinite supply of coins).
  2. Job Sequencing Problem: o Given a set of jobs with deadlines and profits, maximize the total profit if only one job can be scheduled at a time.