Algorithm Strategy: Decrease & Conquer - Exponentiation, Insertion Sort, Graph Traversals , Study notes of Computer Science

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

Pre 2010

Uploaded on 08/04/2009

koofers-user-hoq
koofers-user-hoq 🇺🇸

9 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Decrease and Conquer
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.
1. Integer exponentiation; calculating the positive real power of a number.
2. Insertion sort.
3. The fake coin problem.
4. Graph traversals: BFS (Breadth First Search) and
DFS (Depth First Search).
5. Topological sorting.
6. Binary search trees.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Algorithm Strategy: Decrease & Conquer - Exponentiation, Insertion Sort, Graph Traversals and more Study notes Computer Science in PDF only on Docsity!

Decrease and Conquer

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.

  1. Integer exponentiation; calculating the positive real power of a number.
  2. Insertion sort.
  3. The fake coin problem.
  4. Graph traversals: BFS (Breadth First Search) and DFS (Depth First Search).
  5. Topological sorting.
  6. Binary search trees.

Integer Exponentiation (Algorithm 1)

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

Integer Exponentiation (Algorithm 3)

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

Integer Exponentiation (Algorithm 4)

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.

Algorithm for Insertion Sort

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])

Sample of Insertion Sort

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)

The Fake Coin Problem

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.

The Fake Coin Problem (General Step)

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 = 2K + 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.

Graph Traversal Algorithms

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.

Basic Terms in Graph Theory

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

  1. Vertex VJ and VK are said to be adjacent, and
  2. the edge (VJ, VK) is said to be incident to each of vertices VJ and VK. A path in a graph is defined to be a sequence of edges, beginning at the start vertex and ending at the destination vertex. A graph is said to be connected if and only if each pair of vertices is connected by at least one path.

Intuitive Discussion of BFS and DFS

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)

DFS on a Rooted Tree

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.