Instead, our analysis will determine an equation that relates the number of operations a particular algorithm performs to the size of the input. By combining this information with the number of smaller pieces and their sizes, we can produce a repetition ratio for the algorithm.
WHAT IS ANALYSIS?
In terms of analysis, the initialization cost becomes insignificant as the number of input values increases. Write an algorithm that takes three different integers and determines the largest of the three.
WHAT TO COUNT AND CONSIDER
Write an algorithm in pseudocode to calculate the average of the numbers stored in this file. As the name indicates, the best case for an algorithm is the input that requires the algorithm to take the shortest time.
MATHEMATICAL BACKGROUND
EXERCISES
If two of these dice are rolled, what is the range of possible sums of the values shown on the two dice. What is the chance that each number in that range will fall.
EXERCISES
Suppose we have a fair five-sided die with the numbers 1 to 5 on its sides. For each of the four cases below, what is the chance that the majority of the coins will be tails when you are done.
RATES OF GROWTH
EXERCISES
DIVIDE AND CONQUER ALGORITHMS
We have identified all the elements in the factorial algorithm with respect to the generic DivideAndConquer. This means that L, the minimum depth of a decision tree for classification problems, is of order O(N lg N).
RECURRENCE RELATIONS
EXERCISES
ANALYZING PROGRAMS
You need to do the same with 7 and 15 element ordered list and binary search. It may be that we are looking for the fifth largest value, the seventh smallest value, or the middle value in the list.
SEQUENTIAL SEARCH
If the target is in the list, there are N places where that target can be found. As we have seen, the case where the target is not in the list will take N comparisons.
BINARY SEARCH
EXERCISES
For your answer, you can assume that each node has two arrays called Keys[3] and Links[4] and that you have a function called Compare(KeyList, SearchKey) that returns a positive integer that indicates the matching key or a negative inte - ger indicates the connection to retrieve. For example, Compare would return a value of 2 because the second key matches, and Compare would return a value of –3 because the 7 would be found in the third link associated with the gap between the second and third key values. ) When you have completed your algorithm, do a worst-case and average analysis assuming that the tree is complete and each internal node has four children. You may want to draw a sample tree.) What would be the impact on your two analyzes if the tree were not complete or if some internal nodes had fewer than four children.
SELECTION
On each pass, we initialize largest to the first element in the list, and then compare largest to every other element. If we select an item in the list, we can split the list into two parts - the values greater than the selected one and those less than it. To perform this partitioning, we need to compare this value with all the others and make N 1 comparisons.
PROGRAMMING EXERCISE
If we assume that on average the partitioning process will split the list into two roughly equal halves, we will do about N + N/2 + N/4 + N/8. A based sort is a multipass sort that divides the list into buckets, each pass using a different part of the key. This is repeated until all elements are returned in the now sorted list.
INSERTION SORT
When we look at the inner while loop, we see that the biggest thing this loop will do is if the new element to be added is smaller than all the elements already in the sorted part of the list. The first element to be inserted is the one in the second place of the list. This must now be summed over each of the elements 1 through N 1 it takes.
BUBBLE SORT
EXERCISES
Write a short paragraph explaining exactly why this third version of bubble type will work. Develop a formal argument that proves that the largest element must be in the correct location after the first pass of the BubbleSort loop. Develop a formal argument that proves that if no swaps are made on any pass of BubbleSort, the list must now be in the correct order.
SHELLSORT
EXERCISES
RADIX SORT
EXERCISES
If the key is a number in the range 0 to 264, select two options (one smaller and one larger) for the number of bits to be used in each pass and indicate how many buckets and passes will be needed. If the key is a 40-character string, select two options (one smaller and one larger) for the number of bits to use in each pass, and indicate how many buckets and passes will be needed.
HEAPSORT
Because this routine is not recursive, the root of the heap must always be location 1. We pass in the size of the heap because as the elements of the heap are moved to the list, the heap shrinks. The heap construction phase is therefore linear with respect to the number of elements in the list.
MERGE SORT
EXERCISES
QUICKSORT
It then moves through the list and compares that spin element to the rest of the elements. The largest difference in the size of these two lists occurs when the PivotValue is smaller (or larger) than all other values in the list. Suppose the spin value is greater than all the values in the list.
EXTERNAL POLYPHASE MERGE SORT
There are R runs, giving O(R*S* lg S) = O(N lg S) comparisons in total for the construction of all the runs. Reading large blocks of data is significantly faster than reading each of the elements in the block one after the other. Each time in the merge step must read all the data as part of one run, which means there will be N / (S / 2) = 2R block reads.
ADDITIONAL EXERCISES
EXERCISES
In the run construction step, we'll read one block of data for each run, resulting in a read of block R. In a count sort, we'd compare the first key in the list to all other elements (including itself) and count the number of values that are less than or equal to to this key. When a sorting algorithm is applied to a list of values, we are sometimes interested in what happens if there are duplicate entries in the list.
PROGRAMMING EXERCISES
How do your results relate to the analysis done in this chapter and as part of the exercises. Insertion sort—displays the list at the end of each pass of the outer loop. Bubble sort—display the list at the end of each pass of the outer loop c.
CALCULATING POLYNOMIALS
EXERCISES
MATRIX MULTIPLICATION
This means that each of the elements can be a matrix and, therefore, this method can be applied recursively. Strassen's algorithm is rarely used in actual practice because of the bookkeeping required to use it recursively. How many multiplications and additions are made by Winograd's algorithm for an odd value of the common dimension.
LINEAR EQUATIONS
EXERCISES
Now we would know the value for x4 and could substitute it into one of the two equations in the previous step, which would allow us to solve for the value of x3. The basic plan is to divide the first row by the value in the first column and then subtract multiples of this new first row from each of the other rows. From the description of Section 4.2.1, perform an analysis of the Gauss-Jordan method for solving a system of N linear equations with N unknowns.
EXERCISES
The problem with this process is that on a computer we will get rounding errors which can give inaccurate results. Rounding errors can multiply within a computer program so that a slight rounding difference in one calculation will cause the next to be more inaccurate than the last. As you work through the chapter, rework the examples to make sure you understand them.
STRING MATCHING
EXERCISES
Calculate the slide array values for the Boyer-Moore algorithm for the patterns below.
APPROXIMATE STRING MATCHING
If we use the above value, we are implying that the text is missing a character of the substring. Using the value on the left implies that the substring is missing a character from the text. So the bottom row gives us the best possible matches of the substring ending at that point in the text.
PROGRAMMING EXERCISES
EXERCISES
Don't forget to include the equations made in the calculation of the shift and jump arrays. A spanning tree is a connected subset of a graph that has no cycles and contains all graph nodes and a subset of the edges. A minimal spanning tree is a spanning tree where the sum of the weights for the included edges has the smallest possible total.
GRAPH BACKGROUND AND TERMINOLOGY
EXERCISES
DATA STRUCTURE METHODS FOR GRAPHS
EXERCISES
DEPTH-FIRST AND BREADTH-FIRST TRAVERSAL ALGORITHMS
EXERCISES
MINIMUM SPANNING TREE ALGORITHM
EXERCISES
Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the MST by the Dijkstra-Prim algorithm is the same as the order in which they are encountered in a width-first traversal. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the MST by the Dijkstra-Prim algorithm is the same as the order in which they are encountered in a depth-first traversal.
SHORTEST-PATH ALGORITHM
EXERCISES
Perform an analysis of the shortest path algorithm and count the number of times an edge is checked for nodes added to the edge, to update edges to the edge nodes, or to select the node to move from the edge to the minimum spanning tree. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the shortest-path tree is the same as the order in which they are encountered in a breadth-first traversal. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the shortest-path tree is the same as the order in which they are encountered in a depth-first traversal.
BICONNECTED COMPONENT ALGORITHM
EXERCISES
PARTITIONING SETS
The FindRoot routine begins at Parent[s], which has the location of the parent of element s. However, if s is not a root, we update the result to be the parent of s and check to see if it is the root of the partition. An efficiency improvement not included here will then retrace this path and update all entries along it to point directly to the root.
PROGRAMMING EXERCISES
There are pipeline systems, where each processor does only one step of the task of decoding and executing a program instruction, passing the results to another processor, which does the next step. Data flow systems create a series of processors to perform a task or computation, and then input data is passed from processor to processor to compute the result. We begin with an overview of some of the general concepts related to the structure of parallel computer systems, and then look at parallel algorithms for some of the problems we considered in Chapters 2 through 6.
PARALLELISM INTRODUCTION
So in one operation each of the processors does its division and we have the result. In a loosely coupled machine, each of the processors has its own memory, and communication between processors takes place across "network" cables. This is the architecture of computer clusters, where each element of the cluster is a com-.
THE PRAM MODEL
EXERCISES