# Register now to access 7 million high quality study materials (What's Course Hero?) Course Hero is the premier provider of high quality online educational resources. With millions of study documents, online tutors, digital flashcards and free courseware, Course Hero is helping students learn more efficiently and effectively. Whether you're interested in exploring new subjects or mastering key topics for your next exam, Course Hero has the tools you need to achieve your goals.

1 Page

### Data Str & Algorithm HW Solutions 46

Course: MAP 2302, Fall 2011
School: UNF
Rating:

Word Count: 211

#### Document Preview

Sorting 7.1 7 Internal Base Case: For the list of one element, the double loop is not executed and the list is not processed. Thus, the list of one element remains unaltered and is sorted. Induction Hypothesis: Assume that the list of n elements is sorted correctly by Insertion Sort. Induction Step: The list of n + 1 elements is processed by rst sorting the top n elements. By the induction hypothesis, this is...

Register Now

#### Unformatted Document Excerpt

Coursehero >> Florida >> UNF >> MAP 2302

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Sorting 7.1 7 Internal Base Case: For the list of one element, the double loop is not executed and the list is not processed. Thus, the list of one element remains unaltered and is sorted. Induction Hypothesis: Assume that the list of n elements is sorted correctly by Insertion Sort. Induction Step: The list of n + 1 elements is processed by rst sorting the top n elements. By the induction hypothesis, this is done correctly. The nal pass of the outer for loop will process the last element (call it X ). This is done by the inner for loop, which moves X up the list until a value smaller than that of X is encountered. At this point, has X been properly inserted into the sorted list, leaving the entire collection of n + 1 elements correctly sorted. Thus, by the principle of Mathematical Induction, the theorem is correct. 7.2 void StackSort(AStack<int>& IN) { AStack<int> Temp1, Temp2; while (!IN.isEmpty()) // Transfer to another stack Temp1.push(IN.pop()); IN.push(Temp1.pop()); // Put back one element while (!Temp1.isEmpty()) { // Process rest of elems while (IN.top() > Temp1.top()) // Find elems place Temp2.push(IN.pop()); IN.push(Temp1.pop()); // Put the element in while (!Temp2.isEmpty()) // Put the rest back IN.push(Temp2.pop()); } } 46
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

UNF - MAP - 2302
477.3 The revised algorithm will work correctly, and its asymptotic complexity willremain (n2 ). However, it will do about twice as many comparisons, since itwill compare adjacent elements within the portion of the list already knownto be sorted. Thes
UNF - MAP - 2302
48Chap. 7 Internal Sorting Conceptually (in particular, the linked list version) Mergesort is stable.The array implementations are NOT stable, since, given that the sublistsare stable, the merge operation will pick the element from the lower listbefo
UNF - MAP - 2302
49264013577.9(a) Each call to qsort costs (i log i). Thus, the total cost isni log i = (n2 log n).i=17.107.117.127.13(b) Each call to qsort costs (n log n) for length(L) = n, so the totalcost is (n2 log n).All that we need to do is redene the
UNF - MAP - 2302
50Chap. 7 Internal SortingN7 N8 N9B10B11Again, at each line, there is no information available about the relationshipsof those objects.To merge two such lists, we can do a normal merge, until we reach the pointwere we either compare an element (sa
UNF - MAP - 2302
51Best case: 2 compares.Avg case: 16/6 = 2 2/3 compares.Worst case: 3 compares.(b) Doing a similar approach of building a decision tree for 5 numbersis somewhat overwhelming since there are 120 permutations. A prettygood algorithm can be had be buil
UNF - MAP - 2302
52Chap. 7 Internal SortingSort. Thus, the calls to Insertion Sort are equivalent to the leaf nodes of a fullbinary tree. We know from the Full Binary Tree Theorem that the number ofleaf nodes in a full binary tree of n nodes is n/2 . Thus, if there ar
UNF - MAP - 2302
537.19 There are n possible choices for the position of a given element in the array.Any search algorithm based on comparisons can be modeled using a decisiontree. The tree must have at least n leaf nodes, one for each of the possiblechoices for solut
UNF - MAP - 2302
8File Processing and ExternalSorting8.1 Clearly the prices continue to change. But, the principles remain the same.8.2 The rst question is How many tracks are required by the le? A track holds144 .5K = 72K . Thus, the le requires 5 tracks. The time t
UNF - MAP - 2302
55Latency and read time together require 3.5 8.33 ms. Thus, the time toread the rst track is about 88 ms. The time to read the next three tracks is3 + 2100/3 0.08 + 3.5 8.33 32.2 ms. The last track takes just as longto read since it requires three rot
Georgia Tech - ECE - 3025
Georgia Tech - ECE - 3025
UNF - MAP - 2302
56Chap. 8 File Processing and External Sorting1 2n3 + 3n2 + n 3n2 + 3n(+)=n2662n3 + 6n2 + 4n n/3.6n28.7 The batch method is more efcient when enough sectors are visited to makeprocessing the whole le in sequential order more efcient. Since th
UNF - MAP - 2302
57(b) Cutting the disk I/O time will substantially improve the external sortingtime. A reasonable estimate is that cutting disk I/O time in half will cutthe sorting time by around 1/3.(c) Main memory access time will not help a great deal, since disk
UNF - MAP - 2302
9Searching9.1 The graph shows a straight line from (n + 1)/2 when p0 = 0 to n when p0 =1. Thus, the growth rate in the cost for a search is linear as the probability ofan unsuccessful search increases.9.2 int dictsrch(int array[], int K, int left, in
UNF - MAP - 2302
599.3 At each step, the exponent, call it x, is cut in half. This can only happen log xtimes. Of course, x = log n. Thus, the total cost is O(log log n).9.4 The partition and ndpivot functions remain the same.template &lt;class Elem, class Comp&gt;int fin
UNF - MAP - 2302
60Chap. 9 Searchingelse cfw_count[i]+;while (i &gt; 0) &amp; (count[i] &gt; count[i-1]) cfw_swap(A[i], A[i-1]);swap(count[i], count[i-1]);9.9 template &lt;class Elem&gt;void MoveToFront(Elem A[]) cfw_/ Assume that array is empty to begin withint n = 0;whil
UNF - MAP - 2302
61/ the length of the bit vectors are alwasy a number/ of ints.void inter(int* in1, int* in2, int* out, int n) cfw_for (int i=0; i&lt;n; i+)out[i] = in1[i] &amp; in2[i];/ in1 and in2 are input bit vectors, out is output bit/ vector; n is length of bit ve
UNF - MAP - 2302
62Chap. 9 Searching(c) 379.16 Key: 2 8 31 20 19 18 53 27H1: 2 8 5 7 6 5 1 1H2: 3 9 1 1 2 3 1 5Result of inserting:2 2 OK8 8 OK31 5 OK20 7 OK19 6 OK18 5 Collision. So, try 5+3 = 8. Collision.Then 5+6 = 11. OK53 1 OK27 1 Collision. So, try 1+
UNF - MAP - 2302
63/ Insert e into hash table HTtemplate &lt;class Key, class Elem, class KEComp,class EEComp&gt;bool hashdict&lt;Key, Elem, KEComp, EEComp&gt;:hashInsert(const Elem&amp; e) cfw_int home;/ Home position for eint pos = home = h(getkey(e); / Init probe sequencefor
UNF - MAP - 2302
10Indexing10.110.210.310.410.5(a) A record in the linear index refers to a block of sorted data records.Assuming that the linear index stores a key and a 4 byte block number,the index can hold information for 32K blocks, for a total le size of32
UNF - MAP - 2302
6545678DEERDUCKFROGGOATindex0479111171869(b) seckey2936718392797739primary Nextkeyindex02398113456228133339737-142936557183669279-171111887186-197739-110.6 ISAM is space efcient, more so than the B-
UNF - MAP - 2302
6610.810.9Chap. 10 Indexing33/\- -||1848/\/\- - -||||1223/304652/\/|\/\/\- - | - - - -||||||||1015 20/21 24314547 50 55M-/ \-||D/IS/|\/\- | -|||||BGKPU/\/\/\/\/\-||||||||||ACEHJLN/
UNF - MAP - 2302
6710.1124/48/|\- | -|||same33/4555/|\/\- | - -|||||30/31 384750/52 6010.1218/33/|\- | -|||4/1023same/|\/\- | - -|||||1/2/3 4/5/6 10/12/15 18/19/20/21/22 23/30/3110.1323/33|\-|-|||10/12/15/21/22 23/30/31 45/47/48
UNF - MAP - 2302
68Chap. 10 Indexing10.1612345min050125031,250781,250max502500125,0006,250,000312,500,000
UNF - MAP - 2302
11Graphs11.1 Base Case: A graph with 1 vertex has 1(1 1)/2 = 0 edges. Thus, thetheorem holds in the base case.Induction Hypothesis: A graph with n vertices has at most n(n 1)/2edges.Induction Step: Add a new vertex to a graph of n vertices. The most
UNF - MAP - 2302
70Chap. 11 Graphs123456-1|10202|2 | 1035|3|315|4 | 20 511 10 |5|15 113|6|210 3|-(b) 1 -&gt; 2(10) -&gt; 4(20) -&gt; 6(2) -&gt; \2 -&gt; 1(10) -&gt; 3(3) -&gt; 4(5) -&gt; \3 -&gt; 2(3) -&gt; 5(15) -&gt; \4 -&gt; 1(20) -&gt; 2(5) -&gt; 5(11) -&gt; 6(10) -&gt; \5 -&gt; 3(15) -
UNF - MAP - 2302
7111.8cost for nding all of the shortest paths is no worse than the cost to nd theshortest path between a specied pair of vertices.InitialProcess 4Process 2Process 3Process 6Process 5Process 1123456020 5 0 11 1015 5 8 0 11 1015 5 8 0 11 10
UNF - MAP - 2302
72Chap. 11 GraphsINITIALIZE array Count to 0s;FOR every edge (v, w) / Similar to BFS topo sortCount[w]+;IF the number of vertices with zero Count is not 1THEN return &quot;No Root&quot;;ELSE cfw_do DFS search from the vertex with zero Count;Verify that eve
UNF - MAP - 2302
73231 10 x233454205x5xx15116212x103415585xx1511621215103415585x1815116212x10341558531161511621215103415585311615116212151032-paths:231 10 13233453-paths:231 10 13
UNF - MAP - 2302
74Chap. 11 Graphs231 10 1323345412585616151162121510311.16 The problem is that each entry of the array is set independently, forcing processing of the adjacency list repeatedly from the beginning. This illustratesthe dangers invol
UNF - MAP - 2302
7511.23 If all of the edges are negative, then a smaller number is obtained by pickingmore edges than necessary to span the graph. It is not clear what the desiredanswer should be (1) the smallest value that spans the graph, even if it is nota tree, o
UNF - MAP - 2302
12Lists and Arrays Revisited12.1 Here is the nal Skip List.head252025263031+-&gt; +-&gt; +-&gt; +-&gt; +-&gt; +-&gt; +-&gt; /+-&gt; +-&gt; +-&gt; /+-&gt; +-&gt; /+-&gt; /12.2 For each even numbered node i, i can be written as j 2k for the largest possible integer k . For example
UNF - MAP - 2302
77update[i] = x;/ Keep track of end at level iif (!KEcomp.eq(K, x-&gt;forward[0]-&gt;value)return;/ Value not in listx = forward[0];/ Pointing at node to deletefor (i=0; i&lt;=x-&gt;level; i+) / Fix up the pointersupdate[i]-&gt;forward[i] = x-&gt;forward[i];Elem
UNF - MAP - 2302
78Chap. 12 Lists and Arrays Revisited12.8 void SparseMatrix insert(int r, int c, int val) cfw_for (SMhead* crow = row;(crow-&gt;index &lt;= r) &amp; (crow-&gt;next != NULL);crow = crow-&gt;next); / First, find the rowif (crow-&gt;index != r) / Make a new rowcrow-&gt;ne
UNF - MAP - 2302
79tempc-&gt;nextrow = temp;12.9 void SparseMatrix remove(int r, int c) cfw_/ First, find the rowfor (SMhead* crow = row;(crow-&gt;index &lt;= r) &amp; (crow-&gt;next != NULL);crow = crow-&gt;next);if (crow-&gt;index != r) ERROR; / Not in array/ Now, find the columnf
UNF - MAP - 2302
80Chap. 12 Lists and Arrays Revisited/ For each element, switch row and column pointers,/ and its row/column indices.for (each row)for (each element in the row) cfw_swap(nextrow, nextcol);swap(prevrow, prevcol);swap(row, col);swap(row, col); / S
UNF - MAP - 2302
81/ Insert element from second matrixthis.insert(curr2-&gt;value, curr2-&gt;row,curr2-&gt;col);curr2 = curr2-&gt;nextcol;else cfw_ / This element in both matricesthis.insert(curr1-&gt;value + curr2-&gt;value,curr2-&gt;row, curr2-&gt;col);curr1 = curr1-&gt;nextcol;curr2 =
UNF - MAP - 2302
13Advanced Tree Structures0 /\ 1- -0/\1\1-\\|||0/ \ 10/\1//\/\0/|314250|0/ \1//\0/7||0/ \10/ \1/\/\|1299 1000/ \1/\101113.20/\-/\14/\/\-\/\2299100/\/\/\/\13.182
UNF - MAP - 2302
833/\/314950\74/\/5/\\12/\101113.3 Binary Trie insertion routine. Assume that the Trie class has a root pointernamed root and a member named level to tell how many bits are in thekey.void Trie:insert(int value) cfw_TrieNode* temp =
UNF - MAP - 2302
84Chap. 13 Advanced Tree Structurestemp = temp-&gt;left;currlev-;13.4 void Trie:removehelp(TrieNode*&amp; rt, int val,int level) cfw_if (rt = NULL) cout &lt; val&lt; &quot; is not in the tree.\n&quot;;else if (rt-&gt;value = val) cfw_TrieNode* temp = rt;rt = NULLdele
UNF - MAP - 2302
8518/\1789/\2590\\72 99/\42 7513.7A(20, 90)\\B(95, 85)//C(98, 35)\\D(117, 52)//E(110, 25)13.8 0127-0||C |||||||A|-+-|||D |||||||-+-||B |E |||||||-+-|F|||||||||-13.6
UNF - MAP - 2302
UNF - MAP - 2302
87/\/\A/\/\/\B/\/\/\E/\/\CD13.11A-||||CBD-||||EF13.10
UNF - MAP - 2302
14Analysis Techniques14.1 Guess that the solution is of the forman3 + bn2 + cn + d.Since when n = 0 the summation is 0, we know that d = 0. We have thefollowing simultaneous equations available:a+b+c = 18a + 4b + 2c = 527a + 9b + 3c = 14Solving t
UNF - MAP - 2302
8914.3 From Equation 2.2 we know thatni2 =i=12n3 + 3n2 + n.6Thus, when summing the range a i b, we getb2b3 + 3b2 + b 2a3 + 3a2 + a66i2 =i=a2(b3 a3 ) + 3(b2 a2 ) + (b a).6=14.4 We need to do some rearranging of the summation to get somet
UNF - MAP - 2302
90Chap. 14 Analysis Techniques14.5F (n) = 2 + 4 + + 2n2F (n) = 4 + 8 + + 2n+1When we subtract, we get 2F (n) F (n) = F (n) = 2n+1 2. Thus,n2i = 2n+1 2.i=114.6 Call our summation G(n), thenni2ni = 2n1 + 2 2n2 + 3 2n3 + + n 20 .G(n) =i=1n2G(n
UNF - MAP - 2302
91n log n, so we will guess thatT(n) = ( n log n.To complete the proof we must show that T(n) is in ( n log n), but thisturns out to be impossible.On the other hand, the recurrence is clearly n. So, lets guess that T(n)is in O( n) for c = 4, n = 2.
UNF - MAP - 2302
92Chap. 14 Analysis TechniquesB elements are repeatedly reinserted one fewer times, the next 2B elements2 fewer times, etc. Thus, once we insert 2i B elements, we have done a totalnumber of insertions costing 2i B + 2i1 B + 2i2 2B + + 2i1 B . Thiswor
UNF - MAP - 2302
93(b) Fill an array with 2n + 1 elements, which forces a nal growth to anarray of size 2n+1 . Now, do an arbitrarily long series of alternatinginserts and deletes. This will cause the array to repeatedly shrink andgrow, for bad (n2 ) performance.(c)
UNF - MAP - 2302
15Limits to Computation15.1 This reduction provides an upper bound of O(n log n) for the problem ofmaximum nding (that is the time for the entire process), and a lower boundof constant time for SORTING (since that is the time spent by MaximumFinding
UNF - MAP - 2302
95(b) It is possible to compute xn in log n time, and the rest of the formularequires a constant number of multiplications. Thus, the number ofmultiplications required is polynomial on the input size.15.5 First, we should note that, for both problems,
UNF - MAP - 2302
96Chap. 15 Limits to Computation15.8 Represent a real number in a bin as an innite column of binary digits, similar to the representation of functions in Figure 15.4. Now we can use a simplediagonalization proof. Assume that some assignment of real num