Useful Algorithms Concepts

2

Click here to load reader

Transcript of Useful Algorithms Concepts

Page 1: Useful Algorithms Concepts

Useful Algorithm ConceptsKaiser Md. Nahiduzzaman

Feb 2010

#A. Greedy algorithmSome local optimum is chosen; when the algorithm terminates, we hope that the local optimum is equal to the global optimum.

Examples of Popular Greedy Algorithms: Dijkstra, Prim, Kruskal algorithm.

ACM Uva Examples: 10020, 10340, 10440

Real­life example:To make change in currency, repeatedly dispense the largest denomination. To give out sixty nine dollars we give out a fifty­dollar bill, a ten­dollar bill, a five­dollar bill, two two­dollar bills. In this way, we are guranteed to minimize the number of bills. That is, first consider the largest denominator in 69 which is 50 and the rest is 19. Now consider the largest denominator in 19 which is 10 and the rest is 9. Do it repeatedly.

#B. Divide and ConquerDivide: Smaller problems are solved recursively (except the base cases).Conquer: Solution to the original problem is formed from the solutions to the subproblems.The subproblems should be disjoint (non­overlapping).

Examples: maximum subsequence sum problem, linear­time tree traversal strategies, mergesort, quicksort.

Running Time: T(N) = 2T(N/2) + O(N) i.e O(N log N)

#C. Dynamic ProgrammingA problem that can be mathematically expressed recursively can also be expressed as a recursive algorithm, yielding a significant performance improvement over a naive search.Any recursive mathematical formula could be directly translated to a recursive algorithm, but the underlying reality is that often the compiler will not do justice to the recursive algorithm, and an inefficient  program results. When we suspect that this is likely to be the case, we must provide a little more help to the compiler, by rewriting the recursive algorithm as  a  nonrecursive  algorithm that  systematically   records   the answers   to   the subproblems  in a   table.  One technique that makes use of this approach is known as dynamic programming.

Examples: Matrix multiplication, all­pairs shortest path, optimal binary search tree, longest common subsequence

ACM Uva Examples: 10131, 10069, 10154, 116, 10003, 10261, 10271, 10201

#D. Backtracking AlgorithmsIt's a clever implementation of exhaustive search. In this algorithm, a large group of possiblities are eliminated in one step which is known as pruning.

Example: Suppose we are given N points, located on the x­axis. Let us assume that the first point's x­coordinate is 0 and the points are given from left to right.If we are given a set of points, it is easy to construct the set of distances between every pair of points.But the turnpike reconstruction problem is to reconstruct a point set from the distances.Suppose we are given the distance set D = {1,2,2,2,3,3,3,4,5,5,5,6,7,8,10}.We know that N = 6. The first number is 0 and the last(6­th) number is 10.We remove 10 from D. Next the largest remaining distance is 8, which means that either the second element is 2 or the 5­th element is 8. We construct a tree for the solutions and after propagating a certain path, we will conclude that one path is not the correct path and we will backtrack to last correct position. We need to do these prunings all over the tree 

Page 2: Useful Algorithms Concepts

to get the correct result.

ACM Uva Examples: 861, 10181, 10128, 10160, 10032, 10001, 704, 10270

#E. Appendix:IMPORTANT WEBSITES/ FOR ACM/ICPC PROGRAMMERS:

ACM online site: http://online­judge.uva.es/and 

  http://cii­judge.baylor.edu/Helping site: http://www.comp.nus.edu.sg/~stevenha/programming/acmoj.html

Online book: http://acm.uva.es/p/Art_of_Programming_Contest_SE_for_uva.pdf

References1. Data Structures and Algorithm Analysis ­ Mark Allen Weiss2. Programming Challenges ­ Steven S. Skiena, Miguel A. Revilla3. Art of Programming Contest – Ahmed Shamsul Arefin