Chapter 5 Decrease- in addition to -Conquer Copyright © 2007 Pearson Addison-Wesley. All righ

Chapter 5 Decrease- in addition to -Conquer Copyright © 2007 Pearson Addison-Wesley. All righ www.phwiki.com

Chapter 5 Decrease- in addition to -Conquer Copyright © 2007 Pearson Addison-Wesley. All righ

Watts, Keller, Meteorologist has reference to this Academic Journal, PHwiki organized this Journal Chapter 5 Decrease- in addition to -Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Decrease- in addition to -Conquer Reduce problem instance to smaller instance of the same problem Solve smaller instance Extend solution of smaller instance to obtain solution to original instance Can be implemented either top-down or bottom-up Also referred to as inductive or incremental approach 3 Types of Decrease in addition to Conquer Decrease by a constant (usually by 1): insertion sort graph traversal algorithms (DFS in addition to BFS) topological sorting algorithms as long as generating permutations, subsets Decrease by a constant factor (usually by half) binary search in addition to bisection method exponentiation by squaring multiplication à la russe Variable-size decrease Euclid’s algorithm selection by partition Nim-like games This usually results in a recursive algorithm.

Institut Suprieure d'Electronique du Nord FR www.phwiki.com

This Particular University is Related to this Particular Journal

What’s the difference Consider the problem of exponentiation: Compute xn Brute Force: Divide in addition to conquer: Decrease by one: Decrease by constant factor: n-1 multiplications T(n) = 2T(n/2) + 1 = n-1 T(n) = T(n-1) + 1 = n-1 Insertion Sort To sort array A[0 n-1], sort A[0 n-2] recursively in addition to then insert A[n-1] in its proper place among the sorted A[0 n-2] Usually implemented bottom up (nonrecursively) Example: Sort 6, 4, 1, 8, 5 6 4 1 8 5 4 6 1 8 5 1 4 6 8 5 1 4 6 8 5 1 4 5 6 8 Pseudocode of Insertion Sort

Analysis of Insertion Sort Time efficiency Cworst(n) = n(n-1)/2 (n2) Cavg(n) n2/4 (n2) Cbest(n) = n – 1 (n) (also fast on almost sorted arrays) Space efficiency: in-place Stability: yes Best elementary sorting algorithm overall Binary insertion sort Graph Traversal Many problems require processing all graph vertices ( in addition to edges) in systematic fashion Graph traversal algorithms: Depth-first search (DFS) Breadth-first search (BFS) Depth-First Search (DFS) Visits graph’s vertices by always moving away from last visited vertex to an unvisited one, backtracks if no adjacent unvisited vertex is available. Recurisve or it uses a stack a vertex is pushed onto the stack when it’s reached as long as the first time a vertex is popped off the stack when it becomes a dead end, i.e., when there is no adjacent unvisited vertex “Redraws” graph in tree-like fashion (with tree edges in addition to back edges as long as undirected graph)

Pseudocode of DFS Example: DFS traversal of undirected graph DFS traversal stack: DFS tree: a b e f c d g h a ab abf abfe abf ab abg abgc abgcd abgcdh abgcd 1 2 5 4 6 3 7 8 Notes on DFS DFS can be implemented with graphs represented as: adjacency matrices: (V2). Why adjacency lists: (V+E). Why Yields two distinct ordering of vertices: order in which vertices are first encountered (pushed onto stack) order in which vertices become dead-ends (popped off stack) Applications: checking connectivity, finding connected components checking acyclicity (if no back edges) finding articulation points in addition to biconnected components searching the state-space of problems as long as solutions (in AI)

Breadth-first search (BFS) Visits graph vertices by moving across to all the neighbors of the last visited vertex Instead of a stack, BFS uses a queue Similar to level-by-level tree traversal “Redraws” graph in tree-like fashion (with tree edges in addition to cross edges as long as undirected graph) Pseudocode of BFS Example of BFS traversal of undirected graph BFS traversal queue: BFS tree: a b e f c d g h a bef efg fg g ch hd d 1 3 2 6 4 7 5 8

Notes on BFS BFS has same efficiency as DFS in addition to can be implemented with graphs represented as: adjacency matrices: (V2). Why adjacency lists: (V+E). Why Yields single ordering of vertices (order added/deleted from queue is the same) Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges DAGs in addition to Topological Sorting A dag: a directed acyclic graph, i.e. a directed graph with no (directed) cycles Arise in modeling many problems that involve prerequisite constraints (construction projects, document version control) Vertices of a dag can be linearly ordered so that as long as every edge its starting vertex is listed be as long as e its ending vertex (topological sorting). Being a dag is also a necessary condition as long as topological sorting to be possible. a b c d a b c d a dag not a dag Topological Sorting Example Order the following items in a food chain fish human shrimp sheep wheat plankton tiger

DFS-based Algorithm DFS-based algorithm as long as topological sorting Per as long as m DFS traversal, noting the order vertices are popped off the traversal stack Reverse order solves topological sorting problem Back edges encountered NOT a dag! Example: Efficiency: The same as that of DFS. b a e f c d g h Source Removal Algorithm Source removal algorithm Repeatedly identify in addition to remove a source (a vertex with no incoming edges) in addition to all the edges incident to it until either no vertex is left or there is no source among the remaining vertices (not a dag) Example: Efficiency: same as efficiency of the DFS-based algorithm, but how would you identify a source How do you remove a source from the dag “Invert” the adjacency lists as long as each vertex to count the number of incoming edges by going thru each adjacency list in addition to counting the number of times that each vertex appears in these lists. To remove a source, decrement the count of each of its neighbors by one. Decrease-by-Constant-Factor Algorithms In this variation of decrease- in addition to -conquer, instance size is reduced by the same factor (typically, 2) Examples: Binary search in addition to the method of bisection Exponentiation by squaring Multiplication à la russe (Russian peasant method) Fake-coin puzzle Josephus problem

Exponentiation by Squaring The problem: Compute an where n is a nonnegative integer The problem can be solved by applying recursively the as long as mulas: For even values of n For odd values of n a n = (a n/2 )2 if n > 0 in addition to a 0 = 1 a n = (a (n-1)/2 )2 a Recurrence: M(n) = M( n/2 ) + f(n), where f(n) = 1 or 2, M(0) = 0 Master Theorem: M(n) (log n) = (b) where b = log2(n+1) Russian Peasant Multiplication The problem: Compute the product of two positive integers Can be solved by a decrease-by-half algorithm based on the following as long as mulas. For even values of n: For odd values of n: n m = 2m n m = 2m + m if n > 1 in addition to m if n = 1 n 2 n – 1 2 Example of Russian Peasant Multiplication Compute 20 26 n m 20 26 10 52 5 104 104 2 208 + 1 416 416 520 Note: Method reduces to adding m’s values corresponding to odd n’s.

Watts, Keller 31 News at 10 PM Weekend - WAAY-TV Meteorologist www.phwiki.com

Fake-Coin Puzzle (simpler version) There are n identically looking coins one of which is fake. There is a balance scale but there are no weights; the scale can tell whether two sets of coins weigh the same in addition to , if not, which of the two sets is heavier (but not by how much, i.e. 3-way comparison). Design an efficient algorithm as long as detecting the fake coin. Assume that the fake coin is known to be lighter than the genuine ones. Decrease by factor 2 algorithm Decrease by factor 3 algorithm (Q3 on page 187 of Levitin) T(n) = log n Variable-Size-Decrease Algorithms In the variable-size-decrease variation of decrease- in addition to -conquer, instance size reduction varies from one iteration to another Examples: Euclid’s algorithm as long as greatest common divisor Partition-based algorithm as long as selection problem Interpolation search Some algorithms on binary search trees Nim in addition to Nim-like games Euclid’s Algorithm Euclid’s algorithm is based on repeated application of equality gcd(m, n) = gcd(n, m mod n) Ex.: gcd(80,44) = gcd(44,36) = gcd(36, 8) = gcd(8,4) = gcd(4,0) = 4 One can prove that the size, measured by the first number, decreases at least by half after two consecutive iterations. Hence, T(n) O(log n) Proof. Assume m > n, in addition to consider m in addition to m mod n. Case 1: n <= m/2. m mod n < n <= m/2. Case 2: n > m/2. m mod n = m-n < m/2. Selection Problem Find the k-th smallest element in a list of n numbers k = 1 or k = n median: k = n/2 Example: 4, 1, 10, 9, 7, 12, 8, 2, 15 median = The median is used in statistics as a measure of an average value of a sample. In fact, it is a better (more robust) indicator than the mean, which is used as long as the same purpose. Digression: Post Office Location Problem Given n village locations along a straight highway, where should a new post office be located to minimize the average distance from the villages to the post office The average! In two dimensions with Manhattan distance Linear programming! Algorithms as long as the Selection Problem The sorting-based algorithm: Sort in addition to return the k-th element Efficiency (if sorted by mergesort): (nlog n) A faster algorithm is based on using the quicksort-like partition of the list. Let s be a split position obtained by a partition (using some pivot): Assuming that the list is indexed from 1 to n: If s = k, the problem is solved; if s > k, look as long as the k-th smallest element in the left part; if s < k, look as long as the (k-s)-th smallest element in the right part. Note: The algorithm can simply continue until s = k. s all are A[s] all are A[s] One-Pile Nim There is a pile of n chips. Two players take turn by removing from the pile at least 1 in addition to at most m chips. (The number of chips taken can vary from move to move.) The winner is the player that takes the last chip. Who wins the game – the player moving first or second, if both player make the best moves possible It’s a good idea to analyze this in addition to similar games “backwards”, i.e., starting with n = 0, 1, 2, Partial Graph of One-Pile Nim with m = 4 Vertex numbers indicate n, the number of chips in the pile. The losing positions as long as the player to move are circled. Only winning moves from a winning position are shown (in bold). Generalization: The player moving first wins iff n is not a multiple of 5 (more generally, m+1); the winning move is to take n mod 5 (n mod (m+1)) chips on every move.

Watts, Keller Meteorologist

Watts, Keller is from United States and they belong to 31 News at 10 PM Weekend – WAAY-TV and they are from  Huntsville, United States got related to this Particular Journal. and Watts, Keller deal with the subjects like Meteorology

Journal Ratings by Institut Suprieure d’Electronique du Nord

This Particular Journal got reviewed and rated by Institut Suprieure d’Electronique du Nord and short form of this particular Institution is FR and gave this Journal an Excellent Rating.