140 likes | 161 Views
Learn how to understand pseudocodes of mysterious programs and explore the brute force strategy for algorithm design. Discover the art of lazy algorithms and how to estimate their running time. Explore examples such as sequential search, string matching, closest pair problem, and convex hull problem. Gain insights into combinatorial problems like the traveling salesman problem, knapsack problem, and assignment problem.
E N D
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 • What is time efficiency of this algorithm?
Sequential Search – Brute Force • Find whether a search key is present in an array • 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 • 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 Hull • Imagine a rubber band around a set of nails • Nails touched by the band extreme points
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
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?