home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!att!att!dptg!ulysses!allegra!princeton!fish.Princeton.EDU!lhjensen
- From: lhjensen@fish.Princeton.EDU (Leif Jensen)
- Subject: Re: Neophyte C programmer needs HELP!
- Message-ID: <1993Jan20.044915.12170@Princeton.EDU>
- Originator: news@nimaster
- Sender: news@Princeton.EDU (USENET News System)
- Nntp-Posting-Host: fish.princeton.edu
- Organization: Princeton University
- References: <1993Jan19.122451.10366@ac.dal.ca> <1993Jan19.213452.4531@organpipe.uug.arizona.edu> <1993Jan19.221115.4901@organpipe.uug.arizona.edu>
- Date: Wed, 20 Jan 1993 04:49:15 GMT
- Lines: 51
-
- In article <1993Jan19.221115.4901@organpipe.uug.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
- >In article <1993Jan19.213452.4531@organpipe.uug.arizona.edu>, dave@cs (Dave Schaumann) writes:
- >>In article <1993Jan19.122451.10366@ac.dal.ca>, francis@ac writes:
- >>>2. If Sn denotes the number of subsets of {1,2,3,...,n} that do *not*
- >>> contain two *consecutive* integers, write an algorithm and program
- >>> to compute the value Sn for any set {1,2,3,...,n}. For example:
- >>> S3=5 since the subsets of {1,2,3} that do not contain consecutive
- >>> integers are {}, {1}, {2}, {3}, {1,3}.
-
- >>This one's a bit harder.
-
- >Actually, not that hard at all... (tho, I suppose it might be harder
- >for those who've never seen dynamic programming before...)
-
- >>It might even be possible to use induction to find a formula...
-
- >Actually, once you start generating solutions, the formula's
- >pretty obvious. Proving it's correct, on the other hand, would
- >be a bit more difficult.
-
- First consider...(created easily with vi)
-
- S_1 = {{},{1}}
- S_1*= {{3},{1,3}}
- S_2 = {{},{1},{2}}
- S_2*= {{4},{1,4},{2,4}}
- S_3 = {{},{1},{2},{3},{1,3}}
- S_3*= {{5},{1,5},{2,5},{3,5},{1,3,5}}
- S_4 = {{},{1},{2},{3},{1,3},{4},{1,4},{2,4}}
- S_4*= {{6},{1,6},{2,6},{3,6},{1,3,6},{4,6},{1,4,6},{2,4,6}}
- S_5 = {{},{1},{2},{3},{1,3},{4},{1,4},{2,4},{5},{1,5},{2,5},{3,5},{1,3,5}}
-
- It's not so hard to prove. For S_n = {a1,a2,...,am}, define
- S_n* = {a1 union {n+2}, a2 union {n+2},..., am union {n+2}}. It is
- easy to see that every element of S_n* is in S_{n+2} and is not an
- element of S_{n+1}. Trivially every element of S_{n+1} is in S_{n+2},
- and it is simple to see that in fact all elements of S_{n+2} are also
- in either S_{n+1} or S_n*. Thus we have a recurrence relation on the
- orders of the S_n: |S_{n+2}|=|S_{n+1}|+|S_n|, and the solution is
- familiar from the Fibinocci sequence.
-
- |S_n| = 5^{-1/2} (\phi^{n+2} - (-1/\phi)^{n+2}),
-
- where \phi is the Golden Ratio (1+\sqrt 5)/2.
-
- How would the dynamic programming solution go? (and why would it
- be preferable to an analytic solution?)
-
- --
- Leif Jensen
- lhjensen@phoenix.princeton.edu
-