0% found this document useful (0 votes)
3 views13 pages

Algo 2545

The document presents a course on Algorithm Design and Analysis, focusing on the basics of recursion and dynamic programming, specifically the Longest Common Subsequence (LCS) problem. It explains dynamic programming as a method for solving complex problems by breaking them into simpler sub-problems and discusses recursion as a technique where a function calls itself. The document includes examples and code implementations to illustrate these concepts.

Uploaded by

momin.swe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Algo 2545

The document presents a course on Algorithm Design and Analysis, focusing on the basics of recursion and dynamic programming, specifically the Longest Common Subsequence (LCS) problem. It explains dynamic programming as a method for solving complex problems by breaking them into simpler sub-problems and discusses recursion as a technique where a function calls itself. The document includes examples and code implementations to illustrate these concepts.

Uploaded by

momin.swe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

PRESENTATRION

Course code: SE 214


Course title: Algorithm Design and Analysis

Submitted to: Dr. Md. Asraf Ali


Department of SWE
Daffodil International university

Submitted by: Md. Momin


ID : 182-35-2545
Department of SWE
Daffodil International university
TOPIC
BASIC OF RECURSION AND EXAMPLE DYNAMIC
PROGRAMMING. (LCS).
WHAT IS DYNAMIC PROGRAMMING
• DYNAMIC PROGRAMMING IS A VERY POWERFUL ALGORITHMIC PARADIGM IN WHICH A
PROBLEM IS SOLVED BY IDENTIFYING A COLLECTION OF SUB-PROBLEMS AND
TACKLING THEM ONE BY ONE, SMALLEST FIRST, USING THE ANSWERS TO SMALL
PROBLEMS TO HELP FIGURE OUT LARGER ONES, UNTIL THE WHOLE LOT OF THEM IS
SOLVED.

• DYNAMIC PROGRAMMING (ALSO KNOWN AS DYNAMIC OPTIMIZATION) IS A METHOD


FOR SOLVING A COMPLEX PROBLEM BY BREAKING IT DOWN INTO A COLLECTION OF
SIMPLER SUB-PROBLEMS, SOLVING EACH OF THOSE SUB-PROBLEMS JUST ONCE, AND
STORING THEIR SOLUTIONS – IDEALLY, USING A MEMORY-BASED DATA STRUCTURE.
WHAT IS RECURSION:
• IN SIMPLE LANGUAGE RECURSION IS THE REPEATED
OCCURRENCE OF A PARTICULAR EVENT.
• IN THE PROGRAMMING LANGUAGE RECURSION IS THE REPEATED
OCCURRENCE OF A PROCEDURE AS A RESULT OF THAT
PROCEDURE IT CALL ITSELF .
• IT’S A METHOD THAT CAN CALL ITSELF IN THE PROGRAM.
• AS WE KNOW ABOUT RECURSION AND DYNAMIC PROGRAMMING. HERE WE
WILL SEE AN EXAMPLE OF USING RECURSION IN ONE OF THE FAMOUS
DYNAMIC PROGRAMMING OPERATION KNOWN AS LCS (LONGEST COMMON
SUBSEQUENCE).

• BEFORE THAT LETS TAKE A LOOK AT THE BASICS OF RECURSION. THEN WE


WILL PROCEED TO LCS.
Base case:
When we use condition to stop its forever going loop.

Void call(int m)
{
if(m>5){
return 0;
}
call(m+1);
printf(“%d\n”,m);
}
Int main()
{
call(1);
return 0;
}

In the above the if codition is used to stop the program when it agrees with the
condition.
Recursive calls:
Calls to the current method.

Each recursive calls Should be defined so that it makes progress towards the base
case.
Types of Recursion:
Single and Multiple recursion:

Recursion that contain single self reference is called single Recursion.


Example: the example I showed on the previouse slide.

Recursion that contain multiple self references is called multiple Recursion.


Example: Fibonacci.
• LETS MOVE ON TO LCS TO SEE THE USE OF
RECURSION IN DYNAMIC PROGRAMMING. BUT
AT FIRST LETS SEE THE LCS THEORY.
LONGEST COMMON SUBSEQUENCE
(LCS)
• A LONGEST SUB­SE­QUENCE IS A SEQUENCE THAT APPEARS IN THE SAME REL­A­
TIVE ORDER, BUT NOT NEC­ES­SAR­ILY CONTIGUOUS(NOT SUB­STRING) IN BOTH
THE STRING.

• EXAM­PLE:
STRING A = "ACBAED";
STRING B = "ABCADF";

LONGEST COMMON SUBSEQUENCE - EXAMPLE


LONGEST COMMON SUBSEQUENCE(LCS): A, C, A, D; LENGTH: 4
APPROACH:
RECUR­SION:
START COM­PAR­ING STRINGS IN REVERSE ORDER ONE CHAR­AC­TER AT A TIME.
NOW WE HAVE 2 CASES -
1. BOTH CHAR­AC­TERS ARE SAME
ADD 1 TO THE RESULT AND REMOVE THE LAST CHAR­AC­TER FROM BOTH THE
STRINGS AND MAKE RECUR­SIVE CALL TO THE MOD­I­FIED STRINGS.

2. BOTH CHAR­AC­TERS ARE DIFFERENT


REMOVE THE LAST CHAR­AC­TER OF STRING 1 AND MAKE A RECUR­SIVE CALL
AND REMOVE THE LAST CHAR­AC­TER FROM STRING 2 AND MAKE A RECUR­SIVE AND
THEN RETURN THE MAX FROM RETURNS OF BOTH RECUR­SIVE CALLS.
LCS CODE
/* A Naive recursive implementation of LCS problem */
#include<bits/stdc++.h>

int max(int a, int b);

/* Returns length of LCS for X[0..m-1], Y[0..n-1] */


int lcs( char *X, char *Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}

/* Utility function to get max of 2 integers */


int max(int a, int b)
{
return (a > b)? a : b;
}

/* Driver program to test above function */


int main()
{
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";

int m = strlen(X);
int n = strlen(Y);

printf("Length of LCS is %d", lcs( X, Y, m, n ) ); //Output will be 4.

return 0;
}
THANK YOU.

You might also like