3.6K views

Uploaded by irul.sioju

- Perl Cheat Sheet
- btncalcular matrices
- peoplesoft inteview questions
- 2005415_FuncSpec_v2.00(for6.3)
- DSA 5.pdf
- midterm report
- Tibco Bw Variables
- Excel VBA Basics
- JAva array
- StataMata.beamer.uksuG14
- Implementing Customer Exit Reporting Variables as Methods
- 25 Kaavian SAP Script
- Lisa ToolKit 3 0 Sources
- Exercise 8.2 - Add a Two Direction SFC for Controlling the Plant V2-1
- Fortran.docx
- Matlab Prog Content
- Unit v 2marks
- 123
- Descriptive Programming in QTP
- Introduction to PHP

You are on page 1of 6

Mata Kuliah :

[ CF-1333 Alpro 2 ]

Disusun oleh :

Fakultas Teknologi Informasi

Institut Teknologi Sepuluh Nopember

Surabaya

Greedy Algorithm

Greedy principle is : “take what you can get now!”.

The example of this problem in our life are :

Choose some of investment type

Finding the shortest way from Bandung to Surabaya

Choose a direction in University

Playing card

to explain the way of greedy algorithm, next i will explain one of the problem that

can be solved by greedy algorithm, there is Coin Changing Problem .

Example :

I have money 500. I want to exchange with available coin. There are 50 as much as

5, 25 as much as 10, and 10 as much as 15. How much coin is needed to this

exchange? The answer of this problem have two combination. There are maximal

combination and minimum combination.

Solution :

To understand the problem, make a table which contain some available coin with

their amount.

For the minimum combination the kind of available coin must be sort

(descending). Here is the table :

0 50 5 Looked for

1 25 10 Looked for

2 10 15 Looked for

For the maximum combination the kind of available coin must be sort

(ascending). Here is the table :

0 10 15 Looked for

1 25 10 Looked for

2 50 5 Looked for

This time I will explain the solution to find minimum combination. The way to find

maximal combination is same. The different is in the procces of sorting coin.

To find minimum combination, the first step , we take the first kind of coin (50).

If after this taking the value still less than or not equals with the value of money

then we must take again the coin. If the first kind of coin (50) was taken all and the

sum still less than or not equals with the value of money then we take again the coin

in the next kind (25). This process will looping until the sum of coin combination is

equals with the money that we want to exchange.

In this process we must pay attention for some case. When we taking the first

kind of coin and weren’t taking all, so we have to take again the kind of this coin. If

after this taking the sum is more than the value of our money, then this taking must

be canceled and we take the next kind of coin. The next process is same with the

previous explanation. The process is looping until the sum of coin combination equals

with the money that we want to exchange.

Pseudocode Coin Changing Problem

From the previous explanation of the solution coin changing problem, I can make

the pseudocode.

The input is the value of money that we want to exchange (n), amount kind of

available coin (counter) and amount of each kind of available coin. Data about the

amount of each kind of available coin must be placed into array. And must be sorting

by ascending or descending, according to our needed (want to find maximal

combianation or minimum combination).

In my pseudocode, I prepare 3 arrays, there are coin array is used for saving

the kind of available coin, denom array is used for save amount of each kind of

coin, and the last array is amount array is used for saving amount of each coin that

used for maximal combination or minimum combination. In this pseudocode I make

3 method . There are 2 variable as global variable. There are n variable , sum

variable (used for saving the sum of coin combination that be arrangged).

1. i 0

2. sum 0

3. while i<counter

4. amount[i] = PROSES(coin[i] , denom [i])

5. i++

6. if sum = n

7. i = ∞

8. PRINT-COIN (counter, coin [], amount[])

PROCCESS (coin , denom)

1. counter 1

2. amount 0

3. while counter <= denom and (sum+coin) <= n

4. sum = sum + coin

5. amount = counter

6. counter++

7. return amount

1. for i 0 to counter

2. print coin[i] “=” amount[i]

that used for saving amount kind of available coin, it was getting from user. Then

coin array contain the kinds of available coin, it was getting from user. The last

parameter is denom array that used for saving amount of each kind of available

coin, it was getting from user.

Variable i is used for saving index value. Starting from 0. variable sum is used for

saving sum from coin combination that be arranged. Variable sum starting from 0.

next in line 2 to 7 progrma will enter looping process. This looping process is used to

fill amount array. The filling process in each amount array element were did in

PROCESS METHOD, so that in line-4 I call PROCESS METHOD and sending

argument coin[i] and denom[i]. Finally this method will call PRINT-COIN METHOD

for print the result.

In line 7 i value is unlimited if the value of sum equals n. If the value of sum

equals n it means the purpose of this process completed and looping process must

be stopped. I change the value of i become unlimited so that unfulfill looping

condition in line-3. and looping was over.

PROCESS METHOD. This method have 2 parameters, there are coin and denom

variable. This two variable is different with coin and denom variable in COUNT-COIN

method. In that method coin and denom variable is array. In this method I make

variable counter, with starting value 1. counter variable is different with counter

variable in COUNT-COIN method. When counter = 1 it means we take one from this

kind of coin checking, when counter=2 it means we take 2 coin, and further. Amount

variable is used for saving amount of coin that starting value 0, amount variable in

this method is different with the variable in COUNT-COIN Method. In line 3-6

program wil enter looping process. This looping process is used to find the amount

that use and count some of coin combination that be arranged. The condition of this

looping process is during counter value <= denom its means during the kind of coin

was cheked is still available and the value from sum+coin still<=n, it means if the

sum from combination that be arranged is not over n. If looping process is finished

so this method will return the value from amount to COUNT-COIN Method.

Method PRINT-COIN. This method is used to print the result of all process

(output) . That is print amount from each of coin that will used.

- Perl Cheat SheetUploaded bydinokid08
- btncalcular matricesUploaded byjovanny
- peoplesoft inteview questionsUploaded byGanesh.am
- 2005415_FuncSpec_v2.00(for6.3)Uploaded byAtu Saito
- DSA 5.pdfUploaded byNishu Rave
- midterm reportUploaded byapi-313058499
- Tibco Bw VariablesUploaded byRameshCh
- Excel VBA BasicsUploaded byKunal Bhodia
- JAva arrayUploaded bydro
- StataMata.beamer.uksuG14Uploaded byMichael Ray
- Implementing Customer Exit Reporting Variables as MethodsUploaded bySilu Leon
- 25 Kaavian SAP ScriptUploaded byhaarush
- Lisa ToolKit 3 0 SourcesUploaded byi336
- Exercise 8.2 - Add a Two Direction SFC for Controlling the Plant V2-1Uploaded byzvonkomihajlovic4891
- Fortran.docxUploaded byAngelic Sales
- Matlab Prog ContentUploaded bychockake
- Unit v 2marksUploaded byElakkiyaSelvaraj
- 123Uploaded byA Nader Madzaman
- Descriptive Programming in QTPUploaded bydukkasrinivasflex
- Introduction to PHPUploaded byMONIKAKEERTHII
- Imp Preparation Topics and Areas Ques( Imp)Uploaded byptejaswi
- Variables+in+AliceUploaded byw4hyu5
- 424_dynamicmagnetsflash8Uploaded bySOlero MAnisku
- programming begin.pptUploaded byRaja Rosenani
- Algorithims Labmanual 3 SolveUploaded byirfan_chand_mian
- HowToMakeAGameInUnity3D.pdfUploaded byPoonzz Prarin
- 10.1.1.146.580Uploaded byYogesh Sarangdhar
- My Book (Progamming 3)Uploaded byMark Balisnomo
- Plsql NotesUploaded byAyub Khan
- Inline AssemblyUploaded byAna Laila

- 1 Chapter (Maths)Uploaded byJanaki Krishnan
- TG NS teasers for CAT-2012.pdfUploaded byParijat Roy
- 1st Grand Test Correct Answer Key1Uploaded byMohammad Javed Iqbal
- SOLN1ef8904F2013Uploaded byTom
- History m.algebraUploaded bySyahira Yusof
- Math 106 Exer13Uploaded byAlex Pumnea
- Menachem Kojman and Saharon Shelah- A ZFC Dowker space in aleph-omega+1: an application of pcf theory to topologyUploaded byKsmweo
- Matteo Scalperf 2017Uploaded byceccarellom
- Vrabie PhD ThesisUploaded byAli Almisbah
- AQA Core 3 Revision BookletUploaded byjake1731
- Pplex IntroUploaded byricardo izquierdo
- Continuity 13 14Uploaded bypreetam
- 05 Gold 1 FP1 Edexcel.pdfUploaded byNishC300
- iit jee maths pncUploaded byThe Rock
- Add Maths Project Function Sarawak 2013Uploaded byrockafella101
- Booth TutorialUploaded bysahil9622394058
- HW Chapter 8Uploaded bycrampingpaul
- Furman 2008 ExamUploaded byBHAAJI0001
- Fourier MathcadUploaded byAlberto Olvera
- 6 BC.pdfUploaded bySaher
- Data Structure Practical QuestionsUploaded byJayeeta nayak
- Taylor Series and Numerical MethodsUploaded byGeorge Ezar N. Quiriado
- h.c.f. & l.c.m (Amcat)Uploaded byGaurav
- Cálculo - James StewartUploaded byLeonardo García
- Chapter5 AUploaded bySulaiman Dawood Barry
- Enumerative Combinatorics - Peter J CameronUploaded byBug Menot
- Zeros of Cubic FunctionsUploaded byhamiltop
- finite difference wikipediaUploaded byletter_ashish4444
- Activity 2Uploaded byJusztinAquino
- Ma131 Exam RevisedUploaded byOllie da Silva