MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.

Slides:



Advertisements
Similar presentations
CSC 421: Algorithm Design & Analysis
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Theory of Algorithms: Decrease and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 5: Decrease and Conquer
Chapter 4 Decrease-and-Conquer. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend.
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
Design & Analysis of Algorithms CS315
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
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 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
Design and Analysis of Algorithms - Chapter 51 Decrease and Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller.
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
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.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Graph & BFS.
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 5
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
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.
MA/CSSE 473 Day 22 Gray Code More Decrease and Conquer Algorithms No class Day 22 in because of Abby's broken arm.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
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.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
UNIT-II DECREASE-AND-CONQUER ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 5:
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Divide and Conquer Strategy
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Now, Chapter 5: Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review
Chapter 4 Divide-and-Conquer
Chapter 5 Decrease-and-Conquer
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 5 Decrease-and-Conquer
Chapter 5.
Decrease-and-Conquer
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
Analysis and design of algorithm
Presentation transcript:

MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS

MA/CSSE 473 Day 14 In-class exam (Friday, Sept 28) will cover Levitin chapters 1-3, 5, part of 4. Dasgupta excerpt (on ANGEL), class sessions 1-15, and HW 1-6. Student Questions Matrix Multiplication (Strassen) Decrease and Conquer examples DFS summary

Reminder: The Master Theorem The Master Theorem for Divide and Conquer recurrence relations: Consider T(n) = aT(n/b) + Ѳ(n k ) The solution is –Ѳ(n k )if a < b k –Ѳ(n k log n)if a = b k –Ѳ(n log b a )if a > b k

Ordinary Matrix Multiplication How many additions and multiplications are needed to compute the product of two 2x2 matrices? C 00 C 01 A 00 A 01 B 00 B 01 = * C 10 C 11 A 10 A 11 B 10 B 11 [ ] Q1

Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be computed as follows: C 00 C 01 A 00 A 01 B 00 B 01 = * C 10 C 11 A 10 A 11 B 10 B 11 M 1 + M 4 - M 5 + M 7 M 3 + M 5 = M 2 + M 4 M 1 + M 3 - M 2 + M 6 [ ] Values of M 1, M 2, …, M 7 are on the next slide

Formulas for Strassen’s Algorithm M 1 = (A 00 + A 11 )  (B 00 + B 11 ) M 2 = (A 10 + A 11 )  B 00 M 3 = A 00  (B 01 - B 11 ) M 4 = A 11  (B 10 - B 00 ) M 5 = (A 00 + A 01 )  B 11 M 6 = (A 10 - A 00 )  (B 00 + B 01 ) M 7 = (A 01 - A 11 )  (B 10 + B 11 ) How many additions and multiplications? Q2

The Recursive Algorithm We multiply square matrices whose size is a power of 2 (if not, pad with zeroes) Break up each matrix into four N/2 x N/2 submatrices. Recursively multiply the parts. How many additions and multiplications? If we do "normal matrix multiplication" recursively using divide and conquer? If we use Strassen's formulas? Q3

Analysis of Strassen’s Algorithm If N is not a power of 2, matrices can be padded with zeros. Number of multiplications: M(N) = 7M(N/2) + C, M(1) = 1 Solution: M(N) =  (N log 2 7 ) ≈ N vs. N 3 of brute-force algorithm. What if we also count the additions? Algorithms with better asymptotic efficiency are known but they are even more complex. Q4-5

DECREASE AND CONQUER

Decrease and Conquer Algorithms What does the term mean? –Reduce problem instance to smaller instance of the same problem –Solve smaller instance –Extend solution of smaller instance to obtain solution to original instance Also referred to as inductive or incremental approach Can be implemented either top-down or bottom-up Three variations. Decrease by –constant amount –constant factor –variable amount Q6

Decrease by constant vs by half

One Problem, Four approaches Recall the problem of integer exponentiation: Compute a n, where n is a power of 2: –Brute Force: a n = a*a*a*a*...*a –Divide and conquer: a n = a n/2 * a n/2 –Decrease by one: a n = a n-1 * a –Decrease by constant factor: a n = (a n/2 ) 2

Variable Decrease Examples Euclid's algorithm –b and a % b are smaller than a and b, but not by a constant amount or constant factor Interpolation search –The two sides of the partitioning element are smaller than n, but can be anything from 0 to n-1.

Interpolation Search Searches a sorted array similar to binary search but estimates location of the search key in A[l..r] by using its value v. Specifically, the values of the array’s elements are assumed to increase linearly from A[l] to A[r] Location of v is estimated as the x-coordinate of the point on the straight line through (l, A[l]) and (r, A[r]) whose y-coordinate is v: x = l +  (v - A[l])(r - l)/(A[r] – A[l] )  Q7

Interpolation Search Running time Average case:  (log (log n)) Worst:  (n) What can lead to worst-case behavior? Social Security numbers of US residents Phone book (Wilkes-Barre) CSSE department employees, Red and blue are current employees

Some "decrease by one" algorithms Insertion sort Selection Sort Depth-first search of a graph Breadth-first search of a graph Q7

Review: Analysis of Insertion Sort Time efficiency C worst (n) = n(n-1)/2  Θ (n 2 ) C avg (n) ≈ n 2 /4  Θ (n 2 ) C best (n) = n - 1  Θ (n) (also fast on almost-sorted arrays) Space efficiency: in-place (constant extra storage) Stable: yes Binary insertion sort –use Binary search, then move elements to make room for inserted element

Graph Traversal Many problems require processing all graph vertices (and edges) in systematic fashion Most common Graph traversal algorithms: –Depth-first search (DFS) –Breadth-first search (BFS)

Depth-First Search (DFS) Visits a graph’s vertices by always moving away from last visited vertex to unvisited one, backtracks if no adjacent unvisited vertex is available Uses a stack –a vertex is pushed onto the stack when it’s reached for the first time –a vertex is popped off the stack when it becomes a dead end, i.e., when there are no adjacent unvisited vertices “Redraws” graph in tree-like fashion (with tree edges and back edges for undirected graph) –A back edge is an edge of the graph that goes from the current vertex to a previously visited vertex (other than the current vertex's parent).

Notes on DFS DFS can be implemented with graphs represented as: –adjacency matrix: Θ (V 2 ) –adjacency list: Θ (|V|+|E|) Yields two distinct ordering of vertices: –order in which vertices are first encountered (pushed onto stack) –order in which vertices become dead-ends (popped off stack) Applications: –checking connectivity, finding connected components –checking acyclicity –finding articulation points –searching state-space of problems for solution (AI)

Pseudocode for DFS

Example: DFS traversal of undirected graph ab ef cd gh DFS traversal stack: DFS tree: Q8-10