brainstate

gzc/CLRS · source

Snapshot 0 · 2026-11-04T12:47:22Z · C++ · MIT

Content fingerprint828158417ebc87b8f0781348ee87bd50f223d0ff7656c6e8b985bce4bd423153

GitHub description at capture

:notebook:Solutions to Introduction to Algorithms

README at capture

# Solutions to CLRS.
Solutions to *Introduction to Algorithms* by Charles E. Leiserson, Clifford Stein, Ronald Rivest, and Thomas H. Cormen (CLRS).

![](https://images-na.ssl-images-amazon.com/images/I/51n%2B3GEQvYL._SX433_BO1,204,203,200_.jpg)

## Contributor
1. [Soyn](https://github.com/Soyn)
2. [idf](https://github.com/idf)
3. [W4anD0eR96](https://github.com/W4anD0eR96)
4. [knight42](https://github.com/knight42)
5. [ajinkyakolhe112](https://github.com/ajinkyakolhe112)
6. [an-yun](https://github.com/an-yun)
7. [y1y](https://github.com/y1y)
8. [RepapLataf](https://github.com/RepapLataf)
9. [Ghost---Shadow](https://github.com/Ghost---Shadow)
10. [wonjunetai](https://github.com/wonjunetai)
11. [suensky](https://github.com/suensky)
12. [xwu64](https://github.com/xwu64)
13. [ryuxin](https://github.com/ryuxin)
14. [Puriney](https://github.com/Puriney)
15. [wild-flame](https://github.com/wild-flame)
16. [zhangysh1995](https://github.com/zhangysh1995)
17. [DarthUjj](https://github.com/DarthUjj)
18. [VMatrix1900](https://github.com/VMatrix1900)
19. [Jingru](https://github.com/Jingru)
20. [prasook-jain](https://github.com/prasook-jain)
21. [Mundhey](https://github.com/Mundhey)
22. [Cokile](https://github.com/Cokile)
23. [wuchichung](https://github.com/wuchichung)
24. [saurabhvyas](https://github.com/saurabhvyas)
25. [codemukul95](https://github.com/codemukul95)
26. [JasonQSY](https://github.com/JasonQSY)
27. [imbrobits](https://github.com/imbrobits)
28. [zhanglanqing](https://github.com/zhanglanqing)
29. [tushar-rishav](https://github.com/tushar-rishav)
30. [ravgill](https://github.com/ravgill)
31. [Mad_Kingu](https://github.com/Mad-Kingu)
32. [kotoz](https://github.com/Kottoz)

If I miss your name here, please pull a request to me to fix.

You maybe interested in another repo [gitstats](https://github.com/gzc/gitstats) which generates repo contribution of CLRS.

## This repo needs your help.
If you are interested in this project, you could complete problems which are marked **"UNSOLVED"** in the following list. Or if you are interested in certain chapters that have not been completed, you could fork this project and issue a pull request to this repo. Appreciate your efforts.

如果你感兴趣,可以完成没有完成的题(下面有个UNSOLVED列表),或者如果你对某章节感兴趣想要完成,可以fork这个项目然后pull request进这个repo。

In order to speed up this project, we will ignore any hard problems (for instance, problems in the very end of each chapter) and review them when finishing mediocre problems. Moreover, we will only focus on sections that are interesting. You could also help to finish these hard problems.

If a problem is too easy to solve, we'll mark it as **straightforward** in order to speed up the progress.

***

<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td><font size="4px" color="#0x888888">Chapter</font></td>
	    <td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Section</font></td>
    </tr>
    <tr> <td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Part I: Foundations</font></td></tr>
       <tr>
    	<td align="center">I</td>
		<td align="center"><a href="./C01-The-Role-of-Algorithms-in-Computing/1.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C01-The-Role-of-Algorithms-in-Computing/1.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C01-The-Role-of-Algorithms-in-Computing/problem.md"><font color="black">p</font></td>
    </tr>
          <tr>
    	<td align="center">II</td>
		<td align="center"><a href="./C02-Getting-Started/2.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C02-Getting-Started/2.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C02-Getting-Started/2.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C02-Getting-Started/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
    	<td align="center">III</td>
		<td align="center"><a href="./C03-Growth-of-Functions/3.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C03-Growth-of-Functions/3.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C03-Growth-of-Functions/problem.md"><font color="black">p</font></td>
    </tr>
              <tr>
    	<td align="center">IV</td>
		<td align="center"><a href="./C04-Recurrences/4.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C04-Recurrences/4.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C04-Recurrences/4.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C04-Recurrences/4.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C04-Recurrences/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
    	<td align="center">V</td>
		<td align="center"><a href="./C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
		<td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888"> Part II: Sorting and Order Statistics</font></td>
    </tr>
    <tr>
    	<td align="center">VI</td>
		<td align="center"><a href="./C06-Heapsort/6.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C06-Heapsort/6.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C06-Heapsort/6.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C06-Heapsort/6.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C06-Heapsort/6.5.md"><font color="black">5</font></td>
		<td align="center"><a href="./C06-Heapsort/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
    	<td align="center">VII</td>
		<td align="center"><a href="./C07-Quicksort/7.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C07-Quicksort/7.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C07-Quicksort/7.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C07-Quicksort/7.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C07-Quicksort/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
    	<td align="center">VIII</td>
		<td align="center"><a href="./C08-Sorting-in-Linear-Time/8.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C08-Sorting-in-Linear-Time/8.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C08-Sorting-in-Linear-Time/8.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C08-Sorting-in-Linear-Time/8.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C08-Sorting-in-Linear-Time/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
    	<td align="center">IX</td>
		<td align="center"><a href="./C09-Medians-and-Order-Statistics/9.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C09-Medians-and-Order-Statistics/9.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C09-Medians-and-Order-Statistics/9.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C09-Medians-and-Order-Statistics/problem.md"><font color="black">p</font></td>
    </tr>
        <tr>
		<td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Part III: Data Structures</font></td>
    </tr>
    <tr>
    	<td align="center">X</td>
		<td align="center"><a href="./C10-Elementary-Data-Structures/10.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C10-Elementary-Data-Structures/10.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C10-Elementary-Data-Structures/10.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C10-Elementary-Data-Structures/10.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C10-Elementary-Data-Structures/problem.md"><font color="black">p</font></td>
    </tr>
    <tr>
    	<td align="center">XI</td>
		<td align="center"><a href="./C11-Hash-Tables/11.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C11-Hash-Tables/11.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C11-Hash-Tables/11.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C11-Hash-Tables/11.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C11-Hash-Tables/11.5.md"><font color="black">5</font></td>
		<td align="center"><a href="./C11-Hash-Tables/problem.md"><font color="black">p</font></td>
    </tr>
            <tr>
    	<td align="center">XII</td>
		<td align="center"><a href="./C12-Binary-Search-Trees/12.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C12-Binary-Search-Trees/12.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C12-Binary-Search-Trees/12.3.md"><font color="black">3</font></td>
		</tr>
        <tr>
    	<td align="center">XIII</td>
		<td align="center"><a href="./C13-Red-Black-Trees/13.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C13-Red-Black-Trees/13.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C13-Red-Black-Trees/13.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C13-Red-Black-Trees/13.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C13-Red-Black-Trees/problem.md"><font color="black">p</font></td></tr>
		<tr>
    	<td align="center">XIV</td>
		<td align="center"><a href="./C14-Augmenting-Data-Structures/14.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C14-Augmenting-Data-Structures/14.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C14-Augmenting-Data-Structures/14.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C14-Augmenting-Data-Structures/problem.md"><font color="black">p</font></td>
		</tr>
	<tr>
	<td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Part IV: Advanced Design and Analysis Techniques</font></td></tr>
	<tr>
    	<td align="center">XV</td>
		<td align="center"><a href="./C15-Dynamic-Programming/15.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C15-Dynamic-Programming/15.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C15-Dynamic-Programming/15.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C15-Dynamic-Programming/15.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C15-Dynamic-Programming/15.5.md"><font color="black">5</font></td>
		</tr>
	<tr>
    	<td align="center">XVI</td>
		<td align="center"><a href="./C16-Greedy-Algorithms/16.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C16-Greedy-Algorithms/16.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C16-Greedy-Algorithms/16.3.md"><font color="black">3</font></td>
		</tr>
	<tr>
    	<td align="center">XVII</td>
		<td align="center"><a href="./C17-Amortized-Analysis/17.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C17-Amortized-Analysis/17.2.md"><font color="black">2</font></td>
		</tr>
	<tr>
	<td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Part V: Advanced Data Structures</font></td></tr>
	<tr>
    	<td align="center">XVIII</td>
		<td align="center"><a href="./C18-B-Trees/18.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C18-B-Trees/18.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C18-B-Trees/18.3.md"><font color="black">3</font></td>
		</tr>
		<tr>
    	<td align="center">XIX</td>
		<td align="center"><a href="./C19-Binomial-Heaps/19.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C19-Binomial-Heaps/19.2.md"><font color="black">2</font></td>
		</tr>
		<tr>
    	<td align="center">XXI</td>
		<td align="center"><a href="./C21-Data-Structures-for-Disjoint-Sets/21.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C21-Data-Structures-for-Disjoint-Sets/21.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C21-Data-Structures-for-Disjoint-Sets/21.3.md"><font color="black">3</font></td>
		</tr>
	<tr>
	<td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Part VI: Graph Algorithms</font></td></tr>
		<tr>
    	<td align="center">XXII</td>
		<td align="center"><a href="./C22-Elementary-Graph-Algorithms/22.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C22-Elementary-Graph-Algorithms/22.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C22-Elementary-Graph-Algorithms/22.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C22-Elementary-Graph-Algorithms/22.4.md"><font color="black">4</font></td>
		<td align="center"><a href="./C22-Elementary-Graph-Algorithms/22.5.md"><font color="black">5</font></td>
		<td align="center"><a href="./C22-Elementary-Graph-Algorithms/problem.md"><font color="black">p</font></td>
		</tr>
		<tr>
    	<td align="center">XXIII</td>
		<td align="center"><a href="./C23-Minimum-Spanning-Trees/23.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C23-Minimum-Spanning-Trees/23.2.md"><font color="black">2</font></td>
		</tr>
		<tr>
    	<td align="center">XXIV</td>
		<td align="center"><a href="./C24-Single-Source-Shortest-Paths/24.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C24-Single-Source-Shortest-Paths/24.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C24-Single-Source-Shortest-Paths/24.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C24-Single-Source-Shortest-Paths/24.4.md"><font color="black">4</font></td>
		</tr>
		<tr>
    	<td align="center">XXV</td>
		<td align="center"><a href="./C25-All-Pairs-Shortest-Paths/25.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C25-All-Pairs-Shortest-Paths/25.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C25-All-Pairs-Shortest-Paths/25.3.md"><font color="black">3</font></td>
		</tr>
		<tr>
    	<td align="center">XXVI</td>
		<td align="center"><a href="./C26-Flow-networks/26.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C26-Flow-networks/26.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C26-Flow-networks/26.3.md"><font color="black">3</font></td>
		</tr>
		<tr>
	<td align = "center" colspan='20' width = "100%"><font size="4px" color="#0x888888">Part VII: Selected Topics</font></td></tr>
	<tr><td align="center">XXXI</td>
		<td align="center"><a href="./C31-Number-Theoretic-Algorithms/31.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C31-Number-Theoretic-Algorithms/31.2.md"><font color="black">2</font></td>
		</tr>
	<tr>
    	<td align="center">XXXII</td>
		<td align="center"><a href="./C32-String-Matching/32.1.md"><font color="black">1</font></td>
		<td align="center"><a href="./C32-String-Matching/32.2.md"><font color="black">2</font></td>
		<td align="center"><a href="./C32-String-Matching/32.3.md"><font color="black">3</font></td>
		<td align="center"><a href="./C32-String-Matching/32.4.md"><font color="black">4</font></td>
		</tr>
		<tr><td align="center">XXXIII</td>
		<td align="center"><a href="./C33-Computational-Geometry/33.1.md"><font color="black">1</font></td>
		</tr>
		<tr><td align="center">XXXV</td>
		<td align="center"><a href="./C35-Approximation-Algorithms/35.1.md"><font color="black">1</font></td>
		</tr>
</table>

***


## Data Structure&algorithm implementation

### BASIC
* [Heap](./C06-Heapsort/heap.cpp)
* [Priority Queue](./C06-Heapsort/p_queue.h)
* [Quicksort](./C07-Quicksort/quicksort.py)
* [Radixsort](./C08-Sorting-in-Linear-Time/exercise_code/radixSort.cpp)
* [Counting Sort](./C08-Sorting-in-Linear-Time/exercise_code/in_place_counting_sort.py)
* [K-th Finding](./C09-Medians-and-Order-Statistics/worst-case-linear-time.cpp)
* [Deque(py)](./C10-Elementary-Data-Structures/exercise_code/deque.py) [Deque(c++)](./C10-Elementary-Data-Structures/exercise_code/deque.cpp)

### DIVIDE and CONQUER
* [Karatsuba](./other/Karatsuba)


### TREE/ADVANCED
* [BST](./C12-Binary-Search-Trees/BSTree.h)
* [RBT](./C13-Red-Black-Trees/rbtree.cpp)
* [Btree](./C18-B-Trees/btree.cpp)
* [BinomialHeap](./C19-Binomial-Heaps/BinomialHeap.h)  [Driver](././C19-Binomial-Heaps/Main.cpp)
* [SegmentTree](./other/segmentTree.cpp)
* [Trie](./other/trie.cpp)
* [UnionFind](./C21-Data-Structures-for-Disjoint-Sets/uf.cpp)

### DYNAMIC/GREEDY
* [Matrix Chain](./C15-Dynamic-Programming/Matrix-chain-multiplication.c)
* [Huffman](./C16-Greedy-Algorithms/huffman)

### GRAPH
* [Kosaraju's Algorithm ( strongly connected components )](./C22-Elementary-Graph-Algorithms/elementary_graph_algo.py#L70)
* [Maximum Flow](./C26-Flow-networks/maxflow)
* [Floyd-Warshall](./C25-All-Pairs-Shortest-Paths/Floyd_Warshall.cpp)

### GEOMETRY
* [LineIntersection](./C33-Computational-Geometry/twoline.cpp)
* Convex Hull
  * [Graham Scan](./C33-Computational-Geometry/Graham_Scan.py)

### STRING
* [BruteForce](./C32-String-Matching/BF.c)
* [KMP](./C32-String-Matching/KMP.c)
* [DFA](./C32-String-Matching/FA.c)

### UTILITY
* [Split string by delimiter in C++](./other/stringSpilit.cpp)

# UNSOLVED

[31.1.11](./C31-Number-Theoretic-Algorithms/31.1.md#exercises-311-11)
[31.2.7](./C31-Number-Theoretic-Algorithms/31.2.md#exercises-312-7)
[31.2.9](./C31-Number-Theoretic-Algorithms/31.2.md#exercises-312-9)

[32.2.4](./C32-String-Matching/32.2.md#exercises-322-4)
[32.3.4](./C32-String-Matching/32.3.md#exercises-323-4-)
[32.4.6](./C32-String-Matching/32.4.md#exercises-324-6-)

***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
You can also subscribe my [youtube channel](https://www.youtube.com/channel/UCAvvkYnRNyObcHzOCaVgSrQ).

<sub>**Disclaimer**: the solutions in this repository are crowdsourced work, and in any form it neither represents any opinion of nor affiliates to the authors of Introduction to Algorithms or the MIT press.<sub>