CST 370 – Spring 2014 Homework 6 Due: 03/28/2014 (Friday) (11:55 PM) Name: Rick Shingu ID: 1493 1. A detachment of n soldiers must cross a wide and deep river with no bridge in sight. They notice two 12-year-old boys playing in a rowboat by the shore. The boat is so tiny, however, that it can only hold two boys or one soldier. How can the soldiers get across the river and leave the boys in joint possession of the boat? How many times need the boat pass from shore to shore? If I am understanding the question right. It would take 2n-1 amount of times from shore to shore. Here is a chart I created. N=number of solders and A=Amount of times reaching a shore. N 1 2 3 4 5 6 A 1 3 5 7 9 11 2. Assume that you conduct the binary search for the following numbers in an array. 3, 27, 39, 42, 55, 70, 74, 81, 85, 93, 98 (a) Draw the binary search tree for the numbers. Located on the last page of the Homework. (b) What is the largest number of key comparisons made by the binary search in searching for a key in the number(s)? List all numbers of this array that will require the largest number of key comparisons. The largest amount of comparisons would be 4 including the root number comparison for the searching of a number in the BST. The numbers that require the largest number of key comparisons include: 3,39,43,70,98. 3. Let A[0..n-1] be an array of n integer numbers. For simplicity, you can assume that all the numbers are distinct. In the array, a pair of two numbers (A[i], A[j]) is called an inversion if i < j and A[i] > A[j]. (a) Let’s assume that the array size is 3. What is the largest number of inversions in the array? Present a sample array with 3 elements. Array 3 2 1 CST370 Homework 6 The array could hold a maximum of three inversions. They would take place when (A[i],A[j]) (A[1],a[0]) , (A[2],A[0]), and (A[2],A[1]) (b) Answer the same questions for the smallest number of inversions. Present a sample array with 5 elements. Array 5 4 3 2 1 There would be 10 inversions in this array of numbers. IT would take place for (A[i],A[j]), (A[1],A[0]), (A[2],A[0]), (A[2],A[1]), (A[3],A[0]), (A[3],A[1]), (A[3],A[2]), (A[4],A[0]), (A[4],A[1]), (A[4],A[2]), and (A[4],A[3]) (c) Based on your answers to the question (a) and (b), what is the largest number of inversions in the general array with n elements? Similarly, what is the smallest number of inversions in the general array with n elements? I would say that the least amount of inversions would be 0 for the ascending ordered numbers. For descending order it is different. I do not necessarily see a pattern. I would wither be n or 2n. I As mentioned I don’t see a pattern but minimum will be zero. 4. A spider sits at the bottom (point S) of its web, while a fly sits at the top (F). How many different ways can the spider reach the fly by moving along the web’s lines in the directions indicated by the arrows? Explain your answer. This spider web isn’t completely symmetric for there is one arrow that doesn’t allow to you go there in the upper right area. With that being said there should be 127 ways to get to the fly from the spiders location. The way I got this number is to look at each location and see how many ways that you can get to that location. Each location that is pointed to holds a number of ways that it can be traveled to. So in that way you can add up the number of ways to each point until you reach your destination. CST370 Homework 6 5. An array A[0..n- 2] contains n – 1 integers from 1 to n in increasing order. So, one integer in this range is missing. Design an efficient algorithm to find the missing integer in English. Your description should be very clear. What is the time efficiency of your algorithm? I would create another array that would include the number and compare the two sets of data and see what number was an addition in the new array. This would be a linear comparison. This way the time complexity would be included in O(n). This would be the simplest solution if I were able to create that second array. Quick and Easy, but I am unsure if it violates any rules or not. BST Drawing: CST370 Homework 6
© Copyright 2025 Paperzz