algo

module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2025 License: MIT

README ΒΆ

algo: A Collection of High-Performance Algorithms

Go Reference codecov Go Report Card

algo is a Golang library featuring a variety of efficient and well-optimized algorithms designed for diverse problem-solving needs.

The purpose of this package is to implement specific algorithms in a simplified manner based on formulaic documentation available on Wikipedia or other sources. These algorithms been validated in terms of functionality and testing.

πŸ“š Available Algorithms

Algorithm Description
Random Weighted Selection Selects items randomly based on assigned weights. Useful in load balancing, gaming, and AI.
Reservoir Sampling Algorithm R Basic reservoir sampling, replaces elements with probability k/i. Efficient for uniform random sampling.
Reservoir Sampling Algorithm L Optimized reservoir sampling for large N, reduces unnecessary replacements using skipping.
Weighted Reservoir Sampling Selects items with probability proportional to their weights using a heap-based approach. Used in recommendation systems and A/B testing.
Random Sort Reservoir Sampling Uses a min-heap and random priorities to maintain the top k elements in a streaming dataset.
Consistent Hashing Used by distributed systems (CDNs, databases) to evenly distribute requests across servers.

πŸš€ Installation >= go 1.19

go get -u github.com/Ja7ad/algo

βœ… Usage Example

Here’s how you can use the Random Weighted Selection algorithm:

package main

import (
	"fmt"
	"log"
	"github.com/Ja7ad/algo/rws"
)

func main() {
	// Define items with weights
	weightedItems := map[int]string{
		3: "Apple",
		1: "Banana",
		6: "Cherry",
	}

	// Create a selector
	selector, err := rws.NewWeightedSelector(weightedItems)
	if err != nil {
		log.Fatal(err)
	}

	// Pick a random item
	selectedItem, _ := selector.Pick()
	fmt.Println("Selected:", selectedItem)
}

For more details, check the Random Weighted Selection documentation.

πŸ“Œ Contribution

We welcome contributions! Feel free to submit pull requests, open issues, or suggest new algorithms.

πŸ“œ License

This project is licensed under the MIT License.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL