【正文】
er bound is O(2n) for n0 c7100 and c = c6 + lower bound is Ω(2n) for n0 c7100 and c = c6. (100 is used for convenience to insure that 2n n6) (a) f(n)=Θ(g(n)) since log n2 = 2 log n. (b) f(n) is in Ω(g(n)) since nc grows faster than log nc for any c. (c)。 in position 1 with probability , and so on. The number of positions we must examine is 1 in the case where the leftmost “1” is in position 0。 cout endl。 jn。 iipow(2, n)。 swap(array, currpos, i)。 i++) { swap(array, currpos, i)。 } for (int i=currpos。 // Move n1 back } } At each step of the way, the reduction toward the base case is only half as far as the previous time. In theory, this series approaches, but never reaches, 0, so it will go on forever. In practice, the value should bee putationally indistinguishable from zero, and terminate. However, this is terrible programming practice. Chap. 2 Mathematical Preliminaries void allpermute(int array[], int n, int currpos) { if (currpos == (n1)} { printout(array)。 move(t2, goal)。 else { if (curr[n] == t1) swap(t1, t2)。 rpermute(array, n1)。 return fact。 i= n。 (n = 12), Input out of range)。 Assert((n = 0) amp。 // Return the number of elements with a given valueint countInBag(int)。 void sizeof()。 void insert(int)。 bool isInSet(int)。 void sizeof()。 void insert(int)。 else return ()。 ()。 i++) if (S[i] == ’(’) (i)。 for (int i=0。 } b) int checkstring(String Str) { Stack S。 } } if (count == 0) return TRUE。 if (S[i] == ’)’) { if (count == 0) return FALSE。 ilength(S)。 One implementation for the sparse matrix is described in Section Another implementation is a hash table whose search key is a concatenation of the matrix coordinates. Every problem certainly does not have an algorithm. As discussed in Chapter 15, there are a number of reasons why this might be the case. Some problems don’t have a sufficiently clear definition. Some problems, such as the halting problem, are nonputable. For some problems, such as one typically studied by artificial intelligence researchers, we simply don’t know a solution. We must assume that by “algorithm” we mean something posed of steps are of a nature that they can be performed by a puter. If so, than any algorithm can be expressed in C++. In particular, if an algorithm can be expressed in any other puter programming language, then it can be expressed in C++, since all (sufficiently general) puter programming languages pute the same set of functions. The primitive operations are (1) adding new words to the dictionary and (2) searching the dictionary for a given word. Typically, dictionary access involves some sort of preprocessing of the word to arrive at the “root” of the word. A twenty page document (single spaced) is likely to contain about 20,000 words. A user may be willing to wait a few seconds between individual “hits” of misspelled words, or perhaps up to a minute for the whole document to be processed. This means that a check for an individual word can take about 1020 ms. Users will typically insert individual words into the dictionary interactively, so this process can take a couple of seconds. Thus, search must be much more efficient than insertion. The user should be able to find a city based on a variety of attributes (name, location, perhaps characteristics such as population size). The user should also be able to insert and delete cities. These are the fundamental operations of any database system: search, insertion and deletion. A reasonable database has a time constraint that will satisfy the patience of a typical user. For an insert, delete, or exact match query, a few seconds is satisfactory. If the database is meant to support range queries and mass deletions, the entire operation may be allowed to take longer, perhaps on the order of a minute. However, the time spent to process individual cities within the range must be appropriately reduced. In practice, the data representation will need to be such that it acmodates efficient processing to meet these time constraints. In particular, it may be necessary to support operations that process range queries efficiently by processing all cities in the range as a batch, rather than as a series of operations on individual cities. Students at this level are likely already familiar with binary search. Thus, they should typically respond with sequential search and binary search. Binary search should be described as better since it typically needs to make fewer parisons (and thus is likely to be much faster). The answer to this question is discussed in Chapter 8. Typical measures of cost will be number of parisons and number of swaps. Tests should include running timings on sorted, reverse sorted, and random lists of various sizes. Chap. 1 Data Structures and Algorithms The first part is easy with the hint, but the second part is rather difficult to do without a stack. a) bool checkstring(string S) { int count = 0。 int getvalue(Matrix M1, int row, int col)。 Matrix transpose(Matrix M1)。 // and 1 for s1s2. int strcmp(String s1, String s2) // Copy a string int strcpy(String source, String destination) The answer to this question is provided by the ADT for lists given in Chapter 4. One’s pliment stores the binary representation of positive numbers, and stores the binary representation of a negative number with the bits inverted. Two’s pliment is the same, except that a negative number has its bits inverted and then one is added (for reasons of efficiency in hardware implementation). This representation is the physical implementation of an ADT defined by the normal arithmetic operations, declarations, and other support given by the programming language for integers.