Dynamic Programming Tutorial &Practice on Longest Common Sub-sequence.

Slides:



Advertisements
Similar presentations
Longest Common Subsequence
Advertisements

CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Overview What is Dynamic Programming? A Sequence of 4 Steps
Algorithms Dynamic programming Longest Common Subsequence.
Chapter 7 Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
Chapter 7 Dynamic Programming 7.
§ 8 Dynamic Programming Fibonacci sequence
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming Code
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
7 -1 Chapter 7 Dynamic Programming Fibonacci Sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Optimal binary search trees
1 Dynamic Programming Jose Rolim University of Geneva.
Lecture 7 Topics Dynamic Programming
Dynamic Programming From An Excel Perspective. Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow.
Algorithms and Data Structures Lecture X
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dynamic Programming UNC Chapel Hill Z. Guo.
Chapter 5 Dynamic Programming 2001 년 5 월 24 일 충북대학교 알고리즘연구실.
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt.
CS 8833 Algorithms Algorithms Dynamic Programming.
CSC 211 Data Structures Lecture 13
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Dynamic Programming David Kauchak cs302 Spring 2013.
LIMITATIONS OF ALGORITHM POWER
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Chapter 15 Dynamic Programming Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Dynamic Programming Tutorial &Practice on Longest Common Sub-sequence.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Dynamic Programming Several problems Principle of dynamic programming
Chapter 8 Dynamic Programming.
Dynamic Programming Comp 122, Fall 2004.
CSCE 411 Design and Analysis of Algorithms
Unit 4: Dynamic Programming
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Chapter 8 Dynamic Programming
Unit-4: Dynamic Programming
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Longest Common Subsequence
Dynamic Programming Comp 122, Fall 2004.
Lecture 8. Paradigm #6 Dynamic Programming
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Longest Common Subsequence
Dynamic Programming II DP over Intervals
Algorithms and Data Structures Lecture X
Advanced Analysis of Algorithms
Longest Common Subsequence
Presentation transcript:

Dynamic Programming Tutorial &Practice on Longest Common Sub-sequence Optimal Binary Search Tree

Characteristics of Dynamic Programming Developed by Richard Bellman in To provide accurate solution with the help of series of decisions. Define a small part of problem and find out the optimal solution to small part. Enlarge the small part to next version and again find optimal solution. Continue till problem is solved. Find the complete solution by collecting the solution of optimal problems in bottom up manner.

Characteristics Types of problems and solution strategies. 1. Problem can be solved in stages. 2. Each problem has number of states 3. The decision at a stage updates the state at the stage into the state for next stage. 4. Given the current state, the optimal decision for the remaining stages is independent of decisions made in previous states. 5. There is recursive relationship between the value of decision at a stage and the value of optimum decisions at previous stages.

Characteristics How memory requirement is reduce: How recursion overheads are converted into advantage - By storing the results of previous n-2 computations in computation of n-1 stage and will be used for computation of “n” stage. Not very fast, but very accurate It belongs to smart recursion class, in which recursion results and decisions are used for next level computation. Hence not only results but decisions are also generated. Final collection of results: Bottom Up Approach.

Longest Common Subsequence Given two strings of characters, LCS is a method to find a longest subsequence either continuous or non-continuous and is common in both the strings. The subsequence generation starts from comparison of first character of both the given strings. The two strings may or may not be of equal length. Let “A” and “B” be two strings of length “m” and “n” respectively, and let “i” and “j” be two pointers to handle the two strings. Let matrix c[m,n] be the storage matrix to store the results of comparison related with two strings. The C[m,n] matrix will be handled using pointer “i” and “j”.

If i=0 and j=0, then c[i,j] = 0 If a[1..i] and b[1..j] is compared and a[i] is not equal to b[j]. Then the comparison will be either In between a[1..i-1] and b[1..j]ora[1..i] and b[1..j-1] Then c[i,j] = max[c[i-1,j], c[I,j-1]] If a[1..i] and b[1..j] is compared and a[i] is equal to b[j]. Then c[i,j] = c[i-1,j-1] + 1

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 2D 3V 4E 5G 6T 7A H = Halt Case: String A = No Character String B = All Characters Result = 0 and Halt State

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 2D 3V 4E 5G 6T 7A H = Halt Case: String A = All Characters String B = No Character Result = 0 and Halt State

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 1/D H = Halt D = Diagonal S = Side Case: String A = G String B = G Result = Match Use diagonal value from the cell and add 1 to diagonal value 0+1 i/j GVCEKST 00/H 1G 1/D1/S Case: String A = G String B = V Result = No Match Use Side or Upper cell value and select maximum value If both values are same select UPPER cell value 1/S

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 1/D 1/S 2D0/H 1/U H = Halt D = Diagonal S = Side Case: String A = D String B = G Result = No Match Since the value in Upper cell is greater than Side cell Use UPPER Cell value

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 1/D1/S 2D0/H 1/U H = Halt D = Diagonal S = Side Case: String A = D String B = V Result = No Match Since the value in Upper cell is same as Side cell Use UPPER Cell value 1/U

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 1/D1/S 2D0/H1/U 3V0/H1/U2/D2/S 4E0/H1/U2/U 3/D3/S 5G0/H1/D2/U 3/U 6T0/H1/U2/U 3/U 4/D 7A0/H1/U2/U 3/U 4/U

Example STRING A = G D V E G T A B = G V C E K S T i/j GVCEKST 00/H 1G 1/D1/S 2D0/H1/U 3V0/H1/U2/D2/S 4E0/H1/U2/U 3/D3/S 5G0/H1/D2/U 3/U 6T0/H1/U2/U 3/U 4/D 7A0/H1/U2/U 3/U 4/U

Example:2 STRING A = X M J Y A U ZB = M Z J A W X U i/j01X2M3J4Y5A6U7Z 00/H 1M0/H1/U1/D1/S 2Z0/H1/U 2/D 3J0/H1/U 2/D2/S 4A0/H1/U 2/U 3/D3/S 5W0/H1/U 2/U 3/U 6X0/H1/D1/U2/U 3/U 7U0/H1/U 2/U 3/U4/D4/S

Algorithm: Assumptions: Let the two strings be present in array “a” size “m” and array “b” size “n” handled using pointers “i“ and “j”. The resultant array will be c[m,n] : one instance c[i,j]. The array “c” will be structure: c[i,j].val and c[i,j].dir = “u”, “s”, “d” and “h” Algorithm lcs (a,b: c) { for i = 0 to m do for j = 0 to n do if a[i]=0 or b[j]=0 { c[i,j].val = 0; c[i,j].dir=‘h’ } else { if (a[i] ≠ b[j]) c[i,j].val = max [c[i-1, j].val, c[i,j-1].val] if(c[i-1, j].val >= c[i,j-1].val) c[i,j].dir = ‘u’ else c[i,j].dir=‘s’ else c[i,j].val = c[i-1,j-1].val + 1 c[i,j].dir = ‘d’ }

Algorithm: Printing LCS: Assumptions: Let “A” be the given array. Let “C” be the array containing the details of LCS Algorithm print_lcs (a,c,i,j) { if (i=0 or j=0) return; if(c[i,j] = ‘d’ { print_lcs(a,c,i-1,j-1) print(a[i]) } else { if(c[i,j] = ‘u’ print_lcs(a,c,i-1,j) else print_lcs(a,c,i,j-1) }

Exercises Question 1: String 1:E X P O N E N T I A L String 2:P O L Y N O M I A L String 1:SUBSEQUENCE String 2:CONSEQUENCES

Optimal Binary Search Tree (OBST) Drawback of BST: The first value in the sequence is always used as ROOT node. If the data is present in sorted order, BST will be in the form of linked list. This will increase search time and complexity equation will be not valid. For example if dictionary word are to be stored in BST, it will be in the form of linked list: Apple, Bat, Cat, Dog, Elephant, Fish, Gun, Horse, Ink, Jug, King, Lion Since all the start characters are Greater than “A”, all the values will be on the Right side of the Apple.

Example of word list

Optimal Binary Search Tree Need: Mechanism to decide out of the available values, which value should be used as ROOT, so that the tree is balanced and COST of searching is minimum. Requirement: For selection, the probability of data in the domain text is required. There are two values: Successful probability value and Unsuccessful probability value. Application: OBST is applied to generate the tree for the set of values (keys) with defined successful and unsuccessful search probabilities.

Example: Creation of matrices: “w” = probability matrix “e” = evolution matrix “r” = root matrix Dimensions: n = Total number of keys given in the search space Matrix “w” = [1..n+1, 0..n] Matrix “e” = [1..n+1, 0..n] For example number of keys are K1, K2, K3, K4 then n=4 and matrix “w” and “e” will have dimensions as: [1..5, 0..4]

Optimal Binary Search Tree i01234 pi qi sum

W # ## ### ####0.15 E /10.91/11.54/22.63/3 2# /20.90/31.83/4 3## /31.16/4 4### /4 5####0.15 i01234 pi qi sum

Example 2: OBST i pi qi sum

Solution: Example 2: Animation W e i pi qi sum

Empty matrix structure and formula w e i pi qi sum = =

Empty matrix structure and formula w e i pi qi sum = =

Empty matrix structure and formula W e i pi qi sum

Empty matrix structure and formula e e / i pi qi sum e / / / / / e[1,1] = e[1,0]+e[2,1]+w[1,1]   0.45 e[2,2] = e[2,1]+2[3,2]+w[2,2]   0.50

Empty matrix structure and formula e e /1 1.00/ / / / / e /1 1.00/ / / / e[1,2] = Two options for root between values 1 and 2 are r=1 and r=2 e[1,2] will be minimum of e[1,0]+e[2,2]+w[2,2]  =1.10 [r=1] e[1,1]+e[3,2]+w[2,2]  =1.00 [r=2] /3 First Second 1.00 Final 0.80/2

Empty matrix structure and formula e e /1 1.00/ / /30.60/ /40.85/ /

Empty matrix structure and formula e e /1 1.00/ / / /30.60/ /40.85/ / e /1 1.00/ / /2 1.30/ /30.60/ /40.85/ / /1 1.00/ / /2 1.30/ /30.60/41.25/4, /40.85/ /

Empty matrix structure and formula e e /1 1.00/ / / /2 1.30/ /30.60/41.25/4, /40.85/ / e /1 1.00/ / / /2 1.30/ / /30.60/41.25/4, /40.85/ /

Empty matrix structure and formula /1 1.00/21.30/21.80/22.70/ /2 1.30/22.25/ /30.60/41.25/4, /40.85/ / k2 K3,K4,K5 K1 Refer the cell 35 from the Matrix and Find root Information Take next root as 4 or 5 Consider : 4 Cost of Tree And main root

Empty matrix structure and formula /1 1.00/21.30/21.80/22.70/ /2 1.30/22.25/ /30.60/41.25/4, /40.85/ / k2 K3,K4,K5 K1 k3 k1 k3k5

Empty matrix structure and formula / / / / / / / / / / / 4, / / / k2 K3,K4,K5 K1 k3 k1 k3k5 i pi qi sum do d1 d2 d3 d4 d5 Verification: 1x0.20 = x( ) = x( ) + 3x( )=1.20 4x( ) = 1.00 TOTAL = 2.70 L1 L2 L3 L4 pi qi