Hello, Citizens of the World,
Topic of the day: Efficiency of Algorithms, Big O
Comparison of the Growth of Functions
When M is arbitrarily large, a functions growth depends on the term with the highest base (the dominant term).
Ignore the coefficient of the greatest term in the function.
There are different notations:
20n + 5 = O(n)
200n + 1 000 000 = O(n)
200n^2 + n - 19 = O(n^2)
300n^2 + 12345n + 23 = O(n^2)
75n^3 + 4n^2 = O(n^3)
s + 3333 log n + 12343 n log n = O(n log n)
2^s + 1050s^2 = O(2^s)
2340(2^n) + 5000 (n^200) = O(2^n)
Notice how the value in the brackets is the "dominant term".
Ordering:
20n + 5 = O(n) -> 1
200n + 1 000 000 = O(n) -> 1
200n^2 + n - 19 = O(n^2) -> 3
300n^2 + 12345n + 23 = O(n^2) -> 3
75n^3 + 4n^2 = O(n^3) -> 4
s + 3333 log n + 12343 n log n = O(n log n) -> 2
2^s + 1050s^2 = O(2^s) -> 5
2340(2^n) + 5000 (n^200) = O(2^n) -> 5
Time Complexity of Algorithms:
The worst-case time complexity would be an upper bound on the number of operations that an algorithm does to solve a problem with an input size of s.
Have a good day,
Belinda.
Friday, April 1, 2016
Week 10
Hello, Citizens of the World!
Today's topic:
bst_del_rec(tree, data)
Base Case = If we have a None tree return None
Recursive Case One = If data is less than tree data have it deleted from the left child.
Recursive Case Two = If data is greater than tree data have it deleted from the right child.
If none of the above if statements are true we have found the tree node that we need to delete.
Possible Cases
Case 1 = If the tree node does not have a left child, return the right child.
Case 2 = If the tree node does have a left child, find its largest node, and replace the tree node's data with the largest data that has been found so far. Then delete the largest value.
Recursive vs Iterative Functions:
Recursive:
A recursive function imposes a loop, then loop is implicit and Python is able to take care of it. This costs us time and memory, the cost may be negligible. Every single recursive function can be written in an iterative manner using explicit loops which may or may not require stacks. Though depending on the problem writing it iteratively is a bad idea as it could be extremely time consuming and illegible. This is because the problem may naturally be recursive.
Today's topic:
bst_del_rec(tree, data)
Base Case = If we have a None tree return None
Recursive Case One = If data is less than tree data have it deleted from the left child.
Recursive Case Two = If data is greater than tree data have it deleted from the right child.
If none of the above if statements are true we have found the tree node that we need to delete.
Possible Cases
Case 1 = If the tree node does not have a left child, return the right child.
Case 2 = If the tree node does have a left child, find its largest node, and replace the tree node's data with the largest data that has been found so far. Then delete the largest value.
Recursive vs Iterative Functions:
Recursive:
A recursive function imposes a loop, then loop is implicit and Python is able to take care of it. This costs us time and memory, the cost may be negligible. Every single recursive function can be written in an iterative manner using explicit loops which may or may not require stacks. Though depending on the problem writing it iteratively is a bad idea as it could be extremely time consuming and illegible. This is because the problem may naturally be recursive.
Week 8
Dear Citizens of the World,
Week 8's lecture discussed Binary Trees.
Our generic tree design has changed. We now have two named children called left and right, and a tree that is empty is represented by None.
To evaluate a Binary Tree:
No empty expressions:
If it's a leaf return its value. If it is not a leaf evaluate the left tree, and the right tree. After doing so combine the right, and left values with the binary operation given.
If there are empty expressions:
Use Python's built in eval method
>>>eval("7*9")
63
Inorder:
The recursive definition is to visit the left subtree inorder, visit the node, and then visit the right subtree inorder. The coding for inorder is extremely similar to the above definition.
Preorder:
The recursive definition is to visit the node itself, visit the left subtree preorder, and visit the right subtree preorder. Again, just like the inorder coding, preorder coding is also similar to the given definition.
Postorder:
The recursive definition:
Visit the left subtree postorder, visit the right subtree postorder, and visit the node itself.
Postorder coding is similar to the definition in layout.
Level Order:
Visit the node, visit the node children, visit node's grandchildren and great grandchildren, and so on.
Binary Search Trees
Ordering conditions to Binary Trees:
Data must be comparable. The data in the left subtree must be less than the value of node.data, and the data value in the right subtree must be more than the value of node.data
Height:
If arity is 2:
BST with 1 node has height 1.
BST with 4 nodes has a height of 3.
BST with 7 nodes can have a height of 3.
I will upload a diagram in the future to go into better detail and explanation of height.
Good day! :)
Week 8's lecture discussed Binary Trees.
Our generic tree design has changed. We now have two named children called left and right, and a tree that is empty is represented by None.
To evaluate a Binary Tree:
No empty expressions:
If it's a leaf return its value. If it is not a leaf evaluate the left tree, and the right tree. After doing so combine the right, and left values with the binary operation given.
If there are empty expressions:
Use Python's built in eval method
>>>eval("7*9")
63
Inorder:
The recursive definition is to visit the left subtree inorder, visit the node, and then visit the right subtree inorder. The coding for inorder is extremely similar to the above definition.
Preorder:
The recursive definition is to visit the node itself, visit the left subtree preorder, and visit the right subtree preorder. Again, just like the inorder coding, preorder coding is also similar to the given definition.
Postorder:
The recursive definition:
Visit the left subtree postorder, visit the right subtree postorder, and visit the node itself.
Postorder coding is similar to the definition in layout.
Level Order:
Visit the node, visit the node children, visit node's grandchildren and great grandchildren, and so on.
Binary Search Trees
Ordering conditions to Binary Trees:
Data must be comparable. The data in the left subtree must be less than the value of node.data, and the data value in the right subtree must be more than the value of node.data
Height:
If arity is 2:
BST with 1 node has height 1.
BST with 4 nodes has a height of 3.
BST with 7 nodes can have a height of 3.
I will upload a diagram in the future to go into better detail and explanation of height.
Good day! :)
Subscribe to:
Posts (Atom)