Useful Algorithms Concepts
Short Description
Download Useful Algorithms Concepts...
Description
Useful Algorithm Concepts Kaiser Md. Nahiduzzaman Feb 2010
#A. Greedy algorithm Some 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 Reallife example: To make change in currency, repeatedly dispense the largest denomination. To give out sixty nine dollars we give out a fiftydollar bill, a tendollar bill, a fivedollar bill, two twodollar 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 Conquer Divide: 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 (nonoverlapping). Examples: maximum subsequence sum problem, lineartime tree traversal strategies, mergesort, quicksort. Running Time: T(N) = 2T(N/2) + O(N) i.e O(N log N) #C. Dynamic Programming A 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, allpairs shortest path, optimal binary search tree, longest common subsequence ACM Uva Examples: 10131, 10069, 10154, 116, 10003, 10261, 10271, 10201 #D. Backtracking Algorithms It'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 xaxis. Let us assume that the first point's xcoordinate 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(6th) 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 5th 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
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://onlinejudge.uva.es/ and http://ciijudge.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 References 1. Data Structures and Algorithm Analysis Mark Allen Weiss 2. Programming Challenges Steven S. Skiena, Miguel A. Revilla 3. Art of Programming Contest – Ahmed Shamsul Arefin
View more...
Comments