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.
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. |
go get -u github.com/Ja7ad/algo
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.
We welcome contributions! Feel free to submit pull requests, open issues, or suggest new algorithms.
This project is licensed under the MIT License.