1626482580

# What is an Algorithm?

Haveyou ever heard the word Algorithm? I think many of you might have, whether it was online or maybe in some conversation about technology. It’s a word that gets thrown around a lot. But what exactly does this mean. Well, simply put Algorithm is nothing but a formula for solving a problem. You can take it as a step-by-step guide for solving a problem. For now, you can understand it as a black box that takes input and produces an output. Now, you might be thinking that a recipe does the same work. Is it an algorithm then? Well, yes! Algorithms resemble recipes. The recipe tells us how to accomplish a task by performing some steps. However, an algorithm is a technical term with a more specific meaning.

In computer programming terms, an algorithm is a set of well-defined instructions to solve a particular problem. It takes a set of inputs and produces the desired output.

For example, a search engine is an algorithm that takes a search query as an input and searches its database for items relevant to the search query. It then outputs the results. Ah, that’s a bit of jargon. Let’s make it simple using an example.

Here’s an algorithm to add 3 numbers and print their average:

1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, as inputs in variables num1, num2, and num3 respectively.
4. Declare an integer variable avg to store the average of the 3 numbers.
5. Add the 3 numbers, divide by 3 and store the result in the variable avg.
6. Print the value of variable avg.
7. END

### Algorithm Analysis

Algorithm analysis provides theoretical estimates for the resources (space and time) needed by any algorithm which solves a given computational problem. We can analyze the efficiency of an algorithm by using time and space complexity.

• Time complexity: The amount of time taken by an algorithm to run as a function of the length of the input. Time is an important factor because nobody would like a google search that would take 2–3 hours to give back the results.
• **Space complexity: **The amount of space or memory taken by an algorithm to run as a function of the length of the input. As we know, computer memory is limited, so space is also an important factor in measuring algorithms’ efficiency.

### Algorithms in our daily life

We knowingly or unknowingly use algorithms everywhere around us, from mobile phones in our hands to ATMs in the market. Here are some of the famous algorithms we use daily:

### Algorithms- The future of business success

Algorithms have allowed organizations to scale in ways that weren’t possible even five or 10 years ago. Don’t believe me? Let’s see stats! Google is an organization that has mastered data and algorithms. In 2002, their search algorithm earned them half a billion dollars a year. That’s already good money by any standard, but their investments into algorithms are really paying off now. The latest version of their search algorithm now makes a billion dollars in just one day. In other words, they’re now collecting the same amount of revenue in less than one percent of the time it used to take.

#algorithms

1593347004

## A greedy algorithm is a simple

The Greedy Method is an approach for solving certain types of optimization problems. The greedy algorithm chooses the optimum result at each stage. While this works the majority of the times, there are numerous examples where the greedy approach is not the correct approach. For example, let’s say that you’re taking the greedy algorithm approach to earning money at a certain point in your life. You graduate high school and have two options:

#computer-science #algorithms #developer #programming #greedy-algorithms #algorithms

1596427800

## KMP — Pattern Matching Algorithm

Finding a certain piece of text inside a document represents an important feature nowadays. This is widely used in many practical things that we regularly do in our everyday lives, such as searching for something on Google or even plagiarism. In small texts, the algorithm used for pattern matching doesn’t require a certain complexity to behave well. However, big processes like searching the word ‘cake’ in a 300 pages book can take a lot of time if a naive algorithm is used.

# The naive algorithm

Before, talking about KMP, we should analyze the inefficient approach for finding a sequence of characters into a text. This algorithm slides over the text one by one to check for a match. The complexity provided by this solution is O (m * (n — m + 1)), where m is the length of the pattern and n the length of the text.

Find all the occurrences of string pat in string txt (naive algorithm).

``````#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string pat = "ABA"; // the pattern
string txt = "CABBCABABAB"; // the text in which we are searching

bool checkForPattern(int index, int patLength) {
int i;
// checks if characters from pat are different from those in txt
for(i = 0; i < patLength; i++) {
if(txt[index + i] != pat[i]) {
return false;
}
}
return true;
}

void findPattern() {
int patternLength = pat.size();
int textLength = txt.size();

for(int i = 0; i <= textLength - patternLength; i++) {
// check for every index if there is a match
if(checkForPattern(i,patternLength)) {
cout << "Pattern at index " << i << "\n";
}
}

}

int main()
{
findPattern();
return 0;
}
view raw
main6.cpp hosted with ❤ by GitHub
``````

# KMP approach

This algorithm is based on a degenerating property that uses the fact that our pattern has some sub-patterns appearing more than once. This approach is significantly improving our complexity to linear time. The idea is when we find a mismatch, we already know some of the characters in the next searching window. This way we save time by skip matching the characters that we already know will surely match. To know when to skip, we need to pre-process an auxiliary array prePos in our pattern. prePos will hold integer values that will tell us the count of characters to be jumped. This supporting array can be described as the longest proper prefix that is also a suffix.

#programming #data-science #coding #kmp-algorithm #algorithms #algorithms

1624867080

## Algorithm trading backtest and optimization examples

Algorithm trading backtest and optimization examples.

1593350760

## Genetic Algorithm (GA): A Simple and Intuitive Guide

Learn what are metaheuristics and why we use them sometimes instead of traditional optimization algorithms. Learn the metaheuristic Genetic Algorithm (GA) and how it works through a simple step by step guide.

#genetic-algorithm #algorithms #optimization #metaheuristics #data-science #algorithms

1626429780

## What is Optimization?

• Making something better.
• Increase efficiency.

## Optimization problem

• A problem in which we have to find the values of inputs (also called solutions or decision variables) from all possible inputs in such a way that we get the “best” output values.
• Definition of “best”- Finding the values of inputs that result in a maximum or minimum of a function called the objective function.
• There can be multiple objective functions as well (depends on the problem).

## Optimization algorithm

An algorithm used to solve an optimization problem is called an optimization algorithm.

## Evolutionary Algorithms

Algorithms that simulate physical and/or biological behavior in nature to solve optimization problems.

## Genetic Algorithm (GA)

• It is a subset of evolutionary algorithms that simulates/models Genetics and Evolution (biological behavior) to optimize a highly complex function.
• A highly complex function can be:
• 1. Very difficult to model mathematically.
• 2. Computationally expensive to solve. Eg. NP-hard problems.
• 3. Involves a large number of parameters.

## Background of GA

• Introduced by Prof. John Holland in 1965.
• The first article on GA was published in 1975.
• GA is based on two fundamental biological processes:
• 1. Genetics (by G.J. Mendel in 1865): It is the branch of biology that deals with the study of genes, gene variation, and heredity.
• 2. Evolution (by C. Darwin in 1875): It is the process by which the population of organisms changes over generations.

## Natural selection in Evolution

1. A population of individuals exists in an environment with limited resources.
2. Competition for those resources causes the selection of those fitter individuals that are better adapted to the environment.
3. These individuals act as seeds for the generation of new individuals through recombination and mutation.
4. Evolved new individuals act as initial population and Steps 1 to 3 are repeated.

## Applications of GA

• 1. Acoustics
• 2. Aerospace Engineering
• 3. Financial Markets
• 4. Geophysics
• 5. Materials Engineering
• 6. Routing and Scheduling
• 7. Systems Engineering

## Problems with GA

• 1. Population Selection Problem
• 2. Defining Fitness Function
• 3. Premature or rapid convergence of GA
• 4. Convergence to Local Optima

#evolutionary-algorithms #data-science #genetic-algorithm #algorithm