Saturday, 17 June 2017

Write a non-recursive program to check whether Hamiltonian path exists in undirected graph or not. If exists print it. ( backtracking)

#include<stdio.h>
#define m 30


int x[m];
void next_vertext(int [m][m],int,int);
void ham(int[m][m],int,int);


int main()
{
int n,i,j,e,v1,v2,G[m][m];
printf("\n Enter no of vertices in the graph");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
G[i][j]=0;
x[i]=0;
}
}
printf("\nEnter total edges ");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\nEnter the edge");
scanf("%d%d",&v1,&v2);

G[v1][v2]=1;
G[v2][v1]=1;
}
x[1]=1;
printf("\nHamiltonian cycle\n");
ham(G,n,2);
return 0;
}


void next_vertext(int G[m][m],int n,int k)
{
int j;
while(1)
{
x[k]=(x[k]+1)%(n+1);
if(x[k]==0)
return ;
if(G[x[k-1]][x[k]]!=0)
{
for(j=1;j<k;j++)
{
if(x[j]==x[k])
break;
}
if(j==k)
{
if((k<n)||((k==n)&&G[x[n]][x[1]]!=0))

return ;
}
}
}
}

void ham(int G[m][m],int n,int k)
{
int i;
while(k!=0)
{
next_vertext(G,n,k);
if(x[k]!=0)
{
if(k==n)
{
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t",x[i]);
printf("%d",x[1]);
}
else
k++;
}
else
k--;
}
}
/*
OUTPUT:-


Enter no of vertices in the graph5

Enter total edges 7

Enter the edge1 3

Enter the edge1 4

Enter the edge1 5

Enter the edge 3 4

Enter the edge3 2

Enter the edge2 5

Enter the edge2 4

Hamiltonian cycle

1 3 4 2 5 1
1 4 3 2 5 1
1 5 2 3 4 1
1 5 2 4 3 1


 Enter no of vertices in the graph5

Enter total edges 6

Enter the edge1 2

Enter the edge2 3

Enter the edge3 4

Enter the edge4 2

Enter the edge2 5

Enter the edge5 1

No Hamiltonian cycle
*/


Friday, 16 June 2017

Write a program to solve the travelling salesman problem. Print the path and the cost. ( Branch and Bound)

#include<stdio.h>
#include<stdlib.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("----------------------------------------------------------------------\n");
printf("\n\n\t TRAVELLING SALESMAN PROBLEM\n");
printf("----------------------------------------------------------------------\n");

printf("\n\nEnter Number of Cities: ");
scanf("%d",&n);

printf("\nEnter Cost Matrix: \n");
for( i=0;i<n;i++)
     {
     printf("\n Enter Elements of Row # : %d\n",i+1);
     for( j=0;j<n;j++)
     scanf("%d",&a[i][j]);
     visited[i]=0;
     }

printf("\n\nThe Cost Matrix is:\n");

for( i=0;i<n;i++)
     {
     printf("\n\n");
     for( j=0;j<n;j++)
          printf("\t%d",a[i][j]);

          }         

}

void mincost(int city)
{
int i,ncity;
visited[city]=1;
printf("%d ===> ",city+1);
ncity=least(city);

if(ncity==999)
    {
     ncity=0;
     printf("%d",ncity+1);
     cost+=a[city][ncity];
     return;
     }

mincost(ncity);
}

int least(int c)
{
 int i,nc=999;
 int min=999,kmin;
 for(i=0;i<n;i++)
    {
     if((a[c][i]!=0)&&(visited[i]==0))
     if(a[c][i]<min)
     {
      min=a[i][0]+a[c][i];
      kmin=a[c][i];
      nc=i;
      }
     }

if(min!=999)
cost+=kmin;
return nc;
}

void put()
{
 printf("\n\nMinimum cost:");
 printf("%d",cost);
}

int main()
{
get();
printf("\n\nThe Path is:\n\n");
mincost(0);
put();
}




/*

huzaif@huzaif-Lenovo-Z51-70:~/Desktop/SL2/DAA/6$ ./a.out
----------------------------------------------------------------------


  TRAVELLING SALESMAN PROBLEM
----------------------------------------------------------------------


Enter Number of Cities: 4

Enter Cost Matrix: 

 Enter Elements of Row # : 1
0
10
15 
20

 Enter Elements of Row # : 2
5
0
9
10

 Enter Elements of Row # : 3
6
13
0
12

 Enter Elements of Row # : 4
8
8
9
0


The Cost Matrix is:


 0 10 15 20

 5 0 9 10

 6 13 0 12

 8 8 9 0

The Path is:

1 ===> 2 ===> 4 ===> 3 ===> 1

Minimum cost:35huzaif@huzaif-Lenovo-Z51-70:~/Desktop/SL2/DAA/6$ 

*/

Write a non-recursive program to check whether Hamiltonian path exists in undirected graph or not. If exists print it. ( backtracking)

#include<stdio.h>
#define m 30


int x[m];
void next_vertext(int [m][m],int,int);
void ham(int[m][m],int,int);


int main()
{
int n,i,j,e,v1,v2,G[m][m];
printf("\n Enter no of vertices in the graph");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
G[i][j]=0;
x[i]=0;
}
}
printf("\nEnter total edges ");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\nEnter the edge");
scanf("%d%d",&v1,&v2);

G[v1][v2]=1;
G[v2][v1]=1;
}
x[1]=1;
printf("\nHamiltonian cycle\n");
ham(G,n,2);
return 0;
}


void next_vertext(int G[m][m],int n,int k)
{
int j;
while(1)
{
x[k]=(x[k]+1)%(n+1);
if(x[k]==0)
return ;
if(G[x[k-1]][x[k]]!=0)
{
for(j=1;j<k;j++)
{
if(x[j]==x[k])
break;
}
if(j==k)
{
if((k<n)||((k==n)&&G[x[n]][x[1]]!=0))

return ;
}
}
}
}

void ham(int G[m][m],int n,int k)
{
int i;
while(k!=0)
{
next_vertext(G,n,k);
if(x[k]!=0)
{
if(k==n)
{
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t",x[i]);
printf("%d",x[1]);
}
else
k++;
}
else
k--;
}
}
/*
Output
harshal@harshal-AU909AA-ACJ-CQ3120IX:~$ cd Desktop
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop$ cd print
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$ gcc ham.c
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$ ./a.out

 Enter no of vertices in the graph5

Enter total edges 7

Enter the edge1 3

Enter the edge1 4

Enter the edge1 5

Enter the edge 3 4

Enter the edge3 2

Enter the edge2 5

Enter the edge2 4

Hamiltonian cycle

1 3 4 2 5 1
1 4 3 2 5 1
1 5 2 3 4 1
1 5 2 4 3 1
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$ 
harshal@harshal-AU909AA-ACJ-CQ3120IX:~$ cd Desktop
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop$ cd print
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$ gcc ham.c
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$ ./a.out

 Enter no of vertices in the graph5

Enter total edges 6

Enter the edge1 2

Enter the edge2 3

Enter the edge3 4

Enter the edge4 2

Enter the edge2 5

Enter the edge5 1

No Hamiltonian cycle
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$
harshal@harshal-AU909AA-ACJ-CQ3120IX:~/Desktop/print$ 



*/

Write a recursive program to find the solution of placing n queens on chess board so that no queen takes each other (backtracking).

#include<stdio.h>
#include<stdlib.h>
void nqueens(int,int);
int place(int,int);
void printsolution(int,int[]);
int x[10],count=0;

void printsolution(int n,int x[])
{
 int i,j;
 char c[10][10];
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  c[i][j]='X';
 }
 for(i=1;i<=n;i++)
 c[i][x[i]]='Q';
        printf("Solution %d\n\n",++count);
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   printf("%c\t",c[i][j]);
  }
  printf("\n");
 }
}


int place(int k,int i)
{
 int j;
 for(j=1;j<k;j++)
 {
  if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
  return 0;
 }
 
 return 1;
}


void nqueens(int k,int n)
{

 int i;
 for(i=1;i<=n;i++)
 {
  if(place(k,i)==1)
  {
   x[k]=i; 
   if(k==n)
   printsolution(n,x);
   else
   nqueens(k+1,n);
  }
 }
}


void main()
{
       int n;
        printf("\n enter no of queens");
        scanf("%d",&n);
 nqueens(1,n);
 

}
/*
Output
ProfessionalCipher @www.professionalcipher.blogspot.com:~$ cd Desktop
ProfessionalCipher @www.professionalcipher.blogspot.com:~/Desktop$ cd print
ProfessionalCipher @www.professionalcipher.blogspot.com:~/Desktop/print$ ls
a.out      nqueen.c~                 st.c              str.c~
assTemp.c  outputfile.txt            st.c~             test1.txt
FILE1.ASM  pass.c                    strass.c          test1.txt~
floyd.c    pipe5a.c                  strassen_d.c      warshall.c
floyd.c~   pipe5a.c~                 strassen_d.c~     warshall.c~
hufheap.c  printit3rdasignofsl2.zip  strassens_method
nqueen.c   rqueen.c                  str.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ gcc rqueen.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ./a.out

 enter no of queens5
Solution 1

Q X X X X 
X X Q X X 
X X X X Q 
X Q X X X 
X X X Q X 
Solution 2

Q X X X X 
X X X Q X 
X Q X X X 
X X X X Q 
X X Q X X 
Solution 3

X Q X X X 
X X X Q X 
Q X X X X 
X X Q X X 
X X X X Q 
Solution 4

X Q X X X 
X X X X Q 
X X Q X X 
Q X X X X 
X X X Q X 
Solution 5

X X Q X X 
Q X X X X 
X X X Q X 
X Q X X X 
X X X X Q 
Solution 6

X X Q X X 
X X X X Q 
X Q X X X 
X X X Q X 
Q X X X X 
Solution 7

X X X Q X 
Q X X X X 
X X Q X X 
X X X X Q 
X Q X X X 
Solution 8

X X X Q X 
X Q X X X 
X X X X Q 
X X Q X X 
Q X X X X 
Solution 9

X X X X Q 
X Q X X X 
X X X Q X 
Q X X X X 
X X Q X X 
Solution 10

X X X X Q 
X X Q X X 
Q X X X X 
X X X Q X 
X Q X X X 
ProfessionalCipher @www.professionalcipher.blogspot.com:~/Desktop/print$ 

*/

Write a program to print shortest path and cost for the directed graph using Floyd and Warshal method. ( Dynamic Programming) and verify the complexity

/*
FLOYD
*/



#include<stdio.h>

void main()
{
int wt[10][10],n,i,j;
void floyds(int wt[10][10],int n);

printf("\nHow many vertices you want");
scanf("%d",&n);
printf("Enter the elements");
printf("Enter 999 as infinity value");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\n wt[%d][%d]",i,j);
scanf("%d",&wt[i][j]);
}
}
printf("\n\tComputing All pair shortest path...\n");
floyds(wt,n);

}

void floyds(int wt[10][10],int n)
{
int d[5][10][10],i,j,k;
int min(int,int);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[0][i][j]=wt[i][j];
}
}

for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[k][i][j]=min(d[k-1][i][j],(d[k-1][i][k]+d[k-1][k][j]));
}
}
}
for(k=0;k<=n;k++)
{
printf("r(%d)=\n",k);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",d[k][i][j]);
}
printf("\n");
}
}
}
int min(int a, int b)
{
if(a<b)
return a;
else
return b;
}


/*
Output:
ProfessionalCipher@www.professionalcipher.blogspot.com:~$ cd Desktop
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop$ cd print
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ls
a.out      floyd.c   nqueen.c~       pipe5a.c   st.c~         strassen_d.c~     str.c~      warshall.c
assTemp.c  floyd.c~  outputfile.txt  pipe5a.c~  strass.c      strassens_method  test1.txt   warshall.c~
FILE1.ASM  nqueen.c  pass.c          st.c       strassen_d.c  str.c             test1.txt~
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ gcc floyd.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ./a.out

How many vertices you want3
Enter the elementsEnter 999 as infinity value
 wt[1][1]0

 wt[1][2]8

 wt[1][3]5

 wt[2][1]2

 wt[2][2]0

 wt[2][3]999

 wt[3][1]999

 wt[3][2]1

 wt[3][3]0

 Computing All pair shortest path...
r(0)=
0 8 5 
2 0 999 
999 1 0 
r(1)=
0 8 5 
2 0 7 
999 1 0 
r(2)=
0 8 5 
2 0 7 
3 1 0 
r(3)=
0 6 5 
2 0 7 
3 1 0 
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ 

*/





/*
Output:
ProfessionalCipher@www.professionalcipher.blogspot.com:~$ cd Desktop
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop$ cd print
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ls
a.out      floyd.c   nqueen.c~       pipe5a.c   st.c~         strassen_d.c~     str.c~      warshall.c
assTemp.c  floyd.c~  outputfile.txt  pipe5a.c~  strass.c      strassens_method  test1.txt   warshall.c~
FILE1.ASM  nqueen.c  pass.c          st.c       strassen_d.c  str.c             test1.txt~
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ gcc warshall.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ./a.out

 enter no of nodes3
enter adjency matrix
0
1
1
1
0
1
1
1
0


adjency matrix is:
0 1 1 
1 0 1 
1 1 0 

 intermediate matrix t[0]:
0 1 1 
1 1 1 
1 1 1 

 intermediate matrix t[1]:
1 1 1 
1 1 1 
1 1 1 

 intermediate matrix t[2]:
1 1 1 
1 1 1 
1 1 1 

the path matrix is:
1 1 1 
1 1 1 
1 1 1 
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ 



*/

**********************************************************

FLOYDWARSHALL

*********************************************************

// C Program for Floyd Warshall Algorithm
#include<stdio.h>
 
// Number of vertices in the graph
#define V 4
 
/* Define Infinite as a large enough value. This value will be used
  for vertices not connected to each other */
#define INF 99999
 
// A function to print the solution matrix
void printSolution(int dist[][V]);
 
// Solves the all-pairs shortest path problem using Floyd Warshall algorithm
void floydWarshall (int graph[][V])
{
    /* dist[][] will be the output matrix that will finally have the shortest 
      distances between every pair of vertices */
    int dist[V][V], i, j, k;
 
    /* Initialize the solution matrix same as input graph matrix. Or 
       we can say the initial values of shortest distances are based
       on shortest paths considering no intermediate vertex. */
    for (i = 0; i < V; i++)
        for (j = 0; j < V; j++)
            dist[i][j] = graph[i][j];
 
    /* Add all vertices one by one to the set of intermediate vertices.
      ---> Before start of a iteration, we have shortest distances between all
      pairs of vertices such that the shortest distances consider only the
      vertices in set {0, 1, 2, .. k-1} as intermediate vertices.
      ----> After the end of a iteration, vertex no. k is added to the set of
      intermediate vertices and the set becomes {0, 1, 2, .. k} */
    for (k = 0; k < V; k++)
    {
        // Pick all vertices as source one by one
        for (i = 0; i < V; i++)
        {
            // Pick all vertices as destination for the
            // above picked source
            for (j = 0; j < V; j++)
            {
                // If vertex k is on the shortest path from
                // i to j, then update the value of dist[i][j]
                if (dist[i][k] + dist[k][j] < dist[i][j])
                    dist[i][j] = dist[i][k] + dist[k][j];
            }
        }
    }
 
    // Print the shortest distance matrix
    printSolution(dist);
}
 
/* A utility function to print solution */
void printSolution(int dist[][V])
{
    printf ("Following matrix shows the shortest distances"
            " between every pair of vertices \n");
    for (int i = 0; i < V; i++)
    {
        for (int j = 0; j < V; j++)
        {
            if (dist[i][j] == INF)
                printf("%7s", "INF");
            else
                printf ("%7d", dist[i][j]);
        }
        printf("\n");
    }
}
 
// driver program to test above function
int main()
{
    /* Let us create the following weighted graph
            10
       (0)------->(3)
        |         /|\
      5 |          |
        |          | 1
       \|/         |
       (1)------->(2)
            3           */
    int graph[V][V] = { {0,   5,  INF, 10},
                        {INF, 0,   3, 15},
                        {INF, INF, 0,   1},
                        {INF, INF, INF, 0}
                      };
 
    // Print the solution
    floydWarshall(graph);
    return 0;
}



*********************************************************

WARSAHLL

*********************************************************

/*
Assignment no-3
*/

#include<stdio.h>
void warshall(int [10][10],int);
void main()
 {
  int a[10][10],i,j,n;
  
  printf("\n enter no of nodes");
  scanf("%d",&n);
  printf("enter adjency matrix\n");
   
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)

   scanf("%d",&a[i][j]);
   printf("\n\nadjency matrix is:\n");
   
    for(i=0;i<n;i++)
    {
     for(j=0;j<n;j++)
     {
      printf("%d\t",a[i][j]);
     }
       printf("\n"); 
    }
 warshall(a,n);

 }

void warshall(int p[10][10],int n)
{
 int i,j,k,x,y;
 for(k=0;k<n;k++)

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

  {
   for(i=0;i<n;i++)
   {
    if((p[i][j]==0) && (p[i][k]==1) && (p[k][j]==1))

    {
     p[i][j]=1;
    }
   }
  }
 printf("\n intermediate matrix t[%d]:\n",k);
  for(x=0;x<n;x++)
 {
  for(y=0;y<n;y++)
  {
   printf("%d\t",p[x][y]);
  }
  printf("\n");
 }
 }

printf("\nthe path matrix is:\n");

for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
   {
    printf("%d\t",p[i][j]);
  
   }
  printf("\n");
 }
}



/*
Output:
ProfessionalCipher@www.professionalcipher.blogspot.com:~$ cd Desktop
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop$ cd print
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ls
a.out      floyd.c   nqueen.c~       pipe5a.c   st.c~         strassen_d.c~     str.c~      warshall.c
assTemp.c  floyd.c~  outputfile.txt  pipe5a.c~  strass.c      strassens_method  test1.txt   warshall.c~
FILE1.ASM  nqueen.c  pass.c          st.c       strassen_d.c  str.c             test1.txt~
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ gcc warshall.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ./a.out

 enter no of nodes3
enter adjency matrix
0
1
1
1
0
1
1
1
0


adjency matrix is:
0 1 1 
1 0 1 
1 1 0 

 intermediate matrix t[0]:
0 1 1 
1 1 1 
1 1 1 

 intermediate matrix t[1]:
1 1 1 
1 1 1 
1 1 1 

 intermediate matrix t[2]:
1 1 1 
1 1 1 
1 1 1 

the path matrix is:
1 1 1 
1 1 1 
1 1 1 
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ 



*/

Write a program to implement coding and decoding using Huffman method. (Greedy) Students are expected to write the program with nlogn complexity and verify the same.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

typedef struct treenode
{
 float freq;
 char data[10];
 struct treenode *left,*right;
}treenode;

typedef struct node
{
 treenode *data; 
 struct node *next;
}node;

node *insert(node *,treenode *);
treenode *create();
void encode();
void decode(treenode *);
int n=0;
char alphabets[30][10];
char codes[30][10];

void preorder(treenode *p,int i ,char word[])
 {
 if(p!=NULL)
    {
  if(p->left==NULL && p->right==NULL)
     {
   word[i]=0;
   printf("\n%s --- %s",p->data,word);
                        strcpy(alphabets[n],p->data);
   strcpy(codes[n++],word);
     }
  word[i]='0';
  preorder(p->left,i+1,word);
  word[i]='1';
  preorder(p->right,i+1,word);
    }
 }

void main()
 {
 int op;
 char word[10];
 treenode *root=NULL;
 do
    {
  printf("\n\n1)Create Huffman Tree ");
  printf("\n2)Encode a Message ");
  printf("\n3)Decode a message ");
  printf("\n4)Quit");
  printf("\nEnter Your Choice : ");
  scanf("%d",&op);
  switch(op)
     {
   case 1: root=create();
    printf("\nPrefix codes : \n");
    preorder(root,0,word); 
    break;
   case 2: encode(); break;
   case 3: decode(root);break;
     }
    }while(op!=4);
}

treenode *create()
{
 treenode *p,*t1,*t2;
 node *head;
 int n,i;
 char x[10];
 float probability;
 head=NULL;      
 printf("\nEnter No. of alphabets :");
 scanf("%d",&n);
 for(i=0;i<n;i++)
    {
  
  printf("\nEnter alphabet :");
  scanf("%s",x);
  printf("\nEnter frequency :");
  scanf("%f",&probability);
 
  p=(treenode*)malloc(sizeof(treenode));
  p->left=p->right=NULL;
  strcpy(p->data,x);
  p->freq=probability;
  head=insert(head,p);
   }
 
 for(i=1;i<n;i++)
    {
  t1=head->data;        
  t2=head->next->data;  
  head=head->next->next; 
  p=(treenode *)malloc(sizeof(treenode));
  p->left=t1;
  p->right=t2;
  p->freq=t1->freq+t2->freq;
  head=insert(head,p); 
    }
  
 return(head->data);

} 
 
node *insert(node *head,treenode *t)
{ 
 node *p,*q;
 p=(node *)malloc(sizeof(node));
 p->data=t;
 p->next=NULL;
 if(head==NULL) 
  return(p);
 if(t->freq<head->data->freq)
    {
  p->next=head;
  return(p);
    }
 
 q=head;
 while(q->next!=NULL && t->freq>q->next->data->freq)
  q=q->next;
 p->next=q->next;
 q->next=p;
 return(head);
}

void encode()
{
 char word[30],temp[30];
        int i,j;
 //flushall();
        for(i=0;i<n;i++)
         temp[i]=alphabets[i][0];
        temp[i]='\0';
 printf("\n Enter a Message : ");
 scanf("%s",word);
 printf("\n Encoded Message :");
 for(i=0;word[i]!='\0';i++)
   {
                  for(j=0;j<n;j++) 
                   {
   if(temp[j]==word[i])
    printf("%s",codes[j]);
                    }
   }
          
}

void decode(treenode *p)
{
 char word[90];
        int i;treenode *q;
 //flushall();
 printf("\nEnter an Encoded message : ");
 scanf("%s",word);
 q=p;i=0;
 printf("\nDecoded Message = ");
 while(word[i]!='\0')
    {
  if(word[i]=='0')
   q=q->left;
  else
   q=q->right;
  if(q->left==NULL && q->right==NULL)
      {
   printf("%s",q->data);
   q=p;
      }
  i++;
    }

}
/*
[ProfessionalCipherwww.professionalcipher.blogspot.com]$ 
[ProfessionalCipherwww.professionalcipher.blogspot.com]$ gcc huf.c
[ProfessionalCipherwww.professionalcipher.blogspot.com]$ ./a.out


1)Create Huffman Tree 
2)Encode a Message 
3)Decode a message 
4)Quit
Enter Your Choice : 1

Enter No. of alphabets :6

Enter alphabet :d

Enter frequency :0.1

Enter alphabet :-

Enter frequency :0.2

Enter alphabet :b

Enter frequency :0.35

Enter alphabet :e

Enter frequency :0.4

Enter alphabet :a

Enter frequency :0.5

Enter alphabet :c

Enter frequency :0.5

Prefix codes : 

e --- 00
c --- 01
a --- 10
d --- 1100
- --- 1101
b --- 111

1)Create Huffman Tree 
2)Encode a Message 
3)Decode a message 
4)Quit
Enter Your Choice : 
*/

Write a program to implement matrix multiplication using Strassen's method. (Divide and Conquer)

#include<stdio.h>
int n;

void strassen(int a[][n],int b[][n],int c[][n],int size)
{
 int p1[size/2][size/2];
 int p2[size/2][size/2];
 int p3[size/2][size/2];
 int p4[size/2][size/2];
 int p5[size/2][size/2];
 int p6[size/2][size/2];
 int p7[size/2][size/2];

 int temp1[size/2][size/2];
 int temp2[size/2][size/2];

 int i,j;

 if(size>=2)
 {
  //To calculate part1 i.e. p1=(A00+A11)*(B00+B11)
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p1
    temp1[i][j]=a[i][j]+a[i+size/2][j+size/2];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p1
    temp2[i][j]=b[i][j]+b[i+size/2][j+size/2];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p1,n);

  // To Calculate Part2 i.e. p2=(A10+A11)*B00
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p2
    temp1[i][j]=a[i+size/2][j]+a[i+size/2][j+size/2];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p2
    temp2[i][j]=b[i][j];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p2,n);

  // To Calculate Part3 i.e. p3=A00*(B01-B11)
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p3
    temp1[i][j]=a[i][j];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p3
    temp2[i][j]=b[i][j+size/2]-b[i+size/2][j+size/2];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p3,n);

  // To Calculate Part4 i.e. p4=A11*(B10-B00)
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p4
    temp1[i][j]=a[i+size/2][j+size/2];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p4
    temp2[i][j]=b[i+size/2][j]-b[i][j];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p4,n);
  
  // To Calculate Part5 i.e. p5=(A00+A01)*B11
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p5
    temp1[i][j]=a[i][j]+a[i][j+size/2];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p5
    temp2[i][j]=b[i+size/2][j+size/2];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p5,n);
 
  // To Calculate Part6 i.e. p6=(A10-A00)*(B00+B01)
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p6
    temp1[i][j]=a[i+size/2][j]-a[i][j];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p6
    temp2[i][j]=b[i][j]+b[i][j+size/2];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p6,n);
  
  // To Calculate Part7 i.e. p7=(A01-A11)*(B10+B11)
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //half sub part of p7
    temp1[i][j]=a[i][j+size/2]-a[i+size/2][j+size/2];
   } 
  }
  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    //second sub part of p7
    temp2[i][j]=b[i+size/2][j]+b[i+size/2][j+size/2];
   } 
  }
  n=size/2;
  printf("\n n=%d",n);
  strassen(temp1,temp2,p7,n);

  
  //these value p1.....p7 are put in to following formula to get actual result here as product in matrix c

 
  //To Calculate C11=p1+p4-p5+p7
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    c[i][j]= p1[i][j]+p4[i][j]-p5[i][j]+p7[i][j];
   }
  }

  //To Calculate C12=p3+p5
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    c[i][j+size/2]= p3[i][j]+p5[i][j];
   }
  }
  
  //To Calculate C21=p2+p4
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    c[i+size/2][j]= p2[i][j]+p4[i][j];
   }
  }
 
  //To Calculate C22=p1+p3-p2+p6  
  for(i=0;i<size/2;i++)
  {
   for(j=0;j<size/2;j++)
   {
    c[i+size/2][j+size/2]= p1[i][j]+p3[i][j]-p2[i][j]+p6[i][j];
   }
  } 
 
 }
 else if(size==1)
 {
  c[0][0]=a[0][0]*b[0][0];
 } 

}

int main()
{
 int i,j,temp;

 printf("\nEnter the Size of NXN Matrix:");
 scanf("%d",&n);

 temp=n;

 if(n<=0)
  return 0;

 int a[n][n];
 int b[n][n];
 int c[n][n];

 printf("\nEnter Matrix A:\n"); 
 for(i=0;i<temp;i++)
 {
  for(j=0;j<temp;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }

 printf("\nEnter Matrix B:\n"); 
 for(i=0;i<temp;i++)
 {
  for(j=0;j<temp;j++)
  {
   scanf("%d",&b[i][j]);
  }
 }

 printf("\nMatrix A:\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("\t %d",a[i][j]);
  }
  printf("\n");
 }
 
 printf("\nMatrix B:\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("\t %d",b[i][j]);
  }
  printf("\n");
 }

 strassen(a, b, c, n);
 printf("\nMatrix :\n");
 for(i=0;i<temp;i++)
 {
  for(j=0;j<temp;j++)
  {
   printf("\t %d",c[i][j]);
  }
  printf("\n");
 }

 return 0;

}

/*
OUTPUT-

[ProfessionalCipher@www.professionalcipher.blogspot.com]$ gcc Newfile.c                                                                                                                                                                                
[ProfessionalCipher@www.professionalcipher.blogspot.com]$ ./a.out                                                                                                                                                                                      
                                                                                                                                                                                                     
Enter the Size of NXN Matrix:2                                                                                                                                                                       
                                                                                                                                                                                                     
Enter Matrix A:                                                                                                                                                                                      
1                                                                                                                                                                                                    
2                                                                                                                                                                                                    
3                                                                                                                                                                                                    
4                                                                                                                                                                                                    
                                                                                                                                                                                                     
Enter Matrix B:                                                                                                                                                                                      
4                                                                                                                                                                                                    
3                                                                                                                                                                                                    
2                                                                                                                                                                                                    
1

Matrix A:                                                                                                                                                                                            
         1       2                                                                                                                                                                                   
         3       4                                                                                                                                                                                   
                                                                                                                                                                                                     
Matrix B:                                                                                                                                                                                            
         4       3                                                                                                                                                                                   
         2       1                                                                                                                                                                                   
                                                                                                                                                                                                     
 n=1                                                                                                                                                                                                 
 n=1                                                                                                                                                                                                 
 n=1                                                                                                                                                                                                 
 n=1                                                                                                                                                                                                 
 n=1                                                                                                                                                                                                 
 n=1
 Matrix :                                                                                                                                                                                             
         8       5                                                                                                                                                                                   
         20      13                                                                                                                                                                                  
[ProfessionalCipher@www.professionalcipher.blogspot.com]$                 
*/

Thursday, 15 June 2017

HOW TO REMOVE UBUNTU AND GRUB LOADER FROM WINDOWS

Linux is free, open source and a great operating system, but it will not suit each one of us. So, if you installed Linux (Ubuntu, Linux Mint, Kali or any other) alongside Windows 8  just to check it out, here’s how to fully remove it from your system and boot Windows 8 normally without entering GRUB.



Steps for Uninstalling LINUX(Ubuntu) and GRUB Boot loader from WINDOWS 7,8 &10


Step 1: Install Easy BCD on your Windows PC - Download




Step 2: Easy BCD is used to remove GRUB boot loader  from your machine..
         
            Now  lets remove GRUB Boot Loader. Open Easy BCD and follow the IMAGES.




Step 3: Now you have the GRUB boot loader from your PC. Now lets removed LINUX from youir                 PC. So for removing LINUX OS from your disk.

             Open run and type : diskmgmt.msc


            In Disk Management DELETE the VOLUME and Partition where LINUX is STORED.




Step 4: If you would like to use the free space for your WINDOWS partition then follow the step                   given in Image.


That's it, your done....You have Delete the LINUX successfully from you PC...

NOW RESTART YOUR PC

Thank you for reading.... Share on Social Media

Tuesday, 6 June 2017

TOP 5 FREE BEST BRAIN TRAINING GAMES FOR ANDROID AND iOS

Just like a muscle, our brain needs regular exercise and challenge to reach its full potential. If you don’t task it, it will become sluggish and with time, you will start forgetting things, so the best way to keep you on top of everything is to not only exercise the body but also the mind.
Playing games can be a great way to exercise your mind when you are idle or waiting. There are many brain training games for Android, and the following are some of the best free brain training apps to keep your mind sharp and alert.



Rating - 4.7/5
Want to test your memory skills or give your brain an exercise? Try this fun memory game to improve your memory, speed, get better accuracy, distinguish between colors and much more. This is a fun logical game that gives a good test to your brain.
With this brain game, you can help kids learn to distinguish between different colors and improve their skills. Kids sharpen their minds while having fun playing this game. This logical game is designed for all age groups as it is a skill test game. Also, you can test your reflexes. There are some fun game puzzles in the game of different kinds. You score points and advance levels. Very fun and addictive game that you will completely enjoy.


Rating - 4.6/5
A game of logic with the most unusual & tricky questions that require creative approach.
Looking for a game that is brilliant, creative and strange at the same time? Now you can become a logic mastermind by solving the most unusual and tricky questions you have ever seen!
The game contains tricky questions that incorporate use of memory, logic, attention and the ability to think in unconventional ways.
You will evaluate your cognitive skills and get score attention, memory, flexibility and spatial visual processing abilities. 
Logic master is the first version of a brilliant and unusual puzzle game that tests your attention and ability to think outside the box.


Rating - 4.5/5
Elevate is a brain training program designed to improve attention, speaking skills, processing speed, memory, math skills, and more. Each person is provided with his or her own personalized training program that adjusts over time to maximize results.
The more you train with Elevate, the more you’ll improve critical cognitive skills that are designed to boost productivity, earning power, and self-confidence. Users who train at least 3 times per week have reported dramatic gains and increased confidence.


Rating - 4.2/5
Lumosity is a free app, designed by scientists and designers, which features games designed to improve your memory, cognitive abilities, and problem-solving skills, through a daily mental training program. The games adapt to how well you're doing, constantly offering a challenge or scaling back if you're having a hard time. 
The app's games are the result of common cognitive and neuropsychological tasks transformed by designers into games that challenge the core skills of the mind. The Lumosity team has worked with over 40 university researchers from all over the world both to advance Lumosity and to improve understanding of human cognition.


Rating - 4.1/5
Mind games is one of the best brain training apps for Android. The app has 28 mindware’s brain training games with more than 100 levels of different difficulty. Puzzle games, Math games, word games, etc, are part of this app. You can select any of the game and start improving your memory while enjoying the game.
The app also include your score history, a global top scores list, and graph of your progress to keep the track of your performance.


             These are the five free brain training games for Android. They are extremely helpful to boost your memory so I recommend you to play those games regularly to keep your brain in tip-top shape.

VIDEO:

Thank you for reading.... Share on Social Media

Saturday, 3 June 2017

Sticky Menu for Blogger




Hello everyone, In this post I will show you how to make your website MENU a Sticky Menu

This code was taken from CodePen and re-engineered by me...

Color for your understanding what to replace

ORANGE-  Copy the code to your blogger template.

In your template your div menu class name have to be changed as i am giving color RED for your understanding

Just you have to change the RED color text

Example: 


1. Go to Theme
2.Then click on Edit HTML
3.After that search for menu section in your template
4.See what is your  <div class='span-24'><div class='menu-secondary-container'> Like I have  menu-secondary-container
5.Change it with your name and your done.....





HTML

<div class="container">
<div class="section_top">
  hello
  </div>

<div class="stickycontainer">
<div class="notsticky"></div>
<div class="menu-secondary-container"> <!-- Copy this class name and change all RED text-->
<a href="#whatitdoes" class="whatitdoes">What it Does</a>  
<a href="#whyitworks" class="whyitworks">Why it Works</a>      
<a href="#compare" class="compare">Other</a>  
<a href="#relief" class="relief">Long Lasting Relief</a>
</div>
</div>

  <div class="text">
    <p>content content content</p>
    <p>content content content</p>
    <p>content content content</p>
    <p>content content content</p>
    <p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p><p>content content content</p>
 
  </div>
</div>


CSS (NOTE:Paste the color code in CSS section)

.section_top {
  width: 525px;
  height: 60px;
  display: block;
}

.stickycontainer {
  position:relative;
}

/*Change the class name with your class name (Only RED COLOR)*/
.menu-secondary-container {
width: 525px;
background: #6AC9B9;
height: 42px;
line-height: 42px;
box-shadow: 0 3px 8px -2px rgba(0, 0, 0, 0.3);
-webkit-box-shadow: 0 3px 8px -2px rgba(0, 0, 0, 0.3);
vertical-align:middle;
z-index: 500;
position:absolute;
top: 0;
}

/*Special you have to add this  block of code to 
your template without any change
*/
.totop {
position:fixed;
top: 0;
}

.notsticky {
height: 42px;
}

.menu-secondary-container a {
font-size: 17px;
color: #fff;
margin: 0 8px;
font-weight: 400;
}
.container {
width: 525px;
background: gray;
box-shadow:
0 0 12px 4px rgba(0,0,0,.3),
0 0 120px 80px rgba(255,255,255,.3);
margin: 0 auto;  
  min-height: 800px;
}


JS

<script type='text/javascript'>
      // Sticky Nav
      $(function () {
          // Check the initial Position of the Sticky Header
//Change the class name with your class name (Only RED COLOR)
          var nav = $('.menu-secondary-container');
          if (nav.length) {
              var stickyNavTop = nav.offset().top;
              $(window).scroll(function () {
                  if ($(window).scrollTop() > stickyNavTop) {
//Change the class name with your class name (Only RED COLOR)
                      $('.menu-secondary-container').addClass('.totop');
                  } else {
//Change the class name with your class name (Only RED COLOR)
                      $('.menu-secondary-container').removeClass('.totop');
                  }
              });
          }
      });
</script>


If you have some queries then comment down I will help you as early as possible

Thank you for reading.... Share on Social Media