-1
#include<iostream>
#include<string.h>
int count=0;

using namespace std;


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

int lcs(char *x,char*y ,int m,int n)
{

    int l[m+1][n+1];
    int i,j;

    for( i=0;i<=m;i++)
    {
     for(j=0;j<=n;j++)
        {

        if(i==0 || j==0)
        l[i][j]=0;

        else if(x[i-1]==y[j-1]) 
        l[i][j]=l[i-1][j-1]+1;

        else
        l[i][j] =max(l[i-1][j], l[i][j-1]);

        }
    }


    return l[m][n];


}


int main()
{

    char x[]="AGGTAB";
    char y[]="GXTXAYB";

    int m=strlen(x);
    int n=strlen(y);

    cout<<"The Length Of the Longest Common Subsequence Is  :   "<<lcs(x,y,m,n);
}

The above program is for finding the Largest Common Subsequence solution using dynamic programming . I am able to calculate the length of the LCS but i am unable to deduce the logic for finding the total no. of comparisons the system will make to find the lcs .

I want to find the total no. of comparisons and to print it using a global count variable . Could someone help me out?

Rahul Nori
  • 698
  • 6
  • 17

1 Answers1

0

It depends on what exactly you count as a comparison.

I assume, that by comparison you mean "comparing characters in the string". I.e. i==0 does not count as a comparison. Also comparing the values in the max wouldn't count as comparison, since it does not compare characters from the strings. Also, I didn't go through your program checking if what you do is actually correct - I will just assume it is and focus on your actual question.

That being said, the only comparison of characters that is happening is in the line:

else if(x[i-1]==y[j-1]) 

Hence, each time you do this check you should increment your counter. One way to do this would be to restructure your branches a bit (instead of an else if you could do an else{ if{x[i-1]==y[j-1]} }. If you do that, then you could increment the counter right before the if. Like so:

if(){

}else{ 
 counter++;
 if{x[i-1]==y[j-1]} 

 }else{

 }
}

Another more explicit way to do it would be to have a function doing the check and increment in there. Something like:

bool compareChars(char &first, char &second){
 counter++;
 return first == second;
}

And then you would just do:

else if(compareChars(x[i-1], y[j-1]))

Then it would be very obvious, that each time a comparison is done the counter is incremented.

I didn't thoroughly test this, and of course other ways are possible, still I hope you get the rough idea.

dingalapadum
  • 2,077
  • 2
  • 21
  • 31
  • i want to count the cpu comaparision it will take ? – Namit Pathak Oct 18 '15 at 15:43
  • @NamitPathak : You will have to increment at other places too. Like for instance in the for loop. Again you could create an additional function and put that in your loop condition, like so: `for(int i=0; leq(i, m); i++)`. The function `leq` (less or equal) would then also increment the counter and then return a bool whether the comparison was true or not. Similarly for `i==0` you'll need something like `isZero(i)`. You will also need to increment in `max`. Actually, you will need to replace *all* comparisons by functions where you first increment the counter and then compare. – dingalapadum Oct 19 '15 at 07:03
  • @NamitPathak : If you want to count *all* cpu comparisons, it will get a bit more hairy. I'm not quite sure how to count the comparisons happening in library functions like `cout`. What I would try is: compile the program statically (`-static`) and then with the `-S` flag to see the assembly. Then look for all instructions which do comparison and increment the count right before each of those. But I'm not quite sure about this. Anyway, I think this is a much broader question which probably deserves an own question and answer independent of the problem. You want to ask? Or should I do it? – dingalapadum Oct 19 '15 at 07:16