ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Analysis of Algorithms
    COMP4121
    Progress0 / 28 topics
    Topics
    1. Introduction2. Role of Algorithms in Computing3. Analysis on Nature of Input and Size of Input4. Asymptotic Notations5. Big-O Notation6. Big-Ω Notation7. Big-Θ Notation8. Little-o Notation9. Little-ω Notation10. Sorting Algorithm Analysis11. Loop Invariants12. Recursion and Recurrence Relations13. Algorithm Design Techniques14. Brute Force Approach15. Divide-and-Conquer Approach16. Merge Sort17. Quick Sort18. Greedy Approach19. Dynamic Programming20. Elements of Dynamic Programming21. Search Trees22. Heaps23. Hashing24. Graph Algorithms25. Shortest Paths26. Sparse Graphs27. String Matching28. Introduction to Complexity Classes
    COMP4121›Greedy Approach
    Analysis of AlgorithmsTopic 18 of 28

    Greedy Approach

    7 minread
    1,251words
    Intermediatelevel

    Greedy Approach

    The Greedy Approach is a problem-solving strategy where the solution is built step-by-step, choosing the best possible option at each stage, with the hope of finding the optimal global solution. The key idea behind a greedy algorithm is to make decisions that are locally optimal at each step, assuming that these local choices will lead to the global optimal solution.


    Characteristics of Greedy Algorithms

    A greedy algorithm makes a series of choices by selecting the best option available at each step, without worrying about the consequences of these choices on future decisions. However, this approach does not always guarantee an optimal solution for all types of problems. It works best for problems where a local optimum also leads to a global optimum.

    Key Characteristics:

    1. Greedy Choice Property: The global optimum can be arrived at by selecting a local optimum at each step.
    2. Optimal Substructure: The problem can be broken down into subproblems, and the optimal solution to the problem can be constructed from the optimal solutions of the subproblems.

    When to Use Greedy Algorithms:

    • The problem has optimal substructure, meaning an optimal solution can be constructed from optimal solutions of its subproblems.
    • The problem satisfies the greedy choice property, meaning a locally optimal choice at each step leads to the globally optimal solution.

    Steps Involved in a Greedy Algorithm:

    1. Problem Definition: Understand the problem and how a greedy choice can be made at each step.
    2. Greedy Selection: Choose the locally optimal solution.
    3. Feasibility Check: Ensure that the choice made is feasible and doesn’t violate the constraints.
    4. Update the State: After making a choice, update the problem’s state and reduce the problem’s size.
    5. Repeat: Repeat the process until you have a complete solution.

    Examples of Problems Solved Using Greedy Algorithms:

    1. Fractional Knapsack Problem:

      • Problem: You have a set of items, each with a weight and a value. The goal is to select the items to include in a knapsack of limited capacity to maximize the total value. You can take fractions of items.
      • Greedy Strategy: Sort items by their value-to-weight ratio and take items with the highest ratios first.
      • Optimal Solution: Since you can take fractions of items, this greedy approach yields the optimal solution.
    2. Activity Selection Problem:

      • Problem: You are given a set of activities, each with a start time and finish time. You must select the maximum number of activities that do not overlap.
      • Greedy Strategy: Sort activities by their finish times and select the activity that finishes the earliest, then select the next activity that starts after the current one finishes.
      • Optimal Solution: This greedy choice ensures you select the maximum number of non-overlapping activities.
    3. Huffman Coding:

      • Problem: Given a set of characters and their frequencies, construct an optimal prefix-free binary code for each character, minimizing the total length of the encoded string.
      • Greedy Strategy: At each step, merge the two least frequent characters to form a new node, and repeat the process until all characters are encoded.
      • Optimal Solution: Huffman coding gives the optimal binary code in terms of total length.
    4. Coin Change Problem (for certain coin denominations):

      • Problem: Given an infinite supply of coins with different denominations, find the minimum number of coins needed to make a given amount of money.
      • Greedy Strategy: Start by taking the largest denomination coin possible and subtracting it from the total amount, then repeat with the remaining amount.
      • Optimal Solution: This works for certain coin denominations, but not all (e.g., coin denominations like 1, 3, 4 can fail).
    5. Job Sequencing Problem:

      • Problem: You are given a set of jobs, each with a deadline and profit. The goal is to schedule jobs to maximize total profit while ensuring that no two jobs overlap in their deadlines.
      • Greedy Strategy: Sort jobs by profit in decreasing order and schedule each job at the latest available time slot before its deadline.
      • Optimal Solution: This greedy approach often provides an optimal or near-optimal solution.

    Time Complexity of Greedy Algorithms

    The time complexity of a greedy algorithm depends on the specific problem and the steps involved:

    1. Sorting: Many greedy algorithms require sorting the elements, which takes O(nlog⁡n)O(n \log n)O(nlogn).
    2. Selection and Update: After sorting, the algorithm typically involves iterating through the elements and making selections, which can take O(n)O(n)O(n).

    Therefore, the time complexity is often dominated by the sorting step, resulting in a complexity of O(nlog⁡n)O(n \log n)O(nlogn) for most greedy algorithms.


    Advantages of Greedy Algorithms

    1. Efficiency: Greedy algorithms are often faster than other approaches (e.g., dynamic programming, backtracking) because they make decisions without reconsidering previous choices.
    2. Simplicity: Greedy algorithms are generally easy to implement and require fewer resources compared to other methods.
    3. Optimal Solution: For certain problems (e.g., Fractional Knapsack, Activity Selection), greedy algorithms guarantee an optimal solution.

    Disadvantages of Greedy Algorithms

    1. Not Always Optimal: Greedy algorithms do not always yield the globally optimal solution for all problems (e.g., the 0/1 Knapsack Problem). The greedy choice might not lead to the best solution.
    2. Problem-Specific: Greedy algorithms work only on problems that have the greedy choice property and optimal substructure. For other problems, a more exhaustive approach like dynamic programming or backtracking is required.

    Examples of Greedy Algorithm Problems

    Example 1: Activity Selection Problem

    Given a list of activities, each with a start time and finish time, the objective is to select the maximum number of activities that don’t overlap.

    1. Greedy Approach:
      • Sort activities by their finish times.
      • Select the first activity and then select all subsequent activities that start after the previously selected activity finishes.

    Example:

    Activities: [(1, 3), (2, 5), (4, 6), (6, 7), (5, 8)]

    1. Sort by finish time: [(1, 3), (2, 5), (4, 6), (6, 7), (5, 8)]
    2. Select activities that don’t overlap:
      • Select (1, 3), then (4, 6), then (6, 7).

    Resulting set of non-overlapping activities: [(1, 3), (4, 6), (6, 7)].


    Example 2: Huffman Coding

    The goal is to assign the shortest possible binary codes to the most frequent characters. In Huffman coding, the greedy strategy is used to build the optimal prefix-free binary tree.

    1. Greedy Approach:
      • Start by selecting the two least frequent characters and combine them into a binary tree node.
      • Repeat the process with the new nodes until a full binary tree is built.

    Example:

    Characters: a: 5, b: 9, c: 12, d: 13, e: 16, f: 45

    • Start by combining the least frequent characters (a and b) into a new node.
    • Repeat the process, always combining the two least frequent nodes.

    This results in a Huffman tree that gives the optimal binary codes for each character.


    Summary

    The Greedy Approach is a powerful problem-solving technique used when a locally optimal choice leads to a globally optimal solution. It is most effective when the problem satisfies the greedy choice property and optimal substructure. While greedy algorithms can be highly efficient, they do not guarantee an optimal solution for all problems, and their use is best suited to problems like activity selection, Huffman coding, and certain scheduling or optimization tasks. Their primary advantages include simplicity, speed, and ease of implementation.

    Previous topic 17
    Quick Sort
    Next topic 19
    Dynamic Programming

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,251
      Code examples0
      DifficultyIntermediate