Posts

Showing posts from July 18, 2018

Linear or Sequential Search

Program In C++:-                            //Linear Search #include <iostream> using namespace std; int LinearSearch(int a[],int key,int n) {     for(int i=0;i<n;i++)     {         if(a[i]==key)         {             return i+1;         }     }     return 0; } int main() {     int n,key,index;     cout<<"Enter the size of array:";     cin>>n;     int a[n];     cout<<"Enter the values in Array:\n";     for(int i=0;i<n;i++)         cin>>a[i];     cout<<"Enter the element to be searched:";     cin>>key;     index=LinearSearch(a,key,n);     if (index!=0)         cout<<"Element found at "<<index<<" position";     else         cout<<"Not Found"; } /* Output Enter the size of array:3 Enter the values in Array: 1 2 3 Enter the element to be searched:1 Element found at 1 position */

LCS(Longest Common Sequence) Problem

Program:-                        // LCS Problem #include<stdio.h> int max(int a, int b); 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)); } int max(int a, int b) {     return (a > b)? a : b; } 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 ) );   return 0; } /* Output:- Length of LCS is 4 Process returned 0 (0x0)   execution time : 0.024 s Press any key to continue. */

Knapsack Using Greedy Method

Program:-                                //Knapsack Using Greedy #include<stdio.h> void knapsack(int n, float weight[], float profit[], float capacity) {    float x[20], tp = 0;    int i, j, u;    u = capacity;    for (i = 0; i < n; i++)       x[i] = 0.0;    for (i = 0; i < n; i++) {       if (weight[i] > u)          break;       else {          x[i] = 1.0;          tp = tp + profit[i];          u = u - weight[i];       }    }    if (i < n)       x[i] = u / weight[i];    tp = tp + (x[i] * profit[i]);    printf("\nthe result vector is:- ");    for (i = 0; i < n; i++)       printf("%f\t", x[i]);    printf("\nmaximum profit is:- %f", tp); } int main() {    float weight[20], profit[20], capacity;    int num, i, j;    float ratio[20], temp;    printf("\nEnter the no. of objects:- ");    scanf("%d", &num);    printf("\nenter the weights and profits of each object:-\n&qu

Chain Matrix

Program In C++:-                         // Chain Matrix #include<iostream> using namespace std; void printParenthesis(int i, int j, int n,                       int *bracket, char &name) {     if (i == j)     {         cout << name++;         return;     }     cout << "(";     printParenthesis(i, *((bracket+i*n)+j), n,bracket, name);     printParenthesis(*((bracket+i*n)+j) + 1, j,n, bracket, name);     cout << ")"; } void matrixChainOrder(int p[], int n) {     int m[n][n];     int bracket[n][n];     for (int i=1; i<n; i++)         m[i][i] = 0;     for (int L=2; L<n; L++)     {         for (int i=1; i<n-L+1; i++)         {             int j = i+L-1;             m[i][j] = INT_MAX;             for (int k=i; k<=j-1; k++)             {                 int q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];                 if (q < m[i][j])                 {                     m[i][j] = q;          

BFS(Breadth First Search) Algortihm

Program In C:-                     // Breath first search #include<stdio.h> #include<stdlib.h> #define MAX 100 #define initial 1 #define waiting 2 #define visited 3 int n; int adj[MAX][MAX]; int state[MAX]; void create_graph(); void BF_Traversal(); void BFS(int v); int queue[MAX], front = -1,rear = -1; void insert_queue(int vertex); int delete_queue(); int isEmpty_queue(); int main() {     create_graph();     BF_Traversal();     return 0; } void BF_Traversal() {     int v;     for(v=0; v<n; v++)         state[v] = initial;     printf("enter start vertex for BFS: \n");     scanf("%d", &v);     BFS(v); } void BFS(int v) {     int i;     insert_queue(v);     state[v] = waiting;     while(!isEmpty_queue())     {         v = delete_queue( );         printf("%d ",v);         state[v] = visited;         for(i=0; i<n; i++)         {             if(adj[v][i] == 1 && state[i] == initial)

DFS(Depth First Search) Algorithm

Program In C:-                   // Depth first search #include<stdio.h> void DFS(int); int G[10][10],visited[10],n;    //n is no of vertices and graph is sorted in array G[10][10] void main() {     int i,j;     printf("enter number of vertices:");     scanf("%d",&n);     //read the adjecency matrix     printf("\nenter adjecency matrix of the graph:");     for(i=0;i<n;i++)        for(j=0;j<n;j++)             scanf("%d",&G[i][j]);     //visited is initialized to zero    for(i=0;i<n;i++)         visited[i]=0;     DFS(0); } void DFS(int i) {     int j;     printf("\n%d",i);     visited[i]=1;     for(j=0;j<n;j++)        if(!visited[j]&&G[i][j]==1)             DFS(j); } /* enter number of vertices:4 enter adjecency matrix of the graph:0 1 1 1 1 0 0 0 1 0 0 0 0 1 1 0 0 1 2 3*/                              

Kruskal's Algorithm

Program In C:-                               // kruskal's #include<stdio.h> #define MAX 30 typedef struct edge {     int u,v,w; }edge; typedef struct edgelist {     edge data[MAX];     int n; }edgelist; edgelist elist; int G[MAX][MAX],n; edgelist spanlist; void kruskal(); int find(int belongs[],int vertexno); void union1(int belongs[],int c1,int c2); void sort(); void print(); void main() {     int i,j,total_cost;     printf("\nenter number of vertices:");     scanf("%d",&n);     printf("\nenter the adjacency matrix:\n");     for(i=0;i<n;i++)         for(j=0;j<n;j++)             scanf("%d",&G[i][j]);     kruskal();     print(); } void kruskal() {     int belongs[MAX],i,j,cno1,cno2;     elist.n=0;     for(i=1;i<n;i++)         for(j=0;j<i;j++)         {             if(G[i][j]!=0)             {                 elist.data[elist.n].u=i;                 elist.data[elist.n].v=j;

Ceaser Cipher program

Program  In C++ :- #include <iostream> using namespace std; int main() { int i, x; char str[100]; cout << "Please enter a string:\t"; cin >> str; cout << "\nPlease choose following options:\n"; cout << "1 = Encrypt the string.\n"; cout << "2 = Decrypt the string.\n"; cin >> x; //using switch case statements switch(x) { //first case for encrypting a string case 1: for(i = 0; (i < 100 && str[i] != '\0'); i++) str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value cout << "\nEncrypted string: " << str << endl; break; //second case for decrypting a string case 2: for(i = 0; (i < 100 && str[i] != '\0'); i++) str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value co