Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Monday, July 29, 2013

Anomaly Detection using Oracle R Enterprise (ORE) | SVM for Anomaly Detection

R is an open source scripting language and environment for statistical computing, data analysis & graphics.R provides an integrated suite of software facilities for data manipulation, calculation and graphical display- it's an integrated environment. Around 2 million users in the world are widely using R especially by corporate analysts & data scientists.

Anomaly/Outlier detection has wide applications. It can be used in fraud detection, for example, by detecting unusual usage of credit cards or telecommunication services. In addition, it is useful in customized marketing for identifying the spending behavior of customers with extremely low or extremely high incomes, or in medical analysis for finding unusual responses to various medical treatments.

Friday, June 7, 2013

Huffman Algorithm Implementation in C| Huffman Encoding| Huffman Algorithm Example

Huffman Algorithm is an encoding technique for symbols where most frequently occuring symbols are represented with short length bit strings and least frequently occuring symbols are represented with long bit strings.

This algorithm is widely used as the fundamental encoding algorithm in compressing audio and image files.

Wednesday, May 29, 2013

Solution of Ordinary Differential Equation using Runge-Kutta Method | RK4 method for ODE Solution in C

In numerical analysis, the Runge–Kutta methods are an important family of implicit and explicit iterative methods for the approximation of solutions of ordinary differential equations. These techniques were developed around 1900 by the German mathematicians C. Runge and M. W. Kutta.

There are many variants of the Runge-Kutta method, but the most widely used one is the following known as RK-4 method: 
Given 
y' = f(x,y) 
y(xn) = yn 
we compute in turn 
k1 = hf(xn,yn) 
k 2 = hf (xn+h/2,yn+k1/2)
k3 = hf (xn+h/2,yn+k2/2)
k4 = hf(xn + h, yn + k3) 
yn+1 = yn + (k1 + 2*k2 + 2*k3 + k4 )/6

This algorithm can be implemented  using the following source code in C:

Saturday, May 18, 2013

Newton-Raphson Method in C with source codes

Newton-Raphson method is a method for finding successively better roots (zeros) of a real valued function
x: f(x) = 0.

The algorithm can be implemented in C as follows:


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define h pow(10,-6)
#define error pow(10,-6)
double f(double x)
{
 return(x*x-4);
}
double df(double x)
{
 return 2*x;
}

Implementation of Warshall's Algorithm in C++ with Source Codes

Warshall's algorithm is used to find the transitive closure of a graph. It's one of the efficient method to compute closure path that can be produced. Transitive closure is an important application in graph theory in computer science.
The following provides the source codes for implementing the Warshall's algorithm:



 //.............................USE OF WARSHALL'S ALGORITHM TO CREATE TRANSITIVE CLOSURE OF A GRAPH..........

#include<iostream>
#include<conio.h>

using namespace std;
const int num_nodes =10;

int main()
{
    //..............................................................VARIABLE DECLARATION
    int num_nodes,k,n;
    char i,j,res,c;
    int adj[10][10],path[10][10];

C Source Code for Dijkstra's Shortest Path Algorithm

Dijkstra's Shortest Path Algorithm is the popular algorithm for finding shortest/optimal path in compute science due to its widespread application in different branches- computer networks, distributed systems, signal processing etc.

I've implemented the algorithm for finding the minimum distance between any two nodes:



///......................................SHORTEST PATH ALGORITHM << Dijkstra Algorithm >>......................

#include<stdio.h>
#include<conio.h>
#define MAX_NODES 10
#define INFINITY 1000
#define MEMBER 1
#define NON_MEMBER 0

struct arc{
    int adj;
    int weight;
};
int main(){

    int i,j,k,weight[MAX_NODES][MAX_NODES],num_nodes,source,dest,precede[MAX_NODES];
    int distance[MAX_NODES],permanent_nodes_set[MAX_NODES];
    int current,current_distance,n;
    int smalldist,newdist;
    char s,d,node;

    printf("\n\t\t................................................................................\n");
    printf("\t\t\t\tSHORTEST PATH ALGORITHM << Dijkstra Algorithm >>");
    printf("\n\t\t................................................................................\n");
    printf("\n\n\t\t\tAssign 1000 as INFINITY for non-adjacent nodes !!\n\n");
    printf("\nEnter the number of nodes in the GRAPH :");
    scanf("%d",&n);

    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
            {
                printf("\nEnter weight for arc[%c][%c] :",65+i,65+j);
                scanf("%d",&weight[i][j]);
                weight[j][i] =weight[i][j];
                weight[i][i] =INFINITY;
            }


    printf("\nEnter the source _node :");scanf(" %c",&s);
    source =(int)(s-97);
    printf("\nEnter the destination_node :");scanf(" %c",&d);
    dest = (int)(d-97);

    printf("\n\t\t\tThe shortest path is :>>\n\n\t\t\t");
    for(i=0;i<n;i++)
    {
        permanent_nodes_set[i] =NON_MEMBER;
        distance[i] =INFINITY;
    }
    permanent_nodes_set[source]=MEMBER;
    distance[source]=0;
    current=source;
    while(current!=dest)
    {
        smalldist =INFINITY;
        current_distance =distance[current];
        for(i=0;i<n;i++)
            if(permanent_nodes_set[i] ==NON_MEMBER)
        {
            newdist =current_distance+weight[current][i];
            if(newdist<distance[i])
            {
                distance[i]=newdist;
                precede[i]=current;
                if(node!=(65+current))
                    printf(" %c ->",65+current);
                node =65+current;

            }
            if(distance[i]<smalldist)
            {
                smalldist =distance[i];
                k =i;
            }

        }

         current =k;
         permanent_nodes_set[current] =MEMBER;

        }

        printf(" %c ->",65+dest);
        printf("\n\n\t\t\tThe shortest distance = %d\n",distance[dest]);

        getch();
        return 0;
}

Priority Queue Implementation: C source codes

A priority queue is data structure in which intrinsic ordering of the elements does determine the results of its basic operations. These are of two types- ascending (e.g. queue) and descending (e.g. stack).
The following source code implements ascending type priority queue in C:


// Ascending Priority Queue Implementation in C
#include<iostream.h>
    #include<conio.h>

    const int MAX=5;

    class pqueue
    {
          int front,rear;
        public:
          struct data
          {
           int val,p,o;
          }d[MAX];

Merge Sort Algorithm with C source Codes

Here, I've presented the merge sort algorithm as the straight merge sort. The basic idea is to break the file into n subfiles of size 1 and merge adjacent disjoint pairs of files and repeating the process until we've single file remaining of size n.
 The C Source codes is as follows:


// Merge sort implementation in C
#include<stdio.h>

void getdata(int arr[],int n)
{
 int i;
  printf("\nEnter the data:");
  for(i=0;i<n;i++)
    {
     scanf("%d",&arr[i]);
    }
}

Saturday, May 11, 2013

DBSCAN Algorithm Implementation in MATLAB


Density Based Spatial Clustering of Applications with Noise (DBSCAN) Algorithm locates regions of high density that are separated from one another by regions of low density. DBSCAN is a center based approach to clustering in which density is estimated for a particular point in the data set by counting the number of points within the specified radius, ɛ, of that point.
The center based approach to density allows us to classify a point as one of the three:

     Core points: These points are in the interior of the dense region
   Border points:These points are not the core points, but fall within the neighborhood of the core points
   Noise points: A noise point is a point that is neither a core point nor a border point.
            The formal definition of DBSCAN algorithm is illustrated below: