You are on page 1of 16

CAVITE STATE UNIVERSITY NAIC CAMPUS BUCANA NAIC CAVITE

The Class NP and the Greedy Algorithm

A Written Report Presented to:

Mrs. Sherelyn Rasdas

The Class NP and the Greedy Algorithm

By:

Maychell M. Alcorin Joyce Naomie Gracia M. Aseo Mary Grace Panganiban

BSCS-II

I. Introduction
There are several kinds of algorithm, but in this report we have to be focused on what we called, The Class NP and the Greedy Algorithm. These include what kind of algorithm it is and its characteristic why it is called a greedy? The advantages and disadvantages of this algorithm and there are some examples provided. Huffmans Code it was a technique for compressing a data that helps us how to make a shorter the process of solving the problem were the characters can convert into variable bit string and the most frequently occurrence was converted into a shortest bit string. The importance of Class NP algorithm, its uses and characteristics is also discussed.

II. Definition of Greedy Algorithm


Greedy algorithm is any algorithm that follows the problem solving met heuristic of making the locally optimal choice at each stage with the hope of finding the global optimum. A greedy algorithm might also be called a single-minded algorithm because it is an algorithm that gobbles up all of its favorites first. Greedy algorithm always selects the best step among the immediate choices, without looking ahead for the other possible solutions. It can be thought of as a backtracking algorithm where at each decision point the best option is already known and thus can be picked without having to recurse over any of the alternative options. The strategy of greedy algorithm is to take what it can get now. It is an algorithm that is fixed to its choice.

III.Advantage and Disadvantage of Greedy Algorithm


Greedy algorithm is simple and efficient these are its advantage. It can take all it favorites and then this algorithm choose the best option in just simple selecting. The option that greedy selects is efficient, it is useful and well-organized among others. According to the way of selecting an option of a greedy algorithm, it may miss the best path. Because of being single- minded and without looking for other possible solutions, greedy algorithms do not always produce optimal solutions.

IV.

The Name Greedy

The name greedy comes from the fact that the algorithm make decisions based on a single criterion or criteria, instead of a global analysis that would take into account the decisions effect on further steps.

V. Applications that use Greedy Algorithm


A. A Simple Scheduling Problem Nonpreemptive Scheduling states that once a job is started, it must run to completion. Job J1 J2 J3 J4 Example: Running Time 15 8 3 10

J1

J2

J3

J4

15

23

26

36

Schedule #1: Based on the job arrangement Average Completion time: 25

J3 0 3

J2 11

J4 21

J1 36

Schedule#2: Arranged by shortest job first Average Completion Time: 17.75 Conclusion: Operating System scheduler generally gives precedence to shorter jobs. B. The Multiprocessor Case We can extend this problem to the case of several processors. The jobs are ordered, and the shortest running time first. Example: Given three processors Job J1 J2 J3 J4 J5 J6 Running Time 3 5 6 10 11 14

J7 J8 J9

15 18 20

J1 J2 J3 0 3 5

J4 J5 J6 6

J7 J8 J9 40

13 16 20 28 34

Completion time J1 J5 J9

J2 J3

J4 J6

J7 J8

6 14 15

20

30

34

38

J2

J5

J8

J6 J1 J3 J4

J9 J7

3 5

9 14 16

19

34

Minimizing the final completion time

VI. Definition of Huffmans Code Is a technique for compressing data. Huffman's greedy algorithm looks at the occurrence of each character and it as a binary string in an optimal way. In this matter there are so many procedures to be used in finding an easiest way to solve the problem, A statistical compression method that converts characters into variable length bit strings. Most-frequently occurring characters are converted to shortest bit strings least frequent, the longest. Compression takes two passes. The first pass analyzes a block of data and creates a tree model based on its contents. Huffman codes are helping us to VIII. Examples of Huffmans Code

1. Suppose we have a data consists of 100,000 characters that we want to compress. The characters in the data occur with following frequencies.

Frequency 45,000 13,000 12,000 16,000 9,000 5,000

We can consider the problem of designing a "binary character code" in which each character is represented by a unique binary string.

Fixed Length Code In fixed length code, needs 3 bits to represent six (6) characters. a Frequency Fixed Length 000 code 001 010 011 100 101 45,000 b 13,000 c 12,000 d 16,000 e 9,000 f 5,000

This method requires 3, 000 000 bits to code the entire file. How do we get 3000,000? 1. First we need to add the total number of characters which are 45,000 + 13,000 + 12,000 + 16,000 + 9,000 + 5,000 = 1,000 000. 2. Then add each character which is assigned in 3 bit codeword => 3 * 1,000 000 = 3,000 000 bits. In this moment the Fixed-length code are requires 3, 000 000 bits while the variable code requires 224,000 bits it was equal in saving of approximately 25%. Prefix Codes In which no codeword is a prefix of other codeword. The reason prefix codes are desirable is that they simply encoding (compression) and decoding it is also a minimum expected codeword length equivalently, in a tree with minimum weighted path length. It is also another technique in coding, so that we can makes shorter the way in finding solution in compressing data.

Variable-length code can do better by giving frequent characters short codewords and infrequent characters long codewords. a 45,000 0 code b 13,000 101 c 12,000 100 d 16,000 111 e 9,000 1101 f 5,000 1100

Frequency Fixed Length

Character 'a' are 45,000 each character 'a' assigned 1 bit codeword. 1 * 45,000 = 45,000 bits. Characters (b, c, d) are 13,000 + 12,000 + 16,000 = 41,000 each character assigned 3 bit codeword 3 * 41,000 = 123,000 bits Characters (e, f) are 9,000 + 5,000 = 14,000 each character assigned 4 bit codeword. 4 * 14,000 = 56,000 bits. Implies that the total bits are: 45,000 + 123,000 + 56,000 = 224,000 bits IX. Encoding Concatenate the codewords representing each characters of the file.

String Encoding TEA SEA TEN 10 00 010 011 00 010 10 00 110

A. Examples of Encoding From variable-length codes table, we code the3-character file abc as: a 0 b 101 c 100 => 0.101.100 = 0101100

Huffmans Code B. Using standard coding scheme C. Character a Code 000 Frequency 10 Total Bits 30

e i s t space newline Total

001 010 011 100 101 110

15 12 3 4 13 1

45 36 9 12 39 3 174

This representation of this character can be found in starting at the root and recording the path, by using a 0 to indicate the left branch and a1 to indicate the right branch. B. Binary Tree

s p

nl

Representation of the original code in a tree Theorem A Binary tree that is not full cannot correspond to an optimal prefix code. Proof

Frequency Fixed Length code Variable-length

a b c d 45,000 13,000 12,000 16,000 000 001 010 011

e 9,000 100

f 5,000 101

0 Code

101

100

111

1101

1100

Fixed-length code is not optimal since binary tree is not full. From now on consider only full binary tree If C is the alphabet from which characters are drawn, then the tree for an optimal prefix code has exactly |c| leaves (one for each letter) and exactly |c|-1 internal orders. Given a tree T corresponding to the prefix code, computes the number of bits required to encode a file. For each character c in C, let f(c) be the frequency of c and let dT(c) denote the depth of c's leaf. Note that dT(c) is also the length of codeword. The number of bits to encode a file is B (T) = S f(c) dT(c) Which define as the cost of the tree T. For example, the cost of the above tree is B (T) = S f(c) dT(c) = 45*1 +13*3 + 12*3 + 16*3 + 9*4 +5*4 = 224 Therefore, the cost of the tree corresponding to the optimal prefix code is 224 (224*1000 = 224000).

X. Definition of Class NP The CLASS NP stands for Non-deterministic Polynomial-time; it does not stand for non-polynomial. Where non-deterministic is just a fancy way of talking about guessing a solution. A. Polynomial-Time Algorithm Algorithms with worst case running time of O(nk), where k is a constant, are called tractable others are called intractable or super-polynomial.

Algorithm is polynomial-time algorithm if there exists a polynomial p(n) such that the algorithm can solve any instance of size n in a time O(p(n)).

Algorithm with a problem requiring (n35) time to solve are essentially intractable for large n. 1. NP Completeness NP-completeness is a form of bad news, because there are many important problems that cant be solved quickly. Why should we care? Every time you encounter NP completeness problems, you are thinking that it is the end of the world! And you may experience nose bleeding in finding better solution to the problem. Sometimes you lost your scalp in too much scratching trying to solve them and do something better: 1.1 Finding the better solution:

Use heuristic. If you cant solve the problem with the good worst case time, you will push your self and shaking your head to find another solution for solving the problem.

Solve the problem approximately instead of exactly. Theres no perfect answer for every problems, but theres an exact answer. And it will take a long time to solve for the perfect and exact answer. Instead of searching for an optimal solution, search for an "almost" optimal one and you can prove it is close to right.

Use an exponential time solution anyway. If you really have to solve the problem exactly, you can settle down to writing an exponential time algorithm and stop worrying about finding a better solution.

Choose a better abstraction. Sometimes you need to ignore some unimportant details for your fast and better algorithm. Because they are just an accessories to the given problems.

B. Classification of Problems

P. Problems that can be solved in polynomial time. ("P" stands for polynomial.) These problems have formed the main material of this course.

NP. This stands for "nondeterministic polynomial time" where nondeterministic is just a fancy way of talking about guessing a solution. A problem is in NP if you can quickly (in polynomial time) test whether a solution is correct (without worrying about how hard it might be to find the

solution). Problems in NP are still relatively easy: if only we could guess the right solution, we could then quickly test it.

PSPACE. Problems that can be solved using a reasonable amount of memory. Too much of memory are not necessary, especially when the problem can be solve using easy method.

EXPTIME. Problems that can be solved in exponential time. Undecidable. You can use any form of algorithm in solving a problem, as long as it produce a right output in every input instances.

You might also like