Algorithm Analysis
Algorithm Analysis measure the speed of the task algorithm suppose to accomplished. Here the necessary maths back ground to analyse the algorithmic complexity.
CONTENTS
- 12 -fold way
- Order of the Growth
- Algorithm Analysis of Sorting
- Algorithm Analysis of Searching
- Appendix
There are two basic rules in the Algorithm analysis:
Rule of sum: if an action is performed making A choices or B choices, then it can be performed
Rule of product: If an action can be performed by making A choices followed by B choices, then it can be performed
Permutation: an ordered list where every object appears exactly once
combinations: When order is not important and the repetition is not allowed, the number of ways to choose k from the distinct n is as for
in general, choosing k
out of n
is same as not choose n-k
out of n
.
According to the Pascal's Triangle:
Again from the pascal triangle:
ordered | repeating | ||
---|---|---|---|
sequence | yes | yes | |
Permutation | yes | no | |
Multisubset | no | yes | |
combination | no | no |
It is sufficent to implement
12 -fold way
This is the mathematical basis for the Algorithm Analysis.
Here D - distinct and I - identical. In the following table, if
A | B | any | at most | at least |
---|---|---|---|---|
D | D | |||
I | D | |||
D | I | 1 if |
||
I | I | 1 if |
In the above table, Stirling numbers of the second kind a
elements into b
nonempty subsets. Stirling numbers of the first kind a
objects with exactly b cycles.
In the above table, a
as the sum of b
positive numbers since the order don't matter. For
Multi-choosing k
objects from a set of n
objects where order is not important but repetition is allowed.
Simplification,
Multinomial theorem where
Principle of Inclusion-Exclusion
Formula for the sterling number:
Java
There are two choices for the intermediate represetiation
- portable machine language
- graph based
Initially Java was created only with byte code interpreter. But current Java has Just In Time (JIT) compiler.
There are two popular representations:
- Stack based - 0 operand
- Register based - 3 operand
Java uses Stack based representation.
If you consider the for single loop in Java
for (int i =0; i < N; i++){
...
}
The running time cost of the above loop is
Order of the Growth
In the Algorithm Analysis, fortunately there are limited models to consider. Here the growth from best running time to worst:
(Knuth shuffle) (Mergesort, QuickSort) (selection = , insertion = 1/4 )
Each instance of the java.util.ArrayList has the capacity, when reach to the capacity, the array need to be increased. Assume, each time ArrayList is double when it reach to the capacity then the equation is
This is because ArrayList is using java array of Object as a implementation. Advantages are that every operation takes constant time of Amortised time( Average running time per operation over the worst case sequence of operations) and less wasted space compared to linked list implementation because linked list is based on object.
Algorithm Analysis of Sorting
There java.lang.Comparable<T>
and java.util.Comparator<T>
is based on the total order which is a binary relation that satisfy:
- Antisymmetry: if
and , then - Transitivity: if
and , then - Totality: either
or or both
Quicksort is little bit faster than Mergesort because Quicksort doesn't exchange the elements always. However, quick sort wort case running time is quadratic (
The java.utils.Arrays sort() method is using Quicksort for the primitives and Mergesort for the objects.
Dijkstart 3-way partitioning is the way to compromise with the duplicate keys because lower bound is reduced linearithmic to linear for most of the applications as follows:
Algorithm | Worst | Average | Best | Remarks |
---|---|---|---|---|
selection | N exchanges | |||
insertions | small N or partially | |||
shell | ? | ? | N | tight code |
merge | ||||
quick | ||||
3-way quick | support duplicate keys | |||
Heapsort | In-place algo. Inner loop is longer than quicksort. Poor use of cache memory. Not stable. |
Algorithm Analysis of Searching
Binary Search Tree (BST) is a binary tree(BT) in symmetric order. BT can be either empty or two disjoint binary trees at left and right(left/right nodes can be null).
In the BST every node has a key:
- Larger than all keys in its left subtree
- smaller than all keys in its right subtree
For the N number of distinct values in random order, the number of comparisons are
Red-Black trees
The java.util.TreeMap
is based on the Red-black tree. Here some of the characteristics of the Red-Black tree:
- Represents 2-3 tree as
- use internal left-leaning link to glue three nodes which is red link
- no node has two red links
- every path from root to null link there are same number of black links.
Appendix
quadratic
logarithmic (
cubic (
Comments
Post a Comment
commented your blog