Sei sulla pagina 1di 11

The Greedy method

General method: Given n inputs choose a sub-


set that satisfies some constraints.

– A subset that satisfies the constraints is called


a feasible solution.

– A feasible solution that maximises or min-


imises a given (objective) function is said to
be optimal.

Often it is easy to find a feasible solution but


difficult to find the optimal solution.

The greedy method suggests that one can de-


vise an algorithm that works in stage. At each
stage a decision is made whether a particular
input is in the optimal solution. This is called
subset paradigm.
1
Example: A child buys candy buys candy val-
ued at less than $1 and gives a $1 bill to the
cashier.

The cashier wishes to return change using the


fewest number of coins. The cashier constructs
the change in stages. At each stage increase
the total amount of change as much as possi-
ble. The added coin should not cause the total
amount of change given so far to exceed the
final desired amount (feasibility).

Suppose that 67 cents is due to the child. The


first two coins selected are quarters. The third
coin cannot be a quarter as it is not feasible.
The third is dime (10 cents), then a nickle (5
cents), and finally two pennis are added to the
change.

2
Example: Your Train breaks down in a desert
and you decide to walk to nearest town. You
have a rucksack but which objects should you
take with you ?

Feasible: Any set of objects is a feasible solu-


tion provided that they are not too heavy, fit
in the rucksack and will help you survive (these
are constraints).

An optimal solution is the one that maximises


or minimises something

– One that minimises the weight carried

– One that fills the rucksack completely (max-


imise)

– One that ensures the most water is taken


etc.
3
Example: You are writing a computer game
and need to store your .wav, .jpg, and .mpg
files on a set of CD-roms. The constraint is the
length of files and the size of the CD (capacity)

A feasible solution is any combination that fits.

An optimal one is the order that minimises the


access time for each file.

– In this case you will have the minimum delay


for the player.

– Hence you might sell more games.

– Have smoother game action.

4
Other examples: You want to work out the
best way to route a phone message through a
mobile phone network.

You are an explorer and find a treasure house


full of gold, jewels, and other stuff. You want
to take as much back with you as possible (i.e.
you are greedy).

A number of users want to run programmes


on a computer. How do you schedule them so
they are executed as quickly as possible.

A factory use a production line to make sev-


eral products. How should you schedule the
production runs to make the most profit.

You run a haulage company and want to work


out how to deliver all your products to a set
outlets with the least cost and time.

You run an airline and want to work out how


best to turn the plane around on landing and
get it flying again.
5
Control Abstraction for Greedy algorithm

Algorithm Greedy(A : set; n : integer){


M akeEmpty(solution);
f or(i = 2; i <= n; i + +){
x = Select(A);
if F easible(solution, x) then
solution = U nion(solution, {x})
}
return solution
}
The function Greedy describes the essential
way that a greedy algorithm will look, once a
particular problem is chosen and the functions
Select, F easible and U nion are properly imple-
mented.

The function Select selects an input from A


whose value is assign to x. F easible is a Boolean-
valued function that determines if x can be in-
cluded into the solution vector. The function
U nion combines x with the solution, and up-
date the objective function.
6
Spanning tree: Let G = (V, E) be an undi-
rected connected graph. A subgraph T = (V, G′)
of G is a spanning tree of G if T is a tree.

e.g. three spanning trees of G.

– Both T and G have same set of vertices V .

– T is connected but has no cycles.

– has (n − 1) edges.

In practical applications, the edges have weights


assigned to them, which may represent the
cost of constructions, the length of link, etc.
7
Minimum Spanning Tree (MST): Given a con-
nected weighted graph G, we wish to find a
set of edges with minimum (sum) weights such
that all vertices are connected.

Feasible solution – The spanning trees T =


(V, E) represent feasible choices

Optimal solution – The spanning tree Topt =


(V, E) with the lowest total cost of edges is the
one we want.

Since the identification of a minimum cost span-


ning tree involves the selection of a subset of
edges, this problem fits the subset paradigm.
8
MST example

What is MST?

How many more trees can you find ?

What is the smallest cost?

9
MST Applications

MST a fundamental problem that arises in many


scenarios

– Network design ( Telephone, electric, com-


puter, road, ...)

– Image processing, LDPC codes,

– Protocol in networks ( Multicast, auto-config


protocol for Ethernet bridges )

– Cluster Analysis (Image, data, ...)

– Approximate algorithms for NP-hard prob-


lems (TSP, Stein tree)

10
MST algorithms

Brute Force: For small graphs, MST by enu-


meration may be possible

We will look at two Greedy Algorithms. They


solve the MST problem by greedy approaches
in two different ways

–Kruskal

greedily expand a forest of trees into an MST

– Prim

greedily grow a tree from a starting vertex

Both yield an optimal solution.

11

Potrebbero piacerti anche