Skip to content
/ algo Public

A Golang library with a collection of diverse and high-performance algorithms for various problem-solving needs.

License

Notifications You must be signed in to change notification settings

Ja7ad/algo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A Golang library with a collection of diverse and high-performance algorithms for various problem-solving needs.

Resources

License

Stars

Watchers

Forks

Languages