ma/csse 473 day 14

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

Upload: genica

Post on 15-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MA/CSSE 473 Day 14

MA/CSSE 473 Day 14

Strassen's Algorithm: Matrix Multiplication

Decrease and Conquer

DFS

Page 2: MA/CSSE 473 Day 14

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

Page 3: MA/CSSE 473 Day 14

Reminder: The Master Theorem• The Master Theorem for Divide and Conquer

recurrence relations:• Consider

T(n) = aT(n/b) + Ѳ(nk)• The solution is

– Ѳ(nk) if a < bk

– Ѳ(nk log n) if a = bk

– Ѳ(nlogba) if a > bk

Page 4: MA/CSSE 473 Day 14

Ordinary Matrix Multiplication How many additions and multiplications are

needed to compute the product of two 2x2 matrices?

C00 C01 A00 A01 B00 B01

= *C10 C11 A10 A11 B10 B11

[ ] [ ] [ ]

Q1

Page 5: MA/CSSE 473 Day 14

Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two

matrices can be computed as follows:

C00 C01 A00 A01 B00 B01

= *C10 C11 A10 A11 B10 B11

M1 + M4 - M5 + M7 M3 + M5

= M2 + M4 M1 + M3 - M2 + M6

[ ] [ ] [ ][ ]

Values of M1, M2, … , M7 are on the next slide

Page 6: MA/CSSE 473 Day 14

Formulas for Strassen’s AlgorithmM1 = (A00 + A11) (B00 + B11)

M2 = (A10 + A11) B00

M3 = A00 (B01 - B11)

M4 = A11 (B10 - B00)

M5 = (A00 + A01) B11

M6 = (A10 - A00) (B00 + B01)

M7 = (A01 - A11) (B10 + B11)

How many additions and multiplications?

Q2

Page 7: MA/CSSE 473 Day 14

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

Page 8: MA/CSSE 473 Day 14

Analysis of Strassen’s AlgorithmIf N is not a power of 2, matrices can be padded

with zeros.Number of multiplications: M(N) = 7M(N/2) + C, M(1) = 1Solution: M(N) = (Nlog 27) ≈ N2.807

vs. N3 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

Page 9: MA/CSSE 473 Day 14

DECREASE AND CONQUER

Page 10: MA/CSSE 473 Day 14

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

Page 11: MA/CSSE 473 Day 14

Decrease by constant vs by half

Page 12: MA/CSSE 473 Day 14

One Problem, Four approaches• Recall the problem of integer exponentiation:

Compute an, where n is a power of 2:– Brute Force: an= a*a*a*a*...*a

– Divide and conquer: an= an/2 * an/2

– Decrease by one: an= an-1* a

– Decrease by constant factor: an= (an/2)2

Page 13: MA/CSSE 473 Day 14

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.

Page 14: MA/CSSE 473 Day 14

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

Page 15: MA/CSSE 473 Day 14

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, 1984-2012

Red and blue are current employees

Page 16: MA/CSSE 473 Day 14

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

Q7

Page 17: MA/CSSE 473 Day 14

Review: Analysis of Insertion Sort• Time efficiency

Cworst(n) = n(n-1)/2 Θ(n2)Cavg(n) ≈ n2/4 Θ(n2)Cbest(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

Page 18: MA/CSSE 473 Day 14

Graph TraversalMany problems require processing all graph

vertices (and edges) in systematic fashion

Most common Graph traversal algorithms:

– Depth-first search (DFS)

– Breadth-first search (BFS)

Page 19: MA/CSSE 473 Day 14

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).

Page 20: MA/CSSE 473 Day 14

Notes on DFS• DFS can be implemented with graphs represented as:

– adjacency matrix: Θ(V2)– 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)

Page 21: MA/CSSE 473 Day 14

Pseudocode for DFS

Page 22: MA/CSSE 473 Day 14

Example: DFS traversal of undirected graph

a b

e f

c d

g h

DFS traversal stack: DFS tree:

Q8-10