










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The 'decrease and conquer' algorithm strategy and its application to various problems, including integer exponentiation, insertion sort, the fake coin problem, and graph traversals. It provides algorithms and explanations for each problem, as well as the time complexity analysis.
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Though the term is not commonly used, “decrease and conquer” describes another powerful algorithm design strategy. We shall use the decrease and conquer strategy to consider solutions to a few problems, some of them common. Here are the problems that we shall discuss.
We consider four solutions to the integer exponentiation problem. Given a real number A and an integer N 0, compute A N , the Nth power of A. The first three algorithms are given to illustrate the differences between the three strategies studied so far. We then show a more efficient algorithm. Brute Force Function F(A, N) R = 1 // R = Result, what a brilliant name! For K = 1 to N Do R = R * A End Do Return R
This algorithm is really just the brute force algorithm redone with recursive calls. Decrease-and-Conquer Function F(A, N) If (N > 1) Then R = F(A, (N – 1)) * A Else If (N = 1) Then R = A Else R = 1 Return R
This algorithm is based on the binary representation of the integer power N. For example, let N = 26 = 16 + 8 + 2 = 11010 in binary. We observe that A 26 = A 16 A 8 A 2 . Function F(A, N) R = 1 AP = A // AP is a to the power P While (N > 0) Do NR = N mod 2 If NR = 1 Then R = R * AP AP = AP * AP N = N / 2 // Integer division: 1/2 = 0. End While Return R One can show that the time complexity of this algorithm is log 2 (N), more accurately log 2 (N). Here we need log 2 (26) = 5 loops.
Here is the basic step of the sort algorithm. Procedure Insert (A[1..N], K, X) // Assumes that A[1..K] is sorted. // Places X in its place by “moving up” others. // Note the required order of moving elements. J = K While (J ≥ 1) And (A[J] > X) Do A[J + 1] = A[J] J = J - 1 End Do A[J + 1] = X Here is the entire algorithm Procedure InsertSort (A[1..N]) For L = 2 to N Do K = L - 1 Insert (A, K, A[L])
Sort the array A[1 .. 8] = (3, 1, 4, 1, 5, 9, 2, 6) L = 2 K = 1 Insert (A[1..8], 1, A[2]) X = 1 A[1 .. 8] = (3, 1, 4, 1, 5, 9, 2, 6) J = 1 A[J] = 3 > X. So A[2] = A[1] and A[1 .. 8] = (3, 3, 4, 1, 5, 9, 2, 6) J = 0 End loop A[J + 1] = A[1] = 1 A[1 .. 8] = (1, 3, 4, 1, 5, 9, 2, 6) L = 3 K = 2 Insert (A[1..8], 2, A[3]) X = 4 A[1 .. 8] = (1, 3, 4, 1, 5, 9, 2, 6) J = 2 A[J] = 3 X, so end loop. A[J + 1] = A[3] = 4 A[1 .. 8] = (1, 3, 4, 1, 5, 9, 2, 6)
We are given N 3 coins, exactly one of which is fake having a weight different from the real coins. We are given a balance scale by which we can compare the weights of single coins or collections of coins. The algorithm is based on the following observation for the 3–coin problem. Consider three coins (C1, C2, C3) with weights (W1, W2, W3). Select two coins at random (call them C1 and C2), place them on the balance scale, and compare the weights. If W1 = W2, then coin C3 is the fake. If W1 W2, then one of coins C1 or C2 is the fake. Remove C2 and replace it with C3. If W1 W3, then C1 is the fake. If W1 = W3, then C2 is the fake.
Given a pile of N coins, one of which is known to be fake, we divide it into two piles P1 and P2. If N is odd, then N = 2K + 1. Let each of P1 and P2 have K coins. There is one coin left out. Place the two piles on the scales and compare W(P1) to W(P2). If W(P1) = W(P2) then the coin left out is the fake. If W(P1) W(P2) then the coin left out is OK and the fake coins is in one of P1 or P2. At this point, we have to decide which pile contains the fake coin. Here is the rule: P1 contains the fake coin if and only if P2 does not.
Many computer science problems are well modeled as graph traversal problems. There are two basic graph traversal methods BFS Breadth First Search DFS Depth First Search A graph comprises a collection of nodes (more later). A graph traversal algorithm “visits” and marks every node in the graph. Marking the vertices once visited prevents the algorithm going into endless loops. More advanced graph traversal methods will use knowledge of the problem in order to limit the number of vertices in the graph that are visited.
Let G = (V, E) be a graph with vertex set V and edge set E. The vertex set V is a non–empty set, often denoted {V 1 , V 2 , .. VN}. The edge set E is a set of pairs of vertices (VJ, VK) in which VJ V(G) and VK V(G). If (VJ, VK) E(G), then
The BFS and DFS algorithms are defined for general graphs, but more easily illustrated on rooted trees. We must specify the adjacencies implied above. A (B, C, D) B (A, E, F) E (B)F (B) C (A, G, H) G (C) H (C) D (A, J, K) J (D) K (D)
When started at the root of a rooted tree, DFS “goes deep”, hence its name. Note that it proceeds immediately to a leaf node. DFS imposes a tree structure on the graph that is searched. For a tree, such as we have above, the imposed tree structure is the tree itself. The red lines in the figure are drawn to suggest a search order. They are not to be interpreted as edges in any graph.