Download Algorithm Design Techniques: Brute Force Approach and Specific Algorithms - Prof. B. Karki and more Exams Computer Science in PDF only on Docsity!
Algorithm Design Techniques
CSC 3102 0.1 B.B. Karki, LSU
B.B. Karki, LSU0.2CSC 3102 Algorithm Basics (Chapters: 1 and 2) Notion of algorithm: Section 1.1 Fundamentals of algorithmic problem solving: Section 1.2 Important problem types: Section 1.3 Analysis of algorithmic efficiency: Sections 2.1 and 2.2 Non-recursive algorithms: Section 2.3 Recursive algorithms: Section 2.4
Brute Force
CSC 3102 0.5 B.B. Karki, LSU
B.B. Karki, LSU0.6CSC 3102 The Simplest Approach Brute force - the simplest of the design strategies Is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions of the concepts involved. Just do it - the brute-force strategy is easiest to apply. Results in an algorithm that can be improved with a modest amount of time. Brute force is important due to its wide applicability and simplicity. Weakness is subpar efficiency of most brute-force algorithms. Important examples: Selection sort, String matching, Convex-hull problem, and Exhaustive search. B.B. Karki, LSU0.7CSC 3102 Selection Sort Algorithm SelectionSort (A[0..n-1]) //Sorts a given array //Input: An array A[0..n-1] of orderable elements //Output: Array A[0..n-1] sorted in ascending order for i ← 0 to n - 2 do min ← i for j ← i + 1 to n - 1 do if A[j] < A[min] min ← j swap A[i] and A[min] € C(n) = 1 j= i+1 n−1 ∑ = [(n −1) − (i +1) +1] = (n −1− i) i= 0 n−2 ∑ i= 0 n−2 ∑ i= 0 n−2 ∑ = (n −1)n2 ∈ Θ(n 2) Scan the list repeatedly to find the elements, one at a time, in an non- decreasing order. On the ith pass through the list, search for the smallest item among the last n - i elements and swap it with A[i]. After n - 1 passes, the list is sorted. B.B. Karki, LSU0.10CSC 3102 Brute-Force Approach for Convex-Hull Problem A brute-force approach is based on the fact: A line segment connecting two points i and j is a part of its convex hull’s boundary if and only if all other points of the set lie on the same side of the straight line through these points. Repeating this test for every pair of points yields a list of line segments that make up the convex hull’s boundary. Find the equation of the straight line through two points (x1, y1) and (x2, y2) ax + by = c, where a = y2 - y1, b = x1 - x2 and c = x1y2 - y1x2. This line divides the plane into two half-planes: For all the points in one of them ax + by > c, while for all other points in the other ax + by < c. For all points on the line itself, ax + by = c. Check whether the expression ax + by - c has the same sign at each of these points To check whether certain points lie on the same side of the line. If yes, the line forms one side of the polygon. Efficiency of the algorithm: O(n3) For each of n(n -1)/2 pairs of distinct points, one needs to find the sign of ax + by - c for each of the other n - 2 points. B.B. Karki, LSU0.11CSC 3102 Exhaustive Search Exhaustive search is a brute-force approach to combinatorial problems. It suggests generating each and every combinatorial object of the problem, selecting those of them of that satisfy the problem’s constraints and then finding a desired object. Impratical for all but applicable to very small instances of problems. Examples: Traveling salesman problem Finding the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it started. Knapsack problem Finding the most valuable list of out-of n items that fit into the knapscak. Assignment problem Finding an assignment of n people to execute n jobs with the smallest total cost. These problems are the examples of so-called NP-hard problems. B.B. Karki, LSU0.12CSC 3102 Traveling Salesman Problem Traveling salesman problem Asks to find the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it started. Finding the shortest Hamiltonian circuit of the graph A cycle that passes through all the vertices of the graph exactly once. A sequence of n +1 adjacent vertices v0, v1, v2, … vn-1, v0. Get all tours by generating all the permutations of n - 1 intermediate cities, compute the tour lengths, and find the shortest among them. Efficiency: Total number of permutations = (n - 1)!/2 Impractical for all but very small values of n. 5 1 7 8 2 3 Tour Length a b c d a 2+8+1+7 = 18 a b d c a 2+3+1+5 = 11 a c b d a 23 a c d b a 11 a d b c a 23 a d c b a 18 More than one optimal solutions. a b dc