Todays Topic - PowerPoint PPT Presentation

About This Presentation
Title:

Todays Topic

Description:

... except: Of the 20 cards, 5 are identical birthday cards, and 7 others are ... ( Swapping two identical cards doesn't create a new permutation. ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 8
Provided by: whi11
Learn more at: https://cs.gmu.edu
Category:
Tags: birthday | cards | todays | topic

less

Transcript and Presenter's Notes

Title: Todays Topic


1
Todays Topic
  • Recursion and backtracking
  • Next Class
  • Dynamic Programming (hopefully)

2
Recursion
  • Base Case stopping point
  • Recursive case divide the problem into smaller
    problems, and combine the solution must move
    toward base case(s).

3
Magic Squares
  • A magic square is a 'matrix' or a 2-dimensional
    grid of numbers where the numbers along any
    dimension add up to a given number (15 for
    example).
  • How to generate?
  •  

4
Generating Squares - 1
  • void Permute(n)
  • for i 1 to 9
  • if Bi0 // if i is free
  • Bi1
  • Ani
  • if n
  • else TestMagic( ) //at the end
  • Bi0

5
Generating Squares - 2
  • void Permute(n)
  • for i 1 to 9
  • if Bi0 // if i is free
  • Bi1 Ani
  • if (n 3 0) // if n is div by 3
  • if RowSum() 15
  • if n
  • else TestMagic()
  • else Permute(n1) //recurse
  • Bi0

6
Exercises
  • Write a function using Recursion to display all
    anagrams of any string entered by the user.
  • Write a function using Recursion to do the
    following You have 20 different cards. You have
    only 7 envelopes. You can therefore send only 7
    cards. Display all possible ways you can fill the
    7 envelopes. (Every card is different)

7
Exercises
  • Same as previous, except Of the 20 cards, 5 are
    identical birthday cards, and 7 others are
    identical anniversary cards. Find all
    permutations, without repetitions. (Swapping two
    identical cards doesn't create a new
    permutation.)
  • Write a function using Recursion to do the
    following You are the manager in a small
    factory. You have 7 workers and 7 jobs to be
    done. Each worker is assigned one and only one
    job. Each worker demands different pay for each
    job. (E.g. Worker Sam demands 10 for welding,
    15 for carpentry, etc. Worker Joe demands 12
    for welding, 5 for carpentry, etc.) Find the
    job-assignment which works out cheapest for you.
Write a Comment
User Comments (0)
About PowerShow.com