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! :)
Friday, March 4, 2016
Week 7
Dear Citizens of the World,
We went over more examples of Recursive Functions this week, including a bit on Tracing Recursive Functions Using Stacks and Recursive Structures.
Pretty much a recursive function is a function which uses itself in its body, which at first sounds weird. When I looked at it at first I got confused, but it's actually not that complicated. The fact that a recursive function has a base case helps a ton and makes the process of calculating so much more sensible, and, in turn, easier! :D
Example of Recursive Functions:
Unicorn(n) = 2n * Unicorn(n - 1)
Unicorn(0) = 2
Notice how the function is calculated using the function itself?
A recursive function must have minimum one base case, and minimum one recursive case.
In the above example:
Base case -> Unicorn(0) = 2
Recursive case -> Unicorn(n - 1)
The base case saves the day, because if we expand the whole entire process we get:
Say n = 2:
Then,
Unicorn(2) = 2(2) * Unicorn(1)
= 4 * (2(1) * Unicorn(0)
= 4 * (2 * 2)
= 4 * (4)
= 16
If we didn't have a base case, we wouldn't be able to calculate the answer to Unicorn(2). We would simply expand the function until we couldn't any longer, which wouldn't help us at all. :P
General Form of Recursion:
if (something that allows us to detect a base case):
(Apply something that does not use recursion)
else:
(A generalized case)
(Apply something that does involve recursion)
I'm still attempting to understand the rest, but until then this is all I have, my apologies. :(
I'll update as soon as I do!
Peace out! :)
We went over more examples of Recursive Functions this week, including a bit on Tracing Recursive Functions Using Stacks and Recursive Structures.
Pretty much a recursive function is a function which uses itself in its body, which at first sounds weird. When I looked at it at first I got confused, but it's actually not that complicated. The fact that a recursive function has a base case helps a ton and makes the process of calculating so much more sensible, and, in turn, easier! :D
Example of Recursive Functions:
Unicorn(n) = 2n * Unicorn(n - 1)
Unicorn(0) = 2
Notice how the function is calculated using the function itself?
A recursive function must have minimum one base case, and minimum one recursive case.
In the above example:
Base case -> Unicorn(0) = 2
Recursive case -> Unicorn(n - 1)
The base case saves the day, because if we expand the whole entire process we get:
Say n = 2:
Then,
Unicorn(2) = 2(2) * Unicorn(1)
= 4 * (2(1) * Unicorn(0)
= 4 * (2 * 2)
= 4 * (4)
= 16
If we didn't have a base case, we wouldn't be able to calculate the answer to Unicorn(2). We would simply expand the function until we couldn't any longer, which wouldn't help us at all. :P
General Form of Recursion:
if (something that allows us to detect a base case):
(Apply something that does not use recursion)
else:
(A generalized case)
(Apply something that does involve recursion)
I'm still attempting to understand the rest, but until then this is all I have, my apologies. :(
I'll update as soon as I do!
Peace out! :)
Week 4
Dear Citizens of the World,
The 4th lecture of CSC148 touched upon Containers, Stacks, Sacks, Unittest, Balanced Paranthesis, and introduced us to Linked Lists.
The professor started off giving us useful definitions of Stacks and Sacks, metaphors to help us understand how they each work, and small bits of information on each data type.
A stack is made up of items of different sorts. The newest items added are placed on top of the stack and the oldest/first items added are placed on the bottom. Stacks are referred to as a LIFO structure which stands for Last In, First Out.
We are also told that we should not try to remove an item from an empty stack. We need to first know whether the stack is empty or has one or more items in it.
Metaphor: A stack of items is like a stack of dishes, the first dish placed on the counter will be the last to be used. This is because there are more dishes above it that need to be used first, and attempting to get a dish from the middle or bottom will cause all the dishes to fall and shatter - resulting in an error.
A sack also contains items of various types, but any new items are placed in a random place in the sack. This means any items removed will come out in a random order as well. Just like a stack we have to see first if the sack is empty or not.
Metaphor: This is similar to a person placing items in their backpack or purse. The items will change location i.e. a sweater might find its way to the bottom of the bag though it was the last thing to be placed inside the backpack.
There are similar traits between a stack and a sack, especially when it comes to use in client code:
s.__init__()
s.__str__()
s.__eq__()
s.add()
s.remove()
s.is_empty()
These traits can be managed in a superclass called Container!
While developing Container we have to look at the previously mentioned methods. Which ones should or should not be implemented?
This decision is dependent on each individual project. The methods used have to suit the projects needs.
To test everything we use Unittest.
Unittest allows us to create test cases, and use these test cases to, well, test. The test cases run independently, and everything is documented and reusable if ever needed again.
unittest.TestCase is not different from extending any other class. A subclass is created and special methods are over ridden.
Balanced Parentheses:
The opening and closing parentheses must match up in many situations. Matching means a opening parentheses followed by a closing parentheses or no parentheses present at all.
Examples of proper formatting:
17 + 5
(34234 * b * h)
Examples of improper formatting:
(3
)m
apple)
)unicorn(
Linked Lists:
Linked list nodes keep enough memory for the object value they want to use, a reference to and a reference to the node that will come next in the list.
Linked Lists are at the moment being implemented as nodes with a value and reference to any like objects.
In all honesty I wish a bit more of an explanation was given on Unittest, Container, and Linked Lists, but only so much can be fit into one lecture, which is unfortunate.
The 4th lecture of CSC148 touched upon Containers, Stacks, Sacks, Unittest, Balanced Paranthesis, and introduced us to Linked Lists.
The professor started off giving us useful definitions of Stacks and Sacks, metaphors to help us understand how they each work, and small bits of information on each data type.
A stack is made up of items of different sorts. The newest items added are placed on top of the stack and the oldest/first items added are placed on the bottom. Stacks are referred to as a LIFO structure which stands for Last In, First Out.
We are also told that we should not try to remove an item from an empty stack. We need to first know whether the stack is empty or has one or more items in it.
Metaphor: A stack of items is like a stack of dishes, the first dish placed on the counter will be the last to be used. This is because there are more dishes above it that need to be used first, and attempting to get a dish from the middle or bottom will cause all the dishes to fall and shatter - resulting in an error.
A sack also contains items of various types, but any new items are placed in a random place in the sack. This means any items removed will come out in a random order as well. Just like a stack we have to see first if the sack is empty or not.
Metaphor: This is similar to a person placing items in their backpack or purse. The items will change location i.e. a sweater might find its way to the bottom of the bag though it was the last thing to be placed inside the backpack.
There are similar traits between a stack and a sack, especially when it comes to use in client code:
s.__init__()
s.__str__()
s.__eq__()
s.add()
s.remove()
s.is_empty()
These traits can be managed in a superclass called Container!
While developing Container we have to look at the previously mentioned methods. Which ones should or should not be implemented?
This decision is dependent on each individual project. The methods used have to suit the projects needs.
To test everything we use Unittest.
Unittest allows us to create test cases, and use these test cases to, well, test. The test cases run independently, and everything is documented and reusable if ever needed again.
unittest.TestCase is not different from extending any other class. A subclass is created and special methods are over ridden.
Balanced Parentheses:
The opening and closing parentheses must match up in many situations. Matching means a opening parentheses followed by a closing parentheses or no parentheses present at all.
Examples of proper formatting:
17 + 5
(34234 * b * h)
Examples of improper formatting:
(3
)m
apple)
)unicorn(
Linked Lists:
Linked list nodes keep enough memory for the object value they want to use, a reference to and a reference to the node that will come next in the list.
Linked Lists are at the moment being implemented as nodes with a value and reference to any like objects.
In all honesty I wish a bit more of an explanation was given on Unittest, Container, and Linked Lists, but only so much can be fit into one lecture, which is unfortunate.
Thursday, January 28, 2016
Week 3
Dear Citizens of the World,
This is supposed to be a blog regarding my experiences attending the course CSC148H (Introduction to Computer Science) provided by the University of Toronto's St. George Campus.
The first lecture, like all first lectures, involved introducing us to the course and what would be required to succeed. It was amusing with the professor's sense of humour and method of introducing us to the information.
My problems begin with the course being an evening class. Sadly, my schedule only allows me to enrol in and attend the evening portions of the course.
The result: An overwhelming urge to rest my eyes and dive into a peaceful oblivion halfway through.
The consequences: I'm failing.
Don't get me wrong, I don't mean I am literally failing the course by the fourth week, but I am clearly behind. The reason seems to be I get distracted. I don't what's going up in my mind, but when I am awake I simply can't seem to pay attention, and the information presented to me isn't being absorbed by my puny little brain. The professor makes sense, but it seems my brain doesn't want me to succeed like he does. The info sweeps in through my right ear, settles itself momentarily and just as fast as it came inside it runs out.
This happened during both the second and third lectures too. I'm going to have to crack open my books and memorize as best as I can or go see a doctor. Who knows? Hopefully, I'll know what's going on in class by the end of the weekend.
Wish me luck! :)
This is supposed to be a blog regarding my experiences attending the course CSC148H (Introduction to Computer Science) provided by the University of Toronto's St. George Campus.
The first lecture, like all first lectures, involved introducing us to the course and what would be required to succeed. It was amusing with the professor's sense of humour and method of introducing us to the information.
My problems begin with the course being an evening class. Sadly, my schedule only allows me to enrol in and attend the evening portions of the course.
The result: An overwhelming urge to rest my eyes and dive into a peaceful oblivion halfway through.
The consequences: I'm failing.
Don't get me wrong, I don't mean I am literally failing the course by the fourth week, but I am clearly behind. The reason seems to be I get distracted. I don't what's going up in my mind, but when I am awake I simply can't seem to pay attention, and the information presented to me isn't being absorbed by my puny little brain. The professor makes sense, but it seems my brain doesn't want me to succeed like he does. The info sweeps in through my right ear, settles itself momentarily and just as fast as it came inside it runs out.
This happened during both the second and third lectures too. I'm going to have to crack open my books and memorize as best as I can or go see a doctor. Who knows? Hopefully, I'll know what's going on in class by the end of the weekend.
Wish me luck! :)
Subscribe to:
Posts (Atom)