Week 11


Problem-Solving Strategies1/31

Five basic problem-solving strategies:

  1. solution by evolution: adapt a known method
  2. divide and conquer: solve partial problem, then extend
  3. generate and test: make possible solutions, test them
  4. approximation
  5. simulation


Generate and Test


Generate and Test3/31

In scenarios where

then a generate and test strategy can be used.

It is necessary that states are generated systematically

Simply generating random states and testing them ...


... Generate and Test4/31

Simple example: checking whether an integer n is prime

Generation is straightforward: Testing is also straightfoward:


... Generate and Test5/31

Function for primality checking:

// check whether n is a prime number
int isPrime(int n) {
	int retval = 1;
	int i; // next number to be tested as possible divisor     

	for (i = 2; i < n; i++) {  // generate
		if (n % i == 0) {  // test
			// i is a divisor => n is not prime
			retval = 0;
		}
	}
	return retval;
}

Can be optimised: end loop after divisor found, change  (i < n)  to  (i*i <= n)


Exercise: Primes6/31

Given a function to test for primality:

int isPrime(int n);

write a function to find the smallest prime larger than a given number:

int nextPrime(int n) { ... }

Then write a program to produce all prime numbers starting from 1.


Example: Subset Sum7/31

Problem to solve ...

Is there a subset S of these numbers with sum(S)=1000?

 34,  38,  39,  43,  55,  66,  67,  84,  85,  91,
101, 117, 128, 138, 165, 168, 169, 182, 184, 186,
234, 238, 241, 276, 279, 288, 386, 387, 388, 389

General problem:

What strategy might we use?


... Example: Subset Sum8/31

Simple generate and test approach:

A: set of n distinct integers

for each subset S of A {
	if (sum(S) == k)
		return YES
}
return NO

How many subsets are there of n elements?

How could we generate them?


Exercise: Generate Subsets9/31

Devise a method to generate subsets

Hints:


Sidetrack: Bit Operators10/31

C can treat basic data types as bit-strings   (unsigned int)

E.g. 'a' == 0x61 == 0110 0001
E.g. 4999 == 0x00001387 == 0001 0011 1000 0111

Operations on bits:


... Sidetrack: Bit Operators11/31

More bit operations:


Exercise: Find Subsets with Sum k12/31

Extend the subset generator

To solve our original problem: Are there any problems with this approach?


... Exercise: Find Subsets with Sum k13/31

Alternative approach ...


... Exercise: Find Subsets with Sum k14/31

Leads to the following divide-and-conquer solution:

int subsetsum(int A[], int n, int k) {
	int retval;
	if (k == 0)
		retval = 1;   // empty set solves this
	else if (n == 0)
		retval = 0;   // no elements => no sums
	else {
		// use considerations from previous page
		int m = A[n-1];
		retval = (subsetsum(A, n-1, k-m) || subsetsum(A, n-1, k));
	}
	return retval;
}


... Exercise: Find Subsets with Sum k15/31

Subset Sum is typical of a class of exponential algorithms

This is a lot slower than quadratic algorithms (e.g. sorting in the worst case)


Approximation


Problem-Solving Strategies17/31

Five basic problem-solving strategies:

  1. solution by evolution   (adapt a known method)
  2. divide and conquer   (solve partial problem, then extend)
  3. generate and test   (make possible solutions, test them)
  4. approximation   (solve a close, but easier, problem)
  5. simulation   (create an executable model)


Approximation18/31

Approximation is often used to solve numerical problems

Essence of approximation:


Exercise: Length of a Curve19/31

Estimate length: approximate curve as sequence of straight lines.

[Diagram:Pic/curveLength-small.png]

Approximate curve length by sum of line lengths


... Exercise: Length of a Curve20/31

Trade-offs in this method:

However, too many steps may lead to higher rounding error.

Each f has an optimal step size ...


... Exercise: Length of a Curve21/31

length = curveLength(0, M_PI, sin);

Convergence when using more and more steps

steps =       0, length = 0.000000
steps =      10, length = 3.815283
steps =     100, length = 3.820149
steps =    1000, length = 3.820197
steps =   10000, length = 3.819753
steps =  100000, length = 3.820198
steps = 1000000, length = 3.820198

Actual answer is 3.820197789...


Example: Finding Roots22/31

Find where a function crosses the x-axis:

[Diagram:Pic/roots2-small.png]

Generate and test: move x1 and x2 together until "close enough"


Simulation


Problem-Solving Strategies24/31

Five basic problem-solving strategies:

  1. solution by evolution   (adapt a known method)
  2. divide and conquer   (solve partial problem, then extend)
  3. generate and test   (make possible solutions, test them)
  4. approximation   (solve a close, but easier, problem)
  5. simulation   (create an executable model)


Simulation25/31

In some problem scenarios

Examples: weather forecasting, traffic flow, queueing, games

Such systems typically require random number generation

Accuracy of results depends on accuracy of model.


Example: Gambling Game26/31

Consider the following game:

Is this game worth playing?

Test: start with $5 and play until you have $0 or $20.

In fact, this example is reasonably easy to solve analytically.


... Example: Gambling Game27/31

We can get a reasonable approximation by simulation


Exercise: Testing the Gambling Game28/31

Implement a program to test the Gambling Game:

Reminder of the rules:


Example: Area inside a Curve29/31

Scenario:

[Diagram:Pic/curveArea-small.png]


... Example: Area inside a Curve30/31

Simulation approach to determining the area:

I.e. we approximate the area within the curve by using the ratio of points inside the curve against those outside

Also known as Monte Carlo estimation


Tips for Last Lab31/31


Course Revision

Three challenging exercises on the focal topic of this course: advanced data structures


Last tutorial is practice for Theory Part of exam


Produced: 12 Oct 2016