Questions tagged [algorithm-analysis]
Questions about the science and art of determining properties of algorithms, often including correctness, runtime and space usage. Use the [runtime-analysis] tag for questions about the runtime of algorithms.
217 questions
197
votes
3
answers
27k
views
Is there a system behind the magic of algorithm analysis?
There are lots of questions about how to analyze the running time of algorithms (see, e.g., runtime-analysis and algorithm-analysis). Many are similar, for instance those asking for a cost analysis ...
50
votes
4
answers
73k
views
How do O and Ω relate to worst and best case?
Today we discussed in a lecture a very simple algorithm for finding an element in a sorted array using binary search. We were asked to determine its asymptotic complexity for an array of $n$ elements.
...
87
votes
6
answers
23k
views
How can we assume that basic operations on numbers take constant time?
Normally in algorithms we do not care about comparison, addition, or subtraction of numbers -- we assume they run in time $O(1)$. For example, we assume this when we say that comparison-based sorting ...
19
votes
3
answers
3k
views
Why use comparisons instead of runtime for comparing two algorithms?
I notice that in a few CS research papers, to compare the efficiency of two algorithms, the total number of key comparison in the algorithms is used rather than the real computing times themselves. ...
5
votes
2
answers
2k
views
How fast can we identifiy almost-duplicates in a list of strings?
I'm having trouble figuring out the upper bound running time for this scenario:
Input:
$N$ number of strings
$M$ upper bound of string length
$T$ threshold for edit distance (2 strings with a ...
14
votes
2
answers
21k
views
Time complexity of a triple-nested loop
Please consider the following triple-nested loop:
...
10
votes
2
answers
16k
views
Proving the lower bound of compares in comparison based sorting
I'm reading Sedgewick and Wayne's book of Algorithm. When I read the following proof in the attached picture, I don't understand why it assumed the comparison number is lg(number of leaves). Any help ...
7
votes
7
answers
7k
views
Is there a meaningful difference between O(1) and O(log n)?
A computer can only process numbers smaller than say $2^{64}$ in a single operation, so even an $O(1)$ algorithm only takes constant time if $n<2^{64}$. If I somehow had an array of $2^{1000}$ ...
3
votes
2
answers
6k
views
Using software to calculate the complexity of an algorithm
I am somewhat a beginner, and I have often seen complexity being calculated for various algorithms but they never actually gave me a very clear idea about how it is done. Can someone please point some ...
17
votes
1
answer
24k
views
What does the 2 in a 2-approximation algorithm mean?
Does the 2 in a 2-approximation algorithm mean the solution is within 2*OPT or OPT/2?
13
votes
1
answer
20k
views
Proof that a randomly built binary search tree has logarithmic height
How do you prove that the expected height of a randomly built binary search tree with $n$ nodes is $O(\log n)$? There is a proof in CLRS Introduction to Algorithms (chapter 12.4), but I don't ...
7
votes
1
answer
5k
views
How can I make sense of amortized accounting method?
Amortized accounting method has to be one of the most abstract analysis technique I have ever seen in my life (maybe aside from the potential method which I haven't read).
In the example of the Stack ...
6
votes
2
answers
26k
views
$T(n)=2T(n/2)+n\log n$ and the Master theorem [duplicate]
According to Introduction to algorithms by Cormen et al,
$$T(n)=2T(n/2)+n\log n$$ is not case 3 of Master Theorem. Can someone explain me why?
And which case of master theorem is it?
4
votes
1
answer
2k
views
Fast, stable, almost in-place radix and merge sorts
I've developed LSD radix sort algorithm that is
stable,
about as fast as the classic LSD radix sort,
require only $O(\sqrt{RN})$ extra space when we sort into R buckets.
The same technique also ...
3
votes
2
answers
6k
views
The order of growth analysis for simple loop
What would the order of growth for this loop be:
int sum = 0;
for (int n = N; n > 0; n /= 2)
for(int i = 0; i < n; i++)
sum++;
The first loop ...
2
votes
2
answers
2k
views
How does the automatic complexity analysis of Codility work?
When completing exercises on Codility.com you submit your code to a server for analysis. You then receive a report containing the detected algorithm complexity of the code.
I was just wondering how ...
15
votes
2
answers
34k
views
Why is the dynamic programming algorithm of the knapsack problem not polynomial? [duplicate]
The dynamic programming algorithm for the knapsack problem has a time complexity of $O(nW)$ where $n$ is the number of items and $W$ is the capacity of the knapsack.
Why is this not a polynomial-time ...
12
votes
1
answer
4k
views
Explaination for Variation of Boyer-Moore Majority voting algorithm
Boyer-Moore's majority vote algorithms can be used to determine the majority element in a linear time and constant space.
The intuition behind finding the majority element is understandable as it ...
10
votes
6
answers
35k
views
Why does DFS only yield tree and back edges on undirected, connected graphs?
Prove that if G is an undirected connected graph, then each of its edges is either in the depth-first search tree or is a back edge.
Now, from intuition and in class lectures by Steven Skiena, I know ...
9
votes
3
answers
34k
views
Big O: Nested For Loop With Dependence
I was given a homework assignment with Big O. I'm stuck with nested for loops that are dependent on the previous loop. Here is a changed up version of my homework question, since I really do want to ...
7
votes
1
answer
1k
views
Runtime of Euclidean Algorithm
Given two $n$-bits numbers $a$ and $b$, I am not sure on how to find the runtime of the euclidean algorithm for finding the $\gcd$ of $a,b$. The problem (for me) in here is that apart from the size of ...
6
votes
3
answers
9k
views
Quicksort vs. insertion sort on linked list: performance
I have written a program to sort Linked Lists and I noticed that my insertion sort works much better than my quicksort algorithm.
Does anyone have any idea why this is?
Insertion sort has a ...
6
votes
1
answer
969
views
What is "polynomial delay?"
I am reading a paper and it uses the expression "polynomial delay" which I don't understand. It is used in conjonction with the big O notation, which I'm familiar with.
Here is a example sentence ...
5
votes
2
answers
4k
views
Finding a O(n) solution to: max difference of pairs array question [closed]
I don't know an O(n) solution to the following:
Given an array of n integers, find the largest difference between any two pairs in the array: however, the larger integer must have a higher index in ...
4
votes
1
answer
971
views
CLRS RAM model Description
I'm seeking some clarification on a description of the RAM model in CLRS on page 23, section 2.2 (Analyzing Algorithms).
Firstly, it is mentioned that we assume integers are represented with $c\cdot\...