introduction-to-algorithms | repository contains C code to implement | Learning library
kandi X-RAY | introduction-to-algorithms Summary
kandi X-RAY | introduction-to-algorithms Summary
This repository contains C code to implement algorithms teached in Introduction to Algorithms.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of introduction-to-algorithms
introduction-to-algorithms Key Features
introduction-to-algorithms Examples and Code Snippets
def find_optimal_binary_search_tree(nodes):
"""
This function calculates and prints the optimal binary search tree.
The dynamic programming algorithm below runs in O(n^2) time.
Implemented from CLRS (Introduction to Algorithms) book.
Community Discussions
Trending Discussions on introduction-to-algorithms
QUESTION
Question: Sort the functions in increasing order of big-O complexity
- f1(n) = (n^0.999999) log n
- f2(n) = 10000000n
- f3(n) = 1.0000001^n
- f4(n) = n^2
My answer to this question is that is: 3, 2, 1, 4 (in increasing order) based on the rule that we can ignore constants.
But the answer I found in the solution booklet is:
The correct order of these functions is f1(n), f2(n), f4(n), f3(n).
I am not able to understand this, Can anyone explain? Here is the solution's explanation if it helps.
Thanks in advance!
...ANSWER
Answered 2020-Nov-01 at 17:44The following facts reveal the ordering:
O(n^k) > O(log(n))
for anyk > 0
.O(k^n) > O(n^b)
for anyk > 1
.
This might feel counter-intuitive since 1.0000001^n
starts off really slow, but we are talking of asymptotic complexity here. The exponential growth, albeit slow in practical scenarios, dominates any polynomial growth as we go towards infinity. And the same is true for polynomial growth being greater than logarithmic growth.
So:
- f3(n), with the exponential growth is of highest complexity.
- f4(n) being greater than f2(n) and f1(n) is quite obvious.
- f1(n) vs f2(n) -- Consider them
n^0.999999 * logn
vsn^0.999999 * n^0.000001
. So what determines the comparison here islogn
vsn^0.000001
. As we have stated in fact (1), polynomial growth > logarithmic growth. So f2(n) > f1(n).
Combining the results, we have O(f1(n)) < O(f2(n)) < O(f4(n)) < O(f3(n))
.
QUESTION
This is my first time writing C++. I am trying to implement QuickSort referencing the MIT open course ware slides (Slides 4 and 17 specifically).
However, there is a bug:
...ANSWER
Answered 2020-Sep-17 at 12:28The slide says for j ← p + 1 to q
, which you turned to for (int j = p+1; j < q; j++)
. This is an off-by-one error.
QUESTION
I was looking into MIT's open courseware first lecture on an introduction to algorithms and there is something that is not terribly obvious to me. You cant start watching the lecture at 24:30 here and the lecture notes with all the details of the 1D peak problem definition and solution in here
The problem goes:
for an array of "n" integer elements find a peak
and gives an example array of size 8: example = [6,7,4,3,2,1,4,5]
Definition of a peak
For the example
array above example[1]
and example[7]
are "peaks" because those numbers are greater or equal than their adjacent element and a special condition for the last element of the array applies that it only needs to be greater than or equal to the element preceding it. that is:
ANSWER
Answered 2020-Jul-09 at 06:08Given the condition above in A why go to the left? instead of the right?
If you would go to the right (without first checking condition B), there is a small probability that the values at the right will keep going down (from left to right), and you would not find a peak there.
At the left side, however, you know that you cannot have that situation, as you have found at least one value that is higher (the neighbour) and could potentially even be a peak. Here is how you can prove that a peak exists at the left side (this is not a description of the algorithm; just a way to prove it):
If the immediate neighbour is not a peak, then possibly the next one to its left is. If not, then possibly the next one to its left....etc. This series will end when finding a peak or arriving at the left most value. If none of the others were peaks, then this one must be it. This only happens when the values never decreased while looking further to the left.
In short, whatever the situation at the left side, there is a peak somewhere there at that side, and that is all we need to know when choosing a side.
Given the condition above in B why go to the right? instead of the left?
This is of course the same reasoning, but mirrored.
Note that you can decide to first check condition B and only then A. When both conditions are true at the same time, you can actually choose which side to go. This is where you got the feeling from that the choice looks "arbitrary". It is indeed arbitrary when both conditions A and B are true.
But also think about the case where one of A and B is true and the other false. If you would go the wrong (downward) way, you have no guarantee that values would ever go up in that direction. And so there is a small probability that there is no peak on that side.
Of course, there still could be a peak on that side, but since we are sure there is one on the other side, it is wise to go for certainty. We don't care about potentially discarding some peaks, as we only need to find one peak.
The binary search algorithm assumes we start from a sorted array, so how come it makes sense to apply it to data that may be unsorted?
Binary search for a particular value would only work in a sorted array, but here we are not looking for a particular value. The conditions of the value we are looking for are less strict. Instead of a particular value, we will be happy with any value that is a local peak.
QUESTION
I'm reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst
function in Python.
Here is the example I'm trying to apply the optimal BST to:
Let us define e[i,j]
as the expected cost of searching an optimal binary search tree containing the keys labeled from i
to j
. Ultimately, we wish to compute e[1, n]
, where n
is the number of keys (5 in this example). The final recursive formulation is:
which should be implemented by the following pseudocode:
Notice that the pseudocode interchangeably uses 1- and 0-based indexing, whereas Python uses only the latter. As a consequence I'm having trouble implementing the pseudocode. Here is what I have so far:
...ANSWER
Answered 2017-Sep-11 at 17:43It appears to me that you made a mistake in the indices. I couldn't make it work as expected but the following code should give you an indication where I was heading at (there is probably an off by one somewhere):
QUESTION
I am doing some remedial work on algorithms as I am taking a graduate course on them in the Fall and was a physics undergrad. Watching this video and at the 38:00 mark he goes over the greedy ascent algorithm for a 2D array. I am confused as he defines the peak as a <= b,c,d,e (with b,c,d and e being the elements to the left,right,top,bottom of the current element 'a'). And then he goes on to say that to find the peak you follow the greatest element bordering on 'a' but what if you have the 2D array:
20 15 13
12 10 10
40 40 40
and started at 13, wouldn't the greedy ascent algorithm incorrectly identify 20 as the peak? How can you search an unsorted array without having to look at every element?
I thank you for your help apologize in advance if this is a silly question.
...ANSWER
Answered 2019-Feb-02 at 20:25Pay close attention to the definitions, as they are not the same as what you would expect intuitively.
he defines the peak as a <= b,c,d,e (with b,c,d and e being the elements to the left,right,top,bottom of the current element 'a')
So there you have it -- a peak is defined as a local maximum (an element larger than all of its immediate neighbors), rather than a global maximum (an element larger than all other elements). By this definition, it's clear that while 20 is not the largest element it is a peak.
(As noted in comments, the definition should probably be a >= b,c,d,e that's probably just a typo in the original post)
QUESTION
The below image is from : 6.006-Introduction to algorithms,
While doing the course 6.006-Introduction to algorithms, provided by MIT OCW, I came across the Rabin-Karp algorithm.
Can anyone help me understand as to why the first rs()==rt() required? If it’s used, then shouldn’t we also check first by brute force whether the strings are equal and then move ahead? Why is it that we are not considering equality of strings when hashing is done from t[0] and then trying to find other string matches?
In the image, rs() is for hash value, and rs.skip[arg] is to remove the first character of that string assuming it is ‘arg’
...ANSWER
Answered 2018-Nov-02 at 21:54Can anyone help me understand as to why the first
rs()==rt()
required?
I assume you mean the one right before the range-loop. If the strings have the same length, then the range-loop will not run (empty range). The check is necessary to cover that case.
If it’s used, then shouldn’t we also check first by brute force whether the strings are equal and then move ahead?
Not sure what you mean here. The posted code leaves blank (with ...
) after matching hashes are found. Let's not forget that at that point, we must compare strings to confirm we really found a match. And, it's up to the (not shown) implementation to continue searching until the end or not.
Why is it that we are not considering equality of strings when hashing is done from t[0] and then trying to find other string matches?
I really don't get this part. Note the first two loops are to populate the rolling hashes for the input strings. Then comes a check if we have a match at this point, and then the loop updating the rolling hashes pair wise, and then comparing them. The entire t
is checked, from start to end.
QUESTION
I'm new to html
but playing with a script to download all PDF
files that a given webpage links to (for fun and avoiding boring manual work) and I can't to find where in the html
document I should look for the data that completes relative paths - I know it is possible since my web browser can do it.
Example: I trying to scrape lecture notes linked to on this page from ocw.mit.edu using R
package rvest
looking at the raw html
or accessing the href
attribute of a
"nodes" I only get relative paths:
ANSWER
Answered 2018-Aug-28 at 17:47The easiest solution that I have found as of today is using the url_absolute(x, base)
function of the xml2
package. For the base parameter, you use the url of the page you retrieved the source from.
This seems less error prone than trying to extract the base url of the address via regexp.
QUESTION
I'm trying to implement the depth-first search (DFS) algorithm for directed graphs as described in Cormen et al., Introduction to Algorithms (3rd ed.). Here is my implementation so far:
...ANSWER
Answered 2017-Aug-21 at 13:22The DFS has to be logically DFS, but programmatically you can try a work around.
writing the DFS in such a way that you can retry it from one of the top functions, if it reaches a near the recursion limit.
Try to use multiprocessing.
PS: Is it possible that an infinite recursion is occurring for the larger dataset? logical error which comes forth when using a larger dataset. If you have datasets of incremental sizes, you could also identify the limit of the algorithm's implementation in python.
QUESTION
The algorithm described to in this this MIT lecture and written out in this SO question for finding a peak in a 1d array makes sense.
So does its analysis of O(log n); we re dividing the array into halves
How can I update it to find all peaks in the array? What would that complexity be?
...ANSWER
Answered 2017-Apr-20 at 18:29For finding all peaks, you can't do any better than just going through the whole array and comparing every element to its neighbors. There's no way to tell whether an element you didn't look at is or isn't a peak, so you have to look at all of them.
Thus, the time complexity is O(n) for n elements.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install introduction-to-algorithms
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page