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

Group No. 13: Minahil Anwar (101631032) Namra Akram (101631039) Laiba Fayyaz (101631028)

The document discusses greedy algorithms and their elements. It provides examples of applying the Huffman coding algorithm and job sequencing with deadlines as greedy algorithms. It also compares greedy algorithms to dynamic programming, noting that greedy algorithms make locally optimal choices at each step while dynamic programming solves subproblems. The key elements of greedy algorithms are the greedy choice property and optimal substructure.

Uploaded by

sadia abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Group No. 13: Minahil Anwar (101631032) Namra Akram (101631039) Laiba Fayyaz (101631028)

The document discusses greedy algorithms and their elements. It provides examples of applying the Huffman coding algorithm and job sequencing with deadlines as greedy algorithms. It also compares greedy algorithms to dynamic programming, noting that greedy algorithms make locally optimal choices at each step while dynamic programming solves subproblems. The key elements of greedy algorithms are the greedy choice property and optimal substructure.

Uploaded by

sadia abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Group No.

13

Minahil Anwar (101631032)


Namra Akram (101631039)
Laiba Fayyaz (101631028)
 HUFFMAN ALGORITHM

 ACTIVITY SELECTION

 JOB SEQUENCING WITH DEADLINES

 ADVANTAGES

 DRAWBACKS

 DIFFERENCE BETWEEN GREEDY ALGORITHM AND DYNAMIC

PROGRAMING

 ELEMENTS OF GREEDY ALGORITHM


“A greedy algorithm, as the name
suggests, always makes the choice that seems
to be the best at that moment. ”
To develop a greedy algorithm We went
through the following steps
 Determine the optimal substructure of the problem.

 Develop a recursive solution.

 Prove that at any stage of the recursion, one of the optimal choices is the greedy choice. Thus, it

is always safe to make the greedy choice.

 Show that all but one of the subproblems induced by having made the greedy choice are empty.

 Develop a recursive algorithm that implements the greedy strategy.

 Convert the recursive algorithm to an iterative algorithm.


What makes an algorithm greedy?
1. Greedy choice property

2. Optimal substructure (ideally)


Greedy choice property: Globally optimal solution can be arrived by
making a locally optimal solution (greedy). The greedy choice property is preferred
since then the greedy algorithm will lead to the optimal, but this is not always the
case – the greedy algorithm may lead to a suboptimal solution. Similar to dynamic
programming, but does not solve subproblems. Greedy strategy more top-down,
making one greedy choice after another without regard to sub solutions.

Optimal substructure: Optimal solution to the problem contains within it


optimal solutions to subproblems. This implies we can solve subproblems and build
up the solutions to solve larger problems.
GREEDY ALGORITHM DYNAMIC PROGRAMMING

 At each step, we quickly make a choice


 At each step, the choice is determined
that currently looks best. A local
based on solutions of sub problems.
optimal(greedy) choice.
 Sub-problems are solved first.
 Greedy choices can be made first before

solving further sub-problems.  Bottom-up approach

 Top-up- approach  Can be slower, more complex

 Usually faster , simpler


 It is technique for compressing data.
 Choose the two lowest frequencies, and make
them brothers, with the root having the combined
frequency.
 Iterate.
TWO MAJOR PARTS IN HUFFMAN
CODING

 Build a Huffman Tree from input characters.

Traverse the Huffman Tree and assign codes to


characters.
STEPS
1. Create a leaf node for each unique character.

2. Extract two nodes with the minimum frequency.

3. Create a new internal node with frequency equal

to the sum of the two nodes frequencies.

4. Repeat steps#2 and #3 until the heap contains

only one node. The remaining node is the root


node and the tree is complete.
 File Size:

10x4 + 20x4 + 30x3 + 40x2 + 50x2 + 60x2 =

40 + 80 + 90 + 80 + 100 + 120 = 510 bits

 The Huffman code:

 Required 510 bits for the file.

 Fixed length code:

 Need 3 bits for 6 characters.

 File has 210 characters.

 Total: 630 bits for the file.


JOB SEQUENCING WITH
DEADLINES
 Greedy is easy to be implemented.

Just search the best choice from the current state that ‘reachable’ (has

any paths or any connections).

 In simple case, greedy often give you the best solution


 In large and complex case, greedy doesn’t always give you
the best solution, because it’s just search and take the best
choice that you can reach from the current state.

 It takes longer time than any other algorithms for big case of

problem.

You might also like