Tutorial by Examples: algorithm

The partition of an integer is a way of writing it as a sum of positive integers. For example, the partitions of the number 5 are: 5 4 + 1 3 + 2 2 + 2 + 1 2 + 1 + 1 + 1 1 + 1 + 1 + 1 + 1 Notice that changing the order of the summands will not create a different partition. The partition f...
public class IntegerPartition { public static int[,] Result = new int[100,100]; private static int Partition(int targetNumber, int largestNumber) { for (int i = 1; i <= targetNumber; i++) { for (int j = 1; j <= largestNumber; j++) ...
Rabin-Karp Algorithm is a string searching algorithm created by Richard M. Karp and Michael O. Rabin that uses hashing to find any one of a set of pattern strings in a text. A substring of a string is another string that occurs in. For example, ver is a substring of stackoverflow. Not to be confuse...
Maximum subarray problem is the method to find the contiguous subarray within a one-dimensional array of numbers which has the largest sum. The problem was originally proposed by Ulf Grenander of Brown University in 1977, as a simplified model for maximum likelihood estimation of patterns in digiti...
Background Theory: Bresenham’s Line Drawing Algorithm is an efficient and accurate raster line generating algorithm developed by Bresenham. It involves only integer calculation so it is accurate and fast. It can also be extended to display circles another curves. In Bresenham line drawing algorith...
Sliding window algorithm is used to perform required operation on specific window size of given large buffer or array. Window starts from the 1st element and keeps shifting right by one element. The objective is to find the minimum k numbers present in each window. This is commonly know as Sliding w...
public class SlidingWindow { public static int[] MaxSlidingWindow(int[] input, int k) { int[] result = new int[input.Length - k + 1]; for (int i = 0; i <= input.Length - k; i++) { var max = input[i]; for (int j = 1; j < k; j++) ...
Suppose that we have a text and a pattern. We need to determine if the pattern exists in the text or not. For example: +-------+---+---+---+---+---+---+---+---+ | Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | +-------+---+---+---+---+---+---+---+---+ | Text | a | b | c | b | c | g | l | x | +-------...
Floyd-Warshall's algorithm is for finding shortest paths in a weighted graph with positive or negative edge weights. A single execution of the algorithm will find the lengths (summed weights) of the shortest paths between all pair of vertices. With a little variation, it can print the shortest path ...
Weighted Job Scheduling Algorithm can also be denoted as Weighted Activity Selection Algorithm. The problem is, given certain jobs with their start time and end time, and a profit you make when you finish the job, what is the maximum profit you can make given no two jobs can be executed in parallel...
Let's say we have a problem of size n. Now for each step of our algorithm(which we need write), our original problem becomes half of its previous size(n/2). So at each step, our problem becomes half. StepProblem1n/22n/43n/84n/16 When the problem space is reduced(i.e solved completely), it cannot ...
Problem definition: An 8 puzzle is a simple game consisting of a 3 x 3 grid (containing 9 squares). One of the squares is empty. The object is to move to squares around into different positions and having the numbers displayed in the "goal state". Given an initial state of 8-puzzle...
Haystack: The string in which given pattern needs to be searched. Needle: The pattern to be searched. Time complexity: Search portion (strstr method) has the complexity O(n) where n is the length of haystack but as needle is also pre parsed for building prefix table O(m) is required for building p...
From VBA array sort function? Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long) Dim pivot As Variant Dim tmpSwap As Variant Dim tmpLow As Long Dim tmpHi As Long tmpLow = inLow tmpHi = inHi pivot = vArray((inLow + inHi) \ 2) While (tmpLow <=...
For those of you that are new to programming in Swift and those of you coming from different programming bases, such as Python or Java, this article should be quite helpful. In this post, we will discuss a simple solution for implementing swift algorithms. Fizz Buzz You may have seen Fizz Buzz wri...
There are certain principles that apply to optimization in any computer language, and Python is no exception. Don't optimize as you go: Write your program without regard to possible optimizations, concentrating instead on making sure that the code is clean, correct, and understandable. If it's too...
Algorithm PMinVertexCover (graph G) Input connected graph G Output Minimum Vertex Cover Set C Set C <- new Set<Vertex>() Set X <- new Set<Vertex>() X <- G.getAllVerticiesArrangedDescendinglyByDegree() for v in X do List<Vertex> adjacentVertices1 <- G....
A binary tree is BST if it satisfies any one of the following condition: It is empty It has no subtrees For every node x in the tree all the keys (if any) in the left sub tree must be less than key(x) and all the keys (if any) in the right sub tree must be greater than key(x). So a straightf...
A generic new() constructor that takes the string name of the desired algorithm as its first parameter also exists to allow access to the above listed hashes as well as any other algorithms that your OpenSSL library may offer. The named constructors are much faster than new() and should be preferred...

Page 2 of 2