Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures.

Slides:



Advertisements
Similar presentations
CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu.
Advertisements

Algorithm Design approaches Dr. Jey Veerasamy. Petrol cost minimization problem You need to go from S to T by car, spending the minimum for petrol. 2.
Data Structures by R.S. Chang, Dept. CSIE, NDHU1 1 Chapter 4 Complexity Analysis 4.1 Computational and Asymptotic Complexity The same problem can frequently.
Sorting Really Big Files Sorting Part 3. Using K Temporary Files Given  N records in file F  M records will fit into internal memory  Use K temp files,
Comments We consider in this topic a large class of related problems that deal with proximity of points in the plane. We will: 1.Define some proximity.
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Types of Algorithms.
Chapter 5 Fundamental Algorithm Design Techniques.
Algorithms + L. Grewe.
Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng
The Divide-and-Conquer Strategy
Introduction to Algorithms
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
For Monday Read 10.3 Homework: –Chapter 10, exercise 10.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
1 Dynamic Programming Jose Rolim University of Geneva.
Advanced Topics in Algorithms and Data Structures Lecture 7.1, page 1 An overview of lecture 7 An optimal parallel algorithm for the 2D convex hull problem,
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
CPSC 320: Intermediate Algorithm Design & Analysis Divide & Conquer and Recurrences Steve Wolfman 1.
Lecture 28 CSE 331 Nov 9, Flu and HW 6 Graded HW 6 at the END of the lecture If you have the flu, please stay home Contact me BEFORE you miss a.
Lecture 32 CSE 331 Nov 18, HW 8 solutions Friday.
Lecture 33 CSE 331 Nov 20, Homeworks Submit HW 9 by 1:10PM HW 8 solutions at the end of the lecture.
CSE 780 Algorithms Advanced Algorithms Greedy algorithm Job-select problem Greedy vs DP.
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
1 Algorithm Design Techniques Greedy algorithms Divide and conquer Dynamic programming Randomized algorithms Backtracking.
Lecture 30 CSE 331 Nov 10, Online Office Hours
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Chapter 19: Searching and Sorting Algorithms
Fundamentals of Algorithms MCS - 2 Lecture # 7
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.
Algorithm Paradigms High Level Approach To solving a Class of Problems.
INTRODUCTION. What is an algorithm? What is a Problem?
Example { 1 – –1} Use Bubble Sort (sort in increasing order} After first pass { –1 50} After Second Pass { } After.
CS 103 Discrete Structures Lecture 12
Lecture 28 CSE 331 Nov 9, Mini project report due WED.
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
Lecture 2: Divide and Conquer
Unit 1. Sorting and Divide and Conquer
Introduction to Recursion
Introduction to the Design and Analysis of Algorithms
Lecture 4 Divide-and-Conquer
Greedy Algorithms / Interval Scheduling Yin Tat Lee
Objectives Introduce different known sorting algorithms
Chapter 2: Getting Started
Algorithms Chapter 3 With Question/Answer Animations
Data Structures and Algorithms
Types of Algorithms.
Medians and Order Statistics
Lecture 27 CSE 331 Nov 3, 2017.
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Greedy Algorithms.
CSCE 222 Discrete Structures for Computing
Types of Algorithms.
Lecture 2: Divide and Conquer
Chapter 1 Introduction.
Richard Anderson Lecture 14 Divide and Conquer
Chapter 6 Dynamic Programming.
Lecture 27 CSE 331 Nov 4, 2016.
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Data Structures by R.S. Chang, Dept. CSIE, NDHU1 Chapter 2 Algorithm Design Human Problems Result Input Data Structures Processing Output Data Structures Computer Algorithms

Data Structures by R.S. Chang, Dept. CSIE, NDHU2 Chapter 2 Algorithm Design For a problem? What is an Optimal Solution? 用最少的 (CPU) 時間 ( 通常是指這點 ) 用最少的記憶體 Example: Given 4 numbers, sort it to nonincreasing order. Method 1: Sequential comparison 1. Find the largest (3 comparisons) 2. Find the second largest (2 comparisons) 3. Find the third largest (1 comparisons) 4. Find the fourth largest A total of 6 comparisons

Data Structures by R.S. Chang, Dept. CSIE, NDHU3 Chapter 2 Algorithm Design For a problem? What is an Optimal Solution? 用最少的 (CPU) 時間 ( 通常是指這點 ) 用最少的記憶體 Example: Given 4 numbers, sort it to nonincreasing order. Method 2: Somewhat clever method a1 a2 a3 a4 a2a4 a2a3 a2a3 a2 a1a3 a3 or a1 (4 comparisons) (5 comparisons)

Data Structures by R.S. Chang, Dept. CSIE, NDHU4 Chapter 2 Algorithm Design 2.1 Greedy Algorithms A greedy algorithm takes an action that seems the best at the given time, without consideration of future actions. Example: Coin changing problem Make change for any amount from $0.01 to $0.99 using the fewest number of coins. The available coins are $0.5, $0.25, $0.1, $0.05, and $0.01. $0.94=$0.5+$0.25+$0.1+$0.05+4*$0.01 A total of 8 coins Greedy algorithm works for U.S. coins. But not necessarily so for others. See Exercise 1 in Page 27.

Data Structures by R.S. Chang, Dept. CSIE, NDHU5 Chapter 2 Algorithm Design 2.1 Greedy Algorithms Hill climbing concept Local optimumGlobal optimum

Data Structures by R.S. Chang, Dept. CSIE, NDHU6 Chapter 2 Algorithm Design 2.2 Divide-and-Conquer Algorithms Example: Finding the minimum number in a list of numbers minimum(a,lower,upper) { if (upper-lower) == 1 return min(a[upper],a[lower]); else return min(minimum(a,lower,(lower+upper)/2), minimum(a,(lower+upper)/2+1,upper)); } Merge sort Quick sort

Data Structures by R.S. Chang, Dept. CSIE, NDHU7 Chapter 2 Algorithm Design 2.2 Divide-and-Conquer Algorithms The closest pair problem d d For each point near the border, there can be at most six candidates for the closest neighbor across the border. Why?

Data Structures by R.S. Chang, Dept. CSIE, NDHU8 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms Often a problem can be solved by divide-and- conquer, but it may turn out to be an inefficient algorithm because much work is duplicated when solving the subproblems. (or because the conquer (merge) step is too complicated)

Data Structures by R.S. Chang, Dept. CSIE, NDHU9 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The Fibonacci numbers Fibonacci(n) { if (n<2) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } F(5) F(3) F(4) F(1) F(2) F(3) F(1) F(2) Many repetitive calculations

Data Structures by R.S. Chang, Dept. CSIE, NDHU10 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The Fibonacci numbers Fibonacci(n) { int data[n]; data[0]=1; data[1]=1; for (j=2; j<n; j++) data[j]=data[j-2]+data[j-1]; return data[n-1]; } A simpler implementation: Example of one-dimensional dynamic programming

Data Structures by R.S. Chang, Dept. CSIE, NDHU11 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The number of combinations

Data Structures by R.S. Chang, Dept. CSIE, NDHU12 Chapter 2 Algorithm Design 2.3 Dynamic Programming Algorithms The number of combinations The Pascal’s triangle n r

Data Structures by R.S. Chang, Dept. CSIE, NDHU13 Chapter 2 Algorithm Design 2.4 Backtracking Algorithms The tic-tac-toe game xx How can a computer play the game? Remember Deep Blue?

Data Structures by R.S. Chang, Dept. CSIE, NDHU14 Chapter 2 Algorithm Design 2.4 Backtracking Algorithms The tic-tac-toe game x x (1,1)C (0,0)H (0,1)H (0,2)H (1,0)H... (0,0)C, (0,1)C, (1,0)C... (0,1)H, (1,0)H, …, (2,2)H (0,1)C, (1,0)C, (1,2)C, (2,0)C... : Computer x : Human

Data Structures by R.S. Chang, Dept. CSIE, NDHU15 Chapter 2 Algorithm Design 2.4 Backtracking Algorithms 3 missionaries and 2 cannibals want to cross the river Condition: 1. A boat can take one or two (must include a missionary) 2. At any time, on either bank, the number of missionaries must not be less than the number of cannibals.