Intro to Comp Science Ch3

Ace your homework & exams now with Quizwiz!

By formal definition, an algorithm must give a result and then halt, but what kind of result should it give?

We demand that an algorithm must give a correct result. An elegant and efficient algorithm that produces wrong results for the problem at than is worse than useless.

Give an equation for the number of comparisons in the pattern matching algorithm

n-m+1 n number of items in the list m is the number of letters in pattern being looked for

If you were to run the sequential search algorithm many times with random NAMEs, and counted the number of comparisons done each time, you find the average to be what?

n/2 when NAME occurs halfway done the list then roughly n/2 comparisons are required; random NAMEs in the list occur before the halfway point about half the time and after the halfway point about half the time, and these cases of less work and more work balance out.

Which is better in terms of time efficiency? order of n^2 or 2^n

n^2

Show what happens to the data set 5,7,2,8,3 in the selection sort algorithm.

original 5 7 2 8 3 / 1st pass 5 7 2 3 / 8 (left of / is unordered right is ordered) 2nd pass 5 3 2 / 7 8 3rd pass 2 3 / 5 7 8 4th pass 2 / 3 5 7 8 5th pass 2 3 5 7 8

Analyze the space efficiency of the selection sort algorithm.

the original list occupies n memory locations, this is the major space requirement. some storage is needed for the marker between the unsorted and sorted sections as well as for the largest so far value and its location in the list. Also the process of exchanges values requires an additional storage location here's why: 1) Copy the current value at position X into location T 2) Copy the current value at position Y into position X 3) Copy the current value at location T into position Y T is needed to save the existing value of X Overall the extra storage compared to the size of the original list is slight so it is considered space efficient.

What was sacrificed in the binary search algorithm?

Generality because the values have to be in a sorted list

If you totaled the number of comparisons in the selection sort algorithm, you would get this equation. (hint: Gauss)

((n-1)/2)n=1/2n^2-1/2n say there were 5 numbers in the data list (5-1)/2)n=1/2(5)^5-1/2(5)=10 comparisons

When NAME occurs somewhere in the middle of the list, it requires between... (hint: comparisons)

1 (best case) and n (worst case) comparisons

What is the pseudocode for binary search?

1) Get the values for NAME, n, N1, ....Nn and T1...Tn 2) Set the value of beginning 1 and set the value of Found to NO 3) Set the value of end to n 4) While Found = NO and beginning is less than or equal to end do steps 5 through 10 5) Set the value of m to the middle value between beginning and end 6) If Name is equal to Nm, the name is found at the midpoint between beginning and end, then do steps 7 and 8 7) Print the telephone number of that person, Tm 8) Set the value of found to YES 9) Else if name precedes Nm alphabetically, then set end = m-1 10) Else (NAME follows Nm alphabetically) set beginning = m+1 11) If (Found=NO) then print the message 'I am sorry but that name is not in the directory' 12) Stop *understand how this works

What is the pseudocode for the sequential search algorithm?

1) Get the values for NAME, n, N1....Nn and T1.... Tn 2) Set the value i to 1 and set the value of FOUND to NO 3) While (Found=NO) and (i< or equal to n) do steps 4 through 7. 4) If NAME is =equal to the ith name on the list Ni, then 5) Print the telephone number of that person, Ti 6) Set the value FOUND to YES 7) Add 1 to the value of i 8 If (FOUND=NO) then 9) Print the message "Sorry, this name is not in the directory" 10) Stop

Write a pseudocode for the flipping pancakes problem where the pancakes must be stacked bottom to top largest to smallest.

1) Get value for PANCAKES 2)While Value of PANCAKES is greater than 0 do steps 3 through 6 3) Insert spatula under the largest pancake in unsorted section 4) Put the spatula at the bottom of the unordered section and flip. 5) Put spatula under bottom of unordered section and flip. 6) Subtract 1 from PANCAKES 7) Stop

What is the pseudocode for the converging pointers algorithm?

1) Get values for n and n data items 2) Set the value of legit to n 3) Set the value of left to 1 4) Set the value of right to n 5) While left is less than right do steps 6 though 10 6) If the item at position left is not 0 then increase left by 1 7) Else (the item at position left is 0) do steps 8 through 10 8) Reduce legit by 1 9) Copy the item at position right to position left 10) Reduce right by 1 11) If the item at position left is 0, then reduce legit by 1 12) Stop *understand how this works

What is the pseudocode for the shuffle left algorithm?

1) Get values for n and the n data items 2) Set the value of legit to n 3) Set the value of left to 1 4) Set the value of right to 2 5) While left is less than or equal to legit do steps 6 through 14 6) If the item at position left is not 0 then do steps 7 & 8 7) Increase left by 1 8) Increase right by 1 9) Else (the item at position left is 0) do steps 10 through 14 10) Reduce legit by 1 11) While right is less than or equal to n do steps 12 and 13. 12) Copy the item at position right into position (right-1) 13) Increase right by 1 14) Set the value of right to (left +1) 15) Stop *understand what this looks like when it passes through a data set

What is the pseudocode for the copy-over algorithm?

1) Get values for n and the n data items 2) Set the value of left to 1 3) Set the value of newposition to 1 4) While left is less than or equal to n do steps 5 through 8 5) If the item at position left is not 0 then do steps 6 & 7 6) Copy the item at position left into newposition in new list 7) Increase newposition by 1 8) Increase left by 1 9) Stop *understand how this works

What is the pseudocode for the selection sort algorithm

1) Get values for n and the n list items 2) Set the marker for the unsorted section at the end of the list 3) While the unsorted section of the list is not empty, do steps 4 through 6 4) Select the Largest number in the unsorted section of the list 5) Exchange the number with the last number in the unsorted section of the list 6) Move the marker for the unsorted section left one position 7) Stop

How did Carl Frederick Grauss add up numbers 1 to 100? Is it efficient? Is it understandable?

1+100=101 2+99=101 3+98=101... 50+51=101 so 50 x 101 = 5,050 or (100/2)101 It was more efficient than doing 1+2+3+4+5.. etc. But how he came up with adding the numbers might not be understandable to some.

How many transistors does an Intel Core i7 980 XE contain?

1.17 billion

The next super computer to be produced by IBM is suppose to produce how many flops?

10 petaflops

If all size cores in an Intel i7 core were put to use, what is its top performance in flops?

100 gigaflops

How does one determine the amount of work the selection sort algorithm does?

1st you must decide what units of work to count. (comparison is in step 4 largest so far algorithm implied) the amount of comparisons decreases at the unsorted section grows smaller (n-1, n-2, n-3...)

If n is 20,000,000 and you run the sequential search algorithm on a computer that could do 50,000 comparisons per second, how many seconds (on average) would it take?

20,000,000/2 comparisons x 1/50,000 seconds/comparison= 200 seconds

The next chip produced by Intel will produce (per core) how many flops?

32 gigaflops per core!

In 2011, the top-speed super computer's performance rate in flops was?

8.2 petaflops

Draw a tree diagram to show the possible hamiltonian circuits in this graph. A-C B-D

A C B A D A D C B C B B C C B imagine another set here... Then you can see A B D C A and A C D B A

Define a Hamiltonian Circuit

A path through a graph that begins and ends at the same node and goes through all other nodes exactly once. If n nodes exists in a graph then the circuit if it exists must go through n links

What does a selection sort algorithm do?

A sorting algorithm that keeps moving larger items toward the back of the list

Should an algorithm provide correct results for some of values that will occur most likely? Or for all values?

All values

Describe what a polynomial bounded means.

An algorithm that does less work than some polynomial expression of the input size n ex: order of n order of n^2 order of lg n

Explain why the bin problem is considered intractable.

An algorithm would have to go through all the possibilities for packing in the maximum value for the bins making it intractable.

For a very small value of n, would you pick an order n^2 or order n? Why or why not.

An order n^2 because for very small values of n it is more efficient in terms of time and resources used. At some point, the order of n will be more efficient though, but this is usually for larger values.

What is the study of the efficiency & correctness of algorithms?

Analysis of algorithms

How do computer scientists summarize the two views of correctness? (correctness/right problem)

Are we solving the right problem?(Are you producing an algorithm that address the correct problem) Are we solving the problem right? (Is it producing correct results?)

Analyze the time efficiency of the converging pointers algorithm.

Best case is a list containing no zeros. worst case is a list containing all zeros. This algorithm repeatedly copies the element at position right into the first position, each time reducing the value of right. Right goes from n to 1, with one copy done at each step, resulting in n-1 copies. This algorithm is an order of n in the worst case.

What is the best case and worst case for the sequential search algorithm?

Best case is if NAME is the first item on the list. Worst case is if NAME is the last item on the list or if the name is not on the list.

What is the time efficiency of this algorithm?

Best case is when NAME is found on the first try. The worst is if NAME is not on the list. Every time this algorithm cuts the list in half it looks like 16/2=8 8/2=4 4/2=2 etc. the number of times n can be cut in half and not go below 1 is called a order of lg n to the base of 2. This is equivalent to 2^m=n In the worst case binary search is a order of lg n algorithm

Analyze the time efficiency of the copy-over algorithm.

Best case occurs if all elements are 0; no copies are done so the work is just order of n. Worst case is if all copies are not 0; then all values are copied over once, also an order n.

What algorithm is more efficient than sequential search? What is it constrained by?

Binary search is more efficient, but the list must be sorted first.

Algorithm's need these 4 essential attributes to be 'desirable.'

Correctness, ease of understanding, efficiency (time/space) and elegance.

If the world's super computer ran a order of n^2 value while a desktop ran a order of n for a huge input value, which would be done first?

Desktop For small values of n the supercomputer has the advantage, but n^2 will eventually become useless as the values get to big

Describe an algorithm's careful use of resources.

Efficiency (time and space)

What is the algorithmic equivalent of style called?

Elegance

Analyze the space efficiency of the copy-over algorithm.

Every non-zero is copied over once into the new list. This is fewer copies than the shuffle-left algorithm, but more space is required because memory to store a second list is needed.

Looking at the hamiltonian tree, the number of nodes that must be examined are...

Examining the bottom row, the number of nodes is equal to how many 'values' are on the bottom row OR 2^n where n represents the level on the tree. level four has 2^4 or 16 nodes

Along with comparisons, the selection sort algorithm also does this.

Exchanges This algorithm does n exchanges, one for each position in the list to put the correct value in position. However the amount of work contributed by exchanges is much less than the amount contributed by comparisons so it can be ignored.

What order of magnitude is the Hamiltonian Circuit?

Exponential algorithm or order of 2^n

What does order of magnitude determine?

How quickly the values grow as n increases

Compare binary and the sequential search algorithm.

In a 100 element list, the sequential search algorithm would do 100 comparisons (order n) while binary would only require 7 (2^7=128)

Compare the shuffle-left, copy-over, and converging pointers algorithms.

In terms of space shuffle-left was better than copy over, but time wise the copy-over algorithm dominated. The converging pointers was the best overall because it has time and space efficiency.

How can space efficiency be judged?

It can be judged by the amount of information the algorithm must store in the computer's memory to do it's job, in addition to the initial data on which the algorithm is operating.

What is the space efficiency of the binary search algorithm?

It requires storage of the original list, and storage to keep track of beginning middle and end.

Is the constant important when comparing different orders of magnitude?

No because regardless of the constant, an order of magnitude n^2 vs order of magnitude n n^2 will for a short time will be faster than n, but eventually with bigger inputs the time will be way to long compared to n.

Do we know what all the correct results are to produce the algorithm?

No, that's the point of creating the algorithm. But there may be a certain standard against which we can check the result for reasonableness, thus giving us a way to determine when a result is obviously incorrect.

Is the amount of work an algorithm does the sane as the number of instructions it executes?

Not all instructions do the same things, so they should not be counted equal. Some instructions are peripheral and others are fundamental.

Is the shuffle left algorithm space efficient?

Only requires 4 memory locations to store the quantities n, legit, left, and right in addition to the original list

Analyze the space efficiency of the converging pointers algorithm.

Only requires 4 memory locations to store the quantities n, legit, left, and right in addition to the original list. The same as the shuffle-left.

Define Flops (floating point operations)

Operations that compare the amount of algorithms (like add or sub) a computer can crank out per second.

What order of magnitude is the selection sort algorithm?

Order n^2 because it does not grow at the same rate as the problem size; it grows at a square rate (look at how comparisons grow)

What is the different between an order n magnitude and an order n^2 magnitude?

Order of n grows at the same rate as the problem. Order of n^2 grows at a squared rate. Comparing the two, an order n^2 will start off with a shorter completion time than order n, but eventually n^2 has larger values than anything that is order of n regardless of the constant value.

What is the equation for the flipping pancakes problem? (Most flips required)

Original 2(n-2)+1=2n-3 Gates (5n+5)/3

What is benchmarking?

Running a program on many data sets to be sure its performance falls within required limits; timing the same algorithm on two different machines

Name peripheral tasks found in the sequential search algorithm.

Setting the initial value of index i and the initial value of Found, writing the output, adjusting Found, and moving the index forward in the list of names.

How do we get a good idea of how much work the sequential search algorithm does?

Simply counting the number of comparisons and then multiplying by some constant factor to account for the index-moving task.

Part of the pseudocode from the selection sort algorithm states "Select the Largest number in the unsorted section of the list." so how does the algorithm select the largest number?

Step 4 is understood that it would eventually be replaced with an algorithm for selecting the largest number from previous algorithms.(largest so far algorithm ch 2) New algorithms can be built up from 'parts' consisting of previous algorithms.

Analyze the time efficiency of the shuffle left algorithm.

The algorithm examines each of n elements to determine if they are 0. The best case is when there are no 0 because no copying is required. This algorithm also copies numbers. The worst case is if all values are 0 then there are n passes during each of which n-1 copies are done. Therefore: n(n-1)^2 = n^2-n Because one of the n terms is n^2 you can determine it is an order of n^2 algorithm in the worst case.

Once an algorithm meets the criteria of correctness and it is solving the right problem, what more could one do to the algorithm to improve it?

The algorithm needs to be useable. It needs to solve multiple instances of the problem, it needs to be maintained if it becomes popular, and it needs to be unambiguous for other programmers to work on.

What parts of the sequential search algorithm take up space?

The algorithm stores the list of names (input) the target name (input) only additional memory required is storage for the index value i and the FOUND indicator. these take up insignificant storage compared to the list of names, therefore it is space efficient.

Describe Time Efficiency.

The amount of work required by the algorithm itself. It is a measure of essential efficiency of the method, independent of the speed of the machine on which it executes or the specific input data being processed.

What is the pattern matching algorithm time efficiency? (pseudocode found in ch2)

The best and worst case are when there is no match within the text at all. The difference hinges between exactly how the pattern fails to be in the text for examples if you're looking for ABA Best case: CCCCC Worst Case: AAAAAAAA In the best case this is a order of n In the worst case this is a order of m*n

What is the central unit of work in a sequential search algorithm?

The comparison of the NAME being searched for against a name in the list. Step 4 through step 7.

Describe a order of magnitude n

The efficiency classification of an algorithm whose work varies as a constant times the input size n; anything that varies at a constant time n (and whose graph follows the basic shape of n).

In today's technology, computers have much more memory and run much faster than a few years ago so efficiency in algorithms seems to be a moot point. Why not wait for the next generation of technology? Explain.

There is some truth to this, but as computer memory and speed increase, the problems found by people become more and more complex so the boundaries of computer resources continue to be pushed. Also, there are algorithms that consume so much time and space they aren't even practical to use. No matter how much time and space you have.

An algorithm may produce a correct result, but it may not be for the right problem. When does this occur?

This can happen when we design an algorithm without a thorough understanding of the real problem we are trying to solve, and is one of the most common causes of incorrect algorithms.

True or False. An important part of computer science is finding algorithms to solve problems.

True

True or False. An order of n will do less work than an order of n^2 algorithm for large enough n, regardless of constant factors or peripheral work.

True

True or False. Few algorithms do the same amount of work on large inputs as on small inputs, simply because most algorithms process data, and more data to process means more work.

True

True or False. If a list becomes 10 times longer, the work increases by much more of a factor of 10; it increases by a factor closer to 100, which is 10^2

True

True or False. This algorithm has no way to recognize that the list might already be sorted

True

True or False. Varying the constants in a order of magnitude can make modest changes, while changing to a different order is like a quantum leap.

True

True or False. Making assumptions about the size of the input on which an algorithm will run can be dangerous.

True A program that runs quickly on small input size may at some point be selected (usually because it seems efficient)to solve instances of the problem with large input size.

When an algorithm is more elegant, what does it usually lack?

Understanding

Explain the issue of accuracy in an result produced from an algorithm.

When a number that is not real is produced from an algorithm, like pi, the computing agent can only give an approximate value usually in decimal form, but how close is close enough for correct?

Why are time and space limited resources?

When an algorithm runs, it takes up time and space. Time isn't necessarily limited, but think of it in the sense that you do not want your algorithm running for days or weeks or years. You want a quick result, therefore time is limited in that sense. Space is limited because computers only come with a certain amount of memory and you do not want a single algorithm to take up all of that space. Algorithms needs to be efficient in terms of time and space.

Is the selection sort algorithm an order of n^2 in all cases?

Yes

Is it an acceptable result if the algorithm produces an error message?

Yes because the correct result may be (for certain values) an error message saying that there is no correct answer.

How do you measure time efficiency?

You identify the fundamental unit (or units) of work of an algorithm and count how many times the work unit is executed.

What is an approximation algorithm?

algorithms that do not solve a problem, but provide a close approximation. for example, the bin packing problem you would take the first object, then try and stuff the remaining object into the first bin that can hold it. It may not give an absolute minimum number of bins needed, but it gives a first cut answer.

A Hamiltonian Circuit is an example of what type of algorithm?

brute force algorithm- one that beats the problem into submission by trying all possibilities

If n is the number of comparisons and c is a constant factor representing the peripheral work it requires how much total work?

cn

An algorithm in which no polynomial bounded algorithms exist.

the problem is called intractable; solvable but the algorithms require so much work at to be virtually useless. The hamiltonian circuit is such a problem, but no one has proved that such an algorithm does not exist to solve it!


Related study sets

AP Gov Chapter 9 - Interest Groups

View Set

Chapter 33: The Preschooler and Family

View Set

Respiratory Practice test NUR212

View Set

Strategic Management Capstone Chapter 5

View Set

Rome republic to empire~ chapter 11 Lesson 3~ Social Studies~ period 2

View Set