AgentSkillsCN

algorithms

适用于选择数据结构、评估哈希函数、需要概率型数据结构,或在Elixir中对比OTP内置功能与专用替代方案的场景。

SKILL.md
--- frontmatter
name: algorithms
description: Use when choosing data structures, evaluating hash functions, needing probabilistic data structures, or comparing OTP built-ins against specialized alternatives for Elixir

Modern Algorithms and Data Structures

Overview

Use OTP built-ins first. Escalate to specialized structures only when profiling shows a bottleneck. For most Elixir applications, Map, MapSet, List, and the OTP modules cover 95% of needs.

Data Structure Decision

ProblemFirst ChoiceSpecialized AlternativeSwitch When
Key-value storeMap:gb_treesNeed ordered traversal or range queries
Unique collectionMapSet:gb_setsNeed sorted set operations
FIFO buffer:queueAlways use :queue, not list ++
Concurrent counter:atomicsAlways, not GenServer
Unique count (large)HyperLogLogMapSet>100K items, ±2% OK
Set membership (large)Cuckoo FilterMapSet>1M items, need deletion
Frequency trackingCount-Min SketchMapMillions of items
Non-crypto hashxxHash3:erlang.phash2Performance critical
Crypto hashBLAKE3SHA-256Performance critical
Graph algorithms:digraphAlways use OTP built-in
Priority queueheap librarySorted list>100 elements

Common Mistakes

  • Lists as queues: queue ++ [item] is O(n); use :queue for O(1)
  • GenServer as counter: Serializes all updates; use :atomics or :counters
  • MD5/SHA1 for non-crypto: 60× slower than xxHash3 for checksums and hash tables
  • Exact counting for analytics: HyperLogLog uses 16 KB where MapSet uses 800 MB (100M items)
  • Bloom filters when needing deletion: Use Cuckoo filters instead

Reference Files

  • otp-builtins.md:queue, :gb_trees, :gb_sets, :atomics, :counters, :array, Okasaki structures
  • probabilistic.md — HyperLogLog, Cuckoo filters, Count-Min Sketch, Bloom filters
  • hash-functions.md — xxHash3, BLAKE3, HighwayHash selection guide
  • sorting-and-search.md — Cache-efficient sorting, BlockQuicksort, pdqsort, B+ trees

Related Skills

  • performance-analyzer: Profiling, benchmarking, latency analysis
  • distributed-systems: Consensus and replication algorithms
  • elixir-patterns: OTP process patterns, ETS usage

Use the algorithms-researcher agent for deep research with paper citations.