CSCE350 Algorithms and Data Structure Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. Outline Learning how to understand Pseudocodes of Mysterious programs Brute Force Strategy for Algorithm Design The art of lazy algorithm is to count/estimate its running time Is it doable within given timeframe? Sequential Search – Smallest Distance of Two numbers Find smallest distance between any 2 numbers // Search key K in A[0..n - 1] ALGORITHM MinDis tan ce( A[0..n 1]) Output:minimum distance between two of its numbers d min for i 0 to n 1 for j 0 to n 1 if i j and A[i ] A[ j ] d min d min | A[i ] A[ j ] | return dmin What is time efficiency of this algorithm? Sequential Search – Brute Force Find whether a search key is present in an array // S e archk e yK in A[0..n-1] ALGO RITHMSequentialSearch( A[0..n ], K ) A[n ] K i0 wh i leA[i ] K do i i 1 if i n re tu rni e l sere tu rn-1 What is time efficiency of this algorithm? Brute-Force String Matching Find a pattern in the text: Pattern – ‘NOT’, text – ‘NOBODY_NOTICED_HIM’ Typical Applications – ‘find’ function in the text editor, e.g., MS-Word, Google search ALGO RITHMBFStringMatch(T [0..n 1], P[0..m 1]) for i 0 to n-m do j0 while j m and P[ j ] T [i j ] j j 1 if j m re turni re turn-1 What is the time efficiency of this algorithm? Closest-Pair and Convex Hull Problems by Brute Force Closest-Pair problem • Given n points in a plane, find the closest pair • How to solve this problem and what is the time efficiency of this algorithm? Convex-Hull problem • Convex hull is the tightest convex polygon that bounds a set of n points in a plane • Convex polygon – any two points in this polygon results in the inclusion of the segment that links these two points also in this polygon Closest-Pair problem Given n points in a plane, find the closest pair Convex/NonConvex Polygons Convex Hull Imagine a rubber band around a set of nails Nails touched by the band extreme points P6 P7 P2 P8 P3 P4 P5 P1 Solve Convex-Hull Problem Connect any pair of points by a line segment. Each line segment partitions the plane to the two half planes If all the n points are on the same side of this line segment ax+by-c >0 or <0 Such a line segment is an edge of the convex-hull polygon What is the time efficiency of this Brute-Force algorithm? For each possible pair of points, we need to check whether all the remaining n-2 points are on the same side of the line segment that connects these pair of points. For Sorting, String Matching, and Convex-Hull problems, we will revisit them by designing more efficient algorithms. Exhaustive Search A brute-force approach to combinatorial problem • Generate each and every element of the problem’s domain • Then compare and select the desirable element that satisfies the set constraints • Involve combinatorial objects such as permutations, combinations, and subsets of a given set • The time efficiency is usually bad – usually the complexity grows exponentially with the input size Three examples • Traveling salesman problem • Knapsack problem • Assignment problem Traveling Salesman Problem Find the shortest tour through a given n cities that visits each city exactly once before returning to the starting city Using graph model: city vertex, road edge, length of the road edge weight. TSP shortest Hamiltonian Circuit – a cycle that passes through all the vertices of the graph exactly once Exhaustive search: • List all the possible Hamiltonian circuits (starting from any vertex) • Ignore the direction • How many candidate circuits do we have? (n-1)!/2 • Very high complexity TSP Example 2 a 5 3 7 8 c 1 Tour b d Length a ---> b ---> c --->d ---> a l = 2 + 8 + 1 + 7 = 18 a ---> b---> d ---> c ---> a l = 2 + 3 + 1 + 5 = 11 a ---> c ---> b ---> d --->a l = 5 + 8 + 3 + 7 = 23 a ---> c ---> d ---> b ---> a l = 5 + 1 + 3 + 2 = 11 a---> d ---> b ---> c ---> a l = 7 + 3 + 8 + 5 = 23 a ---> d ---> c ---> b ---> a l = 7 + 1 + 8 + 2 = 18 optimal optimal Knapsack Problem Given n items of known weight w1, w2, …wn with values v1, v2, ..vn, And a knapsack of capacity W How to select the items to fill the knapsack such that you get max value? Steal money from bank?