Skip to content

Commit

Permalink
Add time and space complexity information to various algorithms (#744)
Browse files Browse the repository at this point in the history
* Add time and space complexity information to various algorithms

* Added the implementation of Kosaraju algorithm

* Added some style issues in articulationpoints.go

* Formatted code with gofmt -s to meet style requirements

---------

Co-authored-by: Rak Laptudirm <[email protected]>
  • Loading branch information
mapcrafter2048 and raklaptudirm authored Nov 16, 2024
1 parent 94689d8 commit 4263dac
Show file tree
Hide file tree
Showing 114 changed files with 385 additions and 72 deletions.
7 changes: 7 additions & 0 deletions cache/lru.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// lru.go
// description : Least Recently Used (LRU) cache
// details : A Least Recently Used (LRU) cache is a type of cache algorithm used to manage memory within a computer. The LRU algorithm is designed to remove the least recently used items first when the cache reaches its limit.
// time complexity : O(1)
// space complexity : O(n)
// ref : https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)

package cache

import (
Expand Down
2 changes: 2 additions & 0 deletions checksum/crc8.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// details:
// A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks
// and storage devices to detect accidental changes to raw data.
// time complexity: O(n)
// space complexity: O(1)
// See more [CRC](https://en.wikipedia.org/wiki/Cyclic_redundancy_check)
// author(s) [red_byte](https://github.com/i-redbyte)
// see crc8_test.go
Expand Down
2 changes: 2 additions & 0 deletions checksum/luhn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// lunh.go
// description: Luhn algorithm
// details: is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, etc [Lunh](https://en.wikipedia.org/wiki/Luhn_algorithm)
// time complexity: O(n)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see lunh_test.go

Expand Down
4 changes: 4 additions & 0 deletions cipher/caesar/caesar.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Package caesar is the shift cipher
// description: Caesar cipher
// details : Caesar cipher is a type of substitution cipher in which each letter in the plaintext is shifted a certain number of places down the alphabet.
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/Caesar_cipher
package caesar

Expand Down
4 changes: 4 additions & 0 deletions cipher/diffiehellman/diffiehellmankeyexchange.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Package diffiehellman implements Diffie-Hellman Key Exchange Algorithm
// description: Diffie-Hellman key exchange
// details : Diffie-Hellman key exchange is a method of securely exchanging cryptographic keys over a public channel by combining private keys of two parties to generate a shared secret key.
// time complexity: O(log(n))
// space complexity: O(1)
// for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI
package diffiehellman

Expand Down
4 changes: 4 additions & 0 deletions cipher/polybius/polybius.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Package polybius is encrypting method with polybius square
// description: Polybius square
// details : The Polybius algorithm is a simple algorithm that is used to encode a message by converting each letter to a pair of numbers.
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/Polybius_square#Hybrid_Polybius_Playfair_Cipher
package polybius

Expand Down
6 changes: 6 additions & 0 deletions cipher/railfence/railfence.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// railfence.go
// description: Rail Fence Cipher
// details: The rail fence cipher is a an encryption algorithm that uses a rail fence pattern to encode a message. it is a type of transposition cipher that rearranges the characters of the plaintext to form the ciphertext.
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/Rail_fence_cipher
package railfence

import (
Expand Down
4 changes: 4 additions & 0 deletions cipher/rot13/rot13.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Package rot13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet.
// description: ROT13
// details: ROT13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. it is a special case of the Caesar cipher
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/ROT13
package rot13

Expand Down
2 changes: 2 additions & 0 deletions cipher/rsa/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// thus both the Encrypt and Decrypt are not a production
// ready implementation. The OpenSSL implementation of RSA
// also adds a padding which is not present in this algorithm.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [Taj](https://github.com/tjgurwara99)
// see rsa_test.go

Expand Down
2 changes: 2 additions & 0 deletions cipher/rsa/rsa2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
rsa2.go
description: RSA encryption and decryption including key generation
details: [RSA wiki](https://en.wikipedia.org/wiki/RSA_(cryptosystem))
time complexity: O(n)
space complexity: O(1)
author(s): [ddaniel27](https://github.com/ddaniel27)
*/
package rsa
Expand Down
2 changes: 2 additions & 0 deletions cipher/transposition/transposition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// description: Transposition cipher
// details:
// Implementation "Transposition cipher" is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext [Transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher)
// time complexity: O(n)
// space complexity: O(n)
// author(s) [red_byte](https://github.com/i-redbyte)
// see transposition_test.go

Expand Down
4 changes: 4 additions & 0 deletions cipher/xor/xor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Package xor is an encryption algorithm that operates the exclusive disjunction(XOR)
// description: XOR encryption
// details: The XOR encryption is an algorithm that operates the exclusive disjunction(XOR) on each character of the plaintext with a given key
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/XOR_cipher
package xor

Expand Down
2 changes: 2 additions & 0 deletions compression/huffmancoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// details:
// We implement the linear-time 2-queue method described here https://en.wikipedia.org/wiki/Huffman_coding.
// It assumes that the list of symbol-frequencies is sorted.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [pedromsrocha](https://github.com/pedromsrocha)
// see also huffmancoding_test.go

Expand Down
3 changes: 3 additions & 0 deletions compression/rlecoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ rlecoding.go
description: run length encoding and decoding
details:
Run-length encoding (RLE) is a simple form of data compression in which runs of data are stored as a single data value and count, rather than as the original run. This is useful when the data contains many repeated values. For example, the data "WWWWWWWWWWWWBWWWWWWWWWWWWBBB" can be compressed to "12W1B12W3B". The algorithm is simple and can be implemented in a few lines of code.
time complexity: O(n)
space complexity: O(n)
ref: https://en.wikipedia.org/wiki/Run-length_encoding
author(s) [ddaniel27](https://github.com/ddaniel27)
*/
package compression
Expand Down
2 changes: 2 additions & 0 deletions conversion/base64.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// base64.go
// description: The base64 encoding algorithm as defined in the RFC4648 standard.
// author: [Paul Leydier] (https://github.com/paul-leydier)
// time complexity: O(n)
// space complexity: O(n)
// ref: https://datatracker.ietf.org/doc/html/rfc4648#section-4
// ref: https://en.wikipedia.org/wiki/Base64
// see base64_test.go
Expand Down
2 changes: 2 additions & 0 deletions conversion/binarytodecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Date: 19-Oct-2021
// https://en.wikipedia.org/wiki/Decimal
// Function receives a Binary Number as string and returns the Decimal number as integer.
// Supported Binary number range is 0 to 2^(31-1).
// time complexity: O(n)
// space complexity: O(1)

package conversion

Expand Down
2 changes: 2 additions & 0 deletions conversion/decimaltobinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Date: 14-Oct-2021
// https://en.wikipedia.org/wiki/Binary_number
// Function receives a integer as a Decimal number and returns the Binary number.
// Supported integer value range is 0 to 2^(31 -1).
// time complexity: O(log(n))
// space complexity: O(1)

package conversion

Expand Down
6 changes: 6 additions & 0 deletions conversion/inttoroman.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// inttoroman.go
// description: Convert an integer to a roman numeral
// details: This program converts an integer to a roman numeral. The program uses a lookup array to convert the integer to a roman numeral.
// time complexity: O(1)
// space complexity: O(1)

package conversion

import (
Expand Down
2 changes: 2 additions & 0 deletions conversion/rgbhex.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// rgbhex.go
// description: convert hex input to red, green and blue and vice versa
// time complexity: O(1)
// space complexity: O(1)
// author(s) [darmiel](https://github.com/darmiel)
// see rgbhex_test.go

Expand Down
2 changes: 2 additions & 0 deletions conversion/romantoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Function receives a string as a roman number and outputs an integer
// Maximum output will be 3999
// Only standard form is supported
// time complexity: O(n)
// space complexity: O(1)

package conversion

Expand Down
2 changes: 2 additions & 0 deletions dynamic/abbreviation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// Given a = "ABcde" and b = "ABCD"
// We can capitalize "c" and "d" in a to get "ABCde" then delete all the lowercase letters (which is only "e") in a to get "ABCD" which equals b.
// Author: [duongoku](https://github.com/duongoku)
// Time Complexity: O(n*m) where n is the length of a and m is the length of b
// Space Complexity: O(n*m) where n is the length of a and m is the length of b
// See abbreviation_test.go for test cases

package dynamic
Expand Down
5 changes: 5 additions & 0 deletions dynamic/binomialcoefficient.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// binomialcoefficient.go
// description: Implementation of the binomial coefficient using dynamic programming
// details: The binomial coefficient C(n, k) is the number of ways to choose a subset of k elements from a set of n elements. The binomial coefficient is calculated using the formula C(n, k) = C(n-1, k-1) + C(n-1, k) with base cases C(n, 0) = C(n, n) = 1.
// time complexity: O(n*k) where n is the number of elements and k is the number of elements to choose
// space complexity: O(n*k) where n is the number of elements and k is the number of elements to choose
package dynamic

import "github.com/TheAlgorithms/Go/math/min"
Expand Down
4 changes: 3 additions & 1 deletion dynamic/catalan.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//The Catalan numbers are a sequence of positive integers that appear in many counting
//problems in combinatorics.
// problems in combinatorics.
// time complexity: O(n²)
// space complexity: O(n)
//reference: https://brilliant.org/wiki/catalan-numbers/

package dynamic
Expand Down
6 changes: 6 additions & 0 deletions dynamic/coinchange.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// coinchange.go
// description: Implementation of the coin change problem using dynamic programming
// details: The coin change problem is a problem that asks for the number of ways to make change for a given amount of money using a given set of coins. The problem can be solved using dynamic programming.
// time complexity: O(n*m) where n is the number of coins and m is the amount of money
// space complexity: O(m) where m is the amount of money

package dynamic

// CoinChange finds the number of possible combinations of coins
Expand Down
2 changes: 2 additions & 0 deletions dynamic/editdistance.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// EDIT DISTANCE PROBLEM
// time complexity: O(m * n) where m and n are lengths of the strings, first and second respectively.
// space complexity: O(m * n) where m and n are lengths of the strings, first and second respectively.
// https://www.geeksforgeeks.org/edit-distance-dp-5/
// https://leetcode.com/problems/edit-distance/

Expand Down
4 changes: 4 additions & 0 deletions dynamic/fibonacci.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// fibonacci.go
// description: Implementation of the Fibonacci sequence using dynamic programming
// time complexity: O(n)
// space complexity: O(1)
package dynamic

// https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/
Expand Down
3 changes: 3 additions & 0 deletions dynamic/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package dynamic

// Knapsack Problem
// https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
// https://en.wikipedia.org/wiki/Knapsack_problem
// time complexity: O(n*maxWeight)
// space complexity: O(n*maxWeight)

import (
"math"
Expand Down
3 changes: 3 additions & 0 deletions dynamic/longestcommonsubsequence.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// LONGEST COMMON SUBSEQUENCE
// DP - 4
// https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/
// https://leetcode.com/problems/longest-common-subsequence/
// time complexity: O(m*n) where m and n are lengths of the strings
// space complexity: O(m*n) where m and n are lengths of the strings

package dynamic

Expand Down
6 changes: 6 additions & 0 deletions dynamic/longestincreasingsubsequence.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// longestincreasingsubsequence.go
// description: Implementation of the Longest Increasing Subsequence using dynamic programming
// reference: https://en.wikipedia.org/wiki/Longest_increasing_subsequence
// time complexity: O(n^2)
// space complexity: O(n)

package dynamic

import (
Expand Down
2 changes: 2 additions & 0 deletions dynamic/longestpalindromicsubsequence.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// longest palindromic subsequence
// time complexity: O(n^2)
// space complexity: O(n^2)
// http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/

package dynamic
Expand Down
2 changes: 2 additions & 0 deletions dynamic/matrixmultiplication.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// matrix chain multiplication problem
// https://en.wikipedia.org/wiki/Matrix_chain_multiplication
// www.geeksforgeeks.org/dynamic_programming-set-8-matrix-chain-multiplication/
// time complexity: O(n^3)
// space complexity: O(n^2)

package dynamic

Expand Down
2 changes: 2 additions & 0 deletions dynamic/rodcutting.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Solution to Rod cutting problem
// https://en.wikipedia.org/wiki/Cutting_stock_problem
// http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/
// time complexity: O(n^2)
// space complexity: O(n)

package dynamic

Expand Down
3 changes: 2 additions & 1 deletion dynamic/subsetsum.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//Given a set of non-negative integers, and a (positive) value sum,
//determine if there is a subset of the given set with sum
//equal to given sum.
//Complexity: O(n*sum)
// time complexity: O(n*sum)
// space complexity: O(n*sum)
//references: https://www.geeksforgeeks.org/subset-sum-problem-dp-25/

package dynamic
Expand Down
2 changes: 2 additions & 0 deletions dynamic/traprainwater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
// Then, it iterates through the array to calculate the amount of trapped rainwater at each position based on the minimum of the left and right maximum heights.
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [TruongNhanNguyen (SOZEL)](https://github.com/TruongNhanNguyen)
package dynamic

Expand Down
2 changes: 2 additions & 0 deletions dynamic/uniquepaths.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// See https://leetcode.com/problems/unique-paths/
// time complexity: O(m*n) where m and n are the dimensions of the grid
// space complexity: O(m*n) where m and n are the dimensions of the grid
// author: Rares Mateizer (https://github.com/rares985)
package dynamic

Expand Down
Loading

0 comments on commit 4263dac

Please sign in to comment.