230 likes | 352 Views
Assignment 10 Sample problems. Generalized Manhattan. Consider a generalized form of Manhattan where n streets and n avenues form a square grid graph. That is, every block is a perfect square with the same area.
E N D
Consider a generalized form of Manhattan where n streets and n avenues form a square grid graph. That is, every block is a perfect square with the same area. For each of the following walking paths, choose the corresponding distanced walked in big O notation. (The length of one block is one unit.)
Start in the southwest corner. Walk north four blocks, east two blocks, south two blocks, west one block.
Start in the southwest corner. Walk north four blocks, east two blocks, south two blocks, west one block. The answer is O(1)
How can we get ? 4+2+2+1=9=O(1)
Start in the southwest corner. Walk diagonally to the northeast corner.
Start in the southwest corner. Walk diagonally to the northeast corner. The answer is O(n)
How can we get ? n+n=2n=O(n)
Start in the southwest corner. Repeat walk up to the north (n-1) street, then turn right to walk east one block, down to the southern-most street and left one block. Stop when the most right southeast avenue is reached.
Start in the southwest corner. Repeat walk up to the north (n-1) street, then turn right to walk east one block, down to the southern-most street and left one block. Stop when the most right southeast avenue is reached. The answer is O(n²)
How can we get ? n*(n-1)+n=n²=O(n²)
Consider the word “blueberry". Suppose we repeatedly pick a single letter at random from anywhere in the word. (Please enter any fractional answers in their decimal form.) • How many times, on average, do we pick before we get the letter “b"? • How many times, on average, do we pick before we get the letter “u"? • How many times, on average, do we pick before we get one of the letters in the word “beer"?
Consider the word “blueberry". Suppose we repeatedly pick a single letter at random from anywhere in the word. (Please enter any fractional answers in their decimal form.) • How many times, on average, do we pick before we get the letter “b"? The answer is 4.5 • How many times, on average, do we pick before we get the letter “u"? The answer is 9 • How many times, on average, do we pick before we get one of the letters in the word “beer"? The answer is 1.5
How can we get ? 1: p=2/9, so the answer is 1/p=4.5 2: p=1/9, so the answer is 1/p=9 3: p=2/9+2/9+2/9=6/9, so the answer is 1/p=9/6=1.5
Suppose you have a bag of 512 chocolates. You decide to give them to your friends by dividing the chocolates into half. For example, you give half of these chocolates to your best friend, then give another half of leftover chocolates to your another friend. Each time, you just give half of the leftover chocolates to one of your friend. How many times total can you divide like this before you only have one chocolate left?
Suppose you have a bag of 512 chocolates. You decide to give them to your friends by dividing the chocolates into half. For example, you give half of these chocolates to your best friend, then give another half of leftover chocolates to your another friend. Each time, you just give half of the leftover chocolates to one of your friend. How many times total can you divide like this before you only have one chocolate left? The answer is 9 2.^9=512
1) What is the minimum number of operations to find the biggest element in an unsorted list of size n? 2) What is the minimum number of operations required to find the biggest element of a sorted (ascending) list of length n?
1) What is the minimum number of operations to find the biggest element in an unsorted list of size n? The answer is n-1 2) What is the minimum number of operations required to find the biggest element of asorted(ascending) list of length n? The answer is 0
How can we get ? 1:for unsorted list, you need compare n-1 times to find the maximum number 2: for sorted list, you do not need compare them again, you can directly get the maximum number from the end of the sorted list