Applied Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Applied Algorithms

Description:

For each choice prepend it to the permutations of what's left after removing the ... For each choice we prepend it to the permutations of list of size three. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: she46
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: Applied Algorithms


1
Applied Algorithms
  • Lecture
  • Backtracking

2
Announcements
  • ACM Student Chapter Meeting
  • Where    FAB-150. Fourth Avenue Building, across
    the hallway from the Linux Lab
  •  When     Wednesday, June 1. 1130AM
  •  Topic    Become an Undergrad Researcher at PSU!
  • Final Exam Date
  • Monday, June 6, 2005
  • 1230 1420 PM

3
Checking for primality
  • Bool prime(int n) // test for primality of n
  • int i int m
  • if (n2) return TRUE
  • if (nlt2) return FALSE
  • m sqrt(n)1
  • for (i2iltmi)
  • if (ni 0) return FALSE
  • return TRUE

4
Sum of 4 primes
  • Why wont this work?
  • for (i2, iltn, i)
  • if (prime(i))
  • for (j2, jltn, j)
  • if (prime(j))
  • for (k2, kltn, k)
  • if (prime(k))
  • for (l2, lltn, l)
  • if (prime(l))
  • if (ijkltarget)
  • printf(ld ld ld ld\n,I,j,k,l)

5
What if we looped over just primes
  • Precompute all the primes from 1 to target
  • Int primessize
  • Void initPrimes(int largest)
  • How many primes are there between 1 and
    10,000,000 ?

6
Lets explore?
  • int countPrimes(int n)
  • int i int count
  • count 0
  • for (i2iltni)
  • if (prime(i)) count
  • if (i100000 0)
  • printf("i ld, num primes
    ld\n",i,count)
  • printf("num primes ld density
    ld\n",count,n/count)

7
Analyze
  • Are there a lot of primes?
  • Whats the average distance between primes?
  • Is this number big?
  • Does this suggest a solution?

8
Largest distance between primes
  • int enumerate(int n)
  • int i int count int last int maxdif
  • count 0
  • last 2
  • maxdif 0
  • for (i3iltni)
  • if (prime(i))
  • count
  • maxdif max(i-last,maxdif)
  • last i
  • if (i100000 0)
  • printf("i ld, num primes ld, maxdif
    ld\n
  • ,i,count,maxdif)

9
Can we find 3 primes?
  • Why is this more feasible?
  • Should be easy to find 3 primes that add up to a
    small integer since we need only loop through
    primes up to that integer.
  • If max154, how many small primes will we need?

10
  • int firstThirty()
  • int i int count
  • count 0
  • for (i2countlt30i)
  • if (prime(i))
  • count
  • printf("ld,",i)

11
Strategy
  • / An array of the first 30 small primes /
  • int sp30
  • 2,3,5,7,11,13,17,19,23,29,31,37,41
  • ,43,47,53,59,61,67,71,73,79,83,89,97
  • ,101,103,107,109,113
  • int ps2003
  • / ps6 2,2,2 psi stores 3 primes that
    add up to i
  • ps7 2,2,3
  • ps8 2,3,3
  • . . .
  • ps199 3,83,113
  • /

12
Computing ps
  • int init_ps_sub(n)
  • int j int k int l
  • for (j0jlt30j) // Find 3 small primes
  • for (k0klt30k) // such that p1p2p3 n
  • for (l0llt30l) // store them in the "ps"
    array
  • if (spjspkspln)
  • psn0 spj
  • psn1 spk
  • psn2 spl
  • return 0
  • printf("Can't find 3 small primes for
    ld\n",n)

13
Computing 4 primes
  • int test(int n)
  • int i int diff
  • if (nlt8) printf("Imposible\n") return 0
  • for (in-6 igt2 i--) // find largest prime
    at least
  • if (prime(i)) // 6 less than n, call
    it "i"
  • diff n-i // then lookup 3 small
    primes
  • printf("ld ld ld ld\n
  • ,i,psdiff0,psdiff1
  • ,psdiff2)
  • return 0

14
In Class Problems
  • Queue 8.6.3
  • Page 179 of the text
  • We will discuss this in class

15
Queues of N people of different height
How can we count how many can see to the left? To
the right?
16
Problem
  • Given a queue of fixed size N.
  • A fixed number of people who can see to the left.
  • A fixed number of people who can see to the
    right.
  • How many way can we arrange the N people to get
    the matching left and right counts?

17
Solution?
  • Compute all possible ways to arrange N people.
  • Throw away those that dont have the correct left
    and right count.
  • Arranging things in different order is called a
    permutation.

18
Computing permutations
  • What are the permutations of 1,2,3 ?
  • Chose an element.
  • Choices are 1, 2, or 3
  • For each choice prepend it to the permutations of
    whats left after removing the choice.
  • Candidates 1,2,3
  • (1,2,3) (2,1,3) (3,1,2)
  • What are the permutations of 2,3?
  • 2,3 and 3,2

19
  • Permutations 1,2,3
  • candidates (1,2,3) (2,1,3)(3,1,2)
  • permutations 2,3 2,3, 3,2
  • 1,3 1,3,3,1
  • 1,2 1,2,2,1
  • 1,2,3 1,3,2
  • 2,1,3,2,3,1
  • 3,1,2,3,2,1

20
  • candidates xs f xs
  • where f before (choiceafter)
  • (choice,reverse beforeafter)
  • (f (choicebefore) after)
  • f before
  • gen2 Int -gt Int -gt Int -gt Int
  • gen2 chosen all (reverse chosen)all
  • gen2 chosen toAdd all add pairs all
  • where pairs candidates toAdd
  • add all all
  • add ((choice,remains)pairs) all
  • add pairs
  • (gen2 (choicechosen)
  • remains all)

21
How many permutations are there?
  • 1,2,3,4
  • 4 choices
  • For each choice we prepend it to the permutations
    of list of size three.
  • Perms( 0) 1 (the empty sequence)
  • Perms( x1) (x1) Perms( x)
  • Perms( n) n!

22
We need some trick
  • Find the tallest person, and then break it into 2
    sub problems. Where can the tallest person be
    given left and right numbers?

23
Bounding the tallest
  • If left n, then tallest must be at position at
    least n,
  • If right m, then tallest must be at position at
    most Max-m1
  • So tallest must be between
  • Left ? tallest ? N-Right1
  • Left3
  • Right3

24
Solving the sub problem
  • k exact number of views to the left required
  • n size of the permutation required
  • chosen is the prefix of the permutation so far
  • remains the set left to choose the next element
    from
  • p2 k n chosen remains all
  • length chosen n (reverse
    chosen,remains)all
  • True add pairs all
  • where pairs candidates remains
  • add all all
  • add ((x,rs)pairs) all
  • let next (xchosen)
  • in if (possible k n next)
  • then (p2 k n next rs (add pairs
    all))
  • else (add pairs all)

25
Whats possible?
  • Sometimes we know a chosen sequence cant lead to
    a good solution.
  • possible k n xs viewl lt k
  • viewl n - len gt k
  • where len length xs
  • viewl countRight xs

26
All possible winners
  • try2 n left right
  • n1 lt leftright
  • True concat (map each positionsN)
  • where positionsN left .. n - right 1
  • each i concat(map f leftChoices)
  • where leftChoices p2 (left-1) (i-1)
  • 1..n-1
  • f (lefts,remains)
  • concat(map add (p2 (right-1)
  • (n-i)
  • remains
    ))
  • where add (rights,_)
  • lefts n
    reverse rights

27
Todays Assignments
  • Read for next time
  • Chapter 9 of the text. pp 188-218
  • Be prepared to answer questions in class next
    Friday from the reading.
  • Programming assignment
  • 8.6.8 Bigger Square Please
  • Page 186
  • Write a solution
  • Submit your solution (until you get it right)
  • Hand in both your program, and the judge output.
  • Those who volunteer to discuss their program get
    class participation points. Email me solutions
    before noon on Friday, May 6.
Write a Comment
User Comments (0)
About PowerShow.com