Posts

Showing posts from February, 2017

Dynamic Programming : Knapsack Problem

Strassen Matrix Multiplication

#include"stdio.h" #include"stdlib.h" /* X Y X*Y +-------+ +-------+ +-------+-------+ | A | B | | E | F | | AE+BG | AF+BH | +---+---+ * +---+---+ = +-------+-------+ | C | D | | G | H | | CE+DG | CF+DH | +---+---+ +---+---+ +---------------+ Seven products: P1 = A(F-H) P2 = (A+B)H P3 = (C+D)E P4 = D(G-E) P5 = (A+D)(E+H) P6 = (B-D)(G+H) P7 = (A-C)(E+F) +-------------+-------------+ | P5+P4-P2+P6 | P1+P2 | X * Y = +-------------+-------------+ | P3+P4 | P1+P5-P3+P7 | +-------------+-------------+ */ /* N is the dimension. NOTE: This code works _only_ on NxN matrix */ #define N 4 /* rs = row start re = row end cs = column start ce = column end a[][] = a 2d array which contains the matrix elements */ typedef struct _m { int rs; int re; int cs; int ce; int a[N][N]; }m; /* m m1 = {0, N-1, 0, N-1, {{1,

What is Hacking ?

The idea of hacking may conjure stylized images of electronic vandalism , espionage , dyed hair , and body piercings. Most people associate hacking with breaking the law and assume that everyone who engages in hacking is criminal. Granted there are people out there who use hacking techniques to break the law, but hacking isn't really about that . In fact , hacking is more about following the law than breaking it. The essence of hacking is finding unintended or overlooked uses for the laws and properties of a given situation and then applying them in new and inventive ways to solve a problem - whatever it may be.            Like many other forms of art, hacking was often misunderstood . The few who got it formed an informal subculture that remained intensely focused on learning and mastering their art.They believed that information should be free and anything that stood in the way of that freedom should be circumvented.

Game Theory - The Science of Decision Making.

Image
Whenever you are hanging out with your friends and having all your social interactions like, “Where should we go?” “Who is the best actor?” “Which is the best song?” have you ever thought about the math or the science behind your decision? This is what Game Theory is all about. It is the science of decision-making; developed, and put into limelight, by the great Mathematician John Forbes Nash Jr., who is a Nobel Laureate (1994).               Forget your preconceptions of what a game is. It is an interaction between people, where people’s payoff is affected by the decisions made by others. This definition of game can, without a doubt, be applied on a game of poke, and also, it can be applied in any practical situation, like at the end of the article; where you can analyze your decision by making use of game theory. Game theory is widely used by mathematicians, economists, political analysts, biologists, military tacticians, and psychologists, to name just a few. Game theory has two ma

Bisection Method using Java

package sem4; import java.util.Scanner; public class BisectionMethod { static double f1(double x){ //return (Math.pow(x, 10)-1); //return (Math.pow(x, 3)+Math.pow(x, 2)+3*x); //return (Math.pow(x, 3)-5*x-7); //return (Math.cos(x)-x*Math.pow(Math.E, x)); //return ((Math.pow(Math.E,-1*x))-x); //return (x-Math.cos(x)); return (x*Math.log10(x)-1.2); } static double squeeze(double a,double b){ assert f1(a)<f1(b); final double inc=a/b; while(f1(a+inc)<0){ a+=inc; } return a; } static double find_root(double a,double b){ // a=squeeze(a,b); int c=1; double ansprev=0,ans; ans=(a+b)/2; if(f1(ans)>0) b=ans; else a=ans; while(ansprev!=ans && c<=30){ ansprev=ans; ans=(a+b)/2; if(f1(ans)>0) b=ans;

Finding Cofactor of 3-D Array using Java

//Cofactors of a 3 Dimensional Array package sem4.AM; public class CoFactors3 {          int temp[][]=new int[2][2];     final int N=3;        void getCofactor(int A[][],int p,int q){         int i=0,j=0;         for(int r=0;r<N;r++){             for(int c=0;c<N;c++){                 if(r!=p && c!=q){                     temp[i][j++]=A[r][c];                     if(j==N-1){                         j=0;                         i++;                     }                 }             }         }     }          int calc_minor(int A[][],int p,int q){         getCofactor(A,p,q);         return temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0];     }          int calc_determinant(int A[][]){         //return A[0][0]*calc_minor(A,0,0)-A[0][1]*calc_minor(A,0,1)+A[0][2]*calc_minor(A,0,2);         int sign=1;         int s=0;         for(int i=0;i<N;i++){             s+=(sign*A[0][i]*calc_minor(A,0,i));             sign*=-1;         }         System.out.println(s);         return s;  

Different Types of Errors using Java Part -2

package sem4; import java.util.Scanner; public class Errors02 { static double roundOff(double no,double figure){ double notemp=no; int ex; int x=0,y=0; double y2; int count=0; while(notemp>1){ count++; notemp/=10; } while(count>figure){ count--; no/=10; x++; } y=(int)no; while(x>0){ y*=10; x--; } no-=y; while(count<figure){ no*=10; y*=10; int t=(int) no; y+=t%10; count++; x++; } y2=y; while(x>0){ y2=y2*(1/10.0); x--; } return y2; } public double sum(double arr[],int r){ int len=arr.length; double sum=0.0; for(int i=0;i<len;i++){ Errors02.roundOff(arr[i], r);

Newton Raphson Method to find roots of transedental equations

package sem4; public class NewtonRaphsonMethod { static double f1(double x){ //return (2*Math.pow(x, 3)-3*x+4); //return (x*Math.log10(x)-1.2); //return (Math.pow(x, 3)-20);//to find squareroots //return (1/x)-7; //return (Math.pow(x, 4)-9*Math.pow(x, 2)-18); //return (Math.pow(x, 3)+x-5); return (3*x-Math.cos(x)-1); //return (x*Math.pow(Math.E,x)-2); //return (x-2*Math.sin(x)); } static double f1d(double x){ //return (0.4343+Math.log10(x)); //return (3*Math.pow(x,2)+1); //return (Math.pow(Math.E,x)*(Math.sin(x)+Math.cos(x))); //return (3*Math.pow(x,2));//to find square roots //return -1/Math.pow(x, 2); return 3+Math.sin(x); //return 3*Math.pow(x,2)+1; //return Math.pow(Math.E,x)+x*Math.pow(Math.E,x); } static double find_root(double a,double b){ int c=1; double x=a,xp=0; while(x!=xp &

Different Types of Errors using Java Part -1

package sem4; import java.util.Scanner; public class Errors { double calcAbsError(double x,double y){ return Math.abs(x-y); } double calcRelativeError(double x,double y){ double a=calcAbsError(x,y); return a/x; } double calcPercentageError(double x,double y){ double r=calcRelativeError(x,y); return r*100; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); double approx_value,abs_error,relative_error,percentage_error,true_value; System.out.println("Enter the Approximate Value"); approx_value=sc.nextDouble(); System.out.println("Enter the True Value"); true_value=sc.nextDouble(); Errors e1=new Errors(); int ch; do{ System.out.println("Enter 1 for Absolute Error,2 for Relative Error,3 For Percentage Error, 4 to Exit"); ch=sc.nextInt(); switch(ch){

Java Program to find Eigen Values of 3x3 matrix

/**Eigen Values of a 3*3 matrix**/ package sem4; import java.util.Scanner; public class EigenValues { int A[][]=new int[3][3]; final int N=3; int s1=0,s2=0,s3=0; void calcs1(){ s1=0; for(int i=0;i<N;i++){ s1+=A[i][i]; } } void calcs2(){ s2=0; int x=1,y=2; for(int i=0;i<N;i++){ s2+=A[x][x]*A[y][y]-A[x][y]*A[y][x]; if(i==0) x=0; else if(i==1) y=1; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); EigenValues e1=new EigenValues(); for(int i=0;i<e1.N;i++) for(int j=0;j<e1.N;j++) e1.A[i][j]=sc.nextInt(); e1.calcs2(); } }

Implementation of Kruskal's Algorithm to constuct minimum spanning tree in C

#include<stdio.h> int cost[100][100],n; //cost matrix for storing the graph,n=no of nodes int t[100][2]; //t is the solution vector int k,l; int conn[100]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; void minEdge(); //to select minimum edge from the graph int kruskals_algo(){  int mincost=0,num=0,i;  for(i=0;i<n-1;i++){   minEdge();   if(formsCycle(i-1)==1){    cost[k][l]=99;    cost[l][k]=99;    i--;    continue;   }   else    mincost=mincost+cost[k][l];   if(cost[i][k]>=99)    num++;   if(cost[i][l]>=99)    num++;   t[i][0]=k;   t[i][1]=l;   conn[l]=k;   cost[k][l]=99;   cost[l][k]=99;   if(num>=n){//to check for isolated nodes    if(allConnected())     return mincost;   }  }  return mincost; } void minEdge(){  int i,j;  int min=98;  for(i=0;i<n;i++)   for(j=0;j<n;j++)    if(cost[i][j]<min){     min=cost[i][j];     k=i;     l=j;    } } int formsCycle(int x) { int i=l; while(conn[i]!=-1){ if(conn[i]==k) return 1; i=conn[i]; } return 0; } int allConnecte

Prims Algorithm Explained with Example and Algorithm

Image
Let's First solve this question and understand the procedure and then change our thinking into algorithm. So what we do is that we first choose any one vertex as our starting point. lets choose 0th vertex as our starting point. Then we search for the nearest vertex from starting vertex with minimum cost In this case it will be 3rd node . so minimum cost will be mincost=2 Now , normally what we do is that we search for the nearest vertex from current vertex with minimum cost and when we don't get then we backtrack to previous vertex .But instead of backtracking what we can do is that we can check for the nearest vertex with minimum cost from current and previous vertex simultaneously. So now we search for nearest vertex with minimum cost from current i.e. 3rd vertex and previous vertex i.e. 0th vertex. so nearest vertex with minimum cost will be   2nd. mincost=2+3=5; Now we search for the next node from 2nd and 0th vertex with minimum cost. select 1st which is nearest from 0th v

Fractional Knapsack Program in C

//Fractional Knapsack Problem is a Greedy Algorithm which //helps in selecting specific items from a collection of items //(the items can be fractioned) //having specific weights and profits associated with them //Under constraints of a sack capacity //Such that the profit is maximum #include<stdio.h> int n,m; double w[100],p[100];//arrays that will store weights and profits of items double x[100];//Array that will store solution //Before calling this function the elements must be sorted on the basis of //decreasing profit by ratio double fractional_knapsack(){ double profit;//for calculating max profit int u,i;//u will keep track of capacity, i is a counter //Initializing the solution vector to 0 for(i=0;i<n;i++){ x[i]=0.0; } u=m; profit=0; //Collecting whatever is possible until capacity allows for(i=0;i<n;i++){ if(w[i]>u) break; x[i]=1; profit+=p[i]; u-=w[i]; } //Taking Fractions of the remaining elements if(i<=n){ x[i]=(double)u/w[i];