LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Subsequence A subsequence of a sequence/string X = is a sequence obtained by deleting 0 or more elements from X. Example: sudan is a subsequence of sesquipedalian.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
For(int i = 1; i
Dynamic Programming.
Programming Languages and Paradigms The C Programming Language.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Structures Spring 2013Programming and Data Structure1.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
1 DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Algorithms Dynamic programming Longest Common Subsequence.
CS420 Lecture 9 Dynamic Programming. Optimization Problems In optimization problems a set of choices are to be made to arrive at an optimum, and sub problems.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
Analysis of Algorithms CS 477/677
Центр атестації педагогічних працівників 2014
Галактики і квазари.
Характеристика ІНДІЇ.
Процюк Н.В. вчитель початкових класів Боярської ЗОШ І – ІІІ ст №4
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Building Java Programs Chapter 13 Searching reading: 13.3.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Dynamic Programming.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Духовні символи Голосіївського району
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
1 MIPS Assembly Language Programming CDA 3101 Discussion Section 04.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Programming Languages and Paradigms
Trees.
Programming Paradigms
Review & Lab assignments
Проф. д-р Васил Цанов, Институт за икономически изследвания при БАН
Електронни услуги на НАП
РАЙОНЕН СЪД - БУРГАС РАБОТНА СРЕЩА СЪС СЪДЕБНИТЕ ЗАСЕДАТЕЛИ ПРИ РАЙОНЕН СЪД – БУРГАС 21 ОКТОМВРИ 2016 г.
Сътрудничество между полицията и другите специалисти в България
НАЦИОНАЛНА АГЕНЦИЯ ЗА ПРИХОДИТЕ
ДОБРОВОЛЕН РЕЗЕРВ НА ВЪОРЪЖЕНИТЕ СИЛИ НА РЕПУБЛИКА БЪЛГАРИЯ
ПО ПЧЕЛАРСТВО ЗА ТРИГОДИШНИЯ
от проучване на общественото мнение,
Васил Големански Ноември, 2006
Програма за развитие на селските райони
ОПЕРАТИВНА ПРОГРАМА “АДМИНИСТРАТИВЕН КАПАЦИТЕТ”
БАЛИСТИКА НА ТЯЛО ПРИ СВОБОДНО ПАДАНЕ В ЗЕМНАТА АТМОСФЕРА
МЕДИЦИНСКИ УНИВЕРСИТЕТ – ПЛЕВЕН
Правна кантора “Джингов, Гугински, Кючуков & Величков”
Безопасност на движението
When a function is called...
Class StudentList class StudentList { private: int numStudents;
Structures in c By Anand George.
Analysis of Algorithms CS 477/677
Presentation transcript:

LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return max(lcs(x, y, i-1, j), lcs(x, y, i, j-1)); end; int x = lcs(“abdabcd”,”cabdaaxy”,7,8);

LCS Dynamic Programming Version int A{0..m,0..n); A{1..m,1..n) = -1; A(0,0..n) = 0; A(1..m,0) = 0; int function lcs (x, y, i, j) begin if (A[i,j] < 0) if (x[i] = y[j]) A[i,j] = lcs(x, y, i-1, j-1) + 1; else A[i,j] = max(lcs(x, y, i-1, j), lcs(x, y, i, j-1)); return A[i,j]; end;

LCS Dynamic Programming Version int c(0..m,0..n); void function lcs (x, y) begin m = length(x); n = length(y); c[1..m,0] = 0; c[0,0..n] = 0; for i = 1 to m for j = 1 to n if (x[i] = y[j]) c[i,j] = c[i-1,j-1] + 1; else if c[i-1,j] >= c[i,j-1] c[i,j] = c[i-1,j]; else c[i,j] = c[i,j-1]; end; // After execution, c[m,n] contains the length of the lcs

LCS Dynamic Programming Version int c(0..m,0..n); char b(0..m,0..n); void function lcs (x, y) begin m = length(x); n = length(y); c[1..m,0] = 0; c[0,0..n] = 0; for i = 1 to m for j = 1 to n if (x[i] = y[j]) c[i,j] = c[i-1,j-1] + 1; b[i,j] = “\”; else if c[i-1,j] >= c[i,j-1] c[i,j] = c[i-1,j]; b[i,j] = “|”; else c[i,j] = c[i,j-1]; b[i,j] = “--”;// After execution, b contains a description of the subsequence end;

LCS Print function print-lcs (b, x, i, j) begin if (i = 0) or (j =0) return; else if b[i,j] = “\” print-lcs (b,x, i-1, j-1); else if b[i,j] = “|” print-lcs(b, x, i-1,j) else // b[i,j] = “--” print-lcs(b, x, i, j-1) end; // Technically b is not necessary

Assembly Line Scheduling Non-Dynamic Version array e[1..2] = {2,4};// Entry times for each assembly line array x[1..2] = {3,2};// Exit times for each assembly line array a[1..2,1..6] = {{7,9,3,4,8,4}, {8,5,6,4,5,7}}; // Station times array t[1..2, 1..5] = {{2,3,1,3,4}, {2,1,2,2,1}}; // Transfer times

ALS Non-Dynamic Version int function shortest_schedule_time (int i, int j)// i is 1..2, j is 1..n begin if (j = 1) return e[i] + a[i,j]; else int f1 = shortest_schedule_time(1, j-1); int f2 = shortest_schedule_time(2, j-1); if (i = 1)// could be eliminated with a two-element array return min( f1 + a[1,j], f2 + t[2,j-1] + a[1,j]); else // i =2 return min( f2 + a[2,j], f1 + t[1,j-1] + a[2,j])); end; void main() { int f1 = shortest_schedule_time(1, n); int f2 = shortest_schedule_time(2, n); print(min(f1 + x[1], f2 + x[2])); }

ALS Dynamic Version #1 struct f_type = {int f1, int f2}; f_type function shortest_schedule_time (j) begin if (j = 1) return (e[1] + a[1,j], e[2] + a[2,j]); else f_type f; f = shortest_schedule_time(j-1); return (min( f.f1 + a[1,j], f.f2 + t[2,j-1] + a[1,j]), min( f.f2 + a[2,j], f.f1 + t[1,j-1] + a[2,j])); end; void main() { f_type f = shortest_schedule_time(n); print(min(f.f1 + x[1], f.f2 + x[2])); }

Assembly Line Scheduling Non-Dynamic Version array e[1..3] = {2,4};// Entry times for each assembly line array x[1..3] = {3,2};// Exit times for each assembly line array a[1..3,1..6] = {{7,9,3,4,8,4}, {8,5,6,4,5,7}}; // Station times array t[1..3, 1..3, 1..5] = {{2,3,1,3,4}, {2,1,2,2,1}}; // Transfer times

ALS Non-Dynamic Version int function shortest_schedule_time (int i, int j)// i is 1..3, j is 1..n begin if (j = 1) return e[i] + a[i,j]; else int f1 = shortest_schedule_time(1, j-1); int f2 = shortest_schedule_time(2, j-1); int f3 = shortest_schedule_time(3, j-1); if (i = 1) // could be eliminated with a two-element array return min( f1 + a[1,j], f2 + t[2,1,j-1] + a[1,j], f3 + t[3,1,j-1] + a[1,j]); else if (i = 2) return min( f2 + a[2,j], f1 + t[1,2, j-1] + a[2,j], f3 + t[3,2,j-1] + a[2,j]); else // i = 3 return min( f3 + a[3,j], f1 + t[1,3, j-1] + a[3,j], f2 + t[2,3,j-1] + a[3,j]); end; void main() { int f1 = shortest_schedule_time(1, n); int f2 = shortest_schedule_time(2, n); int f3 = shortest_schedule_time(3, n); print(min(f1 + x[1], f2 + x[2], f3 + x[3])); }

ALS Dynamic Version #1 struct f_type = {int f1, int f2, int f3}; f_type function shortest_schedule_time (j) begin if (j = 1) return (e[1] + a[1,j], e[2] + a[2,j], e[3] + a[3,j]); else f_type f; f = shortest_schedule_time(j-1); return (min( f.f1 + a[1,j], f.f2 + t[2,1,j-1] + a[1,j], f.f3 + t[3,1,j-1] + a[1,j]), min( f.f2 + a[2,j], f.f1 + t[1,2,j-1] + a[2,j], f.f3 + t[3,2,j-1] + a[3,j]), min( f.f3 + a[3,j], f.f1 + t[1,3,j-1] + a[3,j], f.f2 + t[2,3,j-1] + a[3,j]), end; void main() { f_type f = shortest_schedule_time(n); print(min(f.f1 + x[1], f.f2 + x[2], f.f3 + x[3])); }