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

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

A simple example finding the maximum of a set S of n numbers.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
Algorithm Design Paradigms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Divide-and-Conquer Dr. Yingwu Zhu P65-74, p83-88, p93-96, p
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Theory of Algorithms: Divide and Conquer
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Foundations of Algorithms, Fourth Edition
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover,
Divide and Conquer Strategy
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Introduction to Algorithms: Brute-Force Algorithms.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer.
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Topic: Divide and Conquer
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
Algorithms Dr. Youn-Hee Han April-May 2013
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

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

Outline  Brute Force Strategy for Algorithm Design  Exhaustive search  Divide and Conquer for algorithm design

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

TSP Example

Knapsack Example

Divide and Conquer Strategy for Algorithm Design The most well known algorithm design strategy: Divide instance of problem into two or more smaller instances of the same problem, ideally of about the same size Solve smaller instances recursively Obtain solution to original (larger) instance by combining these solutions obtained for the smaller instances problem subp subS Solution

Polynomial and non-polynomial Complexity 1constant log nlogarithmic nlinear n log n n2n2 quadratic n3n3 cubic 2n2n exponential n!factorial

Assignment Problem n people to be assigned to execute n jobs, one person per job. C[i,j] is the cost if person i is assigned to job j. Find an assignment with the smallest total cost Exhaustive search How many kinds of different assignments? The permutation of n persons  n! Very high complexity Hungarian method – much more efficient  polynomial Job 1Job 2Job 3Job 4 Person Person Person Person 47694

From Assignment Problem, We Found That If the exhaustive-search (brute-force) strategy takes non- polynomial time, it does not mean that there exists no polynomial-time algorithm to solve the same problem In the coming lectures, we are going to learn many such kinds of strategies to design more efficient algorithms. These new strategies may not be as straightforward as brute- force ones One example, the log n –time algorithm to compute a n That’s called divide-and-conquer strategy – the next topic we are going to learn

Divide-and-conquer technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n Possible? HOW? HOW?

An Example Compute the sum of n numbers a 0, a 1, …, a n-1. Question: How to design a brute-force algorithm to solve this problem and what is its complexity? Use divide-and-conquer strategy: What is the recurrence and the complexity of this recursive algorithm? Does it improve the efficiency of the brute-force algorithm?

General Divide and Conquer Recurrence: T(n) = aT(n/b) + f (n) where f (n) ∈ Θ(n k ) a < b k T(n) ∈ Θ(n k ) a = b k T(n) ∈ Θ(n k log n ) a > b k T(n) ∈ Θ( n log b a ) Note: the same results hold with O instead of Θ.

Divide and Conquer Examples Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplication - Strassen’s algorithm Convex hull - QuickHull algorithm

Mergesort Algorithm: Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] Sort arrays B and C Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: –compare the first elements in the remaining unprocessed portions of the arrays –copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.

Mergesort Example

Algorithm in Pseudocode

Merge Algorithm in Pseudocode

Efficiency Recurrence C(n)=2C(n/2)+C merge (n) for n>1, C(1)=0 Basic operation is a comparison and we have C merge (n)=n-1 Using the Master Theorem, the complexity of mergesort algorithm is Θ(n log n) It is more efficient than SelectionSort, BubbleSort and InsertionSort, where the time complexity is Θ(n 2 )

Quicksort Select a pivot (partitioning element) Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than or equal to the pivot Exchange the pivot with the last element in the first (i.e., ≤ sublist) – the pivot is now in its final position Sort the two sublists p A[i] ≤ p A[i] p

The partition algorithm or i = r

Illustrations p all are ≤ p ≥ p... ≤ p all are ≥ p → i j ← p all are ≤ p ≤ p ≥ p all are ≥ p → ij ← p all are ≤ p = p all are ≥ p → i=j ←

QuickSort Algorithm

Quicksort Example

Efficiency of Quicksort Basic operation: key comparison Best case: split in the middle — Θ( n log n) Worst case: sorted array! — Θ( n 2 ) Average case: random arrays — Θ( n log n) Improvements: better pivot selection: median of three partitioning avoids worst case in sorted files switch to insertion sort on small subfiles elimination of recursion these combine to 20-25% improvement