Solutions of Theory of Algorithms assignment 2
Exercise 24.1-4
BELLMAN-FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i 1 to |V[G]| - 1 3 do for each edge (u, v) E[G] 4 do RELAX(u, v, w) 5 for each edge (u, v) E[G] 6 do if d[v] > d[u] + w(u, v) 7 th
Solutions of Theory of Algorithms Assignment 4
Exercise 7.3-1
Randomization version of the algorithm can only enhance the average case performance over all inputs. When the randomized algorithm is so unlucky, it will not be able to change the worst case.
Solutions of Theory of Algorithms Assignment 4
Exercise 7.3-2
In the worst case, number of calls to RANDOM is (n2) In the best case, number of calls to RANDOM is (nlgn)
Solutions of Theory of Algorithms Assignment 4
Exercise 7.4-5
a. INSERTION SORT algorithm running time will be O (n(k-1), and the
QUICKSORT will take n lg(n/k). therefore, the described algorithm will take O(nk + nlg(n/k)
b. K can be chosen arbitrary smal
Solutions of Theory of Algorithms Assignment 4
Exercise 7-4
a. QUICKSORT' is the same algorithm of QUICKSORT,
t herefore it is also correct.
b. The stack depth will be (n) i f the worst case occurs. c. The running t ime will be the same because the algori
Solutions of Theory of Algorithms Assignment 4
Exercise 7-5
a. As in this variation there exists three elements to take their median,
therefore there are six different permutations for them. For each element, it could be greater than, smaller than, or equ
Solutions of Theory of Algorithms Assignment 5
Exercise 8.2-3
The for- loop is used to navigate the whole array we want to sort. It doesn't matter whether we begin navigating the array from its first element to the last or from the last to the first. The
Solutions of Theory of Algorithms Assignment 5
Exercise 8.3-4
As the numbers we want to sort are n integer numbers in the range from 0 to n2-1, so we need d digits to represent them, where d = logn (n2) = 2 So, according to lemma 8.3, RADIX-SORT correctly
Solutions of Theory of Algorithms Assignment 5
Problem 8-2
a. Choose a field in this record that is integer to sort according to it
If key =0 A1 = CountSort (A,B,k) Else A2=CountSort (A,B,k) Sorted Array = A1 + A2
b. Choose a field in this record that is
Solutions of Theory of Algorithms Assignment 5
Exercise 9.3-1
If we divide the elements into groups of 7, T hus, at least half of the n /7 g roups contribute 4 elements that are greater than x, except for the one group that has fewer than 7 elements if 7
Solutions of Theory of Algorithms Assignment 5
Exercise 9.3-3
In the worst case, quick sort running t ime is O(n 2) because the partit ion can be made to obtain two sets; one consists of one element and the rest is i n the other set. If we could guarantee
Solutions of Theory of Algorithms Assignment 6
Exercise 13.2-2
To make any rotation (left or right), we need at least two nodes in the tree. If the tree has only one node (the root), we can't make a rotation at all. So, in general, if the tree consists of
Solutions of Theory of Algorithms Assignment 6
Exercise 13.2-4
If we have a binary search tree consists of n nodes, and we want to transform it to a right chain binary tree, we will need to make a sequence of right rotations to all the nodes that are on t
Solutions of Theory of Algorithms Assignment 6
Exercise 13.3-5
To form a red-black tree, we first insert the first node (n=1) as a root and color it with black. If we have more nodes to insert (n>1), we use RB-INSERT which colors them with red. RB-INSERT
Solutions of Final 2
Question 1
a. Dijkstra will work correctly. When we add the same constant to every edge in the
graph, the difference between the paths will be maintained by the same value. And as the constant is big enough to make all the weights non
Solutions of Theory of Algorithms Assignment 4
Exercise 7.2-3
If the array to be sorted consists of distinct elements and already sorted in decreasing order, that will be the worst case for QuickSort algorithm. Every time the algorithm tries to subdivide
Solutions of Theory of Algorithms Assignment 4
Exercise 7.2-1
T(n) = T(n-1) + n Using substitution method, T(n) c n2 where c > 0 T(n) c n-12 + n c(n2-2n+1) + n cn2 -2cn + c + (n) Therefore T(n) = (n2)
Solutions of Theory of Algorithms Assignment 3
Exercise 16.1-2
Select the last activity to start that is compatible with all previously selected activities is the mirror algorithm of selecting the first activity to finish. It is the same idea as the propo
Solutions of Theory of Algorithms assignment 2
Exercise 23.1-3
If there exists a MST T, it must contain all nodes in the graph including u and v. And the edge (u, v) that belongs to T must have minimum weight among the other edges; otherwise it will not b
Solutions of Theory of Algorithms assignment 2
Exercise 23.2-4
The K ruskal's algorithm running t ime depends on two main factors: 1. the i mplementation of the disjoint set data structure, and 2. The sorting of edges. Being the weight of the edges intege
Solutions of Theory of Algorithms assignment 2
Exercise 20.4-1
It is not necessary that the height of an n-node Fibonacci heap is O(lg n). if we begin inserting nodes with an empty Fibonacci heap, we will get a set of n nodes connected together in the roo
Solutions of Theory of Algorithms assignment 2
Exercise 23.2-2
MST-PRIM(G, w, r) 1 for each u V [G] 2 do key[u] 3 [u] NIL 4 key[r] 0 5 for i=1 to n 6 for j=1 to n "traversing the whole matrix" 7 do usearch for the smallest element in the matrix 8 for each
Solutions of Theory of Algorithms assignment 2
Exercise 20.4-2
If ith operation triggers a contraction to the table (if i-1<1/3) K=2, 4, 8,. In general 2i
Solutions of Theory of Algorithms assignment 2
Exercise 20.2-1
Return the node with key 7. Delete the node and link its children sub trees to the root list as follows (circles are the root list):
Then min[H] = 18, A[0]=0, A[1]=38, A[2]=18, A[3]=0
A[0]=0,
Solutions of Theory of Algorithms assignment
Exercise 17.4-1
We might consider the hash table full in order to decrease the possibility of occurring of collisions. To insert a new element in the table, we need to implement two steps: computing the hash fu