Its finally the end of the year! I really just want to be lazy and do nothing but I have to do this and study for exams.
In our last week we learned about Hash. Basically hash is a way to store references to objects with similar efficiency to a list but without having to allocate large amounts of adjacent memory. I think this is useful when we are using a computer that is storing many lists since each list requires large blocks of adjacent memory and thus memory is not assigned in the most efficient way.
Hashing can only be done on immutable objects since hashing is really just using an algorithm to convert the object into an index to store the object. Thus this algorithm must be consistent at the time of hashing and retrieval.
There is a big problem about hashing though, it is possible to have several objects assigned to the same index. This is called collision and happens a lot more common than it would seem intuitively. Professor Heap showed us that there is an abnormal amount of people with the same birthday when intuitively it should require 365 people before a repeat.
We can solve this with one of two methods:
1. Probing: When we encounter an already filled hash index, we go find the next open one
2. Chaining: Keep a list called a bucket at each hash index.
The approach in this course is to use chaining.
One question I still have about hash is its relation to dictionaries. Are dictionaries hashes? Since we talked about how dictionaries are efficient.
CSC148 Course SLOG
Friday, 8 April 2016
Wednesday, 30 March 2016
Week 11 SLOG - Revisiting
This week I will be revisiting an earlier SLOG to talk about whether my views have changed or developed since then. I will be revisiting week 5's SLOG about Linked Lists.
My view has changed to prefer just using a normal list for Queues and Stacks rather than Linked Lists. The reason for this is that we have to import or implement the Linked List class into whatever project we are working on rather than just using the inbuilt list. Basically, I am too lazy to import it for assignments, since we are required to upload specific files.
I do wonder if I would've saved time on assignment 2's code if I used Linked List for the Stack and Queue in Depth-first search and Breadth-first search. This is because one of the things that I claimed in week 5 was that it is much faster in for large lists. Specifically, I claimed O(n) growth for a Linked List based Stack and Queue contrasting to O(n²) for the list based which I used.
I don't think this mattered too much though since I ended up using deque for the Stacks and Queue and it likely used some kind of efficient data structure. Perhaps even Linked Lists!
My view has changed to prefer just using a normal list for Queues and Stacks rather than Linked Lists. The reason for this is that we have to import or implement the Linked List class into whatever project we are working on rather than just using the inbuilt list. Basically, I am too lazy to import it for assignments, since we are required to upload specific files.
I do wonder if I would've saved time on assignment 2's code if I used Linked List for the Stack and Queue in Depth-first search and Breadth-first search. This is because one of the things that I claimed in week 5 was that it is much faster in for large lists. Specifically, I claimed O(n) growth for a Linked List based Stack and Queue contrasting to O(n²) for the list based which I used.
I don't think this mattered too much though since I ended up using deque for the Stacks and Queue and it likely used some kind of efficient data structure. Perhaps even Linked Lists!
Sunday, 27 March 2016
Week 10 SLOG - Sorting
During this week, we learned about sorting algorithms and running time. The two are related since running time is affected by the sort algorithm and the differences in running time become noticeably in large lists.
When we talk about running time, we are talking about time as a function of the number of items in the list. The simplest way running time can be reduced is by limiting the number of helper function calls. The example we were shown was using a variable or for loop instead of a while loop if we are going through the whole list.
len(n) is called n times, this is an extremely large amount of calls
i = 0
while i != len(n):
(search stuff)
i += 1
len(n) is called only once but n comparisons are made. The comparison is another helper function so this could still be refined.
i = 0
length = len(n)
while i != length:
(search stuff)
i += 1
len(n) is only called once in this code and no comparisons need to be made. We saved 2n function calls by using a for loop which is significant for large lists.
for i in len(n):
(search stuff)
While these save time, the biggest difference in running time will actually be in the algorithm. This is because the fastest unordered list sorting algorithms run with O(n*logn) running time which grows faster than that 2n we saved.
Below are example algorithms we covered in lab and their running time.
Example Algorithms:
Bubble sort: O(n²) running time.
Selection sort: O(n²) running time.
Insertion sort: O(n²) running time.
Merge sort: O(n*logn) running time.
Quick sort: O(n*logn) running time.
Python inbuilt (Tim sort): O(n*logn) running time.
Sunday, 20 March 2016
Week 9 SLOG - Binary Tree
Binary Trees are a special kind of Tree that can only have up to two children. This restriction allows interesting applications such as Binary Search Trees and also makes us have to deal with the code of Binary Trees in a different way.
One thing to note is that Binary Trees can truly be None unlike standard Trees. So we have to check.
In general, we write Binary Tree functions as:
One thing to note is that Binary Trees can truly be None unlike standard Trees. So we have to check.
In general, we write Binary Tree functions as:
def binary_tree_function(tree):
if tree is not None:
# We need to check
if tree.left is None and tree.right is None:
# This is a leaf. Do something
else:
# Do recursion
left = tree_function(tree.left, foo)
# Sometimes we have to do something for the current node too. Do it between the two!
right = tree_function(tree.right, foo)
else:
# Return something appropriate for None
Saturday, 12 March 2016
Week 8 SLOG - Tree
We spent the last two weeks on trees, specifically generic trees a week ago and binary trees during this week. I'll focus on generic trees this week as its fresh in my mind after working on assignment 2.
A Tree is an ADT that consists of the root and possibly internal nodes and leafs. The root is the first node in the Tree and the only node without a parent. Each node can be linked to several other nodes which are called its children. In fact, a LinkedList is a type of Tree with only one child. Since a Tree branches out, it naturally lends itself to recursion for traversing the Tree.
The Tree also can be thought as to contain subtrees as each of its children can have their own children and so on. Some attributes for this are arity, height, path length and depth. The arity is the maximum branching factor of the tree, so the max number of children a node in the tree has. The path length is the number of nodes between a given node and one of its descendants (children, children of children, etc) The height of a tree is 1 + the maximum path length in the tree. The depth is the height of the entire tree minus the height of the given node, which is 0 at the root.
In general for recursive functions of a Tree we do:
def tree_function(tree):
if not tree.children:
# Do something since this is a leaf!
else:
# This is an internal node
# Do something
tree_function(foo) # Do recursion
Wednesday, 2 March 2016
Week 7 SLOG - Recursion
Hello! In this SLOG, I'll be talking about recursion.
Firstly, what is recursion? In computer science, recursion
is when a function calls itself during its execution. The purpose of recursion is
to iterate over heavily nested lists. Recursion helps to reduce code complexity
since it can reuse itself to go deeper into a nested list. This is helpful when
the solution is to do the same thing in various depths of a list.
Before I talk more about recursion, I need to talk about
nested lists and about the depth of a list. The depth of a list is the maximum
number of lists within a list. We describe a non-nested list as having depth 1
and call it a flat list. So the list
[ [] ] is depth 2, while [ [[], []], [] ] is depth 3.
Recursion is a concept that is hard to imagine. I initially
thought that sum_list([]) would have resulted in an infinite loop. This could've
been avoided if I read the code without being primed to think about the
possibility of infinite loops. After being corrected, it became quite obvious
that recursion is just another tool and there is a general way to make it work.
You just need to have a clause for the general case to do recursion and a
clause for when you've reached the deepest part of a list or found what you
were looking for. Recursion could also be avoided by using an implementation of
loops.
The general form of recursion is:
if (condition to detect a base case):
(do something
without recursion)
else:
(do recursion)
Recursion is different from what I expected back in week 3. My
expectation was that we would just call the function in the body. The
implementation of recursion in class uses an if structure which allows us to avoid infinite loops. This is
because we will only do recursion if the base case is not met. In addition, we
usually iterate over the elements of the list for the general case so that it
will eventually reach the deepest level or fulfill the base case.
Sunday, 28 February 2016
Week 6 SLOG - Classes
It seems that I'm still being affected by reading week procrastination. In this SLOG, I'll be talking about Classes. I know that we were learning recursion in Week 6 but I'm saving that for Week 7's SLOG. This is because there are 3 topics we can do for Week 7, recursion, object-oriented programming, and ADTs. I've already done ADTs and object-oriented programming is a vast topic.
Anyways my best interpretation of a class is: "Classes are constructs that have a set of defined methods, such that any objects instantiated from this class will have access to those methods."
The methods that classes have are just functions that can be accessed with class_name.method_name(foo). In addition, there are special methods which Python can call for special operators. Special methods are denoted with "__" in front and in the back of the method name. Classes should at minimum have __init__, __eq__, and __str__ special methods.
Special methods:
__init__: Instructions on how to instantiate an object of the class
__eq__: Compares two objects and determines if they are the same
__str__: The str representation of an object of the class
There are also public and private methods. Private methods are denoted with "_" in front of the method name. There is actually no protection from someone calling them but serves to tell a reader that it should not be called.
Classes are useful since they provide a common API for various things that may need it. This saves a lot of rewriting code when they are inherited. This is called inheritance and will probably be talked about in a later SLOG.
Subscribe to:
Posts (Atom)