0% found this document useful (0 votes)
12 views

Greedy Algorithm

A Greedy Algorithm is a problem-solving method that constructs solutions by making locally optimal choices, aiming for a globally optimal outcome. Key characteristics include the Greedy Choice Property and Optimal Substructure, which allow for effective application. The document also provides an example of the Activity Selection Problem, demonstrating the greedy strategy of selecting activities that finish earliest to maximize the number of non-overlapping activities.

Uploaded by

Subhadip Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Greedy Algorithm

A Greedy Algorithm is a problem-solving method that constructs solutions by making locally optimal choices, aiming for a globally optimal outcome. Key characteristics include the Greedy Choice Property and Optimal Substructure, which allow for effective application. The document also provides an example of the Activity Selection Problem, demonstrating the greedy strategy of selecting activities that finish earliest to maximize the number of non-overlapping activities.

Uploaded by

Subhadip Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Greedy Algorithm – Report

1. Introduction

A Greedy Algorithm is a problem-solving paradigm that builds up a solution piece by piece, always
choosing the option that offers the most immediate benefit (locally optimal choice), hoping that
this leads to a globally optimal solution. It is simple, fast, and often effective for a wide range of
optimization problems.

2. Key Characteristics

To apply a greedy algorithm effectively, a problem must exhibit:

• Greedy Choice Property: A global optimum can be reached by choosing the local optimum
at each step.

• Optimal Substructure: A problem’s solution can be constructed efficiently from optimal


solutions of its subproblems.

3. General Steps

1. Define the objective function (what you want to optimize).

2. Make a greedy choice at each step (locally best option).

3. Check feasibility (does the choice keep the solution valid?).

4. Repeat until a complete solution is formed.

4. Example: Activity Selection Problem

Problem Statement:

Given n activities with start and end times, select the maximum number of non-overlapping
activities.

Greedy Strategy:

• Always pick the activity that finishes earliest.

Pseudocode:

python

CopyEdit

def activity_selection(activities):

activities.sort(key=lambda x: x[1]) # Sort by end time

selected = [activities[0]]
for i in range(1, len(activities)):

if activities[i][0] >= selected[-1][1]:

selected.append(activities[i])

return selected

You might also like