Code Monkey home page Code Monkey logo

clrs's Introduction

Solutions to CLRS.

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

Contributor

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

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

You maybe interested in another repo 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.


Chapter Section
Part I: Foundations
I 1 2 p
II 1 2 3 p
III 1 2 p
IV 1 2 3 4 p
V 1 2 3 4 p
Part II: Sorting and Order Statistics
VI 1 2 3 4 5 p
VII 1 2 3 4 p
VIII 1 2 3 4 p
IX 1 2 3 p
Part III: Data Structures
X 1 2 3 4 p
XI 1 2 3 4 5 p
XII 1 2 3
XIII 1 2 3 4 p
XIV 1 2 3 p
Part IV: Advanced Design and Analysis Techniques
XV 1 2 3 4 5
XVI 1 2 3
XVII 1 2
Part V: Advanced Data Structures
XVIII 1 2 3
XIX 1 2
XXI 1 2 3
Part VI: Graph Algorithms
XXII 1 2 3 4 5 p
XXIII 1 2
XXIV 1 2 3 4
XXV 1 2 3
XXVI 1 2 3
Part VII: Selected Topics
XXXI 1 2
XXXII 1 2 3 4
XXXIII 1
XXXV 1

Data Structure&algorithm implementation

BASIC

DIVIDE and CONQUER

TREE/ADVANCED

DYNAMIC/GREEDY

GRAPH

GEOMETRY

STRING

UTILITY

UNSOLVED

31.1.11 31.2.7 31.2.9

32.2.4 32.3.4 32.4.6


Follow @louis1992 on github to help finish this task. You can also subscribe my youtube channel.

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.

clrs's People

Contributors

ajinkyakolhe112 avatar aladdine avatar an-yun avatar ankesh11 avatar dccif avatar devdoggo avatar gavinq1 avatar gzc avatar hxdnshx avatar idf avatar jingru avatar kestory avatar kfrancuz avatar kimjieun02 avatar leodestiny avatar m2jean avatar mad-kingu avatar meslahik avatar mundhey avatar refraction-ray avatar roopansh avatar ryuxin avatar samolisov avatar saurabhvyas avatar soyn avatar suensky avatar syqian avatar tisonkun avatar y1y avatar zouyonghao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clrs's Issues

Exercise 7.1-2

I have run the code. If the input array is [2, 3, 6, 4, 6, 5, 6], the output will be [2, 3, 4, 6, 6, 5, 6]. This is obviously not right.
Your code works rightly when all elements in the array have the same value. But when only some are the same, problems may arise.

23.1-11 时间复杂度问题

https://github.com/gzc/CLRS/blob/master/C23-Minimum-Spanning-Trees/23.1.md#exercises-231-11-

该题目的思路跟作者是一样的:将权重减少的边添加回到最小生成树T中,一定会形成一个环,再将环中权重最大的边去掉。

因为题目中已经给出了G和T,所以我们只需要在环中寻找权重最大的边,最差情况下就是DFS遍历一遍T。所以时间复杂度应该是 O(V),而不是 O(V + E)

不知道我的思路对不对,感谢查阅。

Exercise 8.4-3

not both 1, the first one is (1 / 4) * 1^2 * 2 + (1 / 4) * 2^2 = 1.5

Excercise 16.1-1

I'm not quite getting what "S" in the parameter stands for. If it stands for start time, shouldn't there also be an finish time input f and check for if two activities are compatible?

27-2-5

could you please help me to solve this exersice:
Prove that an n-input sorting network must contain at least one comparator between
the ith and (i + 1)st lines for all i = 1, 2, . . . , n − 1.

Exercises 7.1-1

3 elements are incorrect according to the question provided.
A should be modified as [13, 19, 3, 5, 12, 8, 7, 4, 21, 2, 6, 11].

Exercise 7.3-2

I think the solution of the best condition is :
T(n)=2T(n/2)+1

Change the title and the book image, please

I feel it's necessary to change the title and the book image to indicate that solutions are for 2nd edition.
I come cross the 4th chapter, and it is completely different.

It just makes readers confused, and a little bit waste of time.

Thanks.

There seems to be an error in exercise 32.1.3

The answer to exercise 32.1.3 says that "then we can get the T(time) = (n-m+1)*[a(1)*b(1)+a(2)*b(2)+,,,+a(m)*b(m)]
So we get the answer.", but after I sum up the series, I find that the result is

(1-d^(-m))/(1-d^(-1))-m/d^(-m)

, instead of expected result

(1-d^(-m))/(1-d^(-1))

, so I think that this answer is not correct.

Explanation for solving 1.2.2 and 1.2.3

Hi, I recently started reading this book. Its good to see this repository with solutions.

For 1.2.2 and 1.2.3, this repository contains only the final answer for it. It would be good to have the explanation of how it was arrived at. Here is what, I have reached at. Please review my method of solving this and suggest a better or good way of solving it. I will raise a PR for the explanation after getting enough feedback.

Exercise 1.2.2

Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in
steps, while merge sort runs in
steps. For which values of n does insertion sort beat merge sort?

Insertion sort beating merge sort means,
for an input size n, time taken by insertion sort ( to give the sorted output ) should be less than the time taken by merge sort.

So, I arrived at an equation like this

<

Figuring out all the values of n, that satisfies the above equation would give us the answer.

The equation can further be simplified, as it contains common factor, like this.

8n.n < 8. 8n. log n

// cancelling 8n on both sides

n < 8 log n

Now, I applied n=1,2,3..... on the above equation to figure out the answer as 2 < n < 43

To figure the ending point I have to manually apply the n value from 1 to 43 which is a time consuming process. So, i tried applying n values as powers of 2 ( as it would reduce the time - considering my base as 2 in log n ), this approach, helped me to arrive at a point that maximum value of n satisfies 32 < final value of n < 64. This method made me to be faster in getting the final solution. Is there method that is even faster than arriving at the answer ?

Is this how we arrive at the answer for the exercise ? Are equations correct ? Can my equation be further reduced ?

Exercise 1.2.3

I was able to arrive at an equation, similar to exercise 1.2.2. So if my above questions are being answered, then I will have a clear understanding of how this exercise is solved.

Exercise 2.1-4

The loop is supposed to start from A.length isn't it?

Shouldn't it be like this?

C = Array[A.length+1]
carry <- 0,
for i <- A.length to 1
    C[i+1] <- (A[i] + B[i] + carry) % 2;
    carry = (A[i] + B[i] + carry)/2;
C[i+1] <- carry;
return C

A new solution about exercise11.3-2

I can't get the main idea of your idea, but i google some solutions, and this one
Answers of Ex11.3-2 is appriate for this question. And i can not get the point of this paragraph

Another way would be using n-degree equation with each character value acting as a co-efficient for the nth term. Example: S[0]*xn + S[1]*x(n-1)+ …. + S[n-1], for better result substitute x = 33 or 37 or 39. This is the famous Horner’s method for finding a hash value.
If you can figure it out. Plz tell me. Thanks a lot.

Exercises 10.4-1

wrong left son in the node with index 1, it should be node with index 7, not node with index 3.

13.4-6

题目写成13.3-6的题目了

15.5-1的代码的小问题

在您的代码的
void printOptimalBST(int i,int j,int r)
{
int rootChild = root[i][j];//子树根节点
if (rootChild == root[1][n])
{
//输出整棵树的根
cout << "k" << rootChild << "是根" << endl;
printOptimalBST(i,rootChild - 1,rootChild);
printOptimalBST(rootChild + 1,j,rootChild);
return;
}
这一段,在我测试的时候出现了数组越界的情况呢,就是在输出最后一片树叶的时候,感觉是不是应该在最前面加上呢? 新手求指教!
if(i==N+1&&j==N)
{
cout<<"d"<<j<<"is k"<<r<<" right child"<<endl;
return ;
}

Exercises 7.4-5

answer:先看快速排序的那部分,当高度为lg(n/k)便停止了快速排序。

应该是高度为lgk的时候,停止了快排。然后快排运行了lgn-lgk层,故时间复杂度应该是O(lgn/k)

The numbering of solutions in chapter 04 is wrong.

The numbering of the solutions is wrong. For example, section's 4.3 exercises have nothing to do with the third edition of the book.

For example on the 3rd edition the exercise 4.3-1 is:

"Show that the solution of T(n) = T(n-1) + n is O(n * n)".

Exercises 3.2-4

answer ,第四行,令m=[lgn]!,应该是多打了个!,令m=[lgn],

exercise 32-4-6

what is the solution of this exercise ??
"Give an efficient algorithm for computing the transition function δ for the stringmatching
automaton corresponding to a given pattern P. Your algorithm should
run in time O(m |�|). (Hint: Prove that δ(q, a) = δ(π[q], a) if q = m or
P[q + 1] �= a.) "
An early reply would be appreciated.

关于15.4-6的代码

能不能再给点详细的解释啊,还是对代码没有太深入的理解呢

Exercise 24.1-1 looks wrong

I think answer should be

~ | s | t | x | y | z
d | 2 | 4 | 6 | 9 | 0
π | z | x | z | s | Ø

~ | s | t | x | y | z
d | 0 | 0 | 2 | 7 | -2
π | Ø | x | z | s | t

Correct me if I'am wrong.

Exercises 22.4-5

首先,运行DFS或者是BFS可以在O(V+E)的时间内统计出每个点的入度和出度,以后在删除边的时候要维护这些信息. 每次输出入度为0的那个点并删除其出边,并维护信息,因此有E条边和V个点,所以要执行O(V)次输出和O(E)次的删除.所以总的运行时间是O(V+E)

每次寻找入度为0的点需要O(V)时间,导致算法的实现不是很高效。

problem 8-3.b

Maybe you need to check your answer. I can't figure out why we need to reverse the string. And i can't get you. Check at 8-3

Exercise 22.5-1

题目和答案不匹配,答案为第三版的答案,但是题目好像不是。

14.2-2

应该不能维护吧?

当插入一个节点,递归到根节点,当根节点直接变为黑色,所有节点的黑高需要加一。 复杂度为O(n)。

Exercise 8.1-3

Hi,
I think it should be "n!/2, n!/n, n!/(2^n) are smaller than 2^n only when n is small"
instead of "increasing speed of n!/2, n!/n, n!/(2^n) are slower than 2^n"

C03 - Problem 2

a )
for 0 < epsilon < 1,

lg^k n > n^epsilon

and for epsilon >= 1,

lg^k n) < n^epsilon

Therefore, the correct answer is
YES YES YES YES YES

These solutions are not reliable

I've just looked at the solution for one exercise about the interval trees in chapter 14, and it's wrong in my opinion. You should not share something that's not proved to be correct, it's not helpful at all!!

Execise 18.1-3

This question is about B-Trees. The question asks for the legal trees with min degree t=2, but the solution is for t=3.
So the correct answer is 4 trees:

     2
  1     3 4 5
         4
  1 2 3     5
        3
  1 2      4 5
      2     4
   1     3     5

Although the last tree is correct according to the rules of the B-Tree, the algorithm provided by the book never generates it.

4.1的练习题答案有误

第一部分里的4.1md里面的答案跟题目对不上。
你看一下。估计是贴的别的部分的答案

ps:看了这个repo的很多题解,非常有用,感谢您的分享。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.