EXAM 1 Flashcards

HW questions and topics chapter 1-6

1
Q

Door in a wall You are facing a wall that stretches infinitely in both direc-
tions. There is a door in the wall, but you know neither how far away nor in
which direction. You can see the door only when you are right next to it. De-
sign an algorithm that enables you to reach the door by walking at most O(n)
steps where n is the (unknown to you) number of steps between your initial
position and the door. [Par95]

A

Let n be the number of steps from your starting position to the door. Then in order to reach the door in at most n steps, then we can walk “i” steps in each direction, doubling i each time we walk in a new direction.

Let k be the smallest integer such that 2^k >= n

in each round i, with step length d_i = 2^i for (i= 1, 2, …k-1), you walk d_i steps in one direction, then walk d_i steps back to the starting point, giving us 2d_i total steps for this round.

Total steps in rounds before finding the door:
total_before_round <= 2(1 + 2 + 4 +…+ 2^k-1) = 2(2^k-1) =

In the last round you walk out 2^k steps till you find the door so the total steps are:

total steps <= 2^k + 2(2^k - 1) = 3(2^k) - 2.

Since 2^k is at most 2n (because 2^(k-1) < n < 2^k)

The total number of steps is at most:
3(2n) -2 = 6n-2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Gadget testing A firm wants to determine the highest floor of its n-story
headquarters from which a gadget can fall without breaking. The firm has two
identical gadgets to experiment with. If one of them gets broken, it cannot be
repaired, and the experiment will have to be completed with the remaining
gadget. Design an algorithm in the best efficiency class you can to solve this
problem.

A

Section 3.2: Sequential Search and Brute-Force String Matching

  • Skipping phase
  • Sequential phas

We solve this problem wiht 2 phases:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Odd pie fight There are n ≥ 3 people positioned on a field (Euclidean plane)
so that each has a unique nearest neighbor. Each person has a cream pie. At a
signal, everybody hurls his or her pie at the nearest neighbor. Assuming that
n is odd and that nobody can miss his or her target, true or false: There always
remains at least one person not hit by a pie

A

represent the situation by constructing a directed graph where each person is a vertex and each vertex has a single outgoing edge pointing to its unique nearest neighbor. This gives exactly
𝑛
n edges (one per person). Now, suppose for contradiction that every person is hit by a pie—this means every vertex has at least one incoming edge. Since there are
𝑛
n edges total, every vertex would then have exactly one incoming edge. In such a case, the graph decomposes into a collection of disjoint cycles.

A key observation is that, under the condition of unique nearest neighbors in the Euclidean plane, the only possible cycle is a 2-cycle (i.e., two people being each other’s nearest neighbor). But if every cycle is a 2-cycle, then the total number of vertices (people) must be even, as each 2-cycle accounts for exactly 2 people. This contradicts the assumption that
𝑛
n is odd.

Thus, our initial supposition (that every person is hit) must be false. There must be at least one person with indegree 0, meaning at least one person is not hit by any pie

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Outline an algorithm to determine whether a connected graph represented
by its adjacency matrix has an Eulerian circuit. What is the efficiency class of
your algorithm?

A

Section 3.4 Brute Force and Exhaustive Search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Consider the partition problem: given n positive integers, partition them into
two disjoint subsets with the same sum of their elements. (Of course, the prob-
lem does not always have a solution.) Design an exhaustive-search algorithm
for this problem. Try to minimize the number of subsets the algorithm needs
to generate.

A

Section 3.4 Brute Force and Exhaustive Search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Consider the clique problem: given a graph G and a positive integer k, deter-
mine whether the graph contains a clique of size k, i.e., a complete subgraph
of k vertices. Design an exhaustive-search algorithm for this problem

A

Section 3.4 Brute Force and Exhaustive Search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Eight-queens problem Consider the classic puzzle of placing eight queens on
an 8 × 8 chessboard so that no two queens are in the same row or in the same
column or on the same diagonal. How many different positions are there so
that
a. no two queens are on the same square?
b. no two queens are in the same row?
c. no two queens are in the same row or in the same column?
Also estimate how long it would take to find all the solutions to the problem by
exhaustive search based on each of these approaches on a computer capable
of checking 10 billion positions per second.

A

Section 3.4 Brute Force and Exhaustive Search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If we define sparse graphs as graphs for which |E| ∈ O(|V |), which implemen-
tation of DFS will have a better time efficiency for such graphs, the one that
uses the adjacency matrix or the one that uses the adjacency lists?

A

Section 3.5 Depth-First Search and Breadth-First Search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Three Jugs Sim´ eon Denis Poisson (1781–1840), a famous French mathemati-
cian and physicist, is said to have become interested in mathematics after
encountering some version of the following old puzzle. Given an 8-pint jug
full of water and two empty jugs of 5- and 3-pint capacity, get exactly 4 pints
of water in one of the jugs by completely filling up and/or emptying jugs into
others. Solve this puzzle by using breadth-first search.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Apply insertion sort to sort the list E, X, A, M, P, L, E in alphabetical order.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Let A[0..n− 1] be an array of n sortable elements. (For simplicity, you may
assume that all the elements are distinct.) A pair (A[i], A[j]) is called an
inversion if i < j and A[i] > A[j].
a. What arrays of size n have the largest number of inversions and what is this
number? Answer the same questions for the smallest number of inversions.
b. Show that the average-case number of key comparisons in insertion sort is
given by the formula
n2
12. Cavg(n) ≈
4.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Show that 3n + 5lg n is Ω(n). Is 3n + 5lgn in O(n)? Justify your answers.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Climbing stairs: Find the number of different ways to climb an n stair staircase if each step is either 1 stair or two stairs. For example, a 3-stair staircase can be climbed three ways: 1-1-1, 1-2, and 2-1. Give the complete recurrence relation. No need to solve the recurrence relation.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Prove that lg (n!) = Θ (n lg n).
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For a given positive integer n, give the formula for the number of bits required to represent n in binary. Then prove that the formula is correct.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

We discussed the code to find the maximum element of an array (from the book/ppt). Now assume that the basic operation is the update of maxval (assignment). On an average, what is the time efficiency of the number of times the basic operation is executed? (Note that this is one example of an algorithm where the best case, worst case and average number of times are all different, for the basic operation.) You may refer to any online material for this problem but again you may not copy the whole answer or even part of the answer. (Again, read and understand the explanation, close it and then write in your own words.) And always cite the source that you referred.

16
Q
  1. Recall the problem of toggling locked doors from Homework 1: Locker doors: There are n lockers in a hallway, numbered sequentially from 1 to n. Initially, all the locker doors are closed. You make n passes by the lockers, each time starting with locker #1. On the ith pass, i = 1, 2, …, n, you toggle the door of every ith locker: if the door is closed, you open it; if it is open, you close it. For eg, when i=3, we will toggle every third door. Determine the asymptotic order of growth for the total number of times all the doors are toggled in the locker doors puzzle in terms of n.
17
Q
  1. Write pseudocode for iterative binary search. Give the best case, worst case and average case analysis for your algorithm. For the average case analysis, you may refer to any source online. But you must cite the source and you may not copy whole or part from any source. (Just read the explanation, close it and then write in your own words.)
18
Q

(a) Write pseudocode for an iterative algorithm that returns the second highest element in an array of n integers. (b) Then use loop invariants to prove the correctness of your algorithm.

19
Q

Study the bubble sort algorithm and then write your own code for it. (Again, please cite your source and do not copy any part of the code.) What is the best case and worst-case time efficiency of the algorithm? If we only consider the number of swaps, what will the best case and worst-case time efficiency? Justify your answer.

20
Q

Define loop invariants for bubble sort

21
Q

Given an array A[0]….A[n-1] of n integers, give the pseudocode to turn this array into a max heap. (You may not use an extra array.) Then justify that turning an array into a heap by your algorithm, takes linear time in the worst case. What is be the best-case running time of your algorithm? Justify your answers.

22
Q

Frying hamburgers There are n hamburgers to be fried on a small grill that
can hold only two hamburgers at a time. Each hamburger must be fried
on both sides, frying one side of a hamburger takes 1 minute, regardless of
whether one or two hamburgers are on the grill. Consider the
following recursive algorithm for executing this task in the minimum amount
of time. If n ≤ 2, fry the hamburger or the two hamburgers together on each
side. If n > 2, fry any two hamburgers together on each side and then apply
the same procedure recursively to the remaining n − 2 hamburgers.
a. Set up and solve the recurrence for the time this algorithm needs
to fry n hamburgers.

23
Q

Outline an algorithm to print a topological sort of a digraph or detect that the graph has a cycle. What will be the time and space complexity of your algorithm?

24
Consider the partition problem: Given n positive integers, partition them into two disjoint subsets with the same sum of their elements. (Of course, the problem may not have a solution.) Design an exhaustive-search algorithm for this problem. You do not have to write pseudocode. You may refer to any source online. Again, you may not copy and always acknowledge your source. What will be the time and space complexity of your algorithm?
25
First a definition: A clique of a graph is a complete subgraph. (We will later discuss the maximum clique problem, which is to find the maximum size clique of the graph. For now, we will only talk about the Decision Clique.) Consider the Decision clique problem: given a graph G and a positive integer k, determine whether the graph contains a clique of size k, i.e., a complete subgraph of k vertices. Outline an exhaustive-search algorithm for this problem. No need to write pseudocode
26
A graph G of n vertices is said to be 2 colorable if each of its vertices can be colored in one of two colors so that no two adjacent vertices (those connected by an edge) have the same color. Design an efficient algorithm for the problem of checking whether a given graph is 2 colorable or not. Also give pseudocode. What is the time and space complexity of your algorithm?
27
A graph is 2-colorable if it is bipartite. We can use BFS to color the graph using two colors so that no two adjacent vertices share the same color. First we initialize an array where each vertex it initially uncolored. Then we perform BFS traversal and for every neighbor of the current vertex, we will assign the alternate color. If the neighbor is already colored then the graph is not bipartite.
28
Given a graph G = (V, E) in adjacency list representation, outline an algorithm to check whether the graph is complete or not. A graph is complete if and only if every vertex is connected to every other vertex. What will be the time and space complexity of your algorithm?
29
Given a directed acyclic graph (DAG), explain what a topological sort is. Then outline an algorithm for finding the topological sort of a DAG. What will be the time complexity of your algorithm?
30
For a given array A[0..n-1], we said that a pair (A[i], A[j]) is an inversion if i < j but A[i] > A[j]. Design a Θ(nlg n) algorithm to find the number of inversions in a given array A of size n.
31
Give pseudocode for a function that takes an array of n numbers and separate them so that all negative numbers come before any positive number. Assume there are no zeros. (All negative numbers should be on the left side of the array and all positive numbers should be on the right side of the array.) Your algorithm should run in linear time
32
We need to search for a given integer x in an n × n matrix of integers in which every row and every column is sorted in increasing order. Design an algorithm for this problem that runs in linear time (in n). Explain the algorithm and give justification for the time complexity
33
Give an informal proof for the Master Theorem. That is, for each of the 3 cases, assume that the premise is true and then explain why the conclusion must be true.
34
A stack of fake coins There are n stacks of n identical-looking coins. All of the coins in one of these stacks are counterfeit, while all the coins in the other stacks are genuine. Every genuine coin weighs 10 grams; every fake weighs 11 grams. You have an analytical scale that can determine the exact weight of any number of coins. a. Devise a brute-force algorithm to identify the stack with the fake coins and determine its worst-case efficiency class. b. What is the minimum number of weighings needed to identify the stack with the fake coins?
35
Page numbering Find the total number of decimal digits needed for num- bering pages in a book of 1000 pages. Assume that the pages are numbered consecutively starting with 1.
Section 2.3 Analysis of Non-recursive algorithms
36
Celebrity problem A celebrity among a group of n people is a person who knows nobody but is known by everybody else. The task is to identify a celebrity by only asking questions to people of the form “Do you know him/her?” Design an efficient algorithm to identify a celebrity or determine that the group has no such person. How many questions does your algorithm need in the worst case?
Section 2.4 Analysis of Recursive Algorithms.
37
Write and Solve the recurrence relation for the Fibonacci sequence:
38
Write and Solve the recurrence relation for the tower of hanoi problem:
The recurrence relation is: 2M(n-1) + 1 for n >1 with M(1) = 1 We get this by first moving the n-1 blocks to pole 2 which takes T(n-1) moves. Then we move the last block to pole 3. which takes 1 move. Then we move the n-1 blocks from pole 2 onto pole 3 which takes T(n-1) moves. The solution to this recurrence relation is