Top Banner

Click here to load reader

Comparitive Analysis of Algorithm strategies

Aug 07, 2015



  1. 1. Algorithm Strategies Presented By M.Talha MCS-13-08
  2. 2. Algorithms Strategy in Assignment Decrease and Conquer Greedy Approach Backtracking Transform and Conquer
  3. 3. Roadmap of Every Strategy Definition Types Example Advantages Disadvantages Algorithm Working Complexity
  4. 4. Decrease and Conquer Definition _Change an instance into one smaller instance of the problem. _ Solve the smaller instance. _ Convert the solution of the smaller instance into a solution for the larger Instance.
  5. 5. Decrease and Conquer Decrease by a Constant insertion sort graph traversal algorithms (DFS and BFS) topological sorting Decrease by a Constant Factor The Fake-coin problem Variable-size decrease Euclids algorithm
  6. 6. Decrease and Conquer Advantages Solve Smaller Instance Take less time Use for shortest path Disadvantages Depends on efficiency of sorting
  7. 7. Breadth-first search Algorithm Definition Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a `search key'[1]) and explores the neighbor nodes first, before moving to the next level neighbors.
  8. 8. Algorithm of BFS Procedure bfs (v) q: = make queue () Enqueue (q, v) Mark v as visited While q is not empty v = dequeue (q) Process v for all unvisited vertices v' adjacent to v Mark v' as visited enqueue(q,v')
  9. 9. Order in which the nodes are expanded
  10. 10. Complexity (V2) if represented by an adjacency table, (|V| + |E|) if represented by adjacency lists
  11. 11. Greedy Approach Greedy algorithms are simple and straightforward. They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future. They are easy to invent, easy to implement and most of the time quite efficient. Many problems cannot be solved correctly by greedy approach. Greedy algorithms are used to solve optimization problems
  12. 12. Uses of Greedy Approach Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible. They are also used in machine learning, business intelligence (BI), artificial intelligence (AI) and programming. Making Change Algorithm Huffman Encoding Algorithm
  13. 13. Greedy Approach Advantages : Very Large number of Feasible Solution. Easy to Implement. Disadvantages : It is much Slower. Does not give optimum result for all problems.
  14. 14. Huffman Encoding Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression. The process of finding and/or using such a code proceeds by means of Huffman codding. The output from Huffman's algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file).
  15. 15. Huffman Encoding Algorithm procedure Huffman(f) Input: An array f[1 _ _ _ n] of frequencies Output: An encoding tree with n leaves let H be a priority queue of integers, ordered by f for i = 1 to n: insert(H; i) for k = n + 1 to 2n 1: i = deletemin(H); j = deletemin(H) create a node numbered k with children i; j f[k] = f[i] + f[j] insert(H; k)
  16. 16. Working of the Huffman Encoding Algorithm
  17. 17. Complexity we use a greedy O(n*log(n)) gfaq/huffman_tutorial.html
  18. 18. Knapsack Problem You have a knapsack that has capacity (weight) W. You have several items 1 to n. Each item Ij has a weight wj and a benefit bj. You want to place a certain number of copies of each item Ij in the knapsack so that : The knapsack weight capacity in not exceeded . the total benefits is maximal. ations
  19. 19. Knapsack Problem a/File:Knapsack.svg
  20. 20. Knapsack Problem Variants 0/1 knapsack Problem The item cannot be divided. Fractional knapsack problem For instance, items are liquid or powders Solvable with a greedy algorithm ations
  21. 21. Greedy Knapsack - Fractional Much Easier For item Ij, let ratioj=bj/wj. This gives you the benefit per measure of weight. Sort the items in descending order of rj. If the next item cannot fit into the knapsack ,break it and pick it partially just to fill the knapsack. ations
  22. 22. Greedy Knapsack Algorithm P and W are arrays contain the profit and weight n objects ordered such that p[i]/w[i]=>p[i+1]/w[1+i] that is in decreasing order , m is the knapsack size and x is the solution vector. GreedyKnapsack(m,n) For(i=0;i
  23. 23. Example - Greedy Knapsack Item A B C D Benefit 50 140 60 60 weight 5 20 10 12 Ratio = B/W 10 7 6 5 Example: Knapsack Capacity W = 30 and ml
  24. 24. Solution All of A, all of B, and ((30-25)/10) of C (and none of D) Weight: 5 + 20 + 10*(5/10) = 30 Value: 50 + 140 + 60*(5/10) = 190 + 30 = 220 ml
  25. 25. Analysis If the items are already sorted into descending order of pi/wi Then the for-loop takes a time in O(n). Therefore , the Total time including the sort is in O(n log n). problem-in-java.html
  26. 26. Backtracking Backtracking is a technique for systematically trying all paths through a state space. Backtracking search begins at the start state and pursues a path until it reaches either a goal or a dead end. If it finds a goal, it quits and returns the solution path. If it reaches a dead end, it backtracks to the most recent node on the path having unexamined siblings and continues down one of these branches,
  27. 27. Backtracking Backtracking is an important tool for solving constraint satisfaction problems, such as crosswords, verbal arithmetic, Sudoku, and many other puzzles. 8 Queen Puzzle Algorithm is an example of Backtracking
  28. 28. Backtracking Advantages: Quick test Pair Matching Following real life Concept Disadvantages : Not widely Implemented Cannot Express Left Recursion More Time and Complexity
  29. 29. Eight queens puzzle The eight queens puzzle is the problem of placing eight chess queens on an 88 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.
  30. 30. Eight queens puzzle Algorithm Function backtrack; Begin SL: = [Start]; NSL: = [Start]; DE: = []; CS: = Start; % initialize: While NSL [ ] do % while there are states to be tried Begin If CS = goal (or meets goal description) then return SL; % on success, return list of states in path. If CS has no children (excluding nodes already on DE, SL, and NSL) then Begin While SL is not empty and CS = the first element of SL do
  31. 31. Eight queens puzzle Algorithm Begin Add CS to DE; Remove first element from SL; Remove first element from NSL; CS: ~ first element of NSL: End Add CS to SL; End
  32. 32. Eight queens puzzle Algorithm Else begin Place children of CS (except nodes already on DE, SL, or NSL) on NSL; CS: = first element of NSL; Add CS to SL; End End;
  33. 33. Eight queens puzzle Algorithm Queens-Puzzle-Tree-Algorithm
  34. 34. Complexity The running time of your algorithm is at most N(N1)(N2)(NK+1), i.e., N!/(NK)!. This is O(NK), i.e., exponential in K n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks- queen-queen-q1009394