Combinatorial Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Combinatorial Functions

Description:

Combinatorial Functions Recursion for problem solving Enumerating Initial segments (prefixes) inits [1,2,3] = [[],[1],[1,2],[1,2,3]] inits [2,3 ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 14
Provided by: cecsWrig9
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Combinatorial Functions


1
Combinatorial Functions
  • Recursion for problem solving

2
Enumerating Initial segments (prefixes)
  • inits 1,2,3 ,1,1,2,1,2,3
  • inits 2,3 , 2,
    2,3
  • fun inits
  • inits (xxs)
  • (map (fn ys gt xys)
  • (inits xs)
  • )
  • fun inits
  • inits xs (inits (init xs))_at_xs

3
Enumerating Subsequences
  • subseq 2,3 ,2,3,2,3
  • subseq 1,2,3
  • , 1,2,3, 1,2,1,3,2,3,1,2,3
  • fun subseq
  • subseq (xxs)
  • let val ss subseq xs
  • in ss_at_(map (fn ys gt xys) ss)
  • end

4
Enumerating permutations
  • perms 2 2
  • perms 1,2 1,2, 2,1
  • perms 0,1,2 0,1,2,1,0,2,1,2,0,
    0,2,1,2,0,1,2,1,0
  • fun interleave x x
  • interleave x (yys) (xyys)
  • (map(fn xsgtyxs)(interleave x ys))
  • fun perms
  • perms (xxs)
  • foldr (op _at_)
  • (map (interleave x) (perms xs))

5
List partitions
  • The list of non-empty lists L1,L2,,Lm forms a
    list-partition of a list L iff
  • concat L1,L2,,Lm L
  • 1,2 -gt 1,2, 1,2
  • 0,1,2 -gt 0,1,2,
  • 0,1,2, 0,1,2,
    0,1,2

6
Counting problem
  • fun cnt_lp 0 0
  • cnt_lp 1 1
  • cnt_lp n 2 (cnt_lp (n-1))
  • ( cnt_lp n 2(n-1) for n gt 0 )
  • Property
  • cnt_lp (length xs)
  • (length (list_partition xs))

7
Constructing List Partitions
  • fun lp
  • lp (x) x
  • lp (yxxs)
  • let val aux lp (xxs) in
  • (map (fn ss gt yss) aux)
  • _at_
  • (map (fn ss gt (y(hd ss))
  • (tl ss)) aux)
  • end

8
Set partition
  • The set of (non-empty) sets s1,s2,,sm
    forms a set partition of s iff the sets sis
    are collectively exhaustive and
    pairwise-disjoint.
  • E.g., set partitions of 1,2,3 -gt
  • 1,2,3,
  • 1, 2,3,
  • 1,2, 3,
  • 2, 1,3,
  • 1,2,3
  • (Cf. list partition, number partition, etc.)

9
Divide and Conquer
m-partition of 1,2,3
1 occurring with with a part in m-partition of
2,3
(m-1)-partition of 2,3
solitary part 1
2-partitions of 1,2,3
1,2,3 U
1,2,3 , 2,1,3
10
Counting problem
  • fun cnt_m_sp 1 n 1
  • cnt_m_sp m n
  • if m gt n then 0
  • else if m n then 1
  • else (cnt_m_sp (m-1) (n-1))
  • (m (cnt_m_sp m (n-1)))
  • Dependency (visualization)
  • Basis Row 1 and diagonal (Half-plane
    inapplicable)
  • Recursive step Previous row, previous column

11
(contd)
  • upto 3 6 3,4,5,6
  • fun upto m n
  • if (m gt n) then
  • else if (m n) then m
  • else m upto (m1) n
  • fun cnt_sp n
  • foldr (op ) 0
  • (map (fn m gt cnt_m_sp m n)
    (upto 1 n))

12
Constructing set partitions
  • fun set_parts s
  • foldr (op_at_)
  • ( map (fn m gt (m_set_parts m s))
  • (upto 1 (length s)) )
  • fun ins_all e
  • ins_all e (sss)
  • (((es)ss)
  • ( map (fn ts gt s ts)
  • (ins_all e ss) ) )

13
  • fun m_set_parts 1 s s
  • m_set_parts m (s as hsts)
  • let val n (length s) in
  • if m gt n then
  • else if m n then
  • foldr (fn (e,ss)gtess ) s
  • else let
  • val p1 (m_set_parts (m-1) ts)
  • val p2 (m_set_parts m ts)
  • in
  • (map (fn ss gt hsss) p1)
  • _at_
  • (foldr (op_at_) (map (ins_all hs) p2 ))
  • end
  • end
Write a Comment
User Comments (0)
About PowerShow.com