<<

CS483-10 Elementary Graph & Transform-and-Conquer

Outline

➣ Depth-first Search Ð cont Instructor: Fei Li ➣ Topological Sort Room 443 ST II ➣ Transform-and-Conquer Ð Gaussian Elimination Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments

[email protected] with subject: CS483

http://www.cs.gmu.edu/∼ lifei/teaching/cs483_fall07/

Based on Introduction to the Design and Analysis of Algorithms by Anany Levitin, Jyh-Ming Lien’s

notes, and Introduction to Algorithms by CLRS.

CS483 Design and Analysis of Algorithms 1 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 2 Lecture 09, September 27, 2007

Depth-first Search (DFS)

➣ The correctness proof: Use an induction method

➣ The overall running time of DFS is O(|V | + |E|). | | The time initializing each vertex is O( V ). ∈ Each edge (u, v) E is examined twice, once exploring u and once exploring v. Therefore takes O(|E|) time.

CS483 Design and Analysis of Algorithms 3 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 4 Lecture 09, September 27, 2007 Topological Sort Outline ➣ An application of DFS ➣ Depth-first Search Ð cont ➣ Input: a (DAG) ➣ Topological Sort ➣ Output: A linear ordering of all its vertices, such that if G contains an edge ➣ Transform-and-Conquer Ð Gaussian Elimination (u, v), then, u appears before v in the ordering.

CS483 Design and Analysis of Algorithms 5 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 6 Lecture 09, September 27, 2007

CS483 Design and Analysis of Algorithms 7 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 8 Lecture 09, September 27, 2007 Topological-Sort

Algorithm 0.1: TOPOLOGICAL-SORT(G(V,E))

Call DFS(G) to compute finishing times f[v] for each vertex v As each vertex is finished, insert it onto the front of a

return (the linked list of vertices)

CS483 Design and Analysis of Algorithms 9 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 10 Lecture 09, September 27, 2007

Transform-and-Conquer

A problem is solved by a transformation ➣ Outline To a simpler/more convenient instance of the same problem (instance simplification) ➣ Depth-first Search Ð cont - Ex: transforming unsorted to sorted ➣ Topological Sort ➣ To a different representation of the same instance (representation change)

➣ Transform-and-Conquer Ð Gaussian Elimination - Ex: transforming list to , tree to balanced tree, ... , etc.

➣ To a different problem for which an is already available (problem reduction) - Ex: transforming multiplication to addition

CS483 Design and Analysis of Algorithms 11 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 12 Lecture 09, September 27, 2007 Transform-and-Conquer

➣ Instance Simplification Instance Simplification

- Element uniqueness ➣ Find if a given array contains unique elements. - Mode (the most repeated element) of an array - What is the transform? - Searching for a value in an array ➣ Find the most repeated element. ➣ Representation Change - What is the transform? - Gaussian elimination ➣ - AVL tree, 2-3 tree Search for a value in an array (including binary search).

- and - What is the transform? ➣ ➣ Problem Reduction

- Least common multiple - What is the transform?

- Paths in a graph - Linear programming (Chapter 10)

CS483 Design and Analysis of Algorithms 13 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 14 Lecture 09, September 27, 2007

Instance Simplification - Presorting

Instance Simplification Instance Simplification: Solve a problem’s instance by transforming it into another simpler/easier instance of the same problem. ➣ Find if a given array contains unique elements. ➣ Presorting - What is the transform? Sorting Many problems involving lists are easier when list is sorted. ➣ Find the most repeated element.

Searching - What is the transform? Sorting

Computing the median (selection problem) ➣ Search for a value in an array (including binary search).

Checking if all elements are distinct (element uniqueness) - What is the transform? Sorting Also: ➣ Quicksort

Topological sorting helps solving some problems for directed Acyclic - What is the transform? Randomization graphs (DAGs).

Presorting is used in many geometric algorithms.

CS483 Design and Analysis of Algorithms 15 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 16 Lecture 09, September 27, 2007 Searching with Presorting

How Fast Can We Sort? ➣ Problem: Search for a given K in A[1...n]

➣ Efficiency of algorithms involving sorting depends on efficiency of sorting. ➣ Presorting-based algorithm: 1. Sort the array by an efficient ➣ Theorem (see Sec. 11.2): log2 n!≈n log2 n comparisons are necessary in the worst case to sort a list of size n by any comparison-based algorithm. 2. Apply binary search ➣ ➣ Note: About n log2 n comparisons are also sufficient to sort array of size n Efficiency: Θ(n log2 n)+O(log2 n)=Θ(n log2 n) (by mergesort). ➣ Good or bad?

➣ Why do we have our dictionaries, telephone directories, etc. sorted?

CS483 Design and Analysis of Algorithms 17 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 18 Lecture 09, September 27, 2007

Searching with Presorting

➣ Problem: Search for a given K in A[1...n] Element Uniqueness with Presorting

➣ Presorting-based algorithm: ➣ Brute force algorithm

1. Sort the array by an efficient sorting algorithm Compare all pairs of elements 2

2. Apply binary search Efficiency: O(n )

➣ Efficiency: Θ(n log2 n)+O(log2 n)=Θ(n log2 n) ➣ Presorting-based algorithm

➣ Good or bad? 1. Sort by efficient sorting algorithm (e.g. mergesort) 2. Scan array to check pairs of adjacent elements ➣ Why do we have our dictionaries, telephone directories, etc. sorted? Efficiency: Θ(n log n)+O(n)=Θ(n log n) Cumulative cost is reduced. 2 2

M · n vs. n log2 n + M · log2 n,givenM is large.

CS483 Design and Analysis of Algorithms 19 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 20 Lecture 09, September 27, 2007 Transform-and-Conquer

➣ Instance Simplification - Element uniqueness Representation Change: Gaussian Elimination - Mode (the most repeated element) of an array Problem: Solve the linear system of a set of n linear equations and n variables. - Searching for a value in an array ➣ Example 1: ➣ Representation Change

- Gaussian elimination

- AVL tree, 2-3 tree a11x + a12y = b1 - Heap and heapsort a21x + a22y = b2 ➣ Problem Reduction (Chapter 10)

- Least common multiple

- Paths in a graph - Linear programming

CS483 Design and Analysis of Algorithms 21 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 22 Lecture 09, September 27, 2007

Representation Change: Gaussian Elimination Representation Change: Gaussian Elimination

Problem: Solve the linear system of a set of n linear equations and n variables. ➣ Given:Asystem of n linear equations in n unknowns with an arbitrary coefficient matrix. ➣ Example 1: ➣ Transform to: An equivalent system of n linear equations in n unknowns with an upper triangular coefficient matrix. a11x + a12y = b1 ➣ Solve the latter by substitutions starting with the last equation and moving up a21x + a22y = b2 to the first one. ➣ Example 2: ➣ Base: If we add a multiple of one equation to another, the overall system of + + ···+ = a11x1 a12x2 a1nxn b1 equations remains equivalent. a21x1 + a22x2 + ···+ a2nxn = b2 . . + + ···+ =  +  + ···+  =  . a11x1 a12x2 a1nxn b1 a11x1 a12x2 a1nxn b1    a21x1 + a22x2 + ···+ a2nxn = b2 a x2 + ···+ a xn = b an1x1 + an2x2 + ···+ annxn = bn 22 2n 2 ......   an1x1 + an2x2 + ···+ annxn = bn annxn = bn

CS483 Design and Analysis of Algorithms 23 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 24 Lecture 09, September 27, 2007 Gaussian Elimination: Example Transformation by Gaussian Elimination

The transformation is accomplished by a sequence of elementary operations on 2x1 − 4x2 + x3 =6 the system’s coefficient matrix (which do not change the system’s solution): 3x1 − x2 + x3 =11 + − = −3 ← − x1 x2 x3 for f 1 to n 1 do Replace each of the subsequent rows (i.e., rows i +1, ..., n) by a difference 2 −41 6 th between that row and an appropriate multiple of the i row to make the new 3 −11 11 th coefficient in the i column of that row 0. 11−1 −3

CS483 Design and Analysis of Algorithms 25 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 26 Lecture 09, September 27, 2007

Gaussian Elimination: Example

Gaussian Elimination: Example 2x1 − 4x2 + x3 =6 3x1 − x2 + x3 =11 2x1 − 4x2 + x3 =6 x1 + x2 − x3 = −3 3x1 − x2 + x3 =11 x1 + x2 − x3 = −3 2 −41 6 3 −11 11 2 − 3 × 1 row 2 row 2 −41 6 11−1 −3 3 − 1 × 1 row 2 row 3 −11 11 2 − 3 × 1 row 2 row 11−1 −3 3 − 1 × 1 2 −41 6 row 2 row 05− 1 2 2 03− 3 −6 3 − 3 × 2 2 −41 6 2 row 5 row 05− 1 2 2 2 −41 6 03− 3 −6 2 05− 1 2 2 00− 6 − 36 5 5

CS483 Design and Analysis of Algorithms 27 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 28 Lecture 09, September 27, 2007 Gaussian Elimination: Example Gaussian Elimination Algorithm

➣ We have: 2 − 4 + =6 x1 x2 x3 Algorithm 0.2: GE(A[1 ···n, 1 ···n],b[1 ···n]) 5 − 1 =2 x2 2 x3 − 6 = − 36 5 x3 5 Append b to A as the last column for i ∈{1 ···n − 1} ➣ ⎧ Then we can solve x3,x2,x1 by backward substitution: ⎪ ∈{ ··· } ⎨⎪for j ⎧ i +1 n ⎨ 3= (− 36 ) (− 6 )=6 do ∈{ ··· } x 5 / 5 ⎪ for k j n 1 ⎩⎪ do x2=(2+() × 6)/5=1 ⎩ A[i,k]A[j,i] 2 do for A[j, k]=A[j, k] − A[i,i] x1= (66+4× 1)/2=2

CS483 Design and Analysis of Algorithms 29 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 30 Lecture 09, September 27, 2007

Backward Substitution Algorithm

Solve Ax = b using Gaussian Elimination Algorithm 0.3: BS(A[1 ···n, 1 ···n +1]) ➣ To solve a linear system Ax = b:WewillcallGE(A,b) and then x = BS(A) ←{ ··· } for j ⎧ n 1 ➣ : ⎪ ← ⎪t 0 ⎨⎪ for k ←{j +1···n} 2 2 2 do T (n)=n +(n − 1) + ···+(n − (n − 1)) = ⎪ ← × 2 2 2 1 3 3 ⎪ do t t + A[j, k] x[k] 1 +2 + ···+ n ≈ n =Θ(n ). ⎩⎪ 3 x[j] ← (A[j, n +1]− t)/A[j, j]

CS483 Design and Analysis of Algorithms 31 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 32 Lecture 09, September 27, 2007 Better Gaussian Elimination Algorithm

Algorithm 0.4: GE(A[1 ···n, 1 ···n],b[1 ···n])

More about Gaussian Elimination Append b to A as the last column ∈{1 ··· − 1} for i 8 n ➣ > Issues with Gaussian Elimination >for j ∈{i +1···n} > > | [ ]| | [ ]|

> do if A j, i > A pivot, i The value of A[j, i]/A[i, i] is repetitively computed > > > then pivot ← j

> Small A[i, i] make the algorithm unstable (numerical errors), e.g., > >for j ∈{i ···n +1} A[j, i]/A[i, i] will be too large to cause over flow. < do do swap(A[i, k],A[pivot, k]) > ➣ > Solution: pivoting: always select the largest A[i, i] >for j ∈{i +1···n +1} > 8 > > ← A[j,i] > <>temp A[i,i] > > ∈{ ··· } > do >for k j n :> :> do for A[j, k]=A[j, k] − A[i, k] × temp

CS483 Design and Analysis of Algorithms 33 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 34 Lecture 09, September 27, 2007

LU Decomposition

➣ LU decomposition (A = LU ) Decompose a matrix into two matrices: an upper triangular matrix and a lower Why Gaussian Elimination? triangular matrix

➣ Solve linear equations Ax = b ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ abc 100 u11 u12 u13 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ➣ LU decomposition ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ def⎦ = ⎣ l21 10⎦ ⎣ 0 u22 u23 ⎦ ➣ Matrix inverse ghi l31 l32 1 00u33 ➣ Compute Determinant ➣ Gaussian elimination can be used to compute L (or U), which is then used to compute U (or L).

➣ LU decomposition is good if you have different bs, i.e., Ax = b ⇒ L(Ux)=b ⇒ Ux = b

CS483 Design and Analysis of Algorithms 35 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 36 Lecture 09, September 27, 2007 Matrix Inverse

Example: ⎡ ⎤ ⎡ ⎤

12 −1 10 ⎣ ⎦ A = ⎣ ⎦ Matrix Inverse 34 01

➣ Compute Inversion ⎡ ⎤ ⎡ ⎤ −1 × −1 12 −1 10 A of an invertible n n matrix A. Recall that AA = I ⎣ ⎦ A = ⎣ ⎦ − − ➣ 0 2 31 We can use Gaussian elimination (Gauss-Jordan elimination to be precise) to ⎡ ⎤ ⎡ ⎤ compute inverse of a matrix. 10 −1 −21 ⎣ ⎦ A = ⎣ ⎦ ➣ Not all n × n matrices can be invertible. Such matrices are called singular. 0 −2 −31 ⎡ ⎤ ⎡ ⎤

10 −1 −21 ⎣ ⎦ A = ⎣ ⎦ 01 3/2 −1/2

CS483 Design and Analysis of Algorithms 37 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 38 Lecture 09, September 27, 2007

Matrix Determinant (cont.)

Matrix Determinant ➣ Using Gaussian elimination, and some properties of determinant:

Interchanging two rows changes the sign of the determinant. ➣ The determinant of a matrix A is denoted det A or |A| Multiplying a row by a scalar multiplies the determinant by that scalar.

➣ When det A =0 , A is invertible. Replacing any row by the sum of that row and any other row does NOT change the determinant. ➣ Example:

2 3 The determinant of a triangular matrix (upper or lower triangular) is the product of the abc diagonal elements. 6 7 det 6 7 = − − + + − 4 def5 aei afh bdi bfg cdh ceg ➣ Example: ghi 2 3 2 3 2 3 53 10.6 10.6 A = 4 5 =5· 4 5 =54 5 −4 −2 −4 −2 00.4 ➣ Time complexity of a brute force algorithm? O(n!) D = x and x/5=0.4 ⇒ x =2

CS483 Design and Analysis of Algorithms 39 Lecture 09, September 27, 2007 CS483 Design and Analysis of Algorithms 40 Lecture 09, September 27, 2007