The Algorithms - Go
Algorithms implemented in Go (for education)
The repository is a collection of open-source implementation of a variety of algorithms implemented in Go and licensed under MIT License.
Read our Contribution Guidelines before you contribute.
List of Algorithms
Packages:
ahocorasick
Functions:
Advanced
: Advanced Function performing the Advanced Aho-Corasick algorithm. Finds and prints occurrences of each pattern.AhoCorasick
: AhoCorasick Function performing the Basic Aho-Corasick algorithm. Finds and prints occurrences of each pattern.ArrayUnion
: ArrayUnion Concats two arrays of int's into one.BoolArrayCapUp
: BoolArrayCapUp Dynamically increases an array size of bool's by 1.BuildAc
: Functions that builds Aho Corasick automaton.BuildExtendedAc
: BuildExtendedAc Functions that builds extended Aho Corasick automaton.ComputeAlphabet
: ComputeAlphabet Function that returns string of all the possible characters in given patterns.ConstructTrie
: ConstructTrie Function that constructs Trie as an automaton for a set of reversed & trimmed strings.Contains
: Contains Returns 'true' if array of int's 's' contains int 'e', 'false' otherwise.CreateNewState
: CreateNewState Automaton function for creating a new state 'state'.CreateTransition
: CreateTransition Creates a transition for function σ(state,letter) = end.GetParent
: GetParent Function that finds the first previous state of a state and returns it. Used for trie where there is only one parent.GetTransition
: GetTransition Returns ending state for transition σ(fromState,overChar), '-1' if there is none.GetWord
: GetWord Function that returns word found in text 't' at position range 'begin' to 'end'.IntArrayCapUp
: IntArrayCapUp Dynamically increases an array size of int's by 1.StateExists
: StateExists Checks if state 'state' exists. Returns 'true' if it does, 'false' otherwise.
Types
Result
: No description provided.
binary
Package binary describes algorithms that use binary operations for different calculations.
Functions:
Abs
: Abs returns absolute value using binary operation Principle of operation: 1) Get the mask by right shift by the base 2) Base is the size of an integer variable in bits, for example, for int32 it will be 32, for int64 it will be 64 3) For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers. 4) Add the mask to the given number. 5) XOR of mask + n and mask gives the absolute value.BitCounter
: BitCounter - The function returns the number of set bits for an unsigned integer numberFastInverseSqrt
: FastInverseSqrt assumes that argument is always positive, and it does not deal with negative numbers. The "magic" number 0x5f3759df is hex for 1597463007 in decimals. The math.Float32bits is alias to *(*uint32)(unsafe.Pointer(&f)) and math.Float32frombits is to *(*float32)(unsafe.Pointer(&b)).IsPowerOfTwo
: IsPowerOfTwo This function uses the fact that powers of 2 are represented like 10...0 in binary, and numbers one less than the power of 2 are represented like 11...1. Therefore, using the and function: 10...0 & 01...1 00...0 -> 0 This is also true for 0, which is not a power of 2, for which we have to add and extra condition.IsPowerOfTwoLeftShift
: IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2LogBase2
: LogBase2 Finding the exponent of n = 2**x using bitwise operations (logarithm in base 2 of n) See moreMeanUsingAndXor
: MeanUsingAndXor This function finds arithmetic mean using "AND" and "XOR" operationsMeanUsingRightShift
: MeanUsingRightShift This function finds arithmetic mean using right shiftReverseBits
: ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result and finally move on to the next bit in the input number. Repeat this till all its bits are processed.SequenceGrayCode
: SequenceGrayCode The function generates an "Gray code" sequence of length nSqrt
: No description provided.XorSearchMissingNumber
: XorSearchMissingNumber This function finds a missing number in a sequence
cache
Functions:
NewLRU
: NewLRU represent initiate lru cache with capacityNewLFU
: NewLFU represent initiate lfu cache with capacityGet
: Get the value by key from LFU cachePut
: Put the key and value in LFU cache
Types
caesar
Package caesar is the shift cipher ref: https://en.wikipedia.org/wiki/Caesar_cipher
Functions:
Decrypt
: Decrypt decrypts by left shift of "key" each character of "input"Encrypt
: Encrypt encrypts by right shift of "key" each character of "input"FuzzCaesar
: No description provided.
checksum
Package checksum describes algorithms for finding various checksums
Functions:
CRC8
: CRC8 calculates CRC8 checksum of the given data.Luhn
: Luhn validates the provided data using the Luhn algorithm.
Types
CRCModel
: No description provided.
coloring
Package coloring provides implementation of different graph coloring algorithms, e.g. coloring using BFS, using Backtracking, using greedy approach. Author(s): Shivam
Functions:
BipartiteCheck
: basically tries to color the graph in two colors if each edge connects 2 differently colored nodes the graph can be considered bipartite
Types
Graph
: No description provided.
combination
Package combination ...
Functions:
Start
: Start ...
Types
Combinations
: No description provided.
compression
Functions:
HuffDecode
: HuffDecode recursively decodes the binary code in, by traversing the Huffman compression tree pointed by root. current stores the current node of the traversing algorithm. out stores the current decoded string.HuffEncode
: HuffEncode encodes the string in by applying the mapping defined by codes.HuffEncoding
: HuffEncoding recursively traverses the Huffman tree pointed by node to obtain the map codes, that associates a rune with a slice of booleans. Each code is prefixed by prefix and left and right children are labelled with the booleans false and true, respectively.HuffTree
: HuffTree returns the root Node of the Huffman tree by compressing listfreq. The compression produces the most optimal code lengths, provided listfreq is ordered, i.e.: listfreq[i] <= listfreq[j], whenever i < j.
Types
-
Node
: No description provided. -
SymbolFreq
: No description provided.
compression_test
Functions:
SymbolCountOrd
: SymbolCountOrd computes sorted symbol-frequency list of input message
conversion
Package conversion is a package of implementations which converts one data structure to another.
Functions:
Base64Decode
: Base64Decode decodes the received input base64 string into a byte slice. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4Base64Encode
: Base64Encode encodes the received input bytes slice into a base64 string. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4BinaryToDecimal
: BinaryToDecimal() function that will take Binary number as string, and return it's Decimal equivalent as integer.DecimalToBinary
: DecimalToBinary() function that will take Decimal number as int, and return it's Binary equivalent as string.FuzzBase64Encode
: No description provided.HEXToRGB
: HEXToRGB splits an RGB input (e.g. a color in hex format; 0x) into the individual components: red, green and blue IntToRoman
: IntToRoman converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.RGBToHEX
: RGBToHEX does exactly the opposite of HEXToRGB: it combines the three components red, green and blue to an RGB value, which can be converted to e.g. HexReverse
: Reverse() function that will take string, and returns the reverse of that string.RomanToInt
: RomanToInt converts a roman numeral string to an integer. Roman numerals for numbers outside the range 1 to 3,999 will return an error. Nil or empty string return 0 with no error thrown.
deque
Package deque implements a Double Ended Queue data structure.
Functions:
New
: New returns a new DoublyEndedQueue.
Types
DoublyEndedQueue
: No description provided.
diffiehellman
Package diffiehellman implements Diffie-Hellman Key Exchange Algorithm for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI
Functions:
GenerateMutualKey
: GenerateMutualKey : generates a mutual key that can be used by only alice and bob mutualKey = (shareKey^prvKey)%primeNumberGenerateShareKey
: GenerateShareKey : generates a key using client private key , generator and primeNumber this key can be made public shareKey = (g^key)%primeNumber
dynamic
Package dynamic is a package of certain implementations of dynamically run algorithms.
Functions:
Abbreviation
: Returns true if it is possible to make a equals b (if b is an abbreviation