Sunteți pe pagina 1din 37

Greedy Approach

Attempt to find an optimal solution in each stage Make decision that appears to be best at that stage

It never reconsiders a decision once made


What is best now, may be worst later!! Fails to find a globally optimal solution

Bin-Packing
INTRODUCTION:

Packing various items of certain sizes into a minimum number of bins with a fixed size Bin size is usually smaller than the sum of all objects

5 1 2 3 4 A B C

Application

Filling up containers Loading trucks with weight capacity Creating file backup in removable media Tapes and songs Breaks and commercials Bandwidth and packets Technology mapping in field-programmable gate array semiconductor chip design

Algorithm (Next-Fit)
FOR all objects i = 1, 2, . . . , n do WHILE ( Current Bin ! <= Available Bin ) IF Object i fits in current bin then Pack object i in current bin. ELSE Go to next bin make it the current bin, and pack object i END IF END WHILE END FOR

Dry Run ( Next-Fit )


3 1 2 4 5 6 A B C

FOR i = 1 6

3 2 4 5 6 1

Dry Run ( Next-Fit )


FOR i = 2 6 3 4 5 6 2 1

A
Execute ELSE STATEMENT! 3 FOR i = 3 6 2 4 5 6 A 1

Dry Run (NextFit)


FOR i = 3 6 3

2
4 5 6 A FOR i = 4 6 1

2 5 6 1

3 4

Dry Run (NextFit)


FOR i = 5 6 2 6 A FOR i = 6 6 Since there are no more Bins available Sixth Object is left un-packed! 2 6 A 1 B C 3 4 1 B C 3 4 5

Analysis Of Algorithm
Packing an object can be done in constant time, the algorithm is dominated by the FOR LOOP, which has a running-time of (n) Every case Algorithm.

Solution ?

Best-Fit Bin Packing


Brute Force Search

All bins are examined in each step Put each new item into a bin that comes

closest to filling it

Algorithm(Best-Fit)
FOR All objects i = 1, 2, . . . , n FOR All bins j = 1, 2, . . .n IF Object i fits in bin j then Calculate remaining capacity after the object has been added. END IF END FOR Pack object i in bin j, where j is the bin with minimum remaining capacity after adding the object (i.e. the object fits best) IF no such bin exists, open a new one and add the object. END FOR

Dry Run (Best-Fit)


FOR i = 1 & j = 1

3 2 4 5 6 A 1 B C

FOR i = 1 & j = 2

3 2 4 5 6 1

Dry Run (Best-Fit)


FOR i = 1 & j = 3

3 2 4 5 6 1

FOR i = 2 & j = 1

3 4 5 6 2 1

Dry Run (Best-Fit)


FOR i = 2 & j = 2

3 4 5 6 A 2 1 B C

FOR i = 2 & j = 3

3 4 5 6

2 1

Dry Run (Best-Fit)


FOR i = 3 & j = 1

3 4 5 6 A B

2 1 C

FOR i = 3 & j = 2

3 4 5 6

2 1

Dry Run (Best-Fit)


FOR i = 3 & j = 3

IF no such bin open new bin and add item

2 4 5 6 1

FOR i = 3

3 4 5 6

2 1

Dry Run (Best-Fit)


FOR i = 4 & j = 1

3 5 6

2 1

FOR i = 4 & j = 2

3 4 5 6

2 1

Dry Run (Best-Fit)


FOR i = 4 & j = 3

IF no such bin open new bin and add item

4 3 5 6 A B

2 1 C

FOR i = 4

3 4 5 6

2 1

Dry Run (Best-Fit)


5
FOR i = 5 & j = 1

3 4 6 A B

2 1 C

FOR i = 5 & j = 2

5 3 4 6 1 2

Dry Run (Best-Fit)


FOR i = 5 & j = 3

5 3 4 6 1 2

A
6
FOR i = 6 & j = 1

5 3 4 1 2

Dry Run (Best-Fit)


FOR i = 6 & j = 2

6
3 4

5
2 1

C 6

FOR i = 6 & j = 3

5 3 4 1 2

Dry Run (Best-Fit)


FOR i = 6 Pack object i in bin j, where j is the bin with remaining capacity ( best-fit )

6 3

5 2

4
1 A
END FOR

Analysis Of Algorithm
Since all bins are examined in each step, the algorithm has an obvious running time of (n2)
This is an every Case time complex algorithm.

Graph Coloring
Color the vertices of graph using as few colors as possible Colors in some sense represent resources

So, graph coloring is to assign a limited number of resources (colors) to a set of vertices (nodes) under incompatibility constraints (adjacent vertices)

Applications
Timetabling Scheduling Frequency assignment Register allocation Air traffic management Train plat forming Sudoku Pattern matching

Algorithm ( First-Fit )
FOR all (vi E) do choose a legal color ci color vi V with ci END FOR

G = ( V , E ) is a Graph C : V { C1,C2, ...,Cn } LEGAL COLORING: G = ( V , E ): C(vi) C(vj) 8(vi, vj) E

Dry Run
D Consider this input with Vertices = 5 & Input Sequence INP = { A, D, E, C, B }

C
E

D FOR VA INP VA= BLUE

C
E

Dry Run
D
FOR VD INP VD = BLUE A B C E

D FOR VE INP VE = BLUE

C
E

Dry Run
D
FOR VC INP VC = GREEN A B C E

FOR VB INP VB = MAROON

C E

Drawback
Given graph could be colored using only 2 colors but greedy choice to the input sequence is not optimal as it uses 3 colors.

Analysis of Algorithm
Every case time complex algorithm, with running time (v)

Solution?

DSATUR algorithm
Color v with the maximal degree repeat color v having the maximal saturation degree if tie of saturation degrees then color v having the maximal uncolored neighbors if tie of uncolored neighbors then color v randomly end if end if until V is empty

Analysis Of Algorithm

DSATUR a sequential coloring algorithm with a dynamically established order of the vertices DSATUR starts by assigning color 1 to a vertex of maximal degree. The vertex to be colored next in the sequential coloring procedure of DSATUR is a vertex x with maximal degs(x) The complexity of DSATUR is O(|V |)

Thank you!

S-ar putea să vă placă și