| | |
Alogrithm Analysis
![]() |
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 0
Dear All,
I am interested In understanding the Time Complexity for
inorder traversal of the Binary Tree.Generally i would be also happy if you help me understand Space Complexity for the same I am also putting a simple code snippet for the Inorder.
void inorder(struct btree *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf(" %d",sr->node_no);
inorder(sr->rightchild);
}
else
return;
}
I am interested In understanding the Time Complexity for
inorder traversal of the Binary Tree.Generally i would be also happy if you help me understand Space Complexity for the same I am also putting a simple code snippet for the Inorder.
void inorder(struct btree *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf(" %d",sr->node_no);
inorder(sr->rightchild);
}
else
return;
}
To traverse a binary tree you have to touch every node in the tree once. From that you infer that the time complexity is
. You can take it further by postulating that because the recursive algorithm actually visits every internal node 2 times** to complete the traversal the time complexity is
where x is the number of internal nodes and y is the number of external nodes and
gives you n.
The space complexity is the same
for the same reason. If you take it further, the most space that's ever going to be maintained at any time is the space for the longest branch because that's the maximum number of stack frames that will be pushed at any given time by the recursion. If the tree isn't balanced then the longest branch could be n nodes which makes the space complexity
. If the tree is balanced, the space complexity can't be more than the height of the tree. The height of a balanced tree is always going to be
so the space complexity if you assume sufficient balance is
.
** because it recurses in and then back out
The space complexity is the same
** because it recurses in and then back out
The truth does not change according to our ability to stomach it.
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 0
Dear Mr Hammirick,
Thanks for the reply, i have partialy understood what you have explained it to me . Since the time Complexity is of level O(2x+y).X are my node of the trees which gets visited twice ,but what are y? You have explained y as External Nodes .I have got Little confusion on it . I would be happy if you kindly explain it little more elaboratly ,you step to this conclusion.
Thanks and Regards,
Brijendra Mishra
Thanks for the reply, i have partialy understood what you have explained it to me . Since the time Complexity is of level O(2x+y).X are my node of the trees which gets visited twice ,but what are y? You have explained y as External Nodes .I have got Little confusion on it . I would be happy if you kindly explain it little more elaboratly ,you step to this conclusion.
Thanks and Regards,
Brijendra Mishra
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 0
Dear All,
Since understading of Induction Is very Important in the Analysis of Algorithm .I have studied it ,but have never practically put them in use ,while desigining my programs. can you kindly explain to me how one should exploit this conceps while desiging an algorithm using loops and recursions. Simple Example will help me to uderstand the concept little clearly.
Thanks and regards,
brijendra mishra.
Since understading of Induction Is very Important in the Analysis of Algorithm .I have studied it ,but have never practically put them in use ,while desigining my programs. can you kindly explain to me how one should exploit this conceps while desiging an algorithm using loops and recursions. Simple Example will help me to uderstand the concept little clearly.
Thanks and regards,
brijendra mishra.
•
•
•
•
X are my node of the trees which gets visited twice ,but what are y?
6
/ \
5 9
/ \ /
1 4 8
/ / \
0 3 7
I'm not sure if you can get away with •
•
•
•
I'd like to nitpick the statement that the height is.
The truth does not change according to our ability to stomach it.
•
•
•
•
Dear All,
Since understading of Induction Is very Important in the Analysis of Algorithm .I have studied it ,but have never practically put them in use ,while desigining my programs. can you kindly explain to me how one should exploit this conceps while desiging an algorithm using loops and recursions. Simple Example will help me to uderstand the concept little clearly.
First, you might think, "okay, to find the sum, well, all we have to do is find the value at index 0, and then add to it the sum of the values from indices 1 to N-1."
So now you're interested in finding the sum from 1 to N-1. And to do that, you just take ray[1] (let's assume that 'ray' is the name of our array) and add it to the sum of the array elements from 2 to N-1. And in general, it looks like we'll want an algorithm that takes the sum of an array from indices i to j, for arbitrary values of i and j, where i <= j. And if i = j, we have a simple problem: finding the sum of an empty slice of array (and that sum is zero).
So we could have
int sum(int* ray, int n) {
return sum_slice(ray, 0, n);
}
int sum_slice(int* a, int i, int j) {
if (i == j) {
return 0;
}
else {
return a[i] + sum_slice(i + 1, j);
}
}Now that solves the problem, and we can prove that sum_slice works using induction. We know it works for the case (i,j) where i = j, since
That's the kind of thinking that you need for recursive algorithms (which includes those implemented by loops -- they're recursive but they just modify the current stack frame instead of making a new one, and you have to be all careful in how you write your code). Of course, there's a much better way of finding the sum of an array, where instead of adding the first element to the remaining sum, you feed off elements into an accumulator, one at a time, which means you're using constant stack space.
Then instead of exploiting the property that a[i] + sum_slice(i+1,j) = sum_slice(i,j), you're exploiting the property that s + a[i] + sum_slice(i+1,j) = s + sum_slice(i,j).
Another example is looking for an element in a sorted array. You can just say, "it's either the first element of the array, or it's in the rest of the array." That's a true statement. So you have
int find(int f, int* a, int i, int j) {
if (i == j) {
return -1;
}
else {
if (a[i] == f) {
return i;
}
else {
return find(f, a, i+1, j);
}
}
}And there's a much better way to implement this: binary search. That uses the fact that if a[i + (j-i)/2] is less than f, we only need search in the range [i+1,j), and if it's greater, we need only search in the range [0,i-1).
So the practice of devising an algorithm for a problem, is that of finding a mathematical property that lets you solve an equivalent problem, one that is easier and can be reached in less work. You use this mathematical property in a recursive (or iterative) manner, and to prove your algorithm works, you end up exploiting that property in your inductive proof.
To get the running time of the algorithm, though, you use some inductive property of mathematics on your running times. That's not used for designing algorithms though.
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Other Threads in the Computer Science Forum
- Previous Thread: Does anyone have any time to help me with my Word Press Theme?
- Next Thread: sytem flowchart
Views: 2554 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for Computer Science
ai algorithm algorithms architecture assembly assignment assignments binary bizarre bletchleypark blogging book brunel bubble cern cheating-failed clueless code codebreaker compiler computer control conversion cryptography data-base database development dfa dissertation dissertationtopic education extensions github givemetehcodez government graphics gui guidelines history homework homeworkassignment homeworkhelp ibm ideas impress info information insertion introductory itcontracts jobs language lazy lighthouse lincence linkbait marketing method mobileapplication ms nerd networkingprojects news notation offtopic openoffice os parser problem programming project ps3 quick recursion result sam-being-cute science security servers sex skills software sorting spoonfeeding sql sql-server stephenfry student student-gets-caught supercomputing syntactic telecom time traffic tree uk verify visualbasic writer ww2






