Ds Study Qs
Ds Study Qs
Ds Study Qs
David Keil
9/08
Note: For some questions, answers are provided. These answers need to be checked to see that they match the questions, number for number. Some other checking and editing is needed, including deletion of questions that are trivial or based on an expectation of too-detailed knowledge. The objective of this set of questions is to help students focus on the main facts, terms, and ideas that underly the course material.
Study questions
T2 T3 T4 T5 T6 T7 T8 T9
David Keil
9/08
3. Algorithm verification
Multiple-choice, T/F
1. A program's correctness (a) may be proven through testing; (b) may be proven mathematically; (c) may be proven through a combination of testing and persuasive language; (d) can never be proven; (e) none of the above 2. An assert statement is used to (a) perform a search; (b) help detect syntax errors; (c) help detect logic errors; (d) help detect user-input errors; (e) document program code 3. In a correct algorithm, a loop invariant is true (a) always; (b) never; (c) at the start of a loop body; (d) throughout execution of loop body; (e) sometimes 4. (T-F) It is easy to prove correctness of a program that works. 5. In an inductive proof, showing that P(0) is true is (a) the base step; (b) the inductive step; (c) unnecessary; (d) sufficient to prove P(x) implies P(x + 1); (e) sufficient to prove P(x) for all x 6. To prove that an algorithm terminates, we usually rely on (a) recursion; (b) break statements; (c) convergence; (d) total correctness; (e) partial correctness 7. Total correctness is proven by showing (a) partial correctness; (b) termination; (c) good test results; (d) partial correctness and termination; (e) termination and good test results 8. An inductive proof argues in part that (a) if a certain assertion is true for n then it is also true for (n+1); (b) a certain assertion is not true for any value n; (c) a certain assertion leads to a contradiction; (d) if one assertion is not true, then a second assertion must be true 9. A valid postcondition is (a) output; (b) input; (c) true throughout the execution of a loop; (d) true after a series of statements execute; (e) true under certain conditions dependent on input.
David Keil
9/08
4. The swap method (a) returns a value; (b) uses value parameters; (c) uses reference parameters; (d) performs multiple swapping operations. 5. Which algorithm is most similar to the way many people would sort a hand of cards? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) Shell sort; (e) Quicksort 6. Which algorithm finds adjacent elements out of order and swaps them, until none are found out of order? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) insertion sort; (e) Quicksort 7. In Quicksort, the pivot is (a) a subscript; (b) an element value; (c) a turning point in the execution of the algorithm; (d) used to leverage performance; (e) none of these 8. A merge algorithm (a) must be recursive; (b) performs a search; (c) requires two or more sorted arrays; (d) all of these; (e) none of these 9. (T-F) The binary search is a way to search any array 10. (T-F) To sort an array normally requires swapping data items that are in order. 11. (T-F) To sort an array normally requires swapping data items that are out of order. 12. The swap method (a) returns a value; (b) uses value parameters; (c) uses reference parameters; (d) performs multiple swapping operations. 13. Which algorithm is most similar to the way many people would sort a hand of cards? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) Shell sort; (e) Quicksort 14. Which algorithm finds adjacent elements out of order and swaps them, until none are found out of order? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) insertion sort; (e) Quicksort 15. In Quicksort, the pivot is (a) a subscript; (b) an element value; (c) a turning point in the execution of the algorithm; (d) used to leverage performance; (e) none of these 16. A merge algorithm (a) must be recursive; (b) performs a search; (c) requires two or more sorted arrays; (d) all of these; (e) none of these Short-answer
5. The analysis of algorithms is most concerned with (a) testing solutions; (b) the documentation of programs; (c) object orientation; (d) the time complexity of programs; (e) all of these 6. For a problem of size n, a solution of the worst time complexity would be (a) 1 (constant time); (b) n (linear); (c) n squared (quadratic); (d) 2 to the n power (exponential); (e) none is worst 7. Each step of the binary-search algorithm (a) reduces the size of the sub-array to be searched by about half; (b) finds the search key; (c) reports failure; (d) moves one array element; (e) compares two array elements 8. The time complexity of the binary search is on the order of (a) 1; (b) n; (c) log2(n); (d) n squared; (e) 2 to the nth power 9. (T-F) The binary search is an efficient way to search any large array 10. (T-F) A linear search must inspect, on average, about half the elements in an array. 11. (T-F) To sort an array normally requires swapping data items that are in order. 12. (T-F) To sort an array normally requires swapping data items that are out of order. 13. log2(100) is closest to (a) 1; (b) 2; (c) 6; (d) 20; (e) 100 14. log2(1000) is closest to (a) 1; (b) 2; (c) 5; (d) 10; (e) 100 15. (T-F) A binary search should be faster than a linear one. 16. The swap method (a) returns a value; (b) uses value parameters; (c) uses reference parameters; (d) performs multiple swapping operations. 17. Which algorithm is most similar to the way many people would sort a hand of cards? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) Shell sort; (e) Quicksort 18. Which algorithm finds adjacent elements out of order and swaps them, until none are found out of order? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) insertion sort; (e) Quicksort 19. Which sort has an execution time proportional to n log2(n)? (a) insertion sort; (b) bubble sort; (c) selection sort; (d) insertion sort; (e) Quicksort 20. In Quicksort, the pivot is (a) a subscript; (b) an element 5. Algorithm complexity value; (c) a turning point in the execution of the algorithm; Multiple-choice, T/F (d) used to leverage performance; (e) none of these 1. (T-F) The Bubble sort algorithm makes on the order of n 21. Where f is a function, O(f (n)) means _______ f (n). passes through an n-element array. (a) exactly; (b) at most; (c) roughly proportional to; 2. What is the fastest sure way to search for a value in an (d) at least; (e) following a specification unsorted array of numbers? (a) calculate hash value; 22. A merge algorithm (a) takes longer than Quicksort; (b) scan from beginning to end until found; (c) sort and (b) performs a search; (c) requires two or more sorted perform binary search; (d) choose random elements until the arrays; (d) all of these; (e) none of these number is found; (e) no fastest way exists. 23. log2(10) is closest to (a) 1; (b) 3; (c) 6; (d) 20; (e) 100 3. To efficiently locate the number 9 in an array of random 24. log2(1000) is closest to (a) 3; (b) 10; (c) 50; (d) 100; integers, we would use a (a) linear search; (b) binary search; (e) 1000 (c) Bubble sort; (d) Quick sort; (e) none of these 25. A linked list is traversed in time (a) O(logn); (b) O(1); 4. To efficiently locate the number 9 in a sorted array of (c) O(n); (d) O(nlogn); (e) O(n2) random integers, we would use a (a) linear search; 26. Big-O notation sets (a) a precise time complexity; (b) a (b) binary search; (c) Bubble sort; (d) Quick sort; (e) none lower bound; (c) an upper bound; (d) an upper and lower of these bound; (e) an indefinite estimate
David Keil
9/08
27. Theta notation sets (a) a precise time complexity; (b) a lower bound; (c) an upper bound; (d) an upper and lower bound; (e) an indefinite estimate 28. Big-Omega notation sets (a) a precise time complexity; (b) a lower bound; (c) an upper bound; (d) an upper and lower bound; (e) an indefinite estimate 29. (T-F) O(log n) problems are considered intractable. 30. (T-F) O(2n) problems are intractable. 31. (T-F) An algorithm with an O(log n) running time is considered fast. 32. (T-F) You might get a bonus for finding an algorithm with an O(2n) running time. 33. Asymptotic analysis relates most closely to the (a) growth of processor speeds; (b) decline in unprocessed data; (c) rate of growth of functions; (d) complexity of source files; (e) constant factors that affect speed 34. (T-F) Algorithm analysis provides us a single formula by which to determine the best data structure to use
David Keil
9/08
Short-answer ST 1
1. What is data abstraction? 2. What is the process of defining new data types called? 3. What is a technical term for a structure or object that contains an array of structures or objects? 4. Name one way to implement a collection in Java.
ST2
1. In Big-O notation, what is the complexity of the binary search? 2. In Big-O notation, what is the complexity of the Bubble sort? 3. In Big-O notation, what is the complexity of the selection sort? 4. In Big-O notation, what is the complexity of the Quicksort? 5. In Big-O notation, what is the complexity of the standard solution to the Towers of Hanoi problem? 6. Give complexities of the following operations. a) Insert a node at the beginning of a linked list. b) Insert a node at the end of a linked list. c) Insert a node in order of value. 7. For a linked list of 100 nodes, what is: a) the worst-case search time, in node accesses? b) The average search time? c) The best-case time? 8. What is the running time of linked-list prepend? 9. What is the running time of linked-list insert, given the address of the node after which the new node is to be inserted? 10. What is the running time of the operation appending a node to a singly-linked list? Doubly linked? 11. What is the running time of the linked-list operation of inserting a node at the end, given the address of the first node? 12. What is the running time of linked-list operation of deleting all nodes? 13. What is the running time to search a linked list of n items organized in ascending order? 14. True/false: (a) O(n2) = O(2n2) (b) O(n2) = O(100n) (c) O(50n) = O(n / 2) (d) O(n) = O(lg n) (e) O(lg n) = O(lg n + 1000) 15. Name a sorting algorithm that is of O(n2) time complexity 16. Name a sorting algorithm that is O(n lg n) average-case 17. In Big-O notation, what are the worst and average-case complexities of (a) the binary search? (b) the Bubble sort? (c) Quicksort? (d) the solution to the Towers of Hanoi problem?
18. If the Bubble sort were improved so that it would be twice as fast, would its complexity category necessarily change, in Big-O notation? Why or why not? 19. Consider this sorting algorithm: for i n to 1 for j 1 to n if A[i] > A[j] swap A[i] with A[j] (a) Is this slower or faster than the selection sort algorithm presented on the slide? (b) How much slower or faster? (c) Discuss its running time in Big-O terms. 20. Find the solution, in big-O notation, for T(n) = (a) 1 if n = 0 2 + T(n1) otherwise (b) 2 if n = 1 2 + T(n1) otherwise (c) 1 if n 1 n + T(n1) + 3 otherwise (d) 1 if n 2 2 + T((n1) / 2) otherwise (e) 1 if n = 0 n + ((n1) / 2) otherwise 21. Simplify: (a) O(3n2 + 2n + 100) (b) O(n / 2 + 2 lg n) (c) O((n + 2)(n 2)) (d) O(60 lg n + 500) 22. What is the best performance obtainable from an algorithm that uses comparisons in solving the problem of sorting an n-element array? 23. Name two sorting algorithms whose time complexity in BigO notation is not the same.
Short-answer
1. What loop invariant can be used to prove that the algorithm below returns true exactly when parameter test is smaller than any element of array A?
Is-lower (A, test). For k 1 to Size(A) if A[k] < test return false return true
2. The fact that a value converges during program execution is evidence of the __________ of an algorithm 3. What kind of comment inside a while statement would help establish that the statement does its job? 4. What two kinds of comment would help establish that a certain sequence of Java statements bring the program from a certain state of affairs to the desired one? 5. Loop invariants, preconditions, and postconditions help document the ____________ of an algorithm. 6. What are the components of a proof of correctness of an algorithm? 7. What special algorithmic technique is used in the solution to the Towers of Hanoi problem? How many times in the pseudocode?
David Keil
9/08
ST 3
1. Fill in the appropriate term for each definition below. (a) A true/false proposition that should be true at a certain point in an algorithms execution, if the algorithm is correct. (b) An assertion that should be true before a certain sequence of steps in an algorithm (c) An assertion that is true at the start of each iteration of a loop (d) An assertion that should be true at the end of a certain sequence of steps (e) A verification method that can at best show an algorithms incorrectness (f) A verification method based on formal techniques used in mathematics (g) A mathematical technique used to prove a general assertion by showing that it is true for one case and that, if it is true for some case, then it is true for the next case after that
Longer-answer questions
1. Write a paragraph or two discussing the problem of searching and sorting arrays. Include considerations of time complexity and use big-O notation to compare algorithms. 2. Write pseudocode for the sorting algorithm of your choice and discuss its complexity. 3. Describe the best, expected, and worst case complexities of various algorithms, using big-O notation. Why is it useful to express an algorithms running time as a function rather than as a number? 4. Write pseudocode or Java code for a method that will efficiently merge its two parameters, sorted arrays A and B of sizes da and db, into the third parameter array, C, leaving C sorted in ascending order. Discuss its complexity. 5. Write pseudocode to insert a value into a linked list of integers that is in ascending order. What is the complexity of the algorithm? 6. Compare the expected and worst-case running times for searching unsorted arrays, sorted arrays, and linked lists. 7. Write pseudocode or Java code for a method that accepts an integer and a sorted integer array as parameters, and that inserts the single integer into the array, in ascending order. What is its complexity? 8. Write an algorithm that will merge two sorted arrays, A and B, of sizes da and db, into the third sorted array, C. Discuss its complexity and argue for its correctness. 9. Write pseudocode for a search of a linked list for a given value. State the pre- and postconditions of the algorithm and write comments that argue persuasively for the correctness of the algorithm. What is the complexity of your algorithm? 10. Write pseudocode or Java code for a method that accepts an array, sorted ascending, as a parameter and removes all duplicates, packing the array to fill spaces left by deletions.
11. Write pseudocode or Java code for a method that accepts an array, not necessarily sorted, as a parameter, and removes all duplicates, packing the array to fill spaces left by deletions. 12. Write an algorithm in pseudocode to determine whether a singly-linked list of integers stores any duplicate values. In Big-O notation, what is the worst-case running time? 13. Write a method that accepts an array of integers as a parameter and returns the length of the longest series of consecutive array elements that have the value 2. What is its complexity? 14. Write a method that accepts an array of integers as a parameter and returns the length of the longest series of consecutive elements that have the same value. What is its complexity? 15. Write a method that takes two C-style strings, s1 and s2, as parameters and returns the location of the first match between s2 and a substring of s1; that is, for find(concat, cat), the return value would be 3. What is the complexity of the method? 16. Modify the partition step in Quicksort to select as a pivot the median (middle) value of five elements of the array taken at random. Compare sort times for some large set of test data, such as the words in the Web files used in your project. Discuss the results. 17. Design or code an implementation of a linked list of floats. Include necessary class declarations and method definitions to implement insertion of one value, and display of all values. 18. Write pseudocode for a search of a linked list for a given value. State the pre- and postconditions of the algorithm and write comments that argue persuasively for the correctness of the algorithm. 19. Write a method that accepts as parameters a sorted array of integers, its occupancy (current size), and a value to be inserted. The method should insert the value at its appropriate location. Write preconditions, postconditions, and loop invariants to argue that the method is correct. 20. Write pseudocode or Java code for an algorithm that finds the sum of the elements of an array of integers, given the array and a value denoting its size. Use preconditions, postconditions, and loop invariants to argue that it is correct. Express its time complexity in big-O notation. 21. Write an algorithm in pseudocode, or code it in Java, to display all the integers from 100 down to 1. Use a loop invariant to show that the algorithm is correct. 22. Write an algorithm in pseudocode, or code it in Java, to input a series of real numbers and find the smallest value among them. Use a loop invariant to show that the algorithm is correct. 23. Using pseudocode, C, or Java, write an algorithm that accepts an array of integers and returns the value os the largest and smallest elements. Show its correctness using preconditions, postconditions, and loop invariants 24. Write an algorithm that tells whether a linked list of integers contains two or more nodes that store a given value. Argue for its correctness and discuss its complexity
David Keil
9/08
25. Write an algorithm that takes an array of integers as a parameter and returns the sum of these integers. Argue for correctness and discuss complexity
26. Write a recursive algorithm (in C, Java, or pseudocode) that accepts a number, n, and finds the sum of all the numbers from 1 to n.
David Keil
9/08
4. a. The base step is concerned with showing one fact about one item of data. 5. c. If a value steadily progresses toward a certain final value, i.e., converges on it, we know that the loop that contains it terminates. 6. d. Partial correctness means provably correct output for all valid input. Good test results are encouraging but cannot contribute to proof of correctness. 7. a. An inductive proof has a base step, which establishes a fact for a base value n, and an inductive step, which establishes that P(n) leads to P(n + 1), for a certain proposition P and all values n. 8. d. A postcondition should assert that a state of affairs exists indicating that an algorithm was successful.
Short-answer
1. 2. 3. 4. The creation of new data types data abstraction collection Define a class that has an array of structures as a member.
6. 7.
8.
Short-answer
1. 2. 3. 4. 5. termination a loop invariant precondition, postcondition correctness (a) termination and (b) partial correctness, i.e., correct output for each valid input 6. Recursion. Twice. 7. Recursion 8. (a) assertion (b) precondition (c) Loop invariant (d) postcondition (e) testing (f) proof (g) induction
3. Algorithm verification
1. c. The assertion will trigger an error message if a condition is false that the programmer believes always to be true during program execution. 2. c. The loop invariants validity at the start of the loop body helps establish that a sequence of statements containing the loop accomplish their task. 3. f. Mathematical proof of correctness is difficult and is usually practical only for certain critical parts of a software project.
David Keil
9/08
5. Algorithm complexity
Multiple-choice, T/F
1. f . On the order of one pass may be necessary to put each element in place. 2. b. The linear search cannot be beaten for unordered arrays. Sorting, then searching with the binary search will take longer than scanning, by any sort algorithm. 3. a. A search is necessary to locate a value. The binary search is available only for sorted arrays. 4. b. The binary search efficiently searches an array sorted ascending or descending. 5. d. Algorithm analysis most often estimates the time of execution as a function of size of input. 6. d. The case where time is greatest is the worst (slowest) solution. 7. a. Each step inspects the middle element of a sub-array and eliminates either all elements to its left or all those to its right. 8. c. If n is 10, the number of steps is about 4; if 1000, about 10; if 1,000,000, about 20. 9. f. A binary search cannot be used on an unsorted array. 10. t. The best case is one access, the worst is the whole array. The average will be half the array. 11. f. Those that are out of order are normally swapped. 12. t. Those that are our of order are normally swapped. 13. c. Since 21 = 2, 22 = 4, 26 = 64, 210 = 1024, the closest value is 6. A logarithm is an exponent. 14. d. Since 21 = 2, 22 = 4, 25 = 32, 210 = 1024, the closest value is 10. A logarithm is an exponent. 15. t. The linear search must inspect every array element; the binary search homes in on its target more quickly. 16. c. The values of the reference parameters are swapped and returned to the calling method. 17. a. The algorithm proceeds by placing successive elements in the unsorted part of the array into their ordered location in the sorted part. 18. b. Thus high-valued elements trickle toward the end of the array. 19. e. Quicksorts running time is O(n log n) 20. b. The pivot value is used in the partition step to arrange all values less than the pivot to its left and all values greater to its right. 21. c. Big-O is a set of functions, all within a constant factor of each other, so an approximation is involved. 22. c. The merge repeatedly takes the lower of two values at the front of sorted input arrays and appends it to an output array. 23. b. Since 23 = 8, the value closest to the base-2 log of 10 is 3. 24. b. Since 210 = 1024, the value closest to the base-2 log of 1000 is 10. 25. c. Each of n nodes may have to be visited. 26. c. Big-O notation estimates the highest (worst-case) running time
27. d. Theta notation a tight bound on running time 28. b. Big-Omega notation estimates best-case running time 29. f. O(log n) problems have logn-time solutions, which are quite efficient. 30. t. O(2n) problems take worst-case time that is exponential in the size of the data set. 31. t. An algorithm with log n running time is quite efficient. 32. f. An algorithm with O(2n) running time can be extremely time consuming. 33. c. Asymptotic analysis relates to the rate of growth of functions. 34. f. Algorithm analysis provides us with tools to help determine which data structure to use for particular applications.
Short-answer
1. 2. 3. 4. 5. 6. Binary search is O(logn) Bubble sort is O(n2) Selection sort is O(n2) Quicksort is O(nlogn) Hanoi is O(2n) a) O(1) b) O(n) c) O(n) a) 100 b) 50.5 c) 1 The running time of linked-list prepend is O(1) The running time of linked-list insert is O(1). The running time of the operation appending a node to a linked-list is O(n). The running time of the linked-list operation of inserting a node at the end, given the address of the first node is O(n). The linked-list operation of deleting all nodes is O(n). Linear search applies to linked lists regardless of ordering: O(n) (a) T (b) F (c) T (d) F (e) T bubble sort, selection sort, insertion sort are all O(n2) Quicksort (a) O(log n), O(log n) (b) O(n2), O(n2) (c) O(n2), O(n lg n) (d) O(2n), O(2n) No -- still O(n2) because nested loop of up to n iterations are still used (a) slower (b) half as fast (c) O(n2) -- takes precisely n2 steps. Wastes much time. (a) O(n) (b) O(n) (c) O(n2) (d) O(lg n) (e) O(n lg n) (a) O(n2) (b) O(n) (c) O(n2) (d) O(lg n) O(n log n) (Selection, insertion, bubble) vs. Quick
7.
8. 9. 10. 11.
18. 19.
David Keil
9/08
10
Longer-answer questions
1. Design or code an implementation of a linked list of floats. Include necessary class declarations and method definitions to implement insertion of one value, and display of all values. 2. Write a series of statements that divide input value a by value b without the division operator and, by use of preconditions, postconditions, and loop invariants, prove or argue persuasively that your code will not crash the program with a divide-by-zero error. 3. Through preconditions, postconditions, loop invariants, or other techniques, argue that the selection-sort algorithm below puts the array A into ascending order. Give better names to m and s.
void selection(int A[],int s)x { for (int i=0; i < s-1; ++i) { int m = i,j; for (j=i+1; j < s; ++j) if (A[j] < A[m]) m = j; swap(A[i],A[m]); } }
7. By use of preconditions, postconditions, and loop invariants, prove or argue persuasively that the pseudocode below will correctly count the number of spaces in a string, s.
count-spaces(s) n 0 I 0 while i < length(s) do if s[I] = n n + 1 i i + 1 }
4. Write an algorithm in pseudocode, or code it in Java, to input a series of real numbers and find the smallest value among them. Use a loop invariant to show that the algorithm is correct. 5. Write a recursive algorithm (in C, Java, or pseudocode) that accepts a number, n, and finds the sum of all the numbers from 1 to n. 6. By use of preconditions, postconditions, and loop invariants, prove or argue persuasively that the code below will terminate rather than hanging the computer.
int i = 1; while (i < { cout << if (i % i *= else i++; } 100) i; 2 == 0) 2;
8. Write a method that accepts as parameters a sorted array of integers, its occupancy (current size), and a value to be inserted. The method should insert the value at its appropriate location. Write preconditions, postconditions, and loop invariants to argue that the method is correct. 9. Write pseudocode for a search of a linked list for a given value. State the pre- and postconditions of the algorithm and write comments that argue persuasively for the correctness of the algorithm. 10. Write pseudocode or Java code for an algorithm that finds the sum of the elements of an array of integers, given the array and a value denoting its size. Use preconditions, postconditions, and loop invariants to argue that it is correct. Express its time complexity in big-O notation. 11. Write an algorithm in pseudocode, or code it in Java, to display all the integers from 100 down to 1. Use a loop invariant to show that the algorithm is correct. 12. Using pseudocode, C, or Java, write an algorithm that accepts an array of integers and returns the value os the largest and smallest elements. Show its correctness using preconditions, postconditions, and loop invariants. 13. Write an algorithm that tells whether a linked list of integers contains two or more nodes that store a given value. Argue for its correctness and discuss its complexity. 14. Write an algorithm that takes an array of integers as a parameter and returns the sum of these integers. Argue for correctness and discuss complexity.
David Keil
9/08
11
5.
6. 7.
2. Linked lists
1. 2. 3. (T-F) A linked list is of a length specified in its declaration. (T-F) A linked-list node type is a simple type. (T-F) The physical order (in memory) of the nodes of a linked list is the same as the order in which nodes are accessed. The data type of a node of a linked list is (a) array; (b) simple; (c) class; (d) collection; (e) reference How many reference members, minimum, must a linkedlist node have? (a) 0; (b) 1; (c) 2; (d) 3; (e) 4 A linked-list node object must have how many members? (a) 0; (b) 1; (c) 2; (d) 3; (e) more than 3 One member of a linked-list node must be a(n) (a) character; (b) integer; (c) node; (d) reference; (e) none of these (a) ; (b) ; (c) ; (d) ; (e) none of these (a) ; (b) ; (c) ; (d) ; (e) none of these
4. Conclusion
1. An advantage of linked lists is (a) low access speed; (b) optimal search speed; (c) zero overhead; (d) expandability; (e) none of these 2. A disadvantage of linked lists is (a) high access time; (b) zero expandability; (c) time consuming insertion and deletion processes; (d) all of these; (e) none of these 3. (T-F) If we know the address of a node in a doubly linked list, then to access its predecessor we must start at the first node in the list. 4. (a) ; (b) ; (c) ; (d) ; (e) none of these 5. (a) ; (b) ; (c) ; (d) ; (e) none of these 6. (a) ; (b) ; (c) ; (d) ; (e) none of these
4. 5.
6. 7.
8. 9.
David Keil
9/08
12
2. Linked lists
6. f. A linked list is of arbitrary length. 7. f. A node is of a class. 8. f. The physical order after an insertion may be different from the physical order in memory.
4. Conclusion
1. f. A doubly-linked-list node has a predecessor link.
David Keil
9/08
13
Short-answer
1. Declare a data type for a linked-list node to store a float. 2. Write Java code or pseudocode to display all data values in a linked list whose first node is pointed to by reference variable p and whose data value in each node is named data. 3. What are three operations appropriate to a linked list? 4. How might you append one linked list, L2, to another, L1? 5. How might you insert one linked list (L2) into another (L1) at some desired location whose predecessor node is stored in reference pn?
Longer-answer questions
1. In one or two paragraphs, state the relationship among these concepts: - Abstract data type - C class or Java class - Data structure - Algorithm 2. Define a collection of library-book data records. Each book has a title, author, and catalog number. In Java, define appropriate data types and define methods to search the collection for a particular catalog number. Discuss two ways to implement the collection other than the one you chose; what are their advantages and disadvantages? 3. Design or code an implementation of a linked list of floats. Include necessary class declarations and method definitions to implement insertion of one value, in ascending numeric order, and display of all values. 4. Write declarations for abstract data types or classes needed to implement a collection of butterflies. A butterfly has a wing span, a color, and a name. Include some appropriate method declarations for operations on the butterflies and the collection. 5. Write a method that takes two parameters that are references to list nodes, and that links the two nodes by making the second node the successor of the first.
Short-answer answers
1.
struct Nodes { float data; nodes* next; }; while (p_node != NULL) { printf(%d,p_node->data); p_node = p_node->next; }
2.
3. Some operations appropriate to a linked list are prepend node, append node, insert node, delete node, search list, delete all nodes. 4. Step to end of L1, assign the address of the first node of L2 as the next of the last node of L1. 5. Save pt as a temporary variable with value pn->next; assign address of L2s first node as pt->next; assign pt as next of last node of L2.
David Keil
9/08
14
3. Stack implementations
1.
2.
3. 4. 5. 6.
(T-F) Stacks and queues may have different implementations in the Java language. The ______ implementation of the stack enables onestep pop and push (a) class; (b) tree; (c) array; (d) linkedlist; (e) none of these Stack can be full in ___ implementation (a) class; (b) tree; (c) array; (d) linked-list; (e) none of these (a) ; (b) ; (c) ; (d) ; (e) none of these (a) ; (b) ; (c) ; (d) ; (e) none of these (a) ; (b) ; (c) ; (d) ; (e) none of these
4. Queue operations
Multiple-choice, T/F
1. A queue is a specialized kind of (a) simple type; (b) array; (c) collection; (d) tree; (e) stack 2. To remove data from a queue, we carry out the ______ operation. (a) dequeue; (b) enqueue; (c) pop; (d) push; (e) traversal 3. The first-in, first-out structure is the (a) list; (b) array; (c) queue; (d) stack; (e) tree 4. (T-F) The queue is an abstract data type. 5. The dequeue operation triggers an error message on (a) full stack; (b) full queue; (c) empty stack; (d) empty queue; (e) null reference 6. The enqueue operation triggers an error message on (a) full stack; (b) full queue; (c) empty stack; (d) empty queue; (e) null reference
2. Stack examples
1. An appropriate data structure for a problem involving the checking of matching parentheses would be a (a) list; (b) array; (c) stack; (d) queue; (e) tree 2. A postfix way of writing arithmetic expressions is (a) recursive; (b) Big-O notation; (c) quadratic; (d) reverse Polish notation; (e) none of these 3. To evaluate an expression in reverse Polish notation, we use ______ for each operator encountered. (a) one push; (b) two pushes; (c) one pop; (d) two pops; (e) none of these 4. To evaluate an expression in reverse Polish notation, we use ______ for each numeric value encountered. (a) one push; (b) two pushes; (c) one pop; (d) two pops; (e) none of these
6. Queue implementations
Multiple-choice, T/F
1. (T-F) A queue may be implemented as an array. 2. In the list implementation of a queue, a new item would be added to the (a) front; (b) back; (c) second node; (d) middle node; (e) none of these
David Keil
9/08
15
Short-answer
1. queue 2. An array for stack contents, int for top subscript; methods pop, push 3. An array for queue contents, int for size of queue; methods enqueue, dequeue 4. (a) read 5, push 5, read 3, push 3, read +, pop 3 and 5, add 5 + 3, push 8, read 2, push 2, read *, pop 2, pop 8, calculate 8 * 2, push 16. (b) read 6, read *, pop 6, get error popping empty stack, terminate (c) read 2, push 2, read 1, push 1, read 3, push 3, read , pop 3, pop 1, compute 13, push 2, pop 2 to display answer, display error on stack not empty at end of input stream (d) read 1, push 1, read 4, push 4, read +, pop 4 and 1, add 1 + 4, push 5, read 5, push 5, read , pop 5, pop 5, calculate 5 5, push 0. (e) read 6, push 6, read 3, push 3, read /, pop 3, pop 6, compute 6 / 3, push 2, read 2, push 2, read +, pop 2, pop 2, calculate 2 + 2, push 4 5. (a) valid (b) invalid ( causes pop of empty stack) (c) invalid (not enough operators, leaves 1 on stack) 6. (a) queue (b) 25 7. (a) O(1) (b) O(1) (c) O(1) (d) O(size) 8. (a) O(1) (b) O(1) (c) O(1) (d) O(1) 9. Running time for insertion is improved from O(n) to O(1) if link to last node is maintained.
David Keil
9/08
16
Longer-answer questions
1. Write an implementation of a queue of sales order numbers. The user should have the options of entering an order or removing it from the queue for order fulfillment. 2. Write pseudocode or Java code to implement the main operations on a queue. Discuss their time complexity. 3. Explain what a postfix expression is, with examples, and what data structure is used to work with it. 4. Write program code or pseudocode to implement the operations that store data in a stack and retrieve data from a stack. Discuss time complexity. 5. Assuming a linked-list implementation of a queue, write methods that make use of the list operations to implement the queue operations. 6. Write a program that reads a Java program and outputs a file with all method definitions removed. To accomplish this, you must create a stack
Short-answer
1. Which data structure would be most appropriate to implement a simulation that determines expected time for customers to wait for a teller to serve them in a bank? 2. What data members and what methods are needed to implement a stack using an array? 3. What data members and what methods are needed to implement a queue using an array? 4. Evaluate the following postfix expressions, showing operations on an appropriate data structure and signalling errors if appropriate: (a) 5 3 + 2 * (b) 6 * 4 + (c) 2 1 3 (d) 1 4 + 5 (e) 6 3 / 2 + 5. Which of the following are valid postfix expressions? (a) 3 1 2 + (b) 1 2 + 3 (c) 1 4 2 / 3 + 6. Consider that your task is to build a program to simulate the arrival of bank customers at a teller line. Up to 1000 customers may appear in a day, there may be up to four tellers available, and up to 25 customers could possibly line up for service at one time, in the opinion of the client. (a) What data structure would you use to represent the customers lined up? (b) If it were implemented as an array, how many elements would the array have? 7. In Big-O notation, what are the running times of the following operations on stacks and queues in the implementations that involve one array and one integer, size? (a) pop (b) push (c) enqueue (d) dequeue 8. In Big-O notation, what are the running times for the operations listed in the previous problem in implementations based on linked lists? 9. In the linked-list implementation of a queue, what improvement in running time is made possible by maintaining a reference to the tail of the queue? 10. What is the result? push 3 push 0 push 1 pop y display y 11.What is the result? push 1 pop x pop y display y
David Keil
9/08
17
12. What is the result? for i 1 to 5 push i for i 1 to 4 pop x pop y display y
David Keil
9/08
18
4. Heap operations
1. The running time of the Heap-insert operation is (a) O(1); (b) O(log n); (c) O(n); (d) O(n log n); (e) O(n2) 2. The running time of the algorithm to delete the minimum value from a heap is (a) O(1); (b) O(log n); (c) O(n); (d) O(n log n); (e) O(n2) 3. The running time of the algorithm to restore the heap property in a tree, given a node whose two subtrees are both heaps, is (a) O(1); (b) O(logn); (c) O(n); (d) O(nlogn); (e) O(n2) 4. (T-F) The heap data structure lets us search for an element with a given value in time O(log n) 5. To find an element of a given value in a heap would take time O(___) (a) 1; (b) log n; (c) n; (d) n log n; (e) n2 6. The common implementation of a heap is (a) array; (b) linked list; (c) doubly-linked structure; (d) multi-linked structure; (e) none of these 7. To sort an array of size n using a heap will take how many steps, on average? (a) 1; (b) log2n; (c) n; (d) n log n; (e) n2
3.The common implementation of a heap is (a) array; (b) linked list; (c) doubly-linked structure; (d) multi-linked structure; (e) none of these 4.The root of a heap implemented as array A is (a) the first element of A; (b) the last element; (c) a reference; (d) the node pointed to by a certain reference; (e) inaccessible 5.(T-F) In a maximum heap, each nodes left child stores a value that is less than that of the parent. 6.(T-F) In a maximum heap, each nodes right child stores a value that is greater than that of the parent. 7.The depth of a heap of size n is close to (a) 1; (b) log2n; (c) the square root of n; (d) n / 2; (e) n2 8.If a heap nodes subscript is 4, then the subscript of its left child is (a) 1; (b) 2; (c) 3; (d) 4; (e) 8 9.A heap is (a) any array; (b) any tree; (c) a complete binary tree; (d) a binary tree in which no node has exactly one child; (e) none of these 10.If a heap nodes subscript is 6, and it is a left child, then the subscript of its parent is (a) 1; (b) 2; (c) 3; (d) 4; (e) 12 11.(T-F) A heap is implemented by a binary search tree with nodes linked by references.
David Keil
9/08
19
Short-answer
1. What is the best-known application for heaps? 2. What is the approximate running time of this algorithm, Build-Heap, which operates on array A? Heap-size[A] length[A] For i length[A] 2 down to 1 Heapify(A, i) 3. Draw a minimum-heap that could be built from these values: { 5, 3, 2, 6, 4 } 4. In a maximum heap, what is the relationship between values stored in a parent and its child? 5. What is the name of the operation that restores the heap property to a tree in which both subtrees of a node have the heap property? 6. How many nodes must be looked at to find the secondlowest value stored in a minimum heap? 7. How many nodes must be looked at to find the highest value stored in a maximum heap of size n? 8. How many steps does it take to find the lowest value in a maximum heap of size n? 9. Name a kind of binary tree in which all leaves are at most one level apart. 10. What is a binary tree in which the bottom-row nodes are filled at the left? 11. Describe the bottom row of a complete binary tree. 12. Name a data structure appropriate for implementing a priority queue in which items may be inserted quickly with arbitrary values and the lowest or highest valued item may be retrieved quickly. 13. Draw a complete binary tree containing the values 4, 2, 5, 9, 8, 6, in that order. 14. Draw a minimum heap containing the values 8, 4, 2, 9, 5, 6 15. What is the heap property for minimum heaps? 16. What is the heap property for maximum heaps? 17. Consider this array:
int A[10] = {7,9,3,5,2,4,6,8,1,10};
(a) Write the corresponding array initialization. (b) Is the tree a heap? If not, redraw it as a heap. 19. Draw the trees that correspond to the heap shown below after inserting (a) 6; and (b) 6, followed by 3
20. Draw the tree that corresponds to the heap shown in the figure under the previous problem, after executing the Extract-min operation. 21. In a sentence or two, and using Big-O notation, explain what is the running time of Heapsort and why. 22. In Big-O notation, what is the complexity of the operation of extracting all values in a priority queue of size n, implemented as (a) an unordered arrray; (b) a heap 23. What is the depth of a complete binary tree with fifteen nodes? 24. How many nodes are there in a complete binary tree of depth 7?
Longer-answer questions
1. Describe in detail the steps in building a heap from an unordered array and using the heap to produce a sorted array. Discuss the complexity. 2. Implement a minimum-heap data structure in Java, using a class and methods to support insertion and retrieval of minimum value. 3. Write Java code, or pseudocode, for Heapify. Include preconditions and postconditions. 4. Explain what the running time for heap insertion and Extract-min are, and why. 5. What are two ways to implement a priority queue? 6. Describe the Heapify operation, its preconditions and postconditions, and what it is used for. 7. Evaluate the healp data structure as a possible way to implement a database that will be frequently searched. 8. Write a simple sorting algorithm that uses a heap for internal storage.
(a) Draw the corresponding complete binary tree; (b) Is it a heap? ______ 13. Suppose the values 9,3,5,2,4,6,8,10 were in a priority queue of print jobs, with highest priority corresponding to lowest number, i.e., 2 is the highest-priority job. Then if you insert a job with priority 3 and then extract two jobs from the priority queue, what was the priority of the second job you extracted?
David Keil
9/08
20
4. Heap operations
1. b. Inserting an item in a heap entails a step to put the item rightmost in the bottom row of the tree, then filter it up the tree by exchanging it a number of times less than or equal to the depth of the tree, logn. 2. b. After determining the minimum value in one step, it is necessary to remove it an restore the heap property, an operation taking up to as many steps as the depth of the tree, logn. 3. b. Heapify takes up to as many steps as the depth of the tree. 4. f. To search for an arbitrary value in a heap, it is necessary to extract the minimum value up to n times. Extracting the minimum and restoring the heap property would take up to log n steps. The search of a heap is worse than a linear search of an array and leaves the heap empty. 5. d. See explanation for previous problems answer. 6. d. Roughly each array element must be inserted in the heap, which takes roughly log n steps for each element.
David Keil
9/08
21
Short-answer
1. Heaps are chiefly an implementation of priority queues. 2. O(n log n). Building a heap from an unsorted array requires on the order of n heapifying steps, each of which is O(logn). 3.
16. The heap property for maximum heaps is: Each non-leaf node stores a value greater than or equal to those stored in its child nodes. 17. (a)
4. The value stored in the parent will be greater than that stored in the child. 5. Heapify. 6. Two nodes must be inspected: the first and the second in the priority queue. 7. One node must be looked at to find the highest value in a maximum heap. 8. n nodes must be accessed to find the lowest value in a maximum heap. 9. complete binary tree 10. complete binary tree 11. The bottom row of a complete binary tree is filled from the left. 12. heap 13. A complete binary tree containing the values 4, 2, 5, 9, 8, 6:
19. (a)
(b)
20. 14. Draw a minimum heap containing the values 8, 4, 2, 9, 5, 6 21. Heap-sort is O(nlogn), because n items must be stored, in log n time each; then n items must be retrieved in log n time each. 22. (a) O(n2) (b) O(n log n) 23. 3 24. 255
15. The heap property for minimum heaps is: Each non-leaf node stores a value less than or equal to those stored in its child nodes.
David Keil
9/08
22
3. 4.
5.
6.
7.
8. 9.
10.
11.
12.
David Keil
9/08
23
3. Tree traversal
1. Traversal of a binary search tree is normally (a) linear; (b) impossible; (c) recursive; (d) risky; (e) circular 2. A binary search tree is traversed (a) depth first; (b) breadth first; (c) in the order in which nodes were inserted; (d) at random; (e) none of these 15. (T-F) The binary search tree shown below could be generated by the insertion of values 3, 5, 2, 4, 1 in that order.
2. How many non-null references does a BST node have? (a) 0; (b) 1; (c) 2; (d) 0 or 1; (e) 0, 1, or 2 3. (a) ; (b) ; (c) ; (d) ; (e) none of these 4. (a) ; (b) ; (c) ; (d) ; (e) none of these 5. (a) ; (b) ; (c) ; (d) ; (e) none of these
6. Performance issues
Multiple-choice, T/F
1. (T-F) The level of two leaf nodes in a balanced tree differs at most by one. 2. A degenerate tree has (a) balanced branches; (b) no nodes; (c) nodes with only 1 reference; (d) almost all branches going uniformly left or right at all levels; (e) none of these 3. Search time on a degenerate tree is (a) constant; (b) logarithmic-time; (c) linear time; (d) quadratic time; (e) none of these
16. (T-F) The binary search tree shown below could be generated by the insertion of values 1, 4, 3, 7, 8 in that order.
5. BST implementation
1. A BST node implemented in Java contains ____ reference(s) (a) 0; (b) 1; (c) 2; (d) 3; (e) more than 3
David Keil
9/08
24
14. Using nodes with only two links each, draw a tree to represent a family whose senior member (Ada) has three children, Betty, Carl, Don, and Emily. Betty has one child, ST1 Carl has none, Don has three, and Emily has two. 1. Which data structure would you use to implement an outline 15. Write a class or structure-type declaration, listing data processor? members of the nodes for the family tree described in the 2. Write a structure-type definition for a general-tree node. previous problem. A persons name is sufficient to describe 3. Name or describe the member data items required to define the person. a general-tree node. 16. Draw a binary search tree of characters for the values S, I, 4. In the logical structure of a general tree, one node may have T, M, E, L, B, inserted in that order. how many siblings? 17. What does this code do? int weight(nodes* p) 5. Write the Java infix expression that corresponds to this tree:
Short-answer
ST2
1. Draw the binary search tree, of characters, that would be formed by inserting the following values in order: S, E, L, I, T, B, M 2. What is the average time complexity of the standard algorithm to insert one new value into a binary search tree of n nodes? 3. What is the rough expected running time of a standard algorithm to build a binary search tree from a random series of distinct values, and then to traverse the tree displaying value in ascending order? 4. What is the binary-search-tree property? 5. What is the time complexity of inserting one new value into a degenerate binary search tree of n nodes? 6. What is the time complexity of inserting one new value into a balanced binary search tree of n nodes? 7. Sketch the binary search tree created by inserting values in the following order: K, M, A, D, H, B, R, Q. 8. List a possible ordering of values that could produce this BST when inserted one by one:
Longer-answer questions
9. 10.
1. What are the advantages and disadvantages of organizing a local area network as a tree? As a ring? 2. Describe three tree-like structures we have encountered in this course and briefly name their implementations. 3. What are some of the applications of general trees? 4. What are some advantages of binary search tree structures over linked lists and arrays? 5. Describe in detail the data members of a general-tree structure and give pseudocode for two or three operations; or code in Java. 6. Write an algorithm to find the total number of nodes in a general tree, given its root. 7. Write pseudocode or Java code for (a) a BST search or (b) traversal. (c) deletion (d) insertion (e) emptying tree 8. Write pseudocode or Java code to delete a node from a binary search tree, given a reference to it and given a reference to the root of the tree. 9. Discuss the relative complexities of the storage, retrieval, and search operations on (a) a stack; (b) a linked list sorted Draw the BST that would be formed by inserting, in order, in ascending order; (c) a binary search tree. 4, 2, 5, 9, 8, 6. 10. Compare and contrast the heap property and the BST In big-O notation, for balanced binary search trees, what are property and discuss implications for operations on these the complexities of structures. (a) node insertion? 11. Compare the complexities of various standard operations, (b) node deletion, for each of the three cases? including searching, on arrays, linked lists, and trees, using (c) tree sorting, from scratch? Big-O notation. Option: use pseudocode or code to illustrate (d) displaying contents of tree using inorder traversal? your points. Write the expression tree for a + 2b c + 4. Draw a binary tree with five nodes and three leaves. A binary tree consists of eight nodes, of which four are leaves. If new nodes are added in such a way that one leaf node becomes a parent with two children, the tree will have how many nodes, including how many leaves?
David Keil
9/08
25
19. In Java, declare a type for a BST node storing a floatingpoint number, and write a method to initialize such a node. 20. Write the pseudocode or program code for BST search and comment it, arguing that it (a) terminates and (b) returns correct result when search key is not in tree. 21. Modify a tree-manipulating file in subdirectory bst (treesort.c, treesort.cpp, or intsort.cpp and the library it uses, bst.h), or write your own BST code from scratch, to prompt for integer values to delete from a binary search tree and to delete them. (See slides.) Display contents of resulting tree. Using data file intsort.dat, test for deletion of 50 (not present), 18, 15, 61 and 83. Submit your new code and the test results. 22. Implement a word-counting program that reads a text file and stores each white-space-delimited string in a node of a binary search tree. The node should also store a count of the number of times the word has been found so far. Use your word counter with the text of a C program. (Challenge: modify the specifications to define a word, or lexeme, as being a white-space-delimited single punctuator (;, ,, :, {, }, (, ), [, ], +, =, -, ., /, <, and arrays? >, *) or a series of characters following or preceding a 17. Write pseudocode or Java code for a BST search or punctuator.) Display in ASCII/alphabetical order the traversal. Argue for its correctness and characterize its lexemes found and their counts. complexity. 23. Write a program that inserts random numeric values into a 18. Declare classs or classes for an operating systems diskbinary search tree. Estimate the average path length in trees directory tree and its contents. Each item, or entry, in the of different sizes, by having your program count the number directory should have a name and a reference to a subtree. of steps necessary to search for a value. Compare this to the An entry that is a file should have a null subtree; a calculated estimates for best, worst, and expected running subdirectorys subtree should store its contents. You need times. not write methods or test your code. Write a sentence or two describing this data structure and draw a diagram of it.
12. Write pseudocode or Java code for one of the following and discuss its complexity: (a) delete a node from a binary search tree, given a reference to it and given a reference to the root of the tree; (b) insert a value into a binary search tree whose root is pointed to by p; (c) display all values in a binary search tree in alphabetical order by key. 13. Write a recursive method that accepts a reference to a BST node as a parameter and returns 0 if the reference is null, otherwise returns one plus the combined weights of the nodes left and right subtrees. That is, your method should return the number of nodes in a BST subtree whose root is pointed to by the parameter. Prove the correctness and discuss the complexity of the method. 14. Compare and contrast heaps and binary search trees. 15. Describe how a binary search tree of integers could be implemented using an array. 16. What are some of the applications of trees? What are some advantages of binary search tree structures over linked lists
David Keil
9/08
26
3. Tree traversal
1. c. The standard tree traversal algorithm is recursive. 2. a. Traversal requires descending a tree to a leaf repeatedly. 3. t. The tree shown contains all values listed, has the binary-search-tree property, and each child node follows its parent node in the chronological order of insertion. 4. f. The tree shown has the binary-search-tree property, but 4 is inserted before 3, so 3 should be below 4.
B. Short-answer
1. A general tree implements an outline.
2. struct nodes { void* data; nodes* child,*r_sibling; };
3. data, child, r_sibling; (optional:) l_sibling, parent 4. A node may have any number of siblings, though only one right sibling directly connected to it.
David Keil
9/08
27
4. t. The deletion of a parent of two nodes may require replacing the nodes value with the value stored in the node that is leftmost in the right subtree of the node. 5. a. Since a leaf has no children, it may simply be unlinked to delete it. 6. c. Linking the parent of a node to the nodes child in effect removes the node from the tree without removing its child. 7. b. A node inserted later is below; a node with a smaller value is inserted to the left.
David Keil
9/08
28
Short-answer
1.
7.
2. O(logn). An insertion is at a leaf location; a leaf is on average at about the depth of the tree. 3. O(nlogn). Each step in building a tree takes O(logn) steps, because the depth of the tree reaches that value. There are n building steps. The traversal takes n steps. The traversals running time is dominated by the building time. 4. The binary search tree property states that a nodes left child stores a value less than or equal to that of the node, and a nodes right child stores a value greater than or equal to that of its parent. 5. O(n), because a degenerate tree is in the form of a linear list, since each node inserted stores a greater value than its predecessor. 6. O(lg n), because a balanced tree branches out evenly, with no path longer than the base-2 logarithm of n, for n nodes.
Traversal: A, B, D, H, K, M, Q 8. 83, 61, 53, 62, 70, 90, or 8, 90, 61, 53, 62, 70, etc. 9. 10. (a) O(lg n) (b) leaf: O(lg n) parent of 1 node: O(lg n) parent of 2 nodes: O(lg n) (c) O(n lg n) (d) O(n) 11.
13. The tree will have ten nodes and five leaves. 14.
16. Counts nodes in a binary search tree. 17. Preorder, postorder, inorder
David Keil
9/08
29
4. Bucketing (chaining)
1. A bucket is used in (a) binary search trees; (b) hashing; (c) linked lists; (d) stacks; (e) queues 2. (T-F) For a hash table of size n, implemented with chaining, lookup can be worse than O(n). 3. (T-F) Chaining is a way to avoid collisions in hash tables 4. Clustering occurs in (a) only hash tables populated by open addressing; (b) only hash tables with chaining; (c) hash tables with open addressing or chaining; (d) perfect hash functions; (e) the Bubble sort 5. Clustering occurs in (a) a linked list; (b) a binary search tree; (c) a hash table; (d) a stack; (e) a queue 6. (T-F) An efficient hash table can be implemented with one linked list. 7. (T-F) An efficient hash table can be implemented with multiple linked lists. 8. (a) ; (b) ; (c) ; (d) ; (e) none of these 9. (a) ; (b) ; (c) ; (d) ; (e) none of these 10. (a) ; (b) ; (c) ; (d) ; (e) none of these
2. Simple hashing
1. (T-F) A bit vector (bit array) can store information about whether a value belongs to a set in a location that depends on the value. 2. Simple hashing involves storing items in locations whose array subscripts are (a) hashed; (b) computed; (c) the values stored; (d) chosen at random; (e) none of these 3. The simple-hashing way to store a set is (a) as a linked list of set elements; (b) as an array of set elements; (c) as an array of Booleans such that the elements whose subscripts are in the set are assigned True; (d) as a string that describes the set; (e) none of these 4. If two keys map to the same slot in a hash table, (a) one must be discarded; (b) processing is the same as if only one key mapped to that slot; (c) the collision situation must be resolved; (d) there is only one way to respond; (e) none of these 5. (a) ; (b) ; (c) ; (d) ; (e) none of these 6. (a) ; (b) ; (c) ; (d) ; (e) none of these
David Keil
9/08
30
Short-answer
1. Collisions are a problem in the use of which data structure? 2. What kind of function is used to uniformly scatter data items, by key, across an array so that the location of a data item can be easily obtained from its value? 3. What is the name of the occurrence when many data values hash to a similar vicinity of the table? 4. What hashing technique uses a hash function to generate a location and an offset? 5. What is the name of the technique used to resolve collitions in a hash table that is stored entirely in an array? 6. The use of linked lists in hash tables is called __________. 7. Supply the terms: (a) A problem that arises when we attempt to store data in a location whose address is calculated from a key value (b) A method for storing data in a location whose address is calculated from a key value (c) A solution to the problem of two keys mapping to the same address (d) A subprogram that scatters data in a seemingly random and uniform way (e) A measure of the density of a hash table
8. Under what circumstances will any hashing scheme base only on arrays break down? 9. In Big-O notation, what is the (a) worst-case running time to store one data item in a hash table of size m storing n items? (b) best-case running time?
Longer-answer questions
1. Describe some problems solved by hashing, some problems it raises for software implementers, and solutions to these problems. 2. Describe two ways to implement a hash table and the chief way in each to resolve the collision problem. Discuss complexity of each. 3. Give arguments for and against the idea that theory is of critical importance in working with computers.
David Keil
9/08
31
5. f. Chaining is a way to resolve collisions; there is no way to avoid them. 6. c. Clustering occurs whenever many collisions occur in the same vicinity of a hash table. 7. c. When a hash function maps many values to the same vicinity of a hash table, we have clustering, a problem. 8. f. A linked list is very slow to search, so it would not be used for an entire hash table, only for part of it. 9. t. The chaining strategy for hash tables uses an array of linked lists.
2. Simple hashing
1. a. Hash functions in effect calculate the location of a key from its value.
4. Bucketing (chaining)
1. c. Two solutions to the collision problem are: implement the buckets as linked lists; store an extra key in the next bucket after the one whose location was returned by the hash function. 2. t. Insertion requires lookup and only a constant amount of steps beyond that. 3. t. Hash-table lookup can be done in constant time if there have been few collisions on insertion. 4. b. Calling a hash function twice makes sense only when a linear probe is the result of a collision.
David Keil
9/08
32
Short-answer
1. 2. 3. 4. 5. 6. a hash table hash function collision double hashing linear probing (open addressing) chaining
7. Supply the terms: (a) collision (b) hashing (c) linear probing (d) hash function (e) load factor 8. when the number of values stored exceeds the array size 9. (a) O(n) (b) O(1)
David Keil
9/08
33
3. Weighted graphs
1. A weighted graph has an adjacency matrix that is (a) integers; (b) vertices; (c) real numbers and ; (d) booleans; (e) none of these 2. (T-F) The Dijkstra algorithm examines all possible paths from each vertex to each other one. 3. (T-F) The Dijkstra algorithm builds a tree of the minimum-weight paths from a single source vertex to each other vertex. 4. A minimal spanning tree is a subset of a (a) binary search tree; (b) undirected graph; (c) weighted graph; (d) array; (e) none of these 5. (a) ; (b) ; (c) ; (d) ; (e) none of these 6. (a) ; (b) ; (c) ; (d) ; (e) none of these
4. Implementations
1. A graph may be fully represented by (a) its vertices; (b) its edges; (c) an adjacency matrix; (d) the degrees of its vertices; (e) none of these 2. A graph may be conveniently represented as (a) a vector of characters; (b) a two-dimensional array of Booleans; (c) a single linked list of Booleans 3. In the array implementation of graphs, what is the type of the array elements? (a) Booleans; (b) characters; (c) integers; (d) structures; (e) arrays 4. The two-dimensional array implementation of graphs tells whether or not a pair of vertices are (a) part of the graph; (b) connected; (c) adjacent; (d) the same; (e) none of these 5. A list implementation of a graph might use an array of linked lists, each list containing all the ________ a vertex. (a) paths containing; (b) vertices adjacent to; (c) edges adjacent to; (d) trees containing; (e) none of these 6. (a) ; (b) ; (c) ; (d) ; (e) none of these
2. Paths
1. A series of edges that connect two vertices is called (a) a path; (b) a cycle; (c) a connection; (d) a tree; (e) a collection 2. A series of edges that form a path from a vertex to itself is (a) a spanning path; (b) a cycle; (c) a connection; (d) a tree; (e) an edge 3. To design a communications network that joins all nodes without excessive lines, we must find a (a) path; (b) connectivity number; (c) minimal spanning three; (d) expression tree; (e) search tree 4. To find a path from one vertex to another, we may use (a) depth-first search; (b) connectivity number; (c) minimal spanning tree; (d) expression tree; (e) search tree 5. A graph in which exactly one path joins any pair of vertices is (a) subgraph; (b) tree; (c) connected graph; (d) cyclic graph; (e) unconnected graph 6. (a) ; (b) ; (c) ; (d) ; (e) none of these
David Keil
9/08
34
Short-answer
1. A two-dimensional array that represents a graph is a(n) _______ matrix. 2. A graph is defined by a set of _________ and _________. 3. A directed or undirected graph may be implemented in C or Java by ___________ or _____________. 4. Write an adjacency matrix and a diagram of a linked-list representation of the graph below.
Longer-answer questions
1. Contrast depth-first and breadth-first search of a graph. 2. Describe the Dijkstra algorithm for finding paths in graphs. 3. Write pseudocode or Java code for an algorithm to convert a graph represented by a matrix to an array-of-lists representation 4. Write pseudocode or Java code for an algorithm to convert a graph represented by an array-of-lists to a matrix representation 5. Describe the Prim algorithm for finding paths in graphs. 6. Describe a way to find a path from a given vertex in a graph to a second given vertex.
5. What is the minimum number of vertices in an undirected cyclic graph? 6. In the graph below, what are the (a) outdegree of vertex a? (b) weight of the shortest path from vertex c to vertex a?
(a) Is it cyclic? (b) Is it connected? (c) List, in order, the vertices in the longest path 8. In Big-O notation, what is the running time of the Dijkstra algorithm? (See slide) Why? 9. Referring to the graphs below, (a) Which are directed? (b) Which are cyclic? (c) What is the degree of a in (i)? (d) What is the weight of the shortest path from a to d in (iii), if the weight of each edge is 1?
10. (a) Is the graph below connected? (b) Is it cyclic? (c) Find a path from a to d
David Keil
9/08
35
2. Paths
1. a. A path joins vertices directly. 2. b. A cycle is a path from a vertex to itself without passing through any other vertex twice. 3. c. The minimal spanning tree contains sufficient edges to connect each vertex to each other, with minimal total weight. 4. a. Depth-first and breadth-first are two kinds of search for paths in a graph. 5. b. A tree is connected and acyclic.
3. Weighted graphs
1. f. A path exists between any two vertices, but not necessarily an edge. 2. d. A subgraph is connecte4d if each of its vertices is connected by some path to each other one. 3. e. If a vertex is adjacent to five other vertices, its degree is 5. 4. b. All vertices in a tree connect and there are no cycles. 5. f. The Dijkstra algorithm builds one tree of paths using a greedy algorithm. It does not examine every possible path. 6. t. The tree produced by the Dijkstra algorithm contains optimal paths from the source node to each other one. 7. b. In a weighted graph, the least-weight path may have more edges than some greater-weight path.
4. Implementations
1. c. The edges are the relation denoted by the matrix. 2. b. A graph may be represented by a matrix (2dimensional array) or by an array of linked lists. 3. a. Each element in the two-dimensional array tells whether the two vertices specified by its two subscripts are adjacent. 4. c. The array is called an adjacency matrix. 5. b. The adjacency matrix of an n-vertex graph may be represented as an n-element array of lists of vertices adjacent to a given one.
David Keil
9/08
36
Short-answer
1. adjacency 2. vertices and edges 3. two-dimensional array of Booleans or an array of lists of integers. 4. 3 5. a b c d e f g h a 1 b 1 c 1 1 1 d 1 1 e 1 1 f 1 g 1 1 1 1 h 1 1
6. (a) 2 (b) 3.2 7. (a) no (b) no (c) a, b, e, d 8. O(n2); nested loops 9. (a) ii, iii (b) iii (c) 1 (d) 4 10. (a) no (b) no
David Keil
9/08
37
2. Java threads
1. The Java new operator invokes (a) dynamic allocation of memory; (b) memory allocation on the stack; (c) deallocation of memory; (d) allocation and deallocation of secondary storage; (e) none of these 2. Threads of equal priority get processor resources by (a) pre-emption; (b) round-robin time slicing; (c) submitting requests; (d) user intervention; (e) none of these
David Keil
9/08
38