33
1 Algorithms & Data Structures for Games Lecture 2A Minor Games Programming

1 Algorithms & Data Structures for Games Lecture 2A Minor Games Programming

Embed Size (px)

Citation preview

1

Algorithms & Data Structures for Games

Lecture 2A

Minor Games Programming

2

Algorithms and Data Structures

Feedback previous lectures Theory: Greedy Algorithms

Jan [email protected]

3

Theory: Greedy Algorithms

Algorithm Design Techniques 10.1 Greedy Algorithms 10.1.1 A Simple Scheduling Problem 10.1.2 Hufman Codes 10.1.3 Approximate Bin Packing Finally: some exercises

4

Greedy Algorithms

Definition: An algorithm that always takes the best immediate, or local, solution while finding an answer.

Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.

http://en.wikipedia.org/wiki/Greedy_algorithm

Coin-changing problem

How to minimize the number of bills and coins, when paying an amount in US currency

Solution: Greedy algorithm: repeatedly dispense

the largest denomination.

5

6

Example

7

Example

Okay, the algorithm usually works. In fact, for the U.S. coin system {1, 5, 10, 25}, it always yields the optimal set. However, for other sets, like {1, 5, 8, 10} and a goal of 13, this greedy algorithm would take one 10, and then three 1's, for a total of four coins, when the two coin solution {5, 8} also exists.

A small exercise

I buy a hamburger in Amsterdam and I pay in coins the amount of 3.98 euro’s.

What is the minimal numbers of coins I need to pay for the burger?

And when I buy 100 burgers?

The euro coins are 1, 2, 5, 10, 20, 50, 100 and 250 cents.

8

9

Problem: A Simple Scheduling Problem

What is the best way to schedule jobs in order to minimize the average completion time?

Greedy Solution: arrange by shortest job first

10

Possible schedules (single processor)

Average completion time: Top: 25, Bottom: 17,75 (optimal)Optimal Schedule: arranged by shortest job first.

11

Multiprocessor case

12

Possible Schedules (3 processors)case I

13

Possible Schedules (3 processors)case II

14

Minimizing the final completion time(much harder to solve …)

A small exercise

How do I schedule on my 2 processor computer the following jobs and get the lowest possible average completion time?

J1(5), J2(2), J3(8), J4(1), J5(6), J6(4) with the restriction J4 can only run when J2 has been finished.

What is the average completion time?

15

16

Huffman Codes

File compression strategy:

Allow the code length to vary from character to character and ensure that the frequently occurring characters have short codes.

http://en.wikipedia.org/wiki/Huffman_coding

17

Using a standard coding scheme

Representation of the original code in a tree

18

19

Optimal prefix code

20

Optimal prefix code:TREE

A small exercise

Please decode the following (hexadecimal) bit string in normal characters, using the optimal prefix code tree of previous slide:

1E06 D922 1

21

22

Huffman’s Algorithm

Greedy, To produce the optimal prefix code See example in figure’s 10.13 thru

10.19

A small exercise

Construct the Huffman code of the following text:

COFFEE TIME FOR YOU!!!!!!!!!!

What is the compression rate?

23

24

Approximate Bin Packing

We are given N items of sizes s1, s2, .., sn. The problem is to pack these items in the fewest number of bins.

25

Approximate Bin Packing

Greedy algorithms to solve the bin packing problem.

On-line bin packing: Next fit First fit Best fit

Off-line bin packing: First fit decreasing Best fit decreasing

On-line and Off-line

On-line: Each item must be placed in a bin before

the next item can be processed.

Off-line: We do not need to do anything until all the

input has been read.

26

Next fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8

27

First fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8

28

Best fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8

29

Off-line

First sort the items in decreasing order.

Then execute first-fit or best-fit

30

31

Optimal packing for:0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8

32

A small exercise

Show the results of all the bin-packing strategies on the input: 0.1, 0.3, 0.5, 0.2, 0.8, 0.4, 0.7

33

Homework

Homework Chapter 10.1

Exercises (should not be handed in!) 10.3 10.7 (program) 10.10