VOOZH about

URL: https://mathworld.wolfram.com/GreedyAlgorithm.html

โ‡ฑ Greedy Algorithm -- from Wolfram MathWorld


๐Ÿ‘ Image

Greedy Algorithm


An algorithm used to recursively construct a set of objects from the smallest possible constituent parts.

Given a set of ๐Ÿ‘ k
integers (๐Ÿ‘ a_1
, ๐Ÿ‘ a_2
, ..., ๐Ÿ‘ a_k
) with ๐Ÿ‘ a_1<a_2<...<a_k
, a greedy algorithm can be used to find a vector of coefficients (๐Ÿ‘ c_1
, ๐Ÿ‘ c_2
, ..., ๐Ÿ‘ c_k
) such that

where ๐Ÿ‘ cยทa
is the dot product, for some given integer ๐Ÿ‘ n
. This can be accomplished by letting ๐Ÿ‘ c_i=0
for ๐Ÿ‘ i=1
, ..., ๐Ÿ‘ k-1
and setting

where ๐Ÿ‘ |_x_|
is the floor function. Now define the difference between the representation and ๐Ÿ‘ n
as

If ๐Ÿ‘ Delta=0
at any step, a representation has been found. Otherwise, decrement the nonzero ๐Ÿ‘ c_i
term with least ๐Ÿ‘ i
, set all ๐Ÿ‘ c_j=0
for ๐Ÿ‘ j<i
, and build up the remaining terms from

for ๐Ÿ‘ j=i-1
, ..., 1 until ๐Ÿ‘ Delta=0
or all possibilities have been exhausted.

For example, McNugget numbers are numbers which are representable using only ๐Ÿ‘ (a_1,a_2,a_3)=(6,9,20)
. Taking ๐Ÿ‘ n=62
and applying the algorithm iteratively gives the sequence (0, 0, 3), (0, 2, 2), (2, 1, 2), (3, 0, 2), (1, 4, 1), at which point ๐Ÿ‘ Delta=0
. 62 is therefore a McNugget number with

If any integer ๐Ÿ‘ n
can be represented with ๐Ÿ‘ c_i=0
or 1 using a sequence (๐Ÿ‘ a_1
, ๐Ÿ‘ a_2
, ...), then this sequence is called a complete sequence.

A greedy algorithm can also be used to break down an arbitrary fraction into an Egyptian fraction in a finite number of steps. For a fraction ๐Ÿ‘ a/b
, find the least integer ๐Ÿ‘ x_1
such that ๐Ÿ‘ 1/x_1<=a/b
, i.e.,

where ๐Ÿ‘ [x]
is the ceiling function. Then find the least integer ๐Ÿ‘ x_2
such that ๐Ÿ‘ 1/x_2<=a/b-1/x_1
. Iterate until there is no remainder. The algorithm gives two or fewer terms for ๐Ÿ‘ 1/n
and ๐Ÿ‘ 2/n
, three or fewer terms for ๐Ÿ‘ 3/n
, and four or fewer for ๐Ÿ‘ 4/n
.