Algorithms Richard Anderson University of Washington July 3

  • Slides: 119
Download presentation
Algorithms Richard Anderson University of Washington July 3, 2008 IUCEE: Algorithms 1

Algorithms Richard Anderson University of Washington July 3, 2008 IUCEE: Algorithms 1

Today’s topics • • Teaching Algorithms Active Learning in Algorithms Big Ideas: Solving Problems

Today’s topics • • Teaching Algorithms Active Learning in Algorithms Big Ideas: Solving Problems in Practice Mysore / Theory Discussion July 3, 2008 IUCEE: Algorithms 2

Text books July 3, 2008 IUCEE: Algorithms 3

Text books July 3, 2008 IUCEE: Algorithms 3

University of Washington Course CSE 421 Introduction to Algorithms (3) Techniques for design of

University of Washington Course CSE 421 Introduction to Algorithms (3) Techniques for design of efficient algorithms. Methods for showing lower bounds on computational complexity. Particular algorithms for sorting, searching, set manipulation, arithmetic, graph problems, pattern matching. Prerequisite: CSE 322; CSE 326. • Algorithm Design, by Jon Kleinberg and Eva Tardos, 2005. • Ten week term – 3 lectures per week (50 minutes) – Midterm, Final July 3, 2008 IUCEE: Algorithms 4

Course overview • • Stable Marriage (2) Basic Graph Algorithms (3) Greedy Algorithms (2)

Course overview • • Stable Marriage (2) Basic Graph Algorithms (3) Greedy Algorithms (2) Graph Algorithms (4) Divide and Conquer and Recurrences (5) Dynamic Programming (5) Network Flow and Applications (5) NP Completeness (3) July 3, 2008 IUCEE: Algorithms 5

Analyzing the course and content • What is the purpose of each unit? –

Analyzing the course and content • What is the purpose of each unit? – Long term impact on students • What are the learning goals of each unit? – How are they evaluated • What strategies can be used to make material relevant and interesting? • How does the context impact the content July 3, 2008 IUCEE: Algorithms 6

Overall course context • Senior level elective – Students are not required to take

Overall course context • Senior level elective – Students are not required to take this class – Approximately half the students take this course – Theory course: no expectation of programming • Data structures is a pre-requisite • Little coordination with data structures course – Some overlap in material – Generally different instructors • Text book highly regarded by faculty • Course is “algorithms by techniques” July 3, 2008 IUCEE: Algorithms 7

Stable Marriage • Very interesting choice for start of the course • Stable Marriage

Stable Marriage • Very interesting choice for start of the course • Stable Marriage is a non-standard topic for the class • Advanced algorithm to start the class with new ideas • Show a series of different algorithmic techniques July 3, 2008 IUCEE: Algorithms 8

All of Computer Science is the Study of Algorithms July 3, 2008 IUCEE: Algorithms

All of Computer Science is the Study of Algorithms July 3, 2008 IUCEE: Algorithms 9

How to study algorithms • Zoology • Mine is faster than yours is •

How to study algorithms • Zoology • Mine is faster than yours is • Algorithmic ideas – Where algorithms apply – What makes an algorithm work – Algorithmic thinking

Introductory Problem: Stable Matching • Setting: – Assign TAs to Instructors – Avoid having

Introductory Problem: Stable Matching • Setting: – Assign TAs to Instructors – Avoid having TAs and Instructors wanting changes • E. g. , Prof A. would rather have student X than her current TA, and student X would rather work for Prof A. than his current instructor. July 3, 2008 IUCEE: Algorithms 11

Formal notions • Perfect matching • Ranked preference lists • Stability July 3, 2008

Formal notions • Perfect matching • Ranked preference lists • Stability July 3, 2008 m 1 w 1 m 2 w 2 IUCEE: Algorithms 12

Example (1 of 3) • • m 1 : w 1 w 2 m

Example (1 of 3) • • m 1 : w 1 w 2 m 2 : w 2 w 1: m 1 m 2 w 2: m 2 m 1 w 1 m 2 w 2

Example (2 of 3) • • m 1 : w 1 w 2 m

Example (2 of 3) • • m 1 : w 1 w 2 m 2 : w 1 w 2 w 1: m 1 m 2 w 2: m 1 m 2 Find a stable matching m 1 w 1 m 2 w 2

Example (3 of 3) • • m 1 : w 1 w 2 m

Example (3 of 3) • • m 1 : w 1 w 2 m 2 : w 2 w 1: m 2 m 1 w 2: m 1 m 2 m 1 w 1 m 2 w 2

A closer look • Stable matchings are not necessarily fair m 1 w 3

A closer look • Stable matchings are not necessarily fair m 1 w 3 w 1 w 2 m 2 w 1 : m 2 m 3 m 1 m 3 w 3 m 1 : w 1 w 2 w 3 m 2 : w 2 w 3 w 1 m 3 : w 2 : m 3 m 1 m 2 w 3 : m 1 m 2 m 3 How many stable matchings can you find?

Example m 1 : w 1 w 2 w 3 m 1 w 1

Example m 1 : w 1 w 2 w 3 m 1 w 1 m 2 w 2 m 3 w 3 m 2 : w 1 w 3 w 2 m 3 : w 1 w 2 w 3 w 1: m 2 m 3 m 1 w 2: m 3 m 1 m 2 w 3: m 3 m 1 m 2

Intuitive Idea for an Algorithm • m proposes to w – If w is

Intuitive Idea for an Algorithm • m proposes to w – If w is unmatched, w accepts – If w is matched to m 2 • If w prefers m to m 2, w accepts • If w prefers m 2 to m, w rejects • Unmatched m proposes to highest w on its preference list that m has not already proposed to

Algorithm Initially all m in M and w in W are free While there

Algorithm Initially all m in M and w in W are free While there is a free m w highest on m’s list that m has not proposed to if w is free, then match (m, w) else suppose (m 2, w) is matched if w prefers m to m 2 unmatch (m 2, w) match (m, w)

Does this work? • Does it terminate? • Is the result a stable matching?

Does this work? • Does it terminate? • Is the result a stable matching? • Begin by identifying invariants and measures of progress – m’s proposals get worse – Once w is matched, w stays matched – w’s partners get better (have lower w-rank)

Claim: The algorithm stops in at most n 2 steps • Why?

Claim: The algorithm stops in at most n 2 steps • Why?

When the algorithms halts, every w is matched • Why? • Hence, the algorithm

When the algorithms halts, every w is matched • Why? • Hence, the algorithm finds a perfect matching

The resulting matching is stable • Suppose m 1 w 1 m 2 w

The resulting matching is stable • Suppose m 1 w 1 m 2 w 2 – m 1 prefers w 2 to w 1 • How could this happen?

Result • Simple, O(n 2) algorithm to compute a stable matching • Corollary –

Result • Simple, O(n 2) algorithm to compute a stable matching • Corollary – A stable matching always exists

Basic Graph Algorithms • This material is necessary review • Terminology varies so cover

Basic Graph Algorithms • This material is necessary review • Terminology varies so cover it again • Formal setting for the course revisited – Big Oh notation again • Debatable on how much depth to go into formal proofs on simple algorithms July 3, 2008 IUCEE: Algorithms 25

Polynomial time efficiency • An algorithm is efficient if it has a polynomial run

Polynomial time efficiency • An algorithm is efficient if it has a polynomial run time • Run time as a function of problem size – Run time: count number of instructions executed on an underlying model of computation – T(n): maximum run time for all problems of size at most n • Why Polynomial Time? – Generally, polynomial time seems to capture the algorithms which are efficient in practice – The class of polynomial time algorithms has many good, mathematical properties July 3, 2008 IUCEE: Algorithms 26

Ignoring constant factors • Express run time as O(f(n)) • Emphasize algorithms with slower

Ignoring constant factors • Express run time as O(f(n)) • Emphasize algorithms with slower growth rates • Fundamental idea in the study of algorithms • Basis of Tarjan/Hopcroft Turing Award July 3, 2008 IUCEE: Algorithms 27

Formalizing growth rates • T(n) is O(f(n)) [T : Z+ R+] – If n

Formalizing growth rates • T(n) is O(f(n)) [T : Z+ R+] – If n is sufficiently large, T(n) is bounded by a constant multiple of f(n) – Exist c, n 0, such that for n > n 0, T(n) < c f(n) • T(n) is O(f(n)) will be written as: T(n) = O(f(n)) – Be careful with this notation July 3, 2008 IUCEE: Algorithms 28

Graph Theory • G = (V, E) – V – vertices – E –

Graph Theory • G = (V, E) – V – vertices – E – edges • Undirected graphs – Edges sets of two vertices {u, v} • Directed graphs – Edges ordered pairs (u, v) • Many other flavors – Edge / vertices weights – Parallel edges – Self loops July 3, 2008 IUCEE: Algorithms 29

Breadth first search • Explore vertices in layers – s in layer 1 –

Breadth first search • Explore vertices in layers – s in layer 1 – Neighbors of s in layer 2 – Neighbors of layer 2 in layer 3. . . s July 3, 2008 IUCEE: Algorithms 30

Testing Bipartiteness • If a graph contains an odd cycle, it is not bipartite

Testing Bipartiteness • If a graph contains an odd cycle, it is not bipartite July 3, 2008 IUCEE: Algorithms 31

Directed Graphs • A Strongly Connected Component is a subset of the vertices with

Directed Graphs • A Strongly Connected Component is a subset of the vertices with paths between every pair of vertices. July 3, 2008 IUCEE: Algorithms 32

Topological Sort • Given a set of tasks with precedence constraints, find a linear

Topological Sort • Given a set of tasks with precedence constraints, find a linear order of the tasks 142 July 3, 2008 143 321 322 341 326 370 378 IUCEE: Algorithms 401 421 431 33

Greedy Algorithms • Introduce an algorithmic paradigm • Its hard to give a formal

Greedy Algorithms • Introduce an algorithmic paradigm • Its hard to give a formal definition of greedy algorithms • Proof techniques are important – Need to formally prove that these things work • New material to students July 3, 2008 IUCEE: Algorithms 34

Greedy Algorithms • Solve problems with the simplest possible algorithm • The hard part:

Greedy Algorithms • Solve problems with the simplest possible algorithm • The hard part: showing that something simple actually works • Pseudo-definition – An algorithm is Greedy if it builds its solution by adding elements one at a time using a simple rule July 3, 2008 IUCEE: Algorithms 35

Greedy solution based on earliest finishing time Example 1 Example 2 Example 3 July

Greedy solution based on earliest finishing time Example 1 Example 2 Example 3 July 3, 2008 IUCEE: Algorithms 36

Scheduling all intervals • Minimize number of processors to schedule all intervals July 3,

Scheduling all intervals • Minimize number of processors to schedule all intervals July 3, 2008 IUCEE: Algorithms 37

Algorithm • Sort by start times • Suppose maximum depth is d, create d

Algorithm • Sort by start times • Suppose maximum depth is d, create d slots • Schedule items in increasing order, assign each item to an open slot • Correctness proof: When we reach an item, we always have an open slot July 3, 2008 IUCEE: Algorithms 38

Scheduling tasks • • Each task has a length ti and a deadline di

Scheduling tasks • • Each task has a length ti and a deadline di All tasks are available at the start One task may be worked on at a time All tasks must be completed • Goal minimize maximum lateness – Lateness = fi – di if fi >= di July 3, 2008 IUCEE: Algorithms 39

Example Time Deadline 2 2 4 3 2 3 July 3, 2008 Lateness 1

Example Time Deadline 2 2 4 3 2 3 July 3, 2008 Lateness 1 3 2 Lateness 3 IUCEE: Algorithms 40

Determine the minimum lateness Time Deadline 6 2 4 3 4 5 5 July

Determine the minimum lateness Time Deadline 6 2 4 3 4 5 5 July 3, 2008 12 IUCEE: Algorithms 41

Homework Scheduling • Tasks to perform • Deadlines on the tasks • Freedom to

Homework Scheduling • Tasks to perform • Deadlines on the tasks • Freedom to schedule tasks in any order July 3, 2008 IUCEE: Algorithms 42

Greedy Algorithm • Earliest deadline first • Order jobs by deadline • This algorithm

Greedy Algorithm • Earliest deadline first • Order jobs by deadline • This algorithm is optimal July 3, 2008 IUCEE: Algorithms 43

Analysis • Suppose the jobs are ordered by deadlines, d 1 <= d 2

Analysis • Suppose the jobs are ordered by deadlines, d 1 <= d 2 <=. . . <= dn • A schedule has an inversion if job j is scheduled before i where j > i • The schedule A computed by the greedy algorithm has no inversions. • Let O be the optimal schedule, we want to show that A has the same maximum lateness as O July 3, 2008 IUCEE: Algorithms 44

Shortest Paths and MST • These graph algorithms are presented in the framework of

Shortest Paths and MST • These graph algorithms are presented in the framework of greedy algorithms • Students will have seen the algorithms previously • Attempt is made to have students really understand the proofs – Classical results July 3, 2008 IUCEE: Algorithms 45

Assume all edges have non-negative cost Dijkstra’s Algorithm S = {}; d[s] = 0;

Assume all edges have non-negative cost Dijkstra’s Algorithm S = {}; d[s] = 0; d[v] = infinity for v != s While S != V Choose v in V-S with minimum d[v] Add v to S For each w in the neighborhood of v d[w] = min(d[w], d[v] + c(v, w)) 0 s July 3, 2008 1 u 1 2 4 2 v 4 y 3 1 1 x 2 2 3 2 5 z. IUCEE: Algorithms 46

Proof • Let v be a vertex in V-S with minimum d[v] • Let

Proof • Let v be a vertex in V-S with minimum d[v] • Let Pv be a path of length d[v], with an edge (u, v) • Let P be some other path to v. Suppose P first leaves S on the edge (x, y) – – P = Psx + c(x, y) + Pyv Len(Psx) + c(x, y) >= d[y] Len(Pyv) >= 0 Len(P) >= d[y] + 0 >= d[v] y x s u July 3, 2008 IUCEE: Algorithms v 47

http: //www. cs. utexas. edu/users/EWD/ • Edsger Wybe Dijkstra was one of the most

http: //www. cs. utexas. edu/users/EWD/ • Edsger Wybe Dijkstra was one of the most influential members of computing science's founding generation. Among the domains in which his scientific contributions are fundamental are – – – – algorithm design programming languages program design operating systems distributed processing formal specification and verification design of mathematical arguments July 3, 2008 IUCEE: Algorithms 48

Greedy Algorithm 1 Prim’s Algorithm • Extend a tree by including the cheapest out

Greedy Algorithm 1 Prim’s Algorithm • Extend a tree by including the cheapest out going edge 15 t 3 10 Construct the MST with Prim’s algorithm starting from vertex a Label the edges in order insertion Julyof 3, 2008 14 13 1 4 e c 20 2 5 7 b u 6 9 s 17 a 8 12 11 g f 22 16 v IUCEE: Algorithms 49

Application: Clustering • Given a collection of points in an rdimensional space, and an

Application: Clustering • Given a collection of points in an rdimensional space, and an integer K, divide the points into K sets that are closest together July 3, 2008 IUCEE: Algorithms 50

K-clustering July 3, 2008 IUCEE: Algorithms 51

K-clustering July 3, 2008 IUCEE: Algorithms 51

Recurrences • Big question on how much depth to cover recurrences – Full mathematical

Recurrences • Big question on how much depth to cover recurrences – Full mathematical coverage – Intuition • Students have little background on recurrences coming in – Generally not covered in earlier courses • My emphasis is in conveying the intuition – Students can look up the formulas when they need them July 3, 2008 IUCEE: Algorithms 52

T(n) <= 2 T(n/2) + cn; T(2) <= c; July 3, 2008 IUCEE: Algorithms

T(n) <= 2 T(n/2) + cn; T(2) <= c; July 3, 2008 IUCEE: Algorithms 53

Recurrence Analysis • Solution methods – Unrolling recurrence – Guess and verify – Plugging

Recurrence Analysis • Solution methods – Unrolling recurrence – Guess and verify – Plugging in to a “Master Theorem” July 3, 2008 IUCEE: Algorithms 54

Unrolling the recurrence July 3, 2008 IUCEE: Algorithms 55

Unrolling the recurrence July 3, 2008 IUCEE: Algorithms 55

Recurrences • Three basic behaviors – Dominated by initial case – Dominated by base

Recurrences • Three basic behaviors – Dominated by initial case – Dominated by base case – All cases equal – we care about the depth July 3, 2008 IUCEE: Algorithms 56

Recurrence Examples • T(n) = 2 T(n/2) + cn – O(n log n) •

Recurrence Examples • T(n) = 2 T(n/2) + cn – O(n log n) • T(n) = T(n/2) + cn – O(n) • More useful facts: – logkn = log 2 n / log 2 k – k log n = n log k July 3, 2008 IUCEE: Algorithms 57

T(n) = a. T(n/b) + f(n) July 3, 2008 IUCEE: Algorithms 58

T(n) = a. T(n/b) + f(n) July 3, 2008 IUCEE: Algorithms 58

What you really need to know about recurrences • Work per level changes geometrically

What you really need to know about recurrences • Work per level changes geometrically with the level • Geometrically increasing (x > 1) – The bottom level wins • Geometrically decreasing (x < 1) – The top level wins • Balanced (x = 1) – Equal contribution July 3, 2008 IUCEE: Algorithms 59

Strassen’s Algorithm Multiply 2 x 2 Matrices: | r s | | a b|

Strassen’s Algorithm Multiply 2 x 2 Matrices: | r s | | a b| |e g| = | t u| | c d| | f h| Where: p 1 = (b + d)(f + g) p 2= (c + d)e r = p 1 + p 4 – p 5 + p 7 s = p 3 + p 5 t = p 2 + p 5 u = p 1 + p 3 – p 2 + p 7 July 3, 2008 p 3= a(g – h) p 4= d(f – e) p 5= (a – b)h p 6= (c – d)(e + g) p 7= (b – d)(f + h) IUCEE: Algorithms 60

Divide and Conquer • Classical algorithmic technique • This is the texts weak point

Divide and Conquer • Classical algorithmic technique • This is the texts weak point • Students are probably already familiar with the sorting algorithms • Lectures generally show off classical results • FFT is a very hard result for the students – CSE students have little to tie it to July 3, 2008 IUCEE: Algorithms 61

Divide and Conquer Algorithms • Split into sub problems • Recursively solve the problem

Divide and Conquer Algorithms • Split into sub problems • Recursively solve the problem • Combine solutions • Make progress in the split and combine stages – Quicksort – progress made at the split step – Mergesort – progress made at the combine step July 3, 2008 IUCEE: Algorithms 62

Closest Pair Problem • Given a set of points find the pair of points

Closest Pair Problem • Given a set of points find the pair of points p, q that minimizes dist(p, q) July 3, 2008 IUCEE: Algorithms 63

Karatsuba’s Algorithm Multiply n-digit integers x and y Let x = x 1 2

Karatsuba’s Algorithm Multiply n-digit integers x and y Let x = x 1 2 n/2 + x 0 and y = y 1 2 n/2 + y 0 Recursively compute a = x 1 y 1 b = x 0 y 0 p = (x 1 + x 0)(y 1 + y 0) Return a 2 n + (p – a – b)2 n/2 + b Recurrence: T(n) = 3 T(n/2) + cn July 3, 2008 IUCEE: Algorithms 64

FFT, Convolution and Polynomial Multiplication • Preview – FFT - O(n log n) algorithm

FFT, Convolution and Polynomial Multiplication • Preview – FFT - O(n log n) algorithm • Evaluate a polynomial of degree n at n points in O(n log n) time – Computation of Convolution and Polynomial Multiplication (in O(n log n)) time July 3, 2008 IUCEE: Algorithms 65

Complex Analysis • • • Polar coordinates: reqi = cos q + i sin

Complex Analysis • • • Polar coordinates: reqi = cos q + i sin q a is a nth root of unity if an = 1 Square roots of unity: +1, -1 Fourth roots of unity: +1, -1, i, -i Eighth roots of unity: +1, -1, i, -i, b + ib, b - ib, -b + ib, -b - ib where b = sqrt(2) July 3, 2008 IUCEE: Algorithms 66

Polynomial Multiplication n-1 degree polynomials A(x) = a 0 + a 1 x +

Polynomial Multiplication n-1 degree polynomials A(x) = a 0 + a 1 x + a 2 x 2 + … +an-1 xn-1, B(x) = b 0 + b 1 x + b 2 x 2 + …+ bn-1 xn-1 C(x) = A(x)B(x) C(x)=c 0+c 1 x + c 2 x 2 + … + c 2 n-2 x 2 n-2 p 1, p 2, . . . , p 2 n A(p 1), A(p 2), . . . , A(p 2 n) B(p 1), B(p 2), . . . , B(p 2 n) C(p 1), C(p 2), . . . , C(p 2 n) C(pi) = A(pi)B(pi) July 3, 2008 IUCEE: Algorithms 67

FFT Algorithm // Evaluate the 2 n-1 th degree polynomial A at // w

FFT Algorithm // Evaluate the 2 n-1 th degree polynomial A at // w 0, 2 n, w 1, 2 n, w 2, 2 n, . . . , w 2 n-1, 2 n FFT(A, 2 n) Recursively compute FFT(Aeven, n) Recursively compute FFT(Aodd, n) for j = 0 to 2 n-1 A(wj, 2 n) = Aeven(w 2 j, 2 n) + wj, 2 n. Aodd(w 2 j, 2 n) July 3, 2008 IUCEE: Algorithms 68

Dynamic Programming • I consider this to be the most important part of the

Dynamic Programming • I consider this to be the most important part of the course • Goal is for them to be able to apply this technique to new problems • Key concepts need to be highlighted so students start to see the structure of dynamic programming solutions July 3, 2008 IUCEE: Algorithms 69

Dynamic Programming • The most important algorithmic technique covered in CSE 421 • Key

Dynamic Programming • The most important algorithmic technique covered in CSE 421 • Key ideas – Express solution in terms of a polynomial number of sub problems – Order sub problems to avoid recomputation July 3, 2008 IUCEE: Algorithms 70

Subset Sum Problem • Let w 1, …, wn = {6, 8, 9, 11,

Subset Sum Problem • Let w 1, …, wn = {6, 8, 9, 11, 13, 16, 18, 24} • Find a subset that has as large a sum as possible, without exceeding 50 July 3, 2008 IUCEE: Algorithms 71

Subset Sum Recurrence • Opt[ j, K ] the largest subset of {w 1,

Subset Sum Recurrence • Opt[ j, K ] the largest subset of {w 1, …, wj} that sums to at most K July 3, 2008 IUCEE: Algorithms 72

Subset Sum Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j

Subset Sum Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – w j] + wj) 4 3 2 1 0 0 0 0 0 {2, 4, 7, 10} July 3, 2008 IUCEE: Algorithms 73

Knapsack Problem • Items have weights and values • The problem is to maximize

Knapsack Problem • Items have weights and values • The problem is to maximize total value subject to a bound on weght • Items {I 1, I 2, … In} – Weights {w 1, w 2, …, wn} – Values {v 1, v 2, …, vn} – Bound K • Find set S of indices to: – Maximize July 3, 2008 S ie. Svi such that S ie. Swi IUCEE: Algorithms <= K 74

Knapsack Recurrence Subset Sum Recurrence: Opt[ j, K] = max(Opt[ j – 1, K],

Knapsack Recurrence Subset Sum Recurrence: Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – w j] + wj) Knapsack Recurrence: July 3, 2008 IUCEE: Algorithms 75

Knapsack Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j –

Knapsack Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – w j] + vj) 4 3 2 1 0 0 0 0 0 Weights {2, 4, 7, 10} Values: {3, 5, 9, 16} July 3, 2008 IUCEE: Algorithms 76

Optimal line breaking and hyphenation • Problem: break lines and insert hyphens to make

Optimal line breaking and hyphenation • Problem: break lines and insert hyphens to make lines as balanced as possible • Typographical considerations: – Avoid excessive white space – Limit number of hyphens – Avoid widows and orphans – Etc. July 3, 2008 IUCEE: Algorithms 77

Longest Common Subsequence • Application of dynamic programming • LCS is one of the

Longest Common Subsequence • Application of dynamic programming • LCS is one of the classic DP algorithms • Space efficiency discussed – Space more expensive than time – If we just want the length of the string, O(n) space is easy – Very clever algorithm allows reconstruction of LCS in O(n) space as well – Included as an advanced topic July 3, 2008 IUCEE: Algorithms 78

Longest Common Subsequence • C=c 1…cg is a subsequence of A=a 1…am if C

Longest Common Subsequence • C=c 1…cg is a subsequence of A=a 1…am if C can be obtained by removing elements from A (but retaining order) • LCS(A, B): A maximum length sequence that is a subsequence of both A and B ocurranec attacggct occurrence tacgacca July 3, 2008 IUCEE: Algorithms 79

LCS Optimization • A = a 1 a 2…am • B = b 1

LCS Optimization • A = a 1 a 2…am • B = b 1 b 2…bn • Opt[ j, k] is the length of LCS(a 1 a 2…aj, b 1 b 2…bk) If aj = bk, Opt[ j, k ] = 1 + Opt[ j-1, k-1 ] If aj != bk, Opt[ j, k] = max(Opt[ j-1, k], Opt[ j, k-1]) July 3, 2008 IUCEE: Algorithms 80

Dynamic Programming Computation July 3, 2008 IUCEE: Algorithms 81

Dynamic Programming Computation July 3, 2008 IUCEE: Algorithms 81

How good is this algorithm? • Is it feasible to compute the LCS of

How good is this algorithm? • Is it feasible to compute the LCS of two strings of length 100, 000 on a standard desktop PC? Why or why not. July 3, 2008 IUCEE: Algorithms 82

Algorithm Performance • O(nm) time and O(nm) space • On current desktop machines –

Algorithm Performance • O(nm) time and O(nm) space • On current desktop machines – n, m < 10, 000 is easy – n, m > 1, 000 is prohibitive • Space is more likely to be the bounding resource than time July 3, 2008 IUCEE: Algorithms 83

Computing LCS in O(nm) time and O(n+m) space • Divide and conquer algorithm •

Computing LCS in O(nm) time and O(n+m) space • Divide and conquer algorithm • Recomputing values used to save space July 3, 2008 IUCEE: Algorithms 84

Divide and Conquer Algorithm • Where does the best path cross the middle column?

Divide and Conquer Algorithm • Where does the best path cross the middle column? • For a fixed i, and for each j, compute the LCS that has ai matched with bj July 3, 2008 IUCEE: Algorithms 85

Divide and Conquer • A = a 1, …, am • Find j such

Divide and Conquer • A = a 1, …, am • Find j such that B = b 1, …, bn – LCS(a 1…am/2, b 1…bj) and – LCS(am/2+1…am, bj+1…bn) yield optimal solution • Recurse July 3, 2008 IUCEE: Algorithms 86

Algorithm Analysis • T(m, n) = T(m/2, j) + T(m/2, n-j) + cnm July

Algorithm Analysis • T(m, n) = T(m/2, j) + T(m/2, n-j) + cnm July 3, 2008 IUCEE: Algorithms 87

Shortest Paths • Shortest paths revisited from the dynamic programming perspective – Dynamic programming

Shortest Paths • Shortest paths revisited from the dynamic programming perspective – Dynamic programming needed if edges have negative cost July 3, 2008 IUCEE: Algorithms 88

Shortest Path Problem • Dijkstra’s Single Source Shortest Paths Algorithm – O(mlog n) time,

Shortest Path Problem • Dijkstra’s Single Source Shortest Paths Algorithm – O(mlog n) time, positive cost edges • General case – handling negative edges • If there exists a negative cost cycle, the shortest path is not defined • Bellman-Ford Algorithm – O(mn) time for graphs with negative cost edges July 3, 2008 IUCEE: Algorithms 89

Shortest paths with a fixed number of edges • Find the shortest path from

Shortest paths with a fixed number of edges • Find the shortest path from v to w with exactly k edges • Express as a recurrence – Optk(w) = minx [Optk-1(x) + cxw] – Opt 0(w) = 0 if v=w and infinity otherwise July 3, 2008 IUCEE: Algorithms 90

If the pointer graph has a cycle, then the graph has a negative cost

If the pointer graph has a cycle, then the graph has a negative cost cycle • If P[w] = x then M[w] >= M[x] + cost(x, w) – Equal when w is updated – M[x] could be reduced after update • Let v 1, v 2, …vk be a cycle in the pointer graph with (vk, v 1) the last edge added – Just before the update • M[vj] >= M[vj+1] + cost(vj+1, vj) for j < k • M[vk] > M[v 1] + cost(v 1, vk) – Adding everything up v 1 v 4 v 2 v 3 • 0 > cost(v 1, v 2) + cost(v 2, v 3) + … + cost(vk, v 1) July 3, 2008 IUCEE: Algorithms 91

Foreign Exchange Arbitrage USD 1. 2 EUR USD EUR CAD 0. 6 USD July

Foreign Exchange Arbitrage USD 1. 2 EUR USD EUR CAD 0. 6 USD July 3, 2008 1. 2 EUR 1. 2 ------ 1. 6 CAD 0. 8 0. 6 ----- 0. 8 EUR USD ------ 0. 8 1. 6 CAD IUCEE: Algorithms 92

Network Flow • This topic move the course into combinatorial optimization • Key is

Network Flow • This topic move the course into combinatorial optimization • Key is to understand what the network flow problem is, and the basic combinatorial theory behind it – Many more sophisticated algorithms not covered July 3, 2008 IUCEE: Algorithms 93

Flow assignment and the residual graph u u 15/20 0/10 5 10 15 15/30

Flow assignment and the residual graph u u 15/20 0/10 5 10 15 15/30 s t 15 s 15 t 5 5/10 20/20 5 v July 3, 2008 20 v IUCEE: Algorithms 94

Find a maximum flow 20 a 20 5 30 s 20 5 b 20

Find a maximum flow 20 a 20 5 30 s 20 5 b 20 c July 3, 2008 d g 5 5 20 5 e 5 h 20 5 10 5 20 20 f IUCEE: Algorithms 10 20 30 t 25 i 95

Ford-Fulkerson Algorithm (1956) while not done Construct residual graph GR Find an s-t path

Ford-Fulkerson Algorithm (1956) while not done Construct residual graph GR Find an s-t path P in GR with capacity b > 0 Add b units along in G If the sum of the capacities of edges leaving S is at most C, then the algorithm takes at most C iterations July 3, 2008 IUCEE: Algorithms 96

Max. Flow – Min. Cut Theorem • There exists a flow which has the

Max. Flow – Min. Cut Theorem • There exists a flow which has the same value of the minimum cut • Proof: Consider a flow where the residual graph has no s-t path with positive capacity • Let S be the set of vertices in GR reachable from s with paths of positive capacity s July 3, 2008 t IUCEE: Algorithms 97

Network Flow Applications • Applications of network flow are very powerful • Problems that

Network Flow Applications • Applications of network flow are very powerful • Problems that look very unlike flow can be converted to network flow • Brings up theme of problem mapping July 3, 2008 IUCEE: Algorithms 98

Problem Reduction • Reduce Problem A to Problem B – Convert an instance of

Problem Reduction • Reduce Problem A to Problem B – Convert an instance of Problem A to an instance Problem B – Use a solution of Problem B to get a solution to Problem A • Practical – Use a program for Problem B to solve Problem A • Theoretical – Show that Problem B is at least as hard as Problem A July 3, 2008 IUCEE: Algorithms 99

Bipartite Matching • A graph G=(V, E) is bipartite if the vertices can be

Bipartite Matching • A graph G=(V, E) is bipartite if the vertices can be partitioned into disjoints sets X, Y • A matching M is a subset of the edges that does not share any vertices • Find a matching as large as possible July 3, 2008 IUCEE: Algorithms 100

Converting Matching to Network Flow s July 3, 2008 IUCEE: Algorithms t 101

Converting Matching to Network Flow s July 3, 2008 IUCEE: Algorithms t 101

Open Pit Mining • Each unit of earth has a profit (possibly negative) •

Open Pit Mining • Each unit of earth has a profit (possibly negative) • Getting to the ore below the surface requires removing the dirt above • Test drilling gives reasonable estimates of costs • Plan an optimal mining operation July 3, 2008 IUCEE: Algorithms 102

Mine Graph July 3, 2008 -4 -3 -2 -1 -1 -3 3 -1 4

Mine Graph July 3, 2008 -4 -3 -2 -1 -1 -3 3 -1 4 -7 -10 -2 8 3 -10 IUCEE: Algorithms 103

Setting the costs s • If p(v) > 0, 3 – cap(v, t) =

Setting the costs s • If p(v) > 0, 3 – cap(v, t) = p(v) – cap(s, v) = 0 3 1 1 -3 • If p(v) < 0 – cap(s, v) = -p(v) – cap(v, t) = 0 -1 -3 • If p(v) = 0 0 3 2 – cap(s, v) = 0 – cap(v, t) = 0 2 1 3 t July 3, 2008 IUCEE: Algorithms 104

Image Segmentation • Separate foreground from background July 3, 2008 IUCEE: Algorithms 105

Image Segmentation • Separate foreground from background July 3, 2008 IUCEE: Algorithms 105

Image analysis • ai: value of assigning pixel i to the foreground • bi:

Image analysis • ai: value of assigning pixel i to the foreground • bi: value of assigning pixel i to the background • pij: penalty for assigning i to the foreground, j to the background or vice versa • A: foreground, B: background • Q(A, B) = S{i in A}ai + S{j in B}bj - S{(i, j) in E, i in A, j in B}pij July 3, 2008 IUCEE: Algorithms 106

Mincut Construction s av u pvu puv v bv t July 3, 2008 IUCEE:

Mincut Construction s av u pvu puv v bv t July 3, 2008 IUCEE: Algorithms 107

NP Completeness • Theory topic from the algorithmic perspective • Students will see different

NP Completeness • Theory topic from the algorithmic perspective • Students will see different aspects of NPCompleteness in other courses • Complexity theory course will prove Cook’s theorem • The basic goal is to remind students of specific NP complete problems • Material is not covered in much depth because of the “last week of the term” problem July 3, 2008 IUCEE: Algorithms 108

Theory of NP-Completeness The Universe NP-Complete NP P July 3, 2008 IUCEE: Algorithms 109

Theory of NP-Completeness The Universe NP-Complete NP P July 3, 2008 IUCEE: Algorithms 109

What is NP? • Problems solvable in non-deterministic polynomial time. . . • Problems

What is NP? • Problems solvable in non-deterministic polynomial time. . . • Problems where “yes” instances have polynomial time checkable certificates July 3, 2008 IUCEE: Algorithms 110

NP-Completeness • A problem X is NP-complete if – X is in NP –

NP-Completeness • A problem X is NP-complete if – X is in NP – For every Y in NP, Y <P X • X is a “hardest” problem in NP • If X is NP-Complete, Z is in NP and X <P Z – Then Z is NP-Complete July 3, 2008 IUCEE: Algorithms 111

History • Jack Edmonds – Identified NP • Steve Cook – Cook’s Theorem –

History • Jack Edmonds – Identified NP • Steve Cook – Cook’s Theorem – NP-Completeness • Dick Karp – Identified “standard” collection of NP-Complete Problems • Leonid Levin – Independent discovery of NP-Completeness in USSR July 3, 2008 IUCEE: Algorithms 112

Populating the NP-Completeness Universe • • • Circuit Sat <P 3 -SAT <P Independent

Populating the NP-Completeness Universe • • • Circuit Sat <P 3 -SAT <P Independent Set 3 -SAT <P Vertex Cover Independent Set <P Clique 3 -SAT <P Hamiltonian Circuit <P Traveling Salesman 3 -SAT <P Integer Linear Programming 3 -SAT <P Graph Coloring 3 -SAT <P Subset Sum <P Scheduling with Release times and deadlines July 3, 2008 IUCEE: Algorithms 113

Find a satisfying truth assignment (x || y || z) && (!x || !y

Find a satisfying truth assignment (x || y || z) && (!x || !y || !z) && (!x || y) && (x || !y) && (y || !z) && (!y || z) July 3, 2008 IUCEE: Algorithms 114

IS <P VC • Lemma: A set S is independent iff V-S is a

IS <P VC • Lemma: A set S is independent iff V-S is a vertex cover • To reduce IS to VC, we show that we can determine if a graph has an independent set of size K by testing for a Vertex cover of size n - K July 3, 2008 IUCEE: Algorithms 115

Graph Coloring • NP-Complete – Graph K-coloring – Graph 3 -coloring July 3, 2008

Graph Coloring • NP-Complete – Graph K-coloring – Graph 3 -coloring July 3, 2008 • Polynomial – Graph 2 -Coloring IUCEE: Algorithms 116

What we don’t know • P vs. NP NP-Complete NP = P NP P

What we don’t know • P vs. NP NP-Complete NP = P NP P July 3, 2008 IUCEE: Algorithms 117

What about ‘negative instances’ • How do you show that a graph does not

What about ‘negative instances’ • How do you show that a graph does not have a Hamiltonian Circuit • How do you show that a formula is not satisfiable? July 3, 2008 IUCEE: Algorithms 118

What about ‘negative instances’ • How do you show that a graph does not

What about ‘negative instances’ • How do you show that a graph does not have a Hamiltonian Circuit • How do you show that a formula is not satisfiable? July 3, 2008 IUCEE: Algorithms 119