diff --git a/d1/d9a/hopcroft__karp_8cpp.html b/d1/d9a/hopcroft__karp_8cpp.html index 9add134aa..a31e02c9f 100644 --- a/d1/d9a/hopcroft__karp_8cpp.html +++ b/d1/d9a/hopcroft__karp_8cpp.html @@ -150,22 +150,22 @@ Functions
Implementation of Hopcroft–Karp algorithm.
The Hopcroft–Karp algorithm is an algorithm that takes as input a bipartite graph and produces as output a maximum cardinality matching, it runs in O(E√V) time in worst case.
-A bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint and independent sets U and V such that every edge connects a vertex in U to one in V. Vertex sets U and V are usually called the parts of the graph. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.
-Given a matching M, edges that are part of matching are called Matching edges and edges that are not part of M (or connect free nodes) are called Not-Matching edges.
-Given a bipartite graphs G = ( V = ( X , Y ) , E ) whose partition has the parts X and Y, with E denoting the edges of the graph, the goal is to find a matching with as many edges as possible. Equivalently, a matching that covers as many vertices as possible.
-Given a matching M, an augmenting path is an alternating path that starts from and ends on free vertices. All single edge paths that start and end with free vertices are augmenting paths.
-A matching M is not maximum if there exists an augmenting path. It is also true other way, i.e, a matching is maximum if no augmenting path exists.
-1) Initialize the Maximal Matching M as empty. 2) While there exists an Augmenting Path P Remove matching edges of P from M and add not-matching edges of P to M (This increases size of M by 1 as P starts and ends with a free vertex i.e. a node that is not part of matching.) 3) Return M.
An implementation of a median calculation of a sliding window along a data stream.
Given a stream of integers, the algorithm calculates the median of a fixed size window at the back of the stream. The leading time complexity of this algorithm is O(log(N), and it is inspired by the known algorithm to [find median from (infinite) data stream](https://www.tutorialcup.com/interview/algorithm/find-median-from-data-stream.htm), with the proper modifications to account for the finite window size for which the median is requested
-The sliding window is managed by a list, which guarantees O(1) for both pushing and popping. Each new value is pushed to the window back, while a value from the front of the window is popped. In addition, the algorithm manages a multi-value binary search tree (BST), implemented by std::multiset. For each new value that is inserted into the window, it is also inserted to the BST. When a value is popped from the window, it is also erased from the BST. Both insertion and erasion to/from the BST are O(logN) in time, with N the size of the window. Finally, the algorithm keeps a pointer to the root of the BST, and updates its position whenever values are inserted or erased to/from BST. The root of the tree is the median! Hence, median retrieval is always O(1)
Time complexity: O(logN). Space complexity: O(N). N - size of window
two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j
Time Complexity --> O(n.log n)
Space Complexity --> O(n) ; additional array temp[1..n]
An implementation of LRU Cache. Lru is a part of cache algorithms (also frequently called cache replacement algorithms or cache replacement policies).
-For a cache of page frame x:
Every time a requested page is not found in cache, that is a miss or page fault, and if the page is present in cache, then its a hit.
-The working principle of the Bubble sort algorithm.
Bubble sort is a simple sorting algorithm used to rearrange a set of ascending or descending order elements. Bubble sort gets its name from the fact that data "bubbles" to the top of the dataset.
-What is Swap?
Swapping two numbers means that we interchange their values. Often, an additional variable is required for this operation. This is further illustrated in the following:
diff --git a/d4/d32/inorder__successor__of__bst_8cpp.html b/d4/d32/inorder__successor__of__bst_8cpp.html index 88bfe2d2f..9a557dc95 100644 --- a/d4/d32/inorder__successor__of__bst_8cpp.html +++ b/d4/d32/inorder__successor__of__bst_8cpp.html @@ -176,21 +176,21 @@ FunctionsAn implementation for finding the Inorder successor of a binary search tree Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal.
-* In this case, the left-most deepest node in the right subtree will
come just after the given node as we go to left deep in inorder.
Implementation to check whether a number is a power of 2 or not.
This algorithm uses bit manipulation to check if a number is a power of 2 or not.
-Let the input number be n, then the bitwise and between n and n-1 will let us know whether the number is power of 2 or not
For Example, If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then the result will be zero, which indicates that it is the power of 2 If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then the result will not be zero , which indicates that it is not the power of 2
Implementation of Jarvis’s algorithm.
Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.
-The idea of Jarvis’s Algorithm is simple, we start from the leftmost point (or point with minimum x coordinate value) and we keep wrapping points in counterclockwise direction.
The idea is to use orientation() here. Next point is selected as the point that beats all other points at counterclockwise orientation, i.e., next point is q if for any other point r, we have “orientation(p, q, r) = counterclockwise”.
diff --git a/d4/d9f/selection__sort__recursive_8cpp.html b/d4/d9f/selection__sort__recursive_8cpp.html index c4230222e..0fa0fdd44 100644 --- a/d4/d9f/selection__sort__recursive_8cpp.html +++ b/d4/d9f/selection__sort__recursive_8cpp.html @@ -152,7 +152,7 @@ FunctionsImplementation of the Selection sort implementation using recursion.
The selection sort algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front (left) of the list, and a sublist of the remaining unsorted items that occupy the rest of the list. Initially, the sorted sublist is empty, and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on the sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
-FindMinIndex This function finds the minimum element of the array(list) recursively by simply comparing the minimum element of array reduced size by 1 and compares it to the last element of the array to find the minimum of the whole array.
SelectionSortRecursive Just like selection sort, it divides the list into two parts (i.e.: sorted and unsorted) and finds the minimum of the unsorted array. By calling the FindMinIndex function, it swaps the minimum element with the first element of the list, and then solves recursively for the remaining unsorted list.
Gram Schmidt Orthogonalisation Process
Takes the input of Linearly Independent Vectors, returns vectors orthogonal to each other.
-Take the first vector of given LI vectors as first vector of Orthogonal vectors. Take projection of second input vector on the first vector of Orthogonal vector and subtract it from the 2nd LI vector. Take projection of third vector on the second vector of Othogonal vectors and subtract it from the 3rd LI vector. Keep repeating the above process until all the vectors in the given input array are exhausted.
For Example: In R2, Input LI Vectors={(3,1),(2,2)} then Orthogonal Vectors= {(3, 1),(-0.4, 1.2)}
diff --git a/d5/d45/sublist__search_8cpp.html b/d5/d45/sublist__search_8cpp.html index 15b17f595..a51494f35 100644 --- a/d5/d45/sublist__search_8cpp.html +++ b/d5/d45/sublist__search_8cpp.html @@ -163,14 +163,14 @@ FunctionsImplementation of the Sublist Search Algorithm
-However MD5 has be know to be cryptographically weak for quite some time, yet it is still widely used. This weakness was exploited by the Flame Malware in 2012
-First of all, all values are expected to be in [little endian] (https://en.wikipedia.org/wiki/Endianness). This is especially important when using part of the bytestring as an integer.
The first step of the algorithm is to pad the message for its length to be a multiple of 64 (bytes). This is done by first adding 0x80 (10000000) and then only zeroes until the last 8 bytes must be filled, where then the 64 bit size of the input will be added
diff --git a/d5/ddb/bogo__sort_8cpp.html b/d5/ddb/bogo__sort_8cpp.html index ed85f4609..499d6960c 100644 --- a/d5/ddb/bogo__sort_8cpp.html +++ b/d5/ddb/bogo__sort_8cpp.html @@ -149,7 +149,7 @@ FunctionsImplementation of Bogosort algorithm
In computer science, bogosort (also known as permutation sort, stupid sort, slowsort, shotgun sort, random sort, monkey sort, bobosort or shuffle sort) is a highly inefficient sorting algorithm based on the generate and test paradigm. Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input.Randomized version is implemented here.
-Shuffle the array untill array is sorted.
Following are some guidelines for contributors who are providing reviews to the pull-requests.
Implementation of 0-1 Knapsack problem.
+Implementation of unbounded 0-1 knapsack problem.
|
+ Algorithms_in_C++ 1.0.0
+
+ Set of algorithms implemented in C++.
+ |
+
Implementation of the Unbounded 0/1 Knapsack Problem. +More...
+#include <iostream>#include <vector>#include <cassert>#include <cstdint>+Namespaces | |
| namespace | dynamic_programming |
| Dynamic Programming algorithms. | |
| namespace | Knapsack |
| Implementation of 0-1 Knapsack problem. | |
+Functions | |
| std::uint16_t | dynamic_programming::unbounded_knapsack::KnapSackFilling (std::uint16_t i, std::uint16_t W, const std::vector< std::uint16_t > &val, const std::vector< std::uint16_t > &wt, std::vector< std::vector< int > > &dp) |
| Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach. | |
| std::uint16_t | dynamic_programming::unbounded_knapsack::unboundedKnapsack (std::uint16_t N, std::uint16_t W, const std::vector< std::uint16_t > &val, const std::vector< std::uint16_t > &wt) |
| Wrapper function to initiate the unbounded knapsack calculation. | |
| static void | tests () |
| self test implementation | |
| int | main () |
| main function | |
Implementation of the Unbounded 0/1 Knapsack Problem.
+The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each item. The goal is to maximize the total value without exceeding the given knapsack capacity. Unlike the 0/1 knapsack, where each item can be taken only once, in this variation, any item can be picked any number of times as long as the total weight stays within the knapsack's capacity.
+Given a set of N items, each with a weight and a value, represented by the arrays wt and val respectively, and a knapsack with a weight limit W, the task is to fill the knapsack to maximize the total value.
The approach uses dynamic programming to build a solution iteratively. A 2D array is used for memoization to store intermediate results, allowing the function to avoid redundant calculations.
+| std::uint16_t dynamic_programming::unbounded_knapsack::KnapSackFilling | +( | +std::uint16_t | i, | +
| + | + | std::uint16_t | W, | +
| + | + | const std::vector< std::uint16_t > & | val, | +
| + | + | const std::vector< std::uint16_t > & | wt, | +
| + | + | std::vector< std::vector< int > > & | dp ) | +
Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach.
+| i | Current index in the value and weight vectors. |
| W | Remaining capacity of the knapsack. |
| val | Vector of values corresponding to the items. |
| wt | Vector of weights corresponding to the items. |
| dp | 2D vector for memoization to avoid redundant calculations. |
| int main | +( | +void | ) | ++ |
+
|
+ +static | +
self test implementation
+| std::uint16_t dynamic_programming::unbounded_knapsack::unboundedKnapsack | +( | +std::uint16_t | N, | +
| + | + | std::uint16_t | W, | +
| + | + | const std::vector< std::uint16_t > & | val, | +
| + | + | const std::vector< std::uint16_t > & | wt ) | +
Wrapper function to initiate the unbounded knapsack calculation.
+| N | Number of items. |
| W | Maximum weight capacity of the knapsack. |
| val | Vector of values corresponding to the items. |
| wt | Vector of weights corresponding to the items. |
The above process is a typical displacement process. When x assigns the value to x, the old value of x is lost. That's why we created a variable z to create the first value of the value of x, and finally, we have assigned to y.
-Bubble Sort Best Case Performance. \(O(n)\). However, you can't get the best status in the code we shared above. This happens on the optimized bubble sort algorithm. It's right down there.
-Bubble Sort Worst Case Performance is \(O(n^{2})\). Why is that? Because if you remember Big O Notation, we were calculating the complexity of the algorithms in the nested loops. The \(n * (n - 1)\) product gives us \(O(n^{2})\) performance. In the worst case all the steps of the cycle will occur.
-Bubble Sort is not an optimal algorithm. In average, \(O(n^{2})\) performance is taken.
Simple C++ implementation of the SHA-1 Hashing Algorithm
SHA-1 is a cryptographic hash function that was developped by the NSA 1995. SHA-1 is not considered secure since around 2010.
-The first step of the algorithm is to pad the message for its length to be a multiple of 64 (bytes). This is done by first adding 0x80 (10000000) and then only zeroes until the last 8 bytes must be filled, where then the 64 bit size of the input will be added
Once this is done, the algo breaks down this padded message into 64 bytes chunks. Each chunk is used for one round, a round breaks the chunk into 16 blocks of 4 bytes. These 16 blocks are then extended to 80 blocks using XOR operations on existing blocks (see code for more details). The algorithm will then update its 160-bit state (here represented used 5 32-bits integer) using partial hashes computed using special functions on the blocks previously built. Please take a look at the wikipedia article for more precision on these operations
Iterative version of Preorder, Postorder, and preorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)
Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current, and pop and elemnt from the stack. Store the data of current into the result array, and start traversing from it. Push both the child node of the current node into the stack, first right child then left child. Repeat the same set of steps untill the Stack becomes empty. And return the result array as the preorder traversal of a tree.
-Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current, and pop and elemnt from the stack. Store the data of current into the result array, and start traversing from it. Push both the child node of the current node into the stack, first left child then right child. Repeat the same set of steps untill the Stack becomes empty. Now reverse the result array and then return it to the calling function as a postorder traversal of a tree.
-Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current. Now iterate and take the current to the extreme left of the tree by traversing only to its left. Pop the elemnt from the stack and assign it to the current. Store the data of current into the result array. Repeat the same set of steps until the Stack becomes empty or the current becomes NULL. And return the result array as the inorder traversal of a tree.
The Disjoint union is the technique to find connected component in graph efficiently.
-In Graph, if you have to find out the number of connected components, there are 2 options
Recursive version of Inorder, Preorder, and Postorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)
-For traversing a (non-empty) binary tree in an inorder fashion, we must do these three things for every node n starting from the tree’s root:
(L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (N) Process n itself. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again.
In normal inorder traversal, we visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse inorder traversal.
-For traversing a (non-empty) binary tree in a preorder fashion, we must do these three things for every node n starting from the tree’s root:
(N) Process n itself. (L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again.
In normal preorder traversal, visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse preorder traversal.
-For traversing a (non-empty) binary tree in a postorder fashion, we must do these three things for every node n starting from the tree’s root:
(L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again. (N) Process n itself.
diff --git a/dd/d24/namespacedynamic__programming.html b/dd/d24/namespacedynamic__programming.html index 6e006a473..4ebe1e6c9 100644 --- a/dd/d24/namespacedynamic__programming.html +++ b/dd/d24/namespacedynamic__programming.html @@ -135,6 +135,7 @@ FunctionsDynamic Programming algorithms.
Dynamic programming algorithms.
+Namespace for dynamic programming algorithms.
for std::vector
Dynamic Programming algorithm.
for IO operations
diff --git a/dd/d47/namespacemath.html b/dd/d47/namespacemath.html index 07e168ba4..1ab7c4428 100644 --- a/dd/d47/namespacemath.html +++ b/dd/d47/namespacemath.html @@ -315,7 +315,7 @@ Functionsfor assert for integral types for std::invalid_argument for std::cout
for std::cin and std::cout for assert
Given a recurrence relation; evaluate the value of nth term. For e.g., For fibonacci series, recurrence series is f(n) = f(n-1) + f(n-2) where f(0) = 0 and f(1) = 1. Note that the method used only demonstrates recurrence relation with one variable (n), unlike nCr problem, since it has two (n, r)
This problem can be solved using matrix exponentiation method.
The direction ratios (DR) are calculated as follows: 1st DR, J: (b * z) - (c * y) 2nd DR, A: -((a * z) - (c * x)) 3rd DR, N: (a * y) - (b * x)
Therefore, the direction ratios of the cross product are: J, A, N The following C++ Program calculates the direction ratios of the cross products of two vector. The program uses a function, cross() for doing so. The direction ratios for the first and the second vector has to be passed one by one seperated by a space character.
Magnitude of a vector is the square root of the sum of the squares of the direction ratios.
-An example of a running instance of the executable program:
Pass the first Vector: 1 2 3 Pass the second Vector: 4 5 6 The cross product is: -3 6 -3 Magnitude: 7.34847
diff --git a/dir_8a20dd5bfd5341a725342bf72b6b686f.html b/dir_8a20dd5bfd5341a725342bf72b6b686f.html index c4ddbf396..99ab72171 100644 --- a/dir_8a20dd5bfd5341a725342bf72b6b686f.html +++ b/dir_8a20dd5bfd5341a725342bf72b6b686f.html @@ -161,6 +161,9 @@ FilesThis repository is a collection of open-source implementation of a variety of algorithms implemented in C++ and licensed under MIT License. These algorithms span a variety of topics from computer science, mathematics and statistics, data science, machine learning, engineering, etc.. The implementations and the associated documentation are meant to provide a learning resource for educators and students. Hence, one may find more than one implementation for the same objective but using a different algorithm strategies and optimizations.
-Online Documentation is generated from the repository source codes directly. The documentation contains all resources including source code snippets, details on execution of the programs, diagrammatic representation of program flow, and links to external resources where necessary. The documentation also introduces interactive source code with links to documentation for C++ STL library functions used. Click on Files menu to see the list of all the files documented with the code.
Documentation of Algorithms in C++ by The Algorithms Contributors is licensed under CC BY-SA 4.0
As a community developed and maintained repository, we welcome new un-plagiarized quality contributions. Please read our Contribution Guidelines.