Featured

Greedy Algorithms

Hey Guys !!! Welcome back to the Algorithm shelf . Next type is all about Greedy Algorithm. You might be learn this type but I think your not able to remind it so lets create permanent access in your memory called “Mind”.

Greedy Algorithm. Greedy algorithms are an approach to… | by ...

As we know the meaning of Greedy is nothing but the behavior of wanting more than you are entitled to. The algorithm is also same as the behavior it makes the choice for the best solution.

Basics:

Greedy Algorithm divides the problem in number of sub-problems ; then it will choose the best solution from all the solutions of sub-problems (Local Optimal , Greedy Choice) The hope : A locally optimal solution will lead to the globally optimal solution ; it might be work for some problems .

Reasons: Designed to achieve optimum solution for a given problem . As being greedy, the closest solution that seems to provide an optimal solution is chosen. It find the locally optimal solution which leads to the globally optimal solution , however greedy algorithm do not provide globally optimal solution directly.

Example to be solved : Greedy Algorithm always go for the networking problems like given below.

  • Traveling Salesman Problem
  • Prim’s Minimal Spanning Tree Algorithm
  • Kruskal’s Minimal Spanning Tree Algorithm
  • Dijkstra’s Minimal Spanning Tree Algorithm
  • Graph – Map Coloring
  • Graph – Vertex Cover
  • Knapsack Problem
  • Job Scheduling Problem

Traveling Salesman Problem : Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point. Note the difference between Hamiltonian Cycle and TSP. The Hamiltoninan cycle problem is to find if there exist a tour that visits every city exactly once. Here we know that Hamiltonian Tour exists (because the graph is complete) and in fact many such tours exist, the problem is to find a minimum weight Hamiltonian Cycle.

For example, consider the graph shown in figure on right side. A TSP tour in the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80. by considering the lowest cost for the tour.

Code : ( Language C )

<code>

#include <stdio.h>
int matrix[25][25], visited_cities[10], limit, cost = 0;
 
int tsp(int c)
{
 int count, nearest_city = 999;
 int minimum = 999, temp;
 for(count = 0; count < limit; count++)
 {
 if((matrix[c][count] != 0) && (visited_cities[count] == 0))
 {
 if(matrix[c][count] < minimum)
 {
 minimum = matrix[count][0] + matrix[c][count];
 }
 temp = matrix[c][count];
 nearest_city = count;
 }
 }
 if(minimum != 999)
 {
 cost = cost + temp;
 }
 return nearest_city;
}
 
void minimum_cost(int city)
{
 int nearest_city;
 visited_cities[city] = 1;
 printf("%d ", city + 1);
 nearest_city = tsp(city);
 if(nearest_city == 999)
 {
 nearest_city = 0;
 printf("%d", nearest_city + 1);
 cost = cost + matrix[city][nearest_city];
 return;
 }
 minimum_cost(nearest_city);
}
 
int main()
{ 
 int i, j;
 printf("Enter Total Number of Cities:\t");
 scanf("%d", &limit);
 printf("\nEnter Cost Matrix\n");
 for(i = 0; i < limit; i++)
 {
 printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1);
 for(j = 0; j < limit; j++)
 {
 scanf("%d", &matrix[i][j]);
 }
 visited_cities[i] = 0;
 }
 printf("\nEntered Cost Matrix\n");
 for(i = 0; i < limit; i++)
 {
 printf("\n");
 for(j = 0; j < limit; j++)
 {
 printf("%d ", matrix[i][j]);
 }
 }
 printf("\n\nPath:\t");
 minimum_cost(0);
 printf("\n\nMinimum Cost: \t");
 printf("%d\n", cost);
 return 0;
}

</code>

Output :

_config.yml

Hope this example is very simple to get the concept clear, Till then stay tuned stay connected to know about the next type. I’ll come back with next blog .

Practice like you've never won. Perform like you've never lost ...
Featured

Dynamic Programming Algorithm

Hello Everyone !!

After very long break welcome back to the “Shelf of Algorithm” , Without wasting time lets start with next type before going to next type just recall all basic concepts which I have share with you in previous blog.

Dynamic programming - Wikipedia
Fig : Example of Dynamic Programming Algorithm

Basics:

A dynamic programming algorithm remembers past results and uses them to find new results. Dynamic programming is similar to the Divide and Conquer used where we have problems, which can be divided into similar sub-problems, so that their results can be re-used. Mostly, these algorithms are used for optimization. It can be solved by using both method that is Top-down and Bottom-up. Without going for proper definition we will see the examples which can be solved by using the dynamic programming.

Examples:

Dynamic programming is generally used for optimization problems. The Following computer problems can be solved by using the dynamic programming.

  • Fibonacci number series
  • Knapsack problem
  • Tower of Hanoi
  • All pair shortest path by Floyd-War shall
  • Shortest path by Dijkstra
  • Project scheduling

We will see and discuss the all important examples which will ask during the interviews in the further blog series of Interview Questions . To get the clear idea about this type lets see one example of 0-1 Knapsack problem asked in the Interview.

Example: Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).

Solution:

knapsack-problem

<code>

  1. #include <stdio.h>
  2.  
  3. int max(int a, int b) { return (a > b)? a : b; }
  4. // Returns the maximum value that can be put in a knapsack of capacity W
  5. int knapsack(int W, int wt[], int val[], int n)
  6. {
  7. int i, w;
  8. int K[n+1][W+1];
  9.  
  10. // Build table K[][] in bottom up manner
  11. for (i = 0; i <= n; i++)
  12. {
  13. for (w = 0; w <= W; w++)
  14. {
  15. if (i==0 || w==0)
  16. K[i][w] = 0;
  17. else if (wt[i-1] <= w)
  18. K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
  19. else
  20. K[i][w] = K[i-1][w];
  21. }
  22. }
  23.  
  24. return K[n][W];
  25. }
  26.  
  27. int main()
  28. {
  29. int val[] = {60, 100, 120};
  30. int wt[] = {10, 20, 30};
  31. int W = 50;
  32. int n = sizeof(val)/sizeof(val[0]);
  33. printf(“\nValue = %d”, knapsack(W, wt, val, n));
  34. return 0;
  35. }

</code>

Output:

<code>

$ gcc knapsack.c -o knapsack
$ ./knapsack
 
Value = 220

</code>

Note: Above function computes the same sub-problems again and again.

Solve the problems as much as to get the best output for your struggle as an input, Till then stay tuned stay connected to know about the next type. I’ll come back with next blog and next type within a few hours.

Featured

Recursive Algorithm

Hey Guys !!! Welcome back to the Algorithm shelf .Today we will see the types of algorithm. As this part is very vast so ,I have included only one type in brief. So let’s begin with the fundamentals.

An algorithm is a set of self contained sequence of instructions or actions that contains finite space or sequence and that will give us a result to a specific problem in a finite amount of time.

Simply we can define as , a logical and mathematical approach to solve or crack a problem using any possible method.

Algorithm is divided into a finite number of types and some of them which is generally used are given in above figure.

1.Recursive algorithms: 

As we are familiar with the concept of recursive function i.e. The function which call itself again and again within  the same function until the given condition is not satisfied that is called a Recursive function.

 similarly, an algorithm is said to be recursive if the same algorithm is invoked in the same body. That is ; one which calls again and again within the same body is known as Recursive algorithm.     

This recursive mechanism is extremely powerful cause many times it express the complex problems clearly that’s why recursive is introduce as an kind of algorithm where it is also called as the direct recursive algorithm.

Suppose the algorithm A calls another algorithm which in turns calls A then it said to be indirect algorithm.

Typically the beginners have the view as recursive mechanism is some how mystical technique which is used for only some special case problems.

But this is very unfortunate because the any algorithm that is written by using a statements like if-then-else statement, while statement can write by using recursive mechanism. Of course the results may have some difficult step’s to understand.

The example given below can explain the Recursive algorithm smoothly:

Example 1: Tower of Hanoi. [1. Move one disk at a time. 2. No time can a disk be on top of a smaller disk. ]

    The tower of Hanoi is a puzzle discovered after the ancient Tower of Brahma ritual.

According to the great personalities at the time of world creation there was a diamond tower with 64 golden disks in position as like the tower A in the above figure.

The simplest way  to solve this problem is : consider the tower having n numbers of disks .  Now we want largest disk at the bottom of our another tower. So we will move the n-1 disk from tower A to tower B. Then move the nth disk from tower A to the tower C . Now just move the n-1 disks from tower B to the tower C. and then we will get the solved Tower of Hanoi . . .

Confused ? . . . OK I’ll explain with very simple steps. So let’s consider we have three disks i.e. Red,White,Green and three towers i.e. A,B,C . All disks are arranged in order such as largest disk is at the bottom of the Tower as shown in first figure.

1. Put the top disk (Red) from Tower A to the Tower C .

2.Put the top disk of Tower A (White ) to the Tower B.

3.Put the Red disk from Tower C to the Tower B.

4. Now put the Green disk from Tower A to the Tower C .

5.Put the Red disk from Tower B to Tower A.

6.I think after this step solution is clear to all of you. 7.Now we have to just put the White, Red disk in Tower C respectively.

  1. Algorithm TowerOfHanoi( n, x, y, z )
  2. // move the top n disks from tower x to tower y.
  3. {
  4. if ( n > = 1 ) then
  5. {
  6. TowerOfHanoi ( n – 1, x, z, y )
  7. write( ” move top disk from tower “, x, “to top of tower”, y );
  8. TowerOfHanoi ( n – 1, z, y, x );
  9. }
  10. }

Example 2: Recursive Algorithm for Sequential Search

Algorithm:   SeqSearch(L, i, j, x)
Input:L is an array, i and j are positive integers, i<=j, and x is the key to be searched for in L.
Output: If x is in L between indexes i and j, then output its index, else output 0.

  1. Algorithm SeqSearch(L, i, j, x)
  2. {
  3. ifi<=j ,then
  4. {
  5. if L(i) = x, then return i ;
  6. else return SeqSearch(L, i+1, j, x)
  7. }
  8. else return 0.
  9. }
  10. // Recursive algorithms can also be used to test objects for membership in a set.

Hope Recursive algorithm is well understood by you all guys . Stay tuned for knowing the next type . Please do write all your queries in comment box.Thank you…

Featured

Algorithms

Hey Everyone!! I’m Sneha sharing with you all ; some interesting facts about algorithm which will give you a rocket to be a Algorithmic .

Introduction to Algorithms:

The word algorithm comes from the name of a Persian author, Abu Ja’far Mohammed ibn Musa al Khowarizmi (c. 780 – c. 850), where Al Khowarizmi rendered as Algoritmi in Latin, led to the term “algorithm”. Al Khowarizmi was the Persian scholar whose contribution was mainly in mathematics, astronomy, and geography.

“Algorithm” word is most effectively used in computer science for programming logic. Computer uses the algorithm to find out the solution of specific problems with in finite amount of time and with efficient use of computer resources such as memory. 

That’s why algorithm is different from words like process, technique, or method.

What is Algorithm?

Algorithm is a set of instructions that are written and executed step by step for solving problems. It is mostly written in pseudo code (i. e human readable rather than machine).

Role of algorithm in programming language:

Any software design process has two phases. 

  1. Problem solving phase: In problem solving phase we create algorithm in pseudo code.
  2. Implementation phase: In implementation phase we translate the pseudo code algorithm into programming language. 

Any algorithm that we write must have the following five characteristics:

  1. Input:   Algorithm must have zero or more number of inputs.
  2. Output:  Algorithm must produce one or more number of outputs with respective to the input.
  3. Definiteness:    Each step of algorithm must be stated clearly and  should be able to jump to the next step with fixed output, if it is the last step then algorithm should get terminated with correct and desired output. 
  4. Finiteness:   Algorithm should have finite number of steps with end statement and execution time of algorithm should be measurable.
  5. Effectiveness:  Each step in algorithm must be easily convertible to programming language.                           

To become algorithmic, following four points must be considered. 

  1. How to devise the algorithm:                                

Creating an algorithm is  an art which is understandable to resolve a problem.

  1. How to validate algorithm:                                   

Once the algorithm is designed, it is a compulsion to prove that it works correctly and gives the correct and desired output.

  1. How to analyze algorithm:                                                              

After execution of algorithm, it uses the central part of a computer that is  CPU (central processing unit)to perform operations and its memory to hold the data. This field includes the task of determining how much computing time and storage required for the given algorithm.              

    This is one of the challenging fields which includes all mathematical calculations.

  1. How to test a program:                                    

Testing of program consists of two parts :               

i.  Debugging:    Debugging is the task in which the faulty result (error) is encountered and then correct them; but the debugging can only encounter the faulty result or errors which are present in the program.So to verify the correctness of the output we use next part of testing i.e. profiling.   

ii.   Profiling:     Profiling is nothing but the performance measurement.  Profiling is the process of executing a correct program on data sets and measuring the time and space it takes to compute the program.

What are the types algorithm?   

There are many types of algorithm but the following five are the most fundamental types of algorithm are:

  1. Recursive algorithms:   

A recursive is a function that is defined in terms of itself; similarly, an algorithm is said to be recursive if the same algorithm is invoked in the body.

2.Randomized  algorithms:          

    Randomized algorithm is categorized into two classes:         

A.   Las Vegas algorithms:    It gives the same output(correct).   

B. Monte Carlo algorithms:  This may give result differ from run to run.

3. Backtracking algorithm:                                        

Many problems which deal with searching for a set of solution or which ask for an optimal solution satisfying some constraints can be solved using the Backtracking Formulation.

4.Divide and conquer algorithm:                                   

Divide and conquer is a strategy in which it suggests to split the input into distinct sub problems, which have compulsion  of same type as the original problem,and must be found to combine sub-solution into one whole solution.

5.Greedy algorithm:                                           

The greedy algorithms are the straightforward design technique.  In this algorithm we must find a feasible solution that either maximize or minimize a given objective function.           

It includes some problems as given below:       

a.   Knapsack problem.   

b.   Job Sequencing problem with deadline.  

C.  Minimum-cost  Spanning trees.       

This problem can be solved with prim’s algorithm or Kruskal’s algorithm

The next blog will give you brief Knowledge about the Types of Algorithms.So please share your views in comment box. Thank you and stay connected . . .

Divide & Conquer

hello Everyone !!

Welcome back to the “Shelf of Algorithm”. Today we will cover Divide and Conquer method , type of algorithm . This type is one of the interesting and simple one ,so I hope you all will get clear easily then Let’s go . . . .

Divide and Conquer method is . . . I think the method name define itself only i.e. We have to break problem into small sub problems ,sub instances and the solution of these sub problems get combined in single solution.

Defination:

Divide and conquer is a strategy in which it suggests to split the input into distinct sub problems, which have compulsion  of same type as the original problem,and must be found to combine sub-solution into one whole solution.

This algorithm is solved with simple three steps as follows:

1. Divide: Split the original problem into sub problem.
2. Conquer: Recursively solve these sub problems.
3. Combine: Appropriately combine the answers.

Suppose given function to determine on n inputs this algorithm gives an idea to split this n inputs into k distinct subsets .

 1  < K <=  n; Where K is sub problems of the original problem, n is the original problem.

Example :  Algorithm for divide and conquer

  1. Algorithm DAndC( P )
  2. {
  3.      If Small( P ) then return S( P );
  4.      Else 
  5.      {
  6.             Divide P into smaller instances P1,  P2, P3, P4, . . . ,Pk , k >= 1;
  7.             Apply DAndC to each of these sub problems;
  8.           Return Combine( DAndC( p1 ), DAndC( p2 ), DAndC( p3 ), . . ,DAndC( pk ) );
  9.      }
  10. }

Step 1: check whether the given problem is smallest or not (i.e. whether it contains single element).

  • DAndC  is initially invoked as DAndC(P), where P is the problem to solve.
  • Small(P) is Boolean valued function  which determine the the function have small size or not .
  • If true : i.e. Given problem conatins single element then
    • {
    • it will show output directly and we havn’t need to split it.
    • }

Step 2: If problem contain more than one element then divide given problem.

  • {
  • the problem is get split into distinct sub problems . 
  •  [ p1, p2 , p3 , . . . . . . pk] .
  • Then sub problems P1,  P2, P3, P4, . . .  ,Pk are solved by the above algorithm.
  • }

Step 3: Apply D&C to all sub problems or sub instances and combine in single solution.

  • apply D&C to all sub problems i.e.
  • D&C( P1) , D&C( P2) ,D&C( P3) , . . . . . . D&C( Pk).
  • combine the all solution in single
  • combine( D&C( P1) , D&C( P2) ,D&C( P3) , . . . . . . D&C( Pk) ).

Examples :

  • Binary Search.
  • Max-Min by D&C.
  • Merge Sort.
  • Quick Sort.
  • Selection sort.

I think examples are the fastest method to understand the concept quickly and clearly. I’ll cover one example in brief , so let’s go for Binary search.

Binary search :

The simple logic of binary search is calculate the mid of element list and compare with search element i.e. search key if element is smaller than mid then replace the high with mid-1 if it is greater than the mid then replace low with mid+1 . We will go with algorithm. . .

  • Algorithm BinSrch( a , i , l , x ) // x is the element to be searched
  • {
    • if ( l = i) then // problem size is small or not
    • {
      • if ( x = a[i] )
        • then return i ;
      • else return 0 ;
    • }
    • else
    • { //break problem into subproblems
      • mid = (i+2) / 2 ;
      • if (x = a[mid]) then return mid //mid is the search element
      • else if( x < a[mid] )
        • return BinSrch( a , i , mid-1 , x )
      • else
        • return BinSrch( a , mid+1 , l , x )
    • }
  • }

Difficult to understand ? ? Let’s consider one exapmle of 14 entries :

-15 , -6 , 0 , 7 , 9 , 23 , 54 , 82 , 101 , 112 , 125 , 131 , 142 , 151

So we have to find out the 151, -14, &9 to search this element we will follow our algorithm so first element ,search key is 151 .

Step 1:

Our element list is large in size so instead of going in if statement we will directly go through the else body .We have calculate the mid of list.

i = 14. //we have 14 element in our list mid = i / 2 // mid = 14 / 2 = 7.

Step 2 :

Check the search key is equal to the value in the mid position. So check … X = a[ mid ]. // Check 151 = a[7]

So we have a[7] = 54 which is not equal to the search key.

Step 3:

Check if x is less than a[7] i.e. check 151 is less than 54 so no it will go to the next else statment and replace the i with mid+1 .Now we have to search between a[8] to a[14] .

Again it will calculate the mid and it will give the value 112 which is again smaller than search key so replace i with mid+1 .after two iteration we will find out our search key i.e. 151 at the location of a[14].

Remaining search keys are -14 and 9 so -14 will not get found as the low get larger than high it will terminate and print that element is not present.

Hope you all get clear about the divide and conquer concept. So let me know if you have any doubt or any questions in comment section given below . Till then stay tuned stay connected to know about the next type. I’ll come back with next blog and next type.

Thank you…

Design a site like this with WordPress.com
Get started