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

Slides:



Advertisements
Similar presentations
Lecture 3: Parallel Algorithm Design
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
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
Strassen's Matrix Multiplication Sibel KIRMIZIGÜL.
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 Jiafen Liu Sept
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
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Foundations of Algorithms, Fourth Edition
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Divide-and-Conquer 7 2  9 4   2   4   7
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine.
Project 2 due … Project 2 due … Project 2 Project 2.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Chapter 4-2 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Divide and Conquer Strategy
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.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 How to Multiply Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. integers, matrices, and polynomials.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 16.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Introduction to Algorithms: Divide-n-Conquer Algorithms
CSCE 210 Data Structures and Algorithms
Chapter 4 Divide-and-Conquer
Chapter 4: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Tree.
CSCE350 Algorithms and Data Structure
Divide-and-Conquer The most-well known algorithm design strategy:
Written Midterm Solutions
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer Merge sort and quick sort Binary search
Divide-and-Conquer The most-well known algorithm design strategy:
Topic: Divide and Conquer
Divide-and-Conquer 7 2  9 4   2   4   7
A Binary Tree is a tree in which each node has at most 2 children
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

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

Outline  Divide and Conquer for algorithm design  Binary Tree traversal  Multiplication of numbers  Strassen’s Matrix Multiplication

Quicksort Example

Example Search for K=70

Time Efficiency In the worst case, no key K exists in this array Although this is an nonrecursive algorithm, we can see that the time efficiency can be analyzed using the recurrence relation T(n)=T(n/2)+1 for n>1, T(1)=1  T(n) --- Θ(logn) Exact solution: Binary search is in fact not a typical example of divide and conquer because it does not solve two subproblems.

Binary Tree Traversals A binary tree T is defined as a finite set of nodes that is either empty or consists of a root and two disjoint binary trees T L and T R called the left and right subtree of the root Internal and external nodes: #ext_node = #internal_node+1

Height of a binary tree Input size n(T): # nodes in T, basic operation: “+” Recurrence  A(n)=n why? If the basic operation is the line to check whether a tree is empty  A(n)=2n+1 why?

Traverse the binary tree List all the nodes Preorder traversal: root  left subtree  right subtree Inorder traversal: left subtree  root  right subtree Postorder traversal: left subtree  right subtree  root What is the efficiency?

Large Integer Multiplication Some applications, notably modern cryptology, require manipulation of integers that are over 100 decimal digits long Such integers are too long to fit a single word of a computer Therefore, they require special treatment Consider the multiplication of two such long integers If we use the classic pen-and-pencil algorithm to multiply two n-digit integers, we need n 2 digit multiplications Can we design a divide-and-conquer algorithm to solve this problem with better efficiency?

The Basic Idea We want to calculate 23 x 14 Since We have Which includes four digit multiplications (n 2 ) But Therefore, we only need three digit multiplications

One Formula Given a=a 1 a 0 and b=b 1 b 0, compute c=a*b We have That means only three digit multiplications are needed to multiply two 2-digit integers

To Multiply Two n-digit integers Assume n is even, write Then To calculate the involved three multiplications – recursion! Stops when n=1

Efficiency The recurrence relation is Solving it by backward substitution for n=2 k yields Therefore,

Strassen’s Matrix Multiplication Brute-Force nxn matrix multiplication needs n 3 number multiplications For example, multiplying two 2x2 matrices needs 2 3 =8 multiplications

We can Reduce to 7 Multiplications

Divide and Conquer Partition the matrix into 4 submtrices with the size n/2xn/2 The above 7-multiplication 18 additions can be used here, but they are n/2xn/2 matrix multiplication and additions now How to calculate the n/2xn/2 matrix multiplication?– recursion! Stop condition, where the matrix size is 1x1. Recurrence for efficiency analysis (based on # multiplication)

Solve the Recurrence Solving it by backward substitution for n=2 k yields Therefore Count the # of additions which is the same as the complexity based on multiplication

Many Improvements Along This Line For example, Coopersmith and Winograd Getting closer to the theoretic lower bound