Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure.

Similar presentations


Presentation on theme: "Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure."— Presentation transcript:

1 Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure

2 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?

3 Sequential Search – Smallest Distance of Two numbers Find smallest distance between any 2 numbers What is time efficiency of this algorithm?

4 Sequential Search – Brute Force Find whether a search key is present in an array What is time efficiency of this algorithm?

5 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?

6 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

7 Closest-Pair problem Given n points in a plane, find the closest pair

8 Convex/NonConvex Polygons

9 Convex Hull Imagine a rubber band around a set of nails Nails touched by the band  extreme points

10 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.

11 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

12 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

13 TSP Example

14 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?


Download ppt "Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure."

Similar presentations


Ads by Google