Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
135 views

Algorithm Exam

The document defines a heap as a nearly complete binary tree where the key of each node is less than or equal to its children, defines the traveling salesman problem as finding the shortest route to visit all cities and return to the starting point, and proves that the time complexity of any general sorting algorithm is Ω(nlogn) because an adversary can force the algorithm to make comparisons that cut the number of possible permutations in half with each step.

Uploaded by

Shah Zaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

Algorithm Exam

The document defines a heap as a nearly complete binary tree where the key of each node is less than or equal to its children, defines the traveling salesman problem as finding the shortest route to visit all cities and return to the starting point, and proves that the time complexity of any general sorting algorithm is Ω(nlogn) because an adversary can force the algorithm to make comparisons that cut the number of possible permutations in half with each step.

Uploaded by

Shah Zaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1. What is the definition of a heap?

Definition
A binary tree is called almost complete, if it is complete except the last
level, and on the last level if a node is in the tree, then all possible
nodes on its left side is in the tree, as well.

Definition
An almost complete tree is called a minimum (maximum) heap, if the
key (= the data in the node) of each node is less (greater) than or equal
to the keys of its children.

3. Describe the counting sort algorithm.

4. What is the main idea of radix sort?


Radix sort algoritm
6. Describe the knapsack problem.
7. Describe the problem of bin packing.
8. Give the pseudo code of the general backtracking algorithm.
9. What is a Hamiltonian circle?
10. What is the traveling salesman problem?

11. Prove that every general sorting algorithms have time complexity Ω( 𝑛 ⋅
log 𝑛).
OTHER WAY:
Proof:
Recall that the sorting algorithm must output a permutation of the input [a1,a2,... ,an].

The key to the argument is that


(a) there are n! different possible permutations the algorithm might output, and
(b) for each of these permutations, there exists an input for which that permutation is the only
correct answer. For instance, the permutation [a3,a1,a4,a2] is the only correct answer for
sorting the input [2, 4, 1, 3]. In fact, if you fix a set of n distinct elements, then there will be a
1-1 correspondence between the different orderings the elements might be in and the
permutations needed to sort them.
Given (a) and (b) above, this means we can fix some set of n! inputs (e.g., all orderings of {1,
2,... ,n}), one for each of the n! output permutations.
Let S be the set of these inputs that are consistent with the answers to all comparisons made
so far (so, initially, |S| = n!).
We can think of a new comparison as splitting S into two groups: those inputs for which the
answer would be YES and those for which the answer would be NO.
Now, suppose an adversary always gives the answer to each comparison corresponding to
the larger group. Then, each comparison will cut down the size of S by at most a factor of 2.
Since S initially has size n!, and by construction, the algorithm at the end must have reduced |
S| down to 1 in order to know which output to produce, the algorithm must make at least log2
(n!) comparisons before it can halt. We can then solve:
log2 (n!) = log2 (n) + log2 (n − 1) + ... + log2 (2) = Ω(n log n).

15. Describe the Ford-Fulkerson algorithm.


14. What is the maximal flow problem?

The input is directed graph (G, E), along with special vertex s and t called the source and
vertex.
The maximum flow problem asks for the largest amount of material that can be transported
from s to t.
13. Determine the longest common subsequence of the following two words:
abbaacaabbabcabacb
baabcccbabcbbbabab
Solution:
2. Give the pseudo code of an algorithm to find the minimum and maximum of an array with
(3/2)*n comparisons.

5. What is the general task of discrete optimization?

You might also like