AgentSkillsCN

clrs-algorithms

参考CLRS的资料,掌握数据结构与算法知识。在实现、讨论或选择数据结构与算法时使用此功能。在算法选择、复杂度分析以及性能优化时自动激活。全面覆盖基础与高级数据结构,并附带伪代码示例。

SKILL.md
--- frontmatter
name: clrs-algorithms
description: "Data structures and algorithms reference based on CLRS. Use this skill when implementing, discussing, or choosing data structures or algorithms. Auto-activates for algorithm selection, complexity analysis, and performance optimization. Comprehensive coverage of fundamental and advanced data structures with pseudocode examples."

CLRS Data Structures & Algorithms Reference

A comprehensive reference for data structures and algorithms based on "Introduction to Algorithms" (CLRS). This skill provides language-agnostic guidance with pseudocode examples that can be translated to any programming language.

When This Skill Activates

This skill automatically activates when you:

  • Ask about or need to implement a data structure
  • Need to choose between data structures for a problem
  • Discuss time/space complexity trade-offs
  • Need algorithm implementations (sorting, searching, graph algorithms)
  • Mention specific structures: B-tree, heap, hash table, graph, etc.

Quick Data Structure Reference

Linear Structures

StructureAccessSearchInsertDeleteUse When
ArrayO(1)O(n)O(n)O(n)Known size, index access
Dynamic ArrayO(1)O(n)O(1)*O(n)Unknown size, frequent append
Linked ListO(n)O(n)O(1)O(1)Frequent insert/delete
StackO(1)O(n)O(1)O(1)LIFO needed
QueueO(1)O(n)O(1)O(1)FIFO needed
DequeO(1)O(n)O(1)O(1)Both ends access

Trees

StructureSearchInsertDeleteUse When
Binary Search TreeO(log n)*O(log n)*O(log n)*Ordered data, frequent search
AVL TreeO(log n)O(log n)O(log n)Guaranteed balance needed
Red-Black TreeO(log n)O(log n)O(log n)Frequent inserts/deletes
B-TreeO(log n)O(log n)O(log n)Disk-based storage
TrieO(m)O(m)O(m)String/prefix operations
HeapO(1)/O(n)O(log n)O(log n)Priority queue needed
Splay TreeO(log n)*O(log n)*O(log n)*Self-adjusting, temporal locality
TreapO(log n)*O(log n)*O(log n)*Randomized balance, split/merge
Interval TreeO(log n)O(log n)O(log n)Interval overlap queries
Order-Statistic TreeO(log n)O(log n)O(log n)Rank/select queries
K-D TreeO(log n)*O(log n)*O(log n)*Multi-dimensional spatial data

Hash-Based

StructureSearchInsertDeleteUse When
Hash TableO(1)*O(1)*O(1)*Fast key-value lookup
Hash SetO(1)*O(1)*O(1)*Unique membership testing
Bloom FilterO(k)O(k)N/AProbabilistic membership

Graphs

StructureSpaceAdd EdgeQuery EdgeUse When
Adjacency ListO(V+E)O(1)O(degree)Sparse graphs
Adjacency MatrixO(V²)O(1)O(1)Dense graphs

Graph Algorithms

AlgorithmTimeUse When
Network FlowO(VE²)Max flow, bipartite matching, min cut
Strongly Connected ComponentsO(V+E)Find SCCs, 2-SAT, dependency analysis

Strings

StructureBuildSearchUse When
Suffix ArrayO(n log n)O(m log n)Space-efficient string matching
Suffix TreeO(n)O(m)Fast pattern matching, LCS
String AlgorithmsO(m)O(n)KMP, Rabin-Karp, Boyer-Moore, Aho-Corasick

Advanced

StructureUse When
Skip ListProbabilistic balanced list
Disjoint SetUnion-find operations
Segment TreeRange queries with updates
Fenwick TreePrefix sums with updates
Fibonacci HeapDijkstra, Prim with O(1) decrease-key
Binomial HeapMergeable priority queue
van Emde Boas TreeInteger keys with O(log log u) operations

Algorithms

AlgorithmUse When
Sorting AlgorithmsQuickSort, MergeSort, HeapSort, RadixSort, and more

* = amortized or average case

Decision Guides

How to Use This Reference

  1. Choosing a structure: Start with the decision guides
  2. Learning a structure: Read the full documentation with examples
  3. Quick reminder: Use the tables above for at-a-glance reference
  4. Implementation: Follow the pseudocode, adapt to your language

Language Translation Notes

The pseudocode in this reference uses these conventions:

  • class for type definitions
  • function for methods/functions
  • -> for method calls on objects
  • // for comments
  • Type hints shown as name: Type

Translate to your language:

  • PHP: class, function, ->, //, type hints in docblocks or PHP 8+
  • JavaScript/TypeScript: class, function/arrow, ., //, TS types
  • Python: class, def, ., #, type hints
  • Java/C#: Direct mapping with new, generics

Based on concepts from "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein (CLRS), MIT Press.