Introduction to Algorithms Jiafen Liu Sept. 2013.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 3: Divide and Conquer Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 3 1.
Advertisements

Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer CIS 606 Spring 2010.
A simple example finding the maximum of a set S of n numbers.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
More on Divide and Conquer. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide and Conquer Strategy
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.
CS4413 Divide-and-Conquer
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Introduction to Algorithms 6.046J/18.401J L ECTURE 3 Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassen’s.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Foundations of Algorithms, Fourth Edition
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Introduction to Algorithms Jiafen Liu Sept
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis and Design of Algorithms. According to math historians the true origin of the word algorism: comes from a famous Persian author named ál-Khâwrázmî.
Analysis of Algorithms
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures and Algorithms A. G. Malamos
Project 2 due … Project 2 due … Project 2 Project 2.
Recurrence Relation. Outline  What is a recurrence relation ?  Solving linear recurrence relations  Divide-and-conquer algorithms and recurrence relations.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
4.Divide-and-Conquer Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Instruction Divide Conquer Combine.
1 Chapter 4 Divide-and-Conquer. 2 About this lecture Recall the divide-and-conquer paradigm, which we used for merge sort: – Divide the problem into a.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Introduction to Algorithms
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure.
Introduction to Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Introduction to Algorithms Jiafen Liu Sept. 2013

Today’s Tasks Divide and Conquer –Binary search –Powering a number –Fibonacci numbers –Matrix multiplication –Strassen’s algorithm –VLSI tree layout

The divide-and-conquer Design Paradigm Divide the problem into subproblems. Conquer the subproblems by solving them recursively. Combine subproblem solutions. Many algorithms use this paradigm. –give me an example, please?

Merge Sort How to express the cost of merge sort?

Merge Sort 1.Divide:Trivial. 2.Conquer:Recursively sort subarrays. 3.Combine:Linear-time merge. # subproblems subproblem size cost of dividing and combining

Recall Master Method Case 1: – Case 2: –

Case 3: – Recall Master Method

Conclusion of Master Method Merge Sort a=2, b=2, case 2( k=0) So,

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find 9

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find 9

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find 9

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find 9

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find 9

Binary search Find an element in a sorted array: –Divide: Check middle element. –Conquer: Recursively search 1 sub array. –Combine: Trivial. Example: Find 9

Recurrence for binary search T(n) = 1T(n/2) + Θ(1) b=2, a=1 Case 2 (k=0) # subproblems subproblem size cost of dividing and combining

Powering a Number Problem: Compute a n, where n ∈ N. Naive algorithm: –How? –Complexity ? The Spot Creativity: –Is this the only and the best algorithm? –Any Suggestions on using divide-and- conquer strategy? Θ(n)

Divide-and-conquer: Complexity: T(n) = T(n/2) + Θ(1) a=1,b=2 =n 0 =1 case 2(k=0) T(n) = Θ(lgn) Powering a Number

Wonderful Fibonacci numbers Background of Fibonacci …. Give me the recursive definition of Fibonacci Numbers? Give me an application of Fibonacci numbers.

Application of Fibonacci There is a staircase of 10 steps, and you can step across one or two steps as you like each time. How many ways we have to step on the stage? What’s the ratio of the two adjacent numbers in the Fibonacci Sequence, that’s to say, what’s the value of F(n- 1)/F(n) when n tends to ∞ ?

Analyze of Fibonacci To computer Fibonacci number F(n), How much time does it take? –Substitution method –Recursion-tree method –Principal method Substitution method –Guess O(2 n ) –Guess = T(n)= In fact, T(n) = Θ(1.618) n (Not required)

Questions? Why this recursive algorithm takes so much time? –Building out the recursion tree for F(n), we can see that there are lots of common subtrees. –Too many duplications that waste time Important in Recursion –Compound Interest Rule

Can we improve the Algorithm? Think about the bottom-up implantation of this recursive algorithm –Compute the Fibonacci numbers in order. –Iterative To computer Fibonacci number F(n), How much time does it take? –T(n) = Θ(n)

Is that the best we can do? Any ideas on how we could compute Fibonacci of n faster than linear time? –Using mathematical properties of Fibonacci numbers – rounded to the nearest integer, where –What we call Naive recursive squaring. Time Complexity: Does this work well? –This method is unreliable, since floating-point arithmetic is prone to round-off errors.

Recursive Squaring Another mathematical property of Fibonacci numbers How to prove that theorem? –Induction on n. What’s the cost of computing F n ? –

Proof of Theorem

Homework Code to implement the four algorithms of computing Fibonacci numbers. –Recursive –Iterative –Approximation –Recursive Squaring Titled with your student number and your name Submit your runnable source code to my mailbox Deadline : before next class

Matrix Multiplication of n*n

Code for Matrix Multiplication for i=1 to n for j=1 to n c ij =0 for k=1 to n c ij =c ij + a ik *b kj Running Time= ?

Let’s do better How to divide-and-conquer? –Recall what we have done before. –n->n/2 Idea: r=ae+bg; s=af+bh; t=ce+dg; u=cf+dh;

Cost of Matrix Multiplication We need: –8 recursive multiplications of matrix in size (n/2)*(n/2) –4 add operations of matrix in size (n/2)*(n/2) –Cost of adding two matrix in size n*n? So, multiplication of matrix in size n*n takes T(n)=8 * T(n/2) + Θ(n 2 ) # subproblems subproblem size cost of dividing and combining

Cost of Matrix Multiplication T(n)=8 * T(n/2) + Θ(n 2 ) a=8,b=2 =n 3 case 1(ε=1) T(n) = Θ(n 3 ) OOPS! No better than the ordinary algorithm !

Strassen’s Idea How Strassen came up with his magic idea? –We should try get rid of as more multiplications as possible. We could do a hundred additions instead. –We can reduce the numbers of subproblems in T(n)=8 * T(n/2) + Θ(n 2 ) –He must be very clever.

Strassen’s Idea Multiply 2*2 matrices with only 7 recursive multiplications. Notes: plus of matrices is commutative, but multiplication is not.

Strassen’s Algorithm 1.Divide: Partition A and B into (n/2)*(n/2) submatrices. And form terms to be multiplied using + and –. 2.Conquer: Perform 7 multiplications (P 1 to P 7 ) of (n/2)×(n/2) submatrices recursively. 3.Combine:Form C (r,s,t,u) using + and –on (n/2)×(n/2) submatrices. Write down cost of each step We got T(n)=7 * T(n/2) + Θ(n 2 )

Cost of Strassen’s Algorithm T(n)=7 * T(n/2) + Θ(n 2 ) a=7,b=2 =n lg7 case 1 T(n) = Θ(n lg7 ) =O(n 2.81 ) Not so surprising? Strassen’s Algorithm is not the best. The best one has O(n ) (Be of theoretical interest only). But it is simple and efficient enough compared with the naïve one when n>=32.

VLSI Layout VLSI: Very Large Scale Integrated circuits. Layout of chips is a very important problem in cost control. Problem: layout n chips in a grid, the control lines forms a binary tree, no cross and with minimum area. Embed a complete binary tree with n leaves in a grid using minimal area.

VLSI Layout Naïve Layout How much area it takes? –Height : H(n) ? –Width: W(n) ? Θ(n) Θ(lgn)

Naïve Layout In fact, we can express using divide-and- conquer strategy. –Height : H(n) = H(n/2) + Θ( 1) = Θ( lgn) –Width: W(n) = 2W(n/2)+ Θ( 1) = Θ( n) –Area = Θ( nlgn)

Improvements How can we reduce the height or the weight to take less area? Hint: Our goal is O(n). What are two functions whose product is n? –Height : H(n) = –Width: W(n) =

Improvements What is a recurrence in the usual master method form whose solution is root n? log b a=1/2

Improvements When log b a=1/2 ? –Guess: b=4, a=2 What form the cost should take? –T(n)=2T(n/4)+f(n) How should we aligned the nodes on a grid? –Imagine H layout.

Here we are H-tree embedding Height : H(n) = 2H(n/4) + Θ( 1) Weigh W(n) = 2W(n/4)+ Θ( 1) H(n) = W(n)= (case 1) Area = Θ (n)

Conclusion Divide and conquer often leads to efficient algorithms, but it is just one of several powerful techniques for algorithm design. Divide-and-conquer algorithms can be analyzed using recurrences and the master method. If we know what running time we are aiming for, think about the recurrence that will get us there. And that’s so-called inspiration.