ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Algorithms in Programming

Updated on July 12, 2013

Introduction

Welcome, In this hub I will discuss about good algorithms design and software Development. Not only good programming can make good software, the choice of the algorithm is an important aspect of good software design.

To analyze an Algorithm we take two factors into consideration, they are the Time and Space, they are measured in respect to size of the input into the algorithm. Computers are supposed to solve complex problems with relatively large inputs.

I will discuss the importance of Algorithm Design for a computer program.

Computer Algorithm

An algorithm is a description of a sequence of activities that constitute a process of getting the desired outputs from the given inputs. An algorithm when expressed in a language understood by a computer is a computer program.

An algorithm should have the following characteristics:
1. Finiteness : it should have terminate in a finite number of steps
2. Definite: Each step should be precisely defined.
3. Effective: Each step should be sufficiently basic to understand.
4 Input and 5. Output: for a given input the algorithm produces a precise output.

Why study Algorithm

Let us consider an algorithm to compute the GCD / HCF of two numbers.

Algo1. using the basic definition

Input n1,n2;
Min=iif(n1<n2,n1,n2)
for i=Min to 1 step -1
if n1 mod i ==0 and n2 mod i ==0 then
return i; break
else continue

Algo2: the Euclid algorithm, the intelligent method.

Algo3: factorizing and grouping we studied at school.

There are many more algorithms iterative and recursive, we can code to make the work done. We know a few Sorting algorithms; Now, the question arises Which algorithm will be better than the other, this comes to the study on Analysis of Algorithms.

The algorithm which produces its output in less time for all expected inputs on a standard computer is obviously better. But, how shall me measure it?

Generally, the resources that are taken into consideration for analyzing algorithm include:
1. Time expected to be taken in executing the instances of the problem generally as a function of size of the instance. we say the Time Complexity of the algorithm.
2. Memory space expected to be required by the computer system in executing the instances of the problem generally as a function of the size of the instances. we say the Space Complexity of the algorithm.

We can write algorithms for our problems in recursive manner or using the iterative approach. There are many well known techniques for designing an algorithm say 1. Divide & Conquer, 2. Dynamic Programming, 3. Greedy approach, 4. Search and traversals, 5. Back Tracking, 6. Branch and Bound.

After the study of an algorithm, good Data Structure choices makes the efficient computer program.

Asympototic Bounds

These are the mathematical functions and notations for the Analysis. I will not be discussing the Asymptotic Analysis nor defining them in this blog, but I shall be using the Asymptotic analysis to analyze the algorithms discussed later. Please check the web for well known asymptotic functions and notations.

The approaches for determining the complexity for executing an algorithm can be empirical or theoretical. In the view of the advantages of the theoretical approach we shall use it as the only approach for computing complexities of algorithms.

Analysis of Algorithms

As, I do not want to be much theoretical, I shall not write the complete algorithms to explain the concepts discussed, and I did not earlier either; but I will discuss the main concept behind the actual algorithm. I would therefore expect everyone reading this blog having some programming experience with the knowledge of computing.

Problem1. Consider a program to find, if the given input number n is a Prime number.
to solve the problem we normally use a loop starting from 2 to the number n-1, computing the mod at each iteration and break the loop if n is found to be a composite number. Clearly, the for-loop executes at-most n times which is obvious in the worst case.
Do we need at-most n iterations in average case? We can first check if the number is even, and increment the loop step by 2 instead of 1 step, thus checking for divisibility by 4,6,8,10...even numbers would not be necessary. Now, making this slight modification to our algorithm would execute in n/2 +1 steps even in worst case which is better.
This can be improved further? We know that if a number n is divisible by a (integer of course) then there also exists another integer b by which n is divisible, and the relation is given by a * b =n; if a , b are distinct then the relation between a & b is either a>b or a<b. Now, we can easily terminate the loop when the loop-counter reaches square-root-of number n. Thus our algorithm performs much better O(√n) even at worst case compared to O(n).

  int number, factor;
  printf("Input a number"); scanf("%d", &number);
  if (number==2 || number==3)
    {printf("The number is a Prime number"); getch(); exit(0);}
  else
    if (number%2==0)
      {printf("The number is a even number"); getch(); exit(0);}
  for (factor=3;;factor+=2)
  {
    if (number%factor==0)
      {printf("The number is a composite number"); break;}
    else
      if (factor*factor>number)
    {printf("The number is Prime"); break;}
  }

more examples on algorithm Analysis

In the next example, I will discuss the problem of computing prefix average.

The input is a single dimension array A of n elements, the output is the array B where for every element i in B is the average of the elements 1 to i of array A.
this is : B[i]=(1/i)∑A[k] for k=1 to i.

We can compute B[i] for every i, that is at every step of i the array A from index 1 to i is computed, and assigned to B[i]. here we will need two nested loops which results the total operations is a quadratic function of input size n. We can modify the algorithm to a linear algorithm by simply avoiding to compute the partial sums repeatedly.

for i=1 to n
sum=sum+A[i]
B[i]=Sum/i
end for

Problem 3. Computation of the Exponent of a number
lastly explaining this problem using an example rather than theoretically defining it.

Suppose I want to compute 5 to the power 15, So instead of computing 5*5*5*5*5*5*5*5*5*5*5*5*5*5*5 14 multiplication steps, ((((5^2)^2)^2)^2)/5 we need only 5 multiplication steps, squaring at each iteration.

In the next problem on Integer multiplication the Karatsuba algorithm is faster than the known methods.

Algorithm Design

There are many more topics of interest in designing efficient algorithms, of which the following topics of interest shall be discussed later on this blog.

The topis of Interest I would like to discuss are, Please check the web for further information.
1. Divide and Conquer
2. Randomization
3. Greedy Technique
4. Dynamic Programming
5. Graph Algorithms
and finally 6. Algorithmically Unsolvable problems.

I hope I was able to give an introduction regarding the importance of algorithm analysis and design of good computer programs. Hope you enjoyed the reading.

Comments

Submit a Comment

No comments yet.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)