question
stringlengths
34
5.67k
answer
stringlengths
20
20.1k
support_files
listlengths
0
4
metadata
dict
Give the dfa[][] array for the Knuth-Morris-Pratt algorithm for the pattern A B R A C A D A B R A, and draw the DFA, in the style of the figures in the text.
5.1.3 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 15, 0): input 0 no 1 is 2 th 3 ti 4 fo 5 al 6 go 7 pe 8 to 9 co 10...
[]
{ "number": "5.3.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Write an efficient method that takes a string txt and an integer M as arguments and returns the position of the first occurrence of M consecutive blanks in the string, txt.length if there is no such occurrence. Estimate the number of character compares used by your method, on typical text and in the worst case.
5.1.4 Trace for 3-way string quicksort (same model as used in the book): -- -- -- 0 no is ai ai ai ai 1 is ai co al -- al 2 th co fo -- al co 3 ti fo al fo -- fo 4 fo al go ...
[]
{ "number": "5.3.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Develop a brute-force substring search implementation BruteForceRL that processes the pattern from right to left (a simplified version of ALGORITHM 5.7).
5.1.5 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 13, 0): input 0 now 1 is 2 the 3 time 4 for 5 all 6 good 7 people 8 ...
[]
{ "number": "5.3.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Give the right[] array computed by the constructor in ALGORITHM 5.7 for the pattern A B R A C A D A B R A.
5.1.6 Trace for 3-way string quicksort (same model as used in the book): ---- --- --- --- 0 now is aid aid aid aid aid 1 is aid come all --- --- all 2 the come for --- all all come 3 time f...
[]
{ "number": "5.3.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Add to KMP a count() method to count occurrences and a searchAll() method to print all occurrences.
5.1.8 Both MSD string sort and 3-way string quicksort examine all characters in the N keys. That number is equal to 1 + 2 + ... + N = (N^2 + N) / 2 characters. MSD string sort, however, generates (R - 1) * N empty subarrays (an empty subarray for all digits in R other than 'a', in every pass) while 3-way string quicks...
[]
{ "number": "5.3.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Add to RabinKarp a count() method to count occurrences and a searchAll() method to print all occurrences.
5.1.10 The total number of characters examined by 3-way string quicksort when sorting N fixed-length strings (all of length W) in the worst case is O(N * W * R). This can be seen with a recurrence relation T(W). The base case T(1) is when all the strings have length 1. An example with R = 3 is { "a", "b", "c" }. In ...
[]
{ "number": "5.3.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
In the Boyer-Moore implementation in ALGORITHM 5.7, show that you can set right[c] to the penultimate occurrence of c when c is the last character in the pattern.
5.1.13 - Hybrid sort Idea: using standard MSD string sort for large arrays, in order to get the advantage of multiway partitioning, and 3-way string quicksort for smaller arrays, in order to avoid the negative effects of large numbers of empty bins. This idea will work well for random strings because, in general, the...
[]
{ "number": "5.3.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Draw the KMP DFA for the following pattern strings. a. AAAAAAB b. AACAAAB c. ABABABAB d. ABAABAAABAAAB e. ABAABCABAABCB
5.1.17 - In-place key-indexed counting LSD and MSD sorts that use only a constant amount of extra space are not stable. Counterexample for LSD sort: The array ["4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720", "1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723"] after being so...
[]
{ "number": "5.3.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
How would you modify the Rabin-Karp algorithm to search for an H-by-V pattern in an N-by-N text?
5.1.22 - Timings Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays used in Mo...
[]
{ "number": "5.3.22", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Write a program that reads characters one at a time and reports at each instant if the current string is a palindrome. Hint : Use the Rabin-Karp hashing idea.
5.1.23 - Array accesses Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays use...
[]
{ "number": "5.3.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Exercise" }
Find all occurrences. Add a method findAll() to each of the four substring search algorithms given in the text that returns an Iterable<Integer> that allows clients to iterate through all offsets of the pattern in the text.
5.1.24 - Rightmost character accessed Running 1 experiment with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small s...
[]
{ "number": "5.3.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Creative Problem" }
Tandem repeat search. A tandem repeat of a base string b in a string s is a substring of s having at least two consecutive copies b (nonoverlapping). Develop and implement a linear-time algorithm that, given two strings b and s, returns the index of the beginning of the longest tandem repeat of b in s. For example, you...
5.5.27 - Long repeats Compressing 2 * 1000 random characters (for N = 1000), with 16000 bits (8 bits per character). % java -cp algs4.jar:. RunLengthEncoding - < 5.5.27_random.txt | java -cp algs4.jar:. BinaryDump 0 53736 bits Compression ratio: 53736 / 16000 = 335% % java -cp algs4.jar:. edu.princeton.cs.algs4.Huf...
[]
{ "number": "5.3.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Creative Problem" }
Random patterns. How many character compares are needed to do a substring search for a random pattern of length 100 in a given text? Answer: None. The method public boolean search(char[] txt) { return false; } is quite effective for this problem, since the chances of a random pattern of length 100 appearing in any text...
5.3.31 - Random patterns None. The method public boolean search(char[] text) { return false; } is quite effective for this problem, since the chances of a random pattern of length 100 appearing in any text are so low that you may consider it to be 0.
[]
{ "number": "5.3.31", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Creative Problem" }
Timings. Write a program that times the four methods for the task of searchng for the substring it is a far far better thing that i do than i have ever done in the text of Tale of Two Cities (tale.txt). Discuss the extent to which your results validate the hypthotheses about performance that are stated in the text.
5.3.39 - Timings Method | Time spent Bruteforce 0.01 Knuth-Morris-Pratt 0.01 Boyer-Moore 0.00 Rabin-Karp 0.04 The results validate the hypotheses about performance stated in the text: Both brute force and Knuth-Morris-Pratt methods took 0.01 seconds t...
[]
{ "number": "5.3.39", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.3, "section_title": "Substring Search", "type": "Experiment" }
Give a brief English description of each of the following REs: a. .* b. A.*A | A c. .*ABBABBA.* d. .* A.*A.*A.*A.*
5.1.2 Trace for LSD string sort (same model as used in the book): input d=1 d=0 output no pa ai ai is pe al al th of co co ti th fo fo fo th go go al th is is go ti no no pe ai of of to al pa pa co no pe ...
[]
{ "number": "5.4.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
What is the maximum number of different strings that can be described by a regular expression with M or operators and no closure operators (parentheses and concatenation are allowed)?
5.1.3 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 15, 0): input 0 no 1 is 2 th 3 ti 4 fo 5 al 6 go 7 pe 8 to 9 co 10...
[]
{ "number": "5.4.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
Draw the NFA corresponding to the pattern ( ( ( A | B ) * | C D * | E F G ) * ) * .
5.1.4 Trace for 3-way string quicksort (same model as used in the book): -- -- -- 0 no is ai ai ai ai 1 is ai co al -- al 2 th co fo -- al co 3 ti fo al fo -- fo 4 fo al go ...
[]
{ "number": "5.4.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
Draw the digraph of ε-transitions for the NFA from Exercise 5.4.4.
5.1.5 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 13, 0): input 0 now 1 is 2 the 3 time 4 for 5 all 6 good 7 people 8 ...
[]
{ "number": "5.4.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
Give the sets of states reachable by your NFA from EXERCISE 5.4.4 after each character match and susbsequent ε-transitions for the input A B B A C E F G E F G C A A B .
5.1.6 Trace for 3-way string quicksort (same model as used in the book): ---- --- --- --- 0 now is aid aid aid aid aid 1 is aid come all --- --- all 2 the come for --- all all come 3 time f...
[]
{ "number": "5.4.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
Write a regular expression for each of the following sets of binary strings: a. Contains at least three consecutive 1s b. Contains the substring 110 c. Contains the substring 1101100 d. Does not contain the substring 110
5.1.8 Both MSD string sort and 3-way string quicksort examine all characters in the N keys. That number is equal to 1 + 2 + ... + N = (N^2 + N) / 2 characters. MSD string sort, however, generates (R - 1) * N empty subarrays (an empty subarray for all digits in R other than 'a', in every pass) while 3-way string quicks...
[]
{ "number": "5.4.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
Write a regular expression for each of the following sets of binary strings: a. Has at least 3 characters, and the third character is 0 b. Number of 0s is a multiple of 3 c. Starts and ends with the same character d. Odd length e. Starts with 0 and has odd length, or starts with 1 and has even length f. Length is at le...
5.1.10 The total number of characters examined by 3-way string quicksort when sorting N fixed-length strings (all of length W) in the worst case is O(N * W * R). This can be seen with a recurrence relation T(W). The base case T(1) is when all the strings have length 1. An example with R = 3 is { "a", "b", "c" }. In ...
[]
{ "number": "5.4.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Exercise" }
Challenging REs. Construct an RE that describes each of the following sets of strings over the binary alphabet: a. All strings except 11 or 111 b. Strings with 1 in every odd-number bit position c. Strings with at least two 0s and at most one 1 d. Strings with no two consecutive 1s
5.1.13 - Hybrid sort Idea: using standard MSD string sort for large arrays, in order to get the advantage of multiway partitioning, and 3-way string quicksort for smaller arrays, in order to avoid the negative effects of large numbers of empty bins. This idea will work well for random strings because, in general, the...
[]
{ "number": "5.4.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Creative Problem" }
Wildcard. Add to NFA the capability to handle wildcards.
5.1.17 - In-place key-indexed counting LSD and MSD sorts that use only a constant amount of extra space are not stable. Counterexample for LSD sort: The array ["4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720", "1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723"] after being so...
[]
{ "number": "5.4.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Creative Problem" }
Proof. Develop a version of NFA that prints a proof that a given string is in the language recognized by the NFA (a sequence of state transitions that ends in the accept state).
5.1.22 - Timings Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays used in Mo...
[]
{ "number": "5.4.22", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.4, "section_title": "Regular Expressions", "type": "Creative Problem" }
Given a example of a uniquely decodable code that is not prefix-free. Answer : Any suffix-free code is uniquely decodable.
5.1.2 Trace for LSD string sort (same model as used in the book): input d=1 d=0 output no pa ai ai is pe al al th of co co ti th fo fo fo th go go al th is is go ti no no pe ai of of to al pa pa co no pe ...
[]
{ "number": "5.5.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Give an example of a uniquely decodable code that is not prefix free or suffix free. Answer : {0011, 011, 11, 1110} or {01, 10, 011, 110}
5.1.3 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 15, 0): input 0 no 1 is 2 th 3 ti 4 fo 5 al 6 go 7 pe 8 to 9 co 10...
[]
{ "number": "5.5.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Are { 01, 1001, 1011, 111, 1110 } and { 01, 1001, 1011, 111, 1110 } uniquely decodable? If not, find a string with two encodings.
5.1.4 Trace for 3-way string quicksort (same model as used in the book): -- -- -- 0 no is ai ai ai ai 1 is ai co al -- al 2 th co fo -- al co 3 ti fo al fo -- fo 4 fo al go ...
[]
{ "number": "5.5.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Use RunLength on the file q128x192.bin from the booksite. How many bits are there in the compressed file?
5.1.5 According to ASC II values, indices of chars 'a' through 'z' start at 97. But this trace indices follow the book convention of 'a' starting at 0. Trace for MSD string sort (same model as used in the book): Top level of sort(array, 0, 13, 0): input 0 now 1 is 2 the 3 time 4 for 5 all 6 good 7 people 8 ...
[]
{ "number": "5.5.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
How many bits are needed to encode N copies of the symbol a (as a function of N)? N copies of the sequence abc?
5.1.6 Trace for 3-way string quicksort (same model as used in the book): ---- --- --- --- 0 now is aid aid aid aid aid 1 is aid come all --- --- all 2 the come for --- all all come 3 time f...
[]
{ "number": "5.5.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Give the result of encoding the strings ab, abab, ababab, abababab, ... (strings consisting of N repetitions of ab) with run-length, Huffman, and LZW encoding. What is the compression ratio as a function of N?
5.1.8 Both MSD string sort and 3-way string quicksort examine all characters in the N keys. That number is equal to 1 + 2 + ... + N = (N^2 + N) / 2 characters. MSD string sort, however, generates (R - 1) * N empty subarrays (an empty subarray for all digits in R other than 'a', in every pass) while 3-way string quicks...
[]
{ "number": "5.5.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
In the style of the figure in the text, show the Huffman coding tree construction process when you use Huffman for the string "it was the age of foolishness”. How many bits does the compressed bitstream require?
5.1.10 The total number of characters examined by 3-way string quicksort when sorting N fixed-length strings (all of length W) in the worst case is O(N * W * R). This can be seen with a recurrence relation T(W). The base case T(1) is when all the strings have length 1. An example with R = 3 is { "a", "b", "c" }. In ...
[]
{ "number": "5.5.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Suppose that all of the symbol frequencies are equal. Describe the Huffman code.
5.1.13 - Hybrid sort Idea: using standard MSD string sort for large arrays, in order to get the advantage of multiway partitioning, and 3-way string quicksort for smaller arrays, in order to avoid the negative effects of large numbers of empty bins. This idea will work well for random strings because, in general, the...
[]
{ "number": "5.5.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Characterize the tricky situation in LZW coding. Solution: Whenever it encounters cScSc, where c is a symbol and S is a string, cS is in the dictionary already but cSc is not.
5.1.17 - In-place key-indexed counting LSD and MSD sorts that use only a constant amount of extra space are not stable. Counterexample for LSD sort: The array ["4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720", "1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723"] after being so...
[]
{ "number": "5.5.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Prove the following fact about Huffman codes: If the frequency of symbol i is strictly larger than the frequency of symbol j, then the length of the codeword for symbol i is less than or equal to the length of the codeword for symbol j.
5.1.22 - Timings Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays used in Mo...
[]
{ "number": "5.5.22", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
What would be the result of breaking up a Huffman-encoded string into five-bit characters and Huffman-encoding that string?
5.1.23 - Array accesses Running 10 experiments with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small subarrays use...
[]
{ "number": "5.5.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
In the style of the figures in the text, show the encoding trie and the compression and expansion processes when LZW is used for the string it was the best of times it was the worst of times
5.1.24 - Rightmost character accessed Running 1 experiment with 1000000 strings for random decimal keys (with fixed-length of 10 characters), random CA license plates, random fixed-length words (with fixed-length of 10 characters) and random variable length items (with given values 'A' and 'B'). The cutoff for small s...
[]
{ "number": "5.5.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Exercise" }
Long repeats. Estimate the compression ratio achieved by run-length, Huffman, and LZW encoding for a string of length 2N formed by concatenating two copies of a random ASCII string of length N (see EXERCISE 5.5.9), under any assumptions that you think are reasonable.
5.5.27 - Long repeats Compressing 2 * 1000 random characters (for N = 1000), with 16000 bits (8 bits per character). % java -cp algs4.jar:. RunLengthEncoding - < 5.5.27_random.txt | java -cp algs4.jar:. BinaryDump 0 53736 bits Compression ratio: 53736 / 16000 = 335% % java -cp algs4.jar:. edu.princeton.cs.algs4.Huf...
[]
{ "number": "5.5.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 5, "chapter_title": "Strings", "section": 5.5, "section_title": "Data Compression", "type": "Creative Problem" }
Molecules travel very quickly (faster than a speeding jet) but diffuse slowly because they collide with other molecules, thereby changing their direction. Extend the model to have a boundary shape where two vessels are connected by a pipe containing two different types of particles. Run a simulation and measure the fra...
Results Time 0 LEFT VESSEL: 10 particles Type 1: 10/10 Type 2: 0/10 RIGHT VESSEL: 10 particles Type 1: 0/10 Type 2: 10/10 Time 1000 LEFT VESSEL: 14 particles Type 1: 9/14 Type 2: 5/14 RIGHT VESSEL: 6 particles Type 1: 1/6 Type 2: 5/6 Time 2000 LEFT VESSEL: 11 particles Type 1: 6/11 Type 2: 5/11 RIGHT VESSEL: 9 parti...
[]
{ "number": "6.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.1, "section_title": "Collision Simulation", "type": "Exercise" }
After running a simulation, negate all velocities and then run the system backward. It should return to its original state! Measure roundoff error by measuring the difference between the final and original states of the system.
Roundoff error: 0.22176242098152166
[]
{ "number": "6.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.1, "section_title": "Collision Simulation", "type": "Exercise" }
Add a method pressure() to Particle that measures pressure by accumulating the number and magnitude of collisions against walls. The pressure of the system is the sum of these quantities. Then add a method pressure() to CollisionSystem and write a client that validates the equation pv = nRT.
Pressure measured with P V = n R T: 5.480029392202992E-4 Pressure measured with wall collisions: 4.394236724937985E-4 Pressure measured with P V = n R T: 3.855098323894351E-4 Pressure measured with wall collisions: 4.389151787833636E-4 Pressure measured with P V = n R T: 4.675754466697337E-4 Pressure measured with wa...
[]
{ "number": "6.11", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.1, "section_title": "Collision Simulation", "type": "Exercise" }
Instrument the priority queue and test Pressure at various temperatures to identify the computational bottleneck. If warranted, try switching to a different priority-queue implementation for better performance at high temperatures.
Tests done with a random baseline temperature T, 100 * T, 10000 * T, 1000000 * T and 100000000 * T. The standard priority queue was used to test all the temperatures. The index priority queue was also used to test the highest temperatures: 1000000 * T and 100000000 * T. Results: **** Standard priority queue tests ***...
[]
{ "number": "6.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.1, "section_title": "Collision Simulation", "type": "Exercise" }
Suppose that, in a three-level tree, we can afford to keep a links in internal memory, between b and 2b links in pages representing internal nodes, and between c and 2c items in pages representing external nodes. What is the maximum number of items that we can hold in such a tree, as a function of a, b, and c?
The maximum number of items that we can hold in such a tree is achieved when there are "a" links in internal memory (the links between the first and second level of the tree), 2b links in pages representing internal nodes (the highest branching possible) and 2c items in pages representing external nodes (the highest nu...
[]
{ "number": "6.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
Estimate the average number of probes per search in a B-tree for S random searches, in a typical cache system, where the T most-recently-accessed pages are kept in memory (and therefore add 0 to the probe count). Assume that S is much larger than T.
As seen on Proposition B, a search in a B-tree of order M with N items requires between log M N and log M/2 N probes. Considering that: 1- We are evaluating the example given in Proposition B, with M = 1000 and N less than 62.5 billion, where the number of probes is less than 4. This means that the B-tree has height 3...
[]
{ "number": "6.18", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
Consider the sibling split (or B*-tree) heuristic for B-trees: When it comes time to split a node because it contains M entries, we combine the node with its sibling. If the sibling has k entries with k < M - 1, we reallocate the items giving the sibling and the full node each about (M+k)/2 entries. Otherwise, we creat...
6.20 - B* trees Bounds on the number of probes used for a search or an insertion in a B*-tree of order M with N items: Between log(M) N and log(2M/3) N probes. This is because almost all internal nodes of the tree have between 2M / 3 and M - 1 links, since they are formed from a split of a full node with M keys and ca...
[]
{ "number": "6.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
Write a program to compute the average number of external pages for a B-tree of order M built from N random insertions into an initially empty tree. Run your program for reasonable values of M and N.
Results: Order M | Number of items | AVG Number of External Pages 4 100000 42865 4 1000000 428592 4 10000000 4276006 16 100000 9424 16 ...
[]
{ "number": "6.21", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
If your system supports virtual memory, design and conduct experiments to compare the performance of B-trees with that of binary search, for random searches in a huge symbol table.
Results: Number of searches | B-tree time | Binary search time 1000 0.005 0.001 100000 0.082 0.013 10000000 6.352 0.910 For random searches in a huge symbol table (with 10,000,000 entries) binary search ha...
[]
{ "number": "6.22", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
For your internal-memory implementation of Page in EXERCISE 6.15, run experiments to determine the value of M that leads to the fastest search times for a B-tree implementation supporting random search operations in a huge symbol table. Restrict your attention to values of M that are multiples of 100.
Results: Order M | Number of searches | Total time 100 1000 0.005 100 100000 0.198 100 10000000 19.973 200 1000 0.002 200 100000 0.195 200 10000000 18...
[]
{ "number": "6.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
Run experiments to compare search times for internal B-trees (using the value of M determined in the previous exercise), linear probing hashing, and red-black trees for random search operations in a huge symbol table.
Results: Number of searches | B-tree time | Linear probing hashing time | Red-Black tree time 1000 0.004 0.001 0.002 100000 0.173 0.019 0.114 10000000 16.659 ...
[]
{ "number": "6.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.2, "section_title": "B-Trees", "type": "Exercise" }
Give, in the style of the figure on page 882, the suffixes, sorted suffixes, index() and lcp() tables for the following strings: a. abacadaba b. mississippi c. abcdefghij d. aaaaaaaaaa
a. abacadaba suffixes sorted suffix array i index(i) lcp(i) 0 abacadaba 0 8 0 a 1 bacadaba 1 6 1 aba 2 acadaba 2 0 3 abacadaba 3 cadaba 3 2 1 acadaba 4 adaba 4 4 1 adaba 5 daba 5 ...
[]
{ "number": "6.25", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.3, "section_title": "Suffix Arrays", "type": "Exercise" }
Identify the problem with the following code fragment to compute all the suffixes for suffix sort: suffix = ""; for (int i = s.length() - 1; i >= 0; i--) { suffix = s.charAt(i) + suffix; suffixes[i] = suffix; }
The problem with the code fragment is that if the substring() method takes linear time and space (which is the case in Java 7) the loop will take quadratic time and space. This wouldn't be a problem in Java 6, where the substring() method takes constant time and space (which would lead to a linear time and space loop).
[]
{ "number": "6.26", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.3, "section_title": "Suffix Arrays", "type": "Exercise" }
Under the assumptions described in SECTION 1.4. give the memory usage of a SuffixArray object with a string of length N.
Suffix * object overhead -> 16 bytes * String reference (text) -> 8 bytes * int value (index) -> 4 bytes * padding -> 4 bytes Amount of memory needed: 16 + 8 + 4 + 4 = 32 bytes SuffixArray * object overhead -> 16 bytes * Suffix[] (suffixes) object overhead -> 16 bytes int value (length) -> 4 bytes int value (...
[]
{ "number": "6.29", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.3, "section_title": "Suffix Arrays", "type": "Exercise" }
Write a SuffixArray client LCS that take two file-names as command-line arguments, reads the two text files, and finds the longest substring that appears in both in linear time. (In 1970, D. Knuth conjectured that this task was impossible.) Hint: Create a suffix array for s#t where s and t are the two text strings and ...
Suffix array with Suffix class: Running time: O(N^2), but on average runs in 2N ln N (as seen in the book, on chapter 6, proposition C). The worst case happens when sorting the suffixes of a string consisting of N copies of the same character, or already sorted suffixes. Memory: 40N + 40 bytes (as seen on exercise 6.29...
[]
{ "number": "6.30", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.3, "section_title": "Suffix Arrays", "type": "Exercise" }
If capacities are positive integers less than M, what is the maximum possible flow value for any st-network with V vertices and E edges? Give two answers, depending on whether or not parallel edges are allowed.
Maximum possible flow value for any st-network with V vertices and E edges (with positive integer capacities less than M): Consider C to be the highest possible edge capacity. 1- Parallel edges not allowed Max flow = (V - 2) * C In this case, since parallel edges are not allowed, each vertex other than the source and ...
[]
{ "number": "6.36", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.4, "section_title": "Maxflow", "type": "Exercise" }
Give an algorithm to solve the maxflow problem for the case that the network forms a tree if the sink is removed.
If the network forms a tree if the sink is removed this means that there are no directed cycles in the network. In other words, intermediate vertices are not directly connected: for each edge e (v->w), either v or w must be the source or the sink vertex. A situation like this would happen: Original network: s...
[]
{ "number": "6.37", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.4, "section_title": "Maxflow", "type": "Exercise" }
If true provide a short proof, if false give a counterexample: a. In any max flow, there is no directed cycle on which every edge carries positive flow b. There exists a max flow for which there is no directed cycle on which every edge carries positive flow c. If all edge capacities are distinct, the max flow is unique...
6.38 - True of false a. In any max flow, there is no directed cycle on which every edge carries positive flow. False. Counterexample: s | ^ 2/2| |1/1 v | 1 | |1/1 v t b. There exists a max flow for which there is no directed cycle on which every edge carries positive flow. True. Proof: ...
[]
{ "number": "6.38", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.4, "section_title": "Maxflow", "type": "Exercise" }
Complete the proof of PROPOSITION G: Show that each time an edge is a critical edge, the length of the augmenting path through it must increase by 2.
Proposition: Each time an edge is a critical edge, the length of the augmenting path through it must increase by 2. Proof: Let df(vertex1, vertex2) be the augmenting path distance from vertex 1 to vertex 2. When edge (u, v) is critical this means that: df(s, v) = df(s, u) + 1 (since augmenting paths are shortest paths...
[]
{ "number": "6.39", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.4, "section_title": "Maxflow", "type": "Exercise" }
Prove that the shortest-paths problem reduces to linear programming.
Reduction from shortest paths problem to linear programming: Method 1: We consider a system of inequalities and equations that involve the following variables: l(u,v) -> variable corresponding to the length of the directed edge u -> v. x(u,v) -> indicator variable for whether edge u -> v is in the shortest path. It ha...
[]
{ "number": "6.50", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Could there be an algorithm that solves an NP-complete problem in an average time of NlogN, if P != NP? Explain your answer.
Could there be an algorithm that solves an NP-complete problem in an average time of N^(log n), if P != NP? Explain your answer. Yes, there could be, it is not possible to affirm either yes or no. An algorithm of average time N^(log n) does not belong to either P or NP, it is part of an intermediate class of algorithm...
[]
{ "number": "6.51", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that someone discovers an algorithm that is guaranteed to solve the boolean satisfiability problem in time proportional to 1.1^N. Does this imply that we can solve other NP-complete problems in time proportional to 1.1^N?
Yes, if the algorithm can guarantee to solve the boolean satisfiability problem in time proportional to 1.1^N this implies that it can solve other NP-complete problems in time proportional to 1.1^N. This is because boolean satisfiability is an NP-complete problem and due to that all problems in NP polynomially reduce t...
[]
{ "number": "6.52", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
What would be the significance of a program that could solve the integer linear programming problem in time proportional to 1.1^N?
A program that could solve the integer linear programming problem in time proportional to 1.1^N would signify that it can also solve any other problem in NP in time proportional to 1.1^N (this is because the integer linear programming problem is an NP-complete problem and due to that all problems in NP polynomially red...
[]
{ "number": "6.53", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Give a poly-time reduction from vertex cover to 0-1 integer linear inequality satisfiability.
Poly-time reduction from vertex cover to 0-1 integer linear inequality satisfiability: We consider a system of inequalities and equations that involve the following variables: x(v) -> binary variable that is equal to 1 if vertex v is selected and 0 otherwise. Given a graph G with a set of vertices V and a set of edge...
[]
{ "number": "6.54", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Prove that the problem of finding a Hamiltonian path in a directed graph is NP-complete, using the NP-completeness of the Hamiltonian-path problem for undirected graphs.
Prove that the problem of finding a Hamiltonian path in a directed graph is NP-complete, using the NP-completeness of the Hamiltonian path problem for undirected graphs. Let's call the Hamiltonian path problem in undirected graphs HPg and the Hamiltonian path problem in directed graphs HPdg. Reduction from HPg to HPd...
[]
{ "number": "6.55", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that two problems are known to be NP-complete. Does this imply that there is a poly-time reduction from one to the other?
Yes, if two problems are known to be NP-complete this implies that there is a poly-time reduction from one to the other. This is because all problems in NP poly-time reduce to any NP-complete problem. All NP-complete problems are in NP, therefore, both problems poly-time reduce from one to the other.
[]
{ "number": "6.56", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that X is NP-complete, X poly-time reduces to Y, and Y poly-time reduces to X. Is Y necessarily NP-complete?
Suppose that X is NP-complete, X poly-time reduces to Y, and Y poly-time reduces to X. Is Y necessarily NP-complete? For Y to be NP-complete the following conditions must be met: 1) Y is in NP 2) Every problem in NP poly-time reduces to Y Condition 2 is true because X poly-time reduces to Y, and since X is NP-complet...
[]
{ "number": "6.57", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that we have an algorithm to solve the decision version of boolean satisfiability, which indicates that there exists an assignment of truth values to the variables that satisfies the boolean expression. Show how to find the assignment.
Given an algorithm that solves the decision version of boolean satisfiability - which indicates whether there exists an assignment of truth values to the variables that satisfies a boolean expression - the assignment can be found with the following method: Consider a boolean expression BE composed by a set of distinct...
[]
{ "number": "6.58", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that we have an algorithm to solve the decision version of the vertex cover problem, which indicates that there exists a vertex cover of a given size. Show how to solve the optimization version of finding the vertex cover of minimum cardinality.
Given an algorithm that solves the decision version of the vertex cover problem - which indicates that there exists a vertex cover of a given size - the optimization version of finding the vertex cover of minimum cardinality can be solved with the following method: Consider a graph G with V vertices and E edges. 1- U...
[]
{ "number": "6.59", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Explain why the optimization version of the vertex cover problem is not necessarily a search problem.
The optimization version of the vertex cover problem is not necessarily a search problem because its output (a vertex cover of minimum cardinality) cannot be certified to be correct in polynomial time. It is possible to certify in polynomial time that the output is a vertex cover, but not that it is a minimum cardinali...
[]
{ "number": "6.60", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that X and Y are two search problems and that X poly-time reduces to Y. Which of the following can we infer? a. If Y is NP-complete then so is X. b. If X is NP-complete then so is Y. c. If X is in P, then Y is in P. d. If Y is in P, then X is in P.
If X and Y are two search problems and X poly-time reduces to Y, we can infer that: b. If X is NP-complete then so is Y. This is because if both X and Y are search problems they are both in NP. If X is NP-complete, then all problems in NP poly-time reduce to it. And if X poly-time reduces to Y, then all problems in NP...
[]
{ "number": "6.61", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }
Suppose that P != NP. Which of the following can we infer? e. If X is NP-complete, then X cannot be solved in polynomial time. f. If X is in NP, then X cannot be solved in polynomial time. g. If X is in NP but not NP-complete, then X can be solved in polynomial time. h. If X is in P, then X is not NP-complete.
If P != NP, we can infer that: a. If X is NP-complete, then X cannot be solved in polynomial time. If P != NP, no NP-complete problem can be solved in polynomial time. d. If X is in P, then X is not NP-complete. If P != NP, then problems in P can be solved in polynomial time whereas problems NP-complete cannot. So X ...
[]
{ "number": "6.62", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 6, "chapter_title": "Context", "section": 6.5, "section_title": "Reductions and Intractability", "type": "Exercise" }