Greedy algorithms

Communication 1

Additional material for the project (e.g. the class ScholarlySearchEngine) and for the course (e.g. a list of exercises about algorithms) will be provided before the holidays

Communication 2

Just a reminder about the date of the exams

  • 23 January 2018

  • 26 February 2018

  • 10 May 2018

  • 22 June 2018

  • 25 September 2018

  • 23 October 2018

They will be uploaded on AlmaEsami soon

For each date:

  • written examination: 10-12

  • project oral colloquium: 13-16

Any question about the previous lecture?

Historic hero: Adriano Olivetti

He was an engineer and entrepreneur

He thought that profit of a company had to be reinvested for the benefits of the whole society

He was known all over the world, in particular in the United States, as one of the most important worldwide manufacturer of typewriters, calculators, and computers

His vision towards electronic products has driven the company even after his death

Typewriters

Lettera 32 was released in 1963 - it was a portable (5.9 kg) mechanical typewriter very popular among journalists and students

Computers

Programma 101 was released in 1965 - first commercial programmable desktop computer, designed for personal use, programmable with assembly-like language

NASA used it to plan the Apollo 11 landing on the Moon

Greedy algorithms

At every stage of execution of a particular algorithm when we are seeking for possible candidates for constructing the solution to a computational problem, this approach makes always the choice that is optimal (i.e. the best one) at that particular moment

It is easy to come up with a greedy algorithm for a problem

An example

Computational problem: to give the change of a certain amount of euros in coins

A possible greedy approach:

  1. Consider the coins to choose for the change as ordered in a decrescent way, from the highest value (i.e. 2 euros) to the lowest one (i.e. 1 cent)

  2. For each kind of value, add in the candidate set of the solution as much coins of that value as possible until their sum is lesser than the remaining of the change to give

  3. Return the selected coins for the change

It cannot be used always

Sometimes the solution found, while it provides a correct solution to the problem, is just sub-optimal - i.e. it is not the best one for solving the problem

For instance, if you are driving from Florence to Bologna, and, at a certain crossroad you find two signs indicating two different routes to get to Bologna

  1. left route: get to Bologna by travelling for 42 kilometers

  2. right route: get to Bologna by travelling for 56 kilometers

Greedy approach: go to left (1)

It cannot predict the existence of traffic in the left route

Characteristics of greedy

Greedy choice property: at a certain step, we can choose the best candidate for improving the set of candidates bringing to a solution - this chioce can be somehow driven by all the previous choices

The problem as an optimal substructure, i.e. optimal solution to a computational problem can be built by considering the optimal solutions to its subproblems

Line wrap

Computational problem: break a text into lines such that it will fit in the available width of a page

Ancillary methods

<string>.split(<string_separator>)

It returns a list of strings where each string in the list represents a word (or a token)

Example: "a b c".split(" ") returns the list ["a", "b", "c"]

<string_separator>.join(<list_of_strings>)

It returns a string composed by all the strings in the list separated by <string_separator>

Example: " ".join(["a", "b", "c"]) returns the string "a b c"

Line wrap: the algorithm

def line_wrap(text, line_width):
    result = []
    space_left = line_width
    line = []

    for word in text.split(" "):
        word_len = len(word)
        if word_len + 1 <= space_left:
            line.append(word)
            space_left = space_left - word_len + 1
        else:
            result.append(" ".join(line))
            line = [word]
            space_left = line_width - word_len

    result.append(" ".join(line))
    return "\n".join(result)

END Greedy algorithms