8-9 | Greedy algorithms Flashcards

1
Q

Greedy algorithm definition

A

Greedy algorithm:

A way of solving a problem by making locally optimal choice with the hope of finding a global optimum

make decisions based on information available at hand without worrying about the effect of these decisions in the future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Types of computational problems

A

Decision problem

Optimization problem

Counting problem

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Greedy algorithm: typical use

A

Typically used to solve optimization problems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Optimization problem definition

A

asks for finding the best possible solution among a set
of all possible solutions to a problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Example of optimization problem

A

Change-making problem

Let the coins we have available are of value:

Devise an algorithm for paying a given amount of units using the smallest number of coins, if there is unlimited supply for each type of coin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Change making optimization problem

Greedy solution strategy?

Does it find the optimal solution?

A

Starting with nothing, at every stage we add to the coins already chosen a coin of largest value available that does not take us over the amount to be paid

why greedy?
At every step chooses the largest coin it can (locally optimal)
without worrying this will us to the best solution in the long run

Greedy algorithm does not find an optimal solution! (may depend on available coins)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Greedy algorithms: general characteristics?

A

Set of candidates
Solution function
Feasibility function
Selection function
Objective function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Greedy algorithms

Set of candidates ?

examples

A

An object / entity which is a part of a problem instance

Two disjoint subsets are accumulated
candidates that have been considered and chosen
candidates that have been considered and rejected

example
- Coins that are available for making a change
- Edges in a graph from which one is to find a shortest path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Greedy algorithms

Solution function?

examples

A

checks whether or not a particular set of candidates constitutes a solution
without worrying if the solution is optimal

example
does the selected coins match the amount to be paid?
does the selected edges give a path from between two specified nodes?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Greedy algorithms

Feasibility function?

examples

A

checks whether or not a set of candidates can be completed to a solution by adding other candidates

here, too, we do not worry about optimality

it is important to know whether or not a given set of candidates allows to build a solution, irrespective of optimality

(to show that the feasibility space is not empty)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Greedy algorithms

Selection function?

examples

A

indicates which of the remaining candidates which have neither been selected nor rejected is the most promising

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Greedy algorithms

Objective function?

examples

A

gives the value of the solution found

Example
- the number of coins used
- the length of a path

whatever value we aim to optimize

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do greedy algorithms work?

A

How do greedy algorithms work?

Initially, set of chosen candidates is empty

At each step
* consider adding the best remaining untried candidate (choice guided by the selection function)
* if the enlarged set is not feasible (guided by the feasibility function) reject the candidate we are currently considering
* otherwise, the candidate remains in the set of chosen candidates, where it stays from now on
* Each time we enlarge the set of candidates we check if it is a solution (guided by the solution function)

When the greedy algorithm works correctly, the first found solution is optimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can greedy algorithms backtrack?

A

There is no turning back in a greedy algorithm

Once a candidate is included in a solution, it stays there for good!

Once a candidate is excluded from a solution, it is never considered again!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

General greedy algorithm

A

function greedy(𝐶)
𝐶 is the set of candidates
𝑆 ← ∅ # we construct the solution in 𝑆
while 𝐶 ≠ ∅ and not solution(𝑆) do
𝑥 ← select(𝐶)
if feasible(𝑆 ∪ {𝑥}) then
𝑆 ← 𝑆 ∪ {𝑥}
if solution(𝑆) then # why check this here?
return 𝑆
else
return there are no solutions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Selection function

A

The key to a greedy algorithm is its selection function
Selection function is related to the objective function

Example
* objective – maximizing profit
selection – remaining item of largest value
* objective – minimizing cost
selection – cheapest remaining candidate

Sometimes, more than one selection function – care is needed in choosing the one which ensures correctness

16
Q

Change-making optimization problem

Set of candidates?

A

coins eg 100, 25, 10, 5, 1

17
Q

Change-making optimization problem

Solution function?

A

checks that the coins we have so far give the exact amount to be paid

18
Q

Change-making optimization problem

Feasibility function?

A

a set of coins in feasible if its total value does not exceed the
amount to be paid

19
Q

Change-making optimization problem

Selection function?

A

chooses the highest-valued coin from those remaining in the set of candidates

20
Q

Change-making optimization problem

Objective function?

A

the number of coins in the solution 2

21
Q

Change-making optimization problem

Greedy algorithm pseudocode?

A

function makeChange(n)
𝐶 = {candidate coins}
𝑆 ← ∅ # 𝑆 contains the solution
𝑠 ← 0 # 𝑠 is the sum of items in 𝑆
while 𝑠 ≠ 𝑛 do
𝑥 ← largest item in 𝐶 such that 𝑥 + 𝑠 ≤ 𝑛
if there is no such item
return no solution found
𝑆 ← 𝑆 ∪ {a coin of value 𝑥}
𝑠 ← 𝑠 + 𝑥
return 𝑆

22
Q

Subgraphs

Definition?

A

A subgraph of a given graph 𝐺 is the graph 𝐻, such that
𝑉(𝐻) ⊆ 𝑉(𝐺) and
𝐸(𝐻) ⊆ 𝐸(𝐺)

23
Q

Subgraphs

Types?

A

A vertex-induced subgraph of 𝐺 contains
* a subset of vertices of 𝐺
* all the edges that connect them in 𝐺

An edge-induced subgraph of 𝐺 contains
* a subset of edges of 𝐺
* all the vertices that are their end points in 𝐺

24
Q

In graphs, what is a clique?

A

graph in which all nodes are mutually connected

25
Q
A