home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!asuvax!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
- From: dave@cs.arizona.edu (Dave Schaumann)
- Newsgroups: comp.lang.c
- Subject: Re: Neophyte C programmer needs HELP!
- Message-ID: <1993Jan22.040237.9860@organpipe.uug.arizona.edu>
- Date: 22 Jan 93 04:02:37 GMT
- References: <1993Jan19.122451.10366@ac.dal.ca> <1993Jan19.213452.4531@organpipe.uug.arizona.edu> <1993Jan19.221115.4901@organpipe.uug.arizona.edu> <1993Jan20.044915.12170@Princeton.EDU>
- Sender: news@organpipe.uug.arizona.edu
- Reply-To: dave@cs.arizona.edu (Dave Schaumann)
- Organization: University of Arizona
- Lines: 67
- In-Reply-To: lhjensen@fish.Princeton.EDU (Leif Jensen)
-
- In article <1993Jan20.044915.12170@Princeton.EDU>, lhjensen@fish (Leif Jensen) writes:
- The problem:
-
- >>>>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}.
-
- >How would the dynamic programming solution go? (and why would it
- >be preferable to an analytic solution?)
-
- Let's consider the "analytic" solution, by which I presume you mean the
- brute force method. This is simply
-
- 1. Generate all possible subsets.
- 2. Count the ones that meet the restriction.
-
- Nice and simple. Unfortunately, there are combinatorially many subsets,
- and so this solution is impractical for very large n.
-
- Hmm. After looking at my algorithms book, perhaps "Dynamic programming"
- isn't quite the best description of what I had in mind...
-
- A typical algorithm to choose m items from n possible choices would be
-
- int Chosen[N] ; /* assume all 0 initially */
-
- void choose( int last_choice, int num, int max ) {
- int i ;
-
- if( num == 0 ) { check( Chosen ) ; return ; }
-
- for( i = last_choice+1 ; i < max ; i++ )
- if( !Chosen[i] ) {
- Chosen[i] = 1 ; choose( i, num-1, max ) ; Chosen[i] = 0 ;
- }
-
- }
-
- The problem is that this algorithm generates lots of "impossible"
- candidates. However, if one simply initializes "i" to "last_choice+2"
- instead of "last_choice+1", all generated subsets fit the requirement.
- Of course, since the number of such subsets increases exponentially,
- it is still an exponential algorithm, but it is much better than
- the first...
-
- Of course, this is not dynamic programming at all. In my defense, I
- can say that I did come to realize this solution by thinking about
- a dynamic programming solution.
-
- BTW, for those wondering, dynamic programming is a method which
- an algorithm that would otherwise have combinatorial time complexity
- uses (typically a 2-dimensional) array to hold intermediate results.
- Exploiting the fact that the combinatorial nature of the algorithm
- results from computing the same functions over and over again, this
- "saving of intermediate values" results in a polynomial-time algorithm.
-
- Those further interested by my (admittedly weak) description of
- dynamic programming are advised to locate a good algorithms book.
-
- I see that _Algorithms_ by Sedgewick (and thus, I suppose, it's various
- permutations) has a chapter on dynamic programming (although I didn't
- see any bibliography of further reading).
-
- --
- Caught an internal error--.brainrc restored
-