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.
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.
Saturday, 13 February 2016
Week 5 SLOG - Linked Lists
We actually didn't learn much this week since Monday was spent to discuss Wednesday's test and assignment 1. As stated, Wednesday was the test. This week's lab and Friday's class were the only instructional classes which we covered Linked Lists. The type of Linked List I'll talk about in this SLOG are singly Linked Lists.
The Linked List is set of nodes. Each node contains its value and the reference to the next node in the Linked List. This allows the nodes to be linked as if it was a list.
The benefit of a Linked List is that it avoids Python's directive to allocate large blocks of adjacent memory for long lists. This means that a Linked List should be able to be extended indefinitely until the computer runs out of memory as its nodes can be widely dispersed throughout the system. It also is beneficial when the list is going to be modified as removing an object from a standard Python List will shift every object that appears after the removed object one index forward. This makes it better than a List in implementing a Queue.
The cons of a Linked List include using more memory for each object in the list as it must also have a reference to the next node in the Linked List.
The benefits were best illustrated in Friday's presentation of a Queue and Stack implemented with List and a Queue and Stack implemented with Linked List. The running time of the Linked List implemented ADTs grew linearly with size increase while the running time of a Queue implemented with List grew in a quadratic manner in relation to size. The List implemented Stack grew in a linear fashion.
The benefit is seen only with large lists so I wonder if we'll be learning about asymptotic running time after reading week. In other news: there wasn't enough time for the test!!!
The Linked List is set of nodes. Each node contains its value and the reference to the next node in the Linked List. This allows the nodes to be linked as if it was a list.
The benefit of a Linked List is that it avoids Python's directive to allocate large blocks of adjacent memory for long lists. This means that a Linked List should be able to be extended indefinitely until the computer runs out of memory as its nodes can be widely dispersed throughout the system. It also is beneficial when the list is going to be modified as removing an object from a standard Python List will shift every object that appears after the removed object one index forward. This makes it better than a List in implementing a Queue.
The cons of a Linked List include using more memory for each object in the list as it must also have a reference to the next node in the Linked List.
The benefits were best illustrated in Friday's presentation of a Queue and Stack implemented with List and a Queue and Stack implemented with Linked List. The running time of the Linked List implemented ADTs grew linearly with size increase while the running time of a Queue implemented with List grew in a quadratic manner in relation to size. The List implemented Stack grew in a linear fashion.
The benefit is seen only with large lists so I wonder if we'll be learning about asymptotic running time after reading week. In other news: there wasn't enough time for the test!!!
Saturday, 6 February 2016
Week 4 SLOG - Abstract Data Types
This week we learned about Stacks and Queues and Abstract Data Types. At first, Stacks seemed rather useless as they operate in the Last-In-First-Out (LIFO) order. Then Danny Heap brought up in class that stacks are used for coding programs itself, as they are used to add and resolve function calls. This was probably quite obvious but I didn't realize this. In hindsight, the python visualizer shown in CSC108 was definitely showing Stacks.
The Queue is similar but instead it operates in a First-In-First-Out (FIFO) order and is the one that is very prevalent in the real world. With Tim Hortens lines (or any retail line) being a prime example. Both of these are called Abstract Data Types (ADT) which are when we represent real world phenomena in the computer world.
Some other common ADTs include Lists, Sacks, and Dictionaries. Lists and Dictionaries are inbuilt classes. Sack is not inbuilt and represents a sack that you throw things into such that you take things out randomly.
The implementation of most of the new ADTs can be done with various inbuilt classes such as List, Tuple, and Dictionary. In class, Stack was implemented as a List for simplicity and for the already inbuilt tools. Sack was implemented as a dictionary to take advantage of the non-ordered nature. Personally, I prefer using List due to familiarity and the useful tools .append(), and .pop().
To sum up, the list of ADTs this week are:
- List: Represents a set of objects in which any object in the List can be accessed and new objects can be added anywhere. Real world example: A list!
- Dictionary: Represents a set of objects in which a key is needed to access an object.
Real world example: A dictionary.
- Stack: Represents a set of objects in which only the most recent object can be accessed.
Real world example: A stack of paperwork.
- Queue: Represents a set of objects in which only the least recent object can be accessed.
Real world example: A checkout line.
- Sack: Represents a set of objects in which produces a random object in the set when removing. Real world example: A lottery draw.
I'll probably add LinkedList next week, once we're done exploring it.
The Queue is similar but instead it operates in a First-In-First-Out (FIFO) order and is the one that is very prevalent in the real world. With Tim Hortens lines (or any retail line) being a prime example. Both of these are called Abstract Data Types (ADT) which are when we represent real world phenomena in the computer world.
Some other common ADTs include Lists, Sacks, and Dictionaries. Lists and Dictionaries are inbuilt classes. Sack is not inbuilt and represents a sack that you throw things into such that you take things out randomly.
The implementation of most of the new ADTs can be done with various inbuilt classes such as List, Tuple, and Dictionary. In class, Stack was implemented as a List for simplicity and for the already inbuilt tools. Sack was implemented as a dictionary to take advantage of the non-ordered nature. Personally, I prefer using List due to familiarity and the useful tools .append(), and .pop().
To sum up, the list of ADTs this week are:
- List: Represents a set of objects in which any object in the List can be accessed and new objects can be added anywhere. Real world example: A list!
- Dictionary: Represents a set of objects in which a key is needed to access an object.
Real world example: A dictionary.
- Stack: Represents a set of objects in which only the most recent object can be accessed.
Real world example: A stack of paperwork.
- Queue: Represents a set of objects in which only the least recent object can be accessed.
Real world example: A checkout line.
- Sack: Represents a set of objects in which produces a random object in the set when removing. Real world example: A lottery draw.
I'll probably add LinkedList next week, once we're done exploring it.
Wednesday, 27 January 2016
Week 3 SLOG - Intro
Greetings! This SLOG (courSe LOG) is a blog for CSC148 that will be updated weekly throughout the semester. Personally, whenever I hear SLOG, I think of winter and slogging through the snow. Anyways, our first post is on the course's topics and the goals of this SLOG.
CSC148's course topics primarily appears to be about modeling and solving real-world problems, and higher level program design. My first overall impression of the topics was that CSC148 is just the advanced form of CSC108. In my opinion, this should hold true for some topics such as exceptions, and design, as these are just expanding on what we learned in CSC108 (testCases, class design). On the other hand, recursion is a new topic and seems prone to infinite loops based on its description. ADTs has just been described this week and seems to be about the idea of modeling real world ideas as a data type on a computer. Finally, efficiency was not a factor we placed much emphasis on, having only talked about it briefly for sorting algorithms and thus will be something to learn.
The goals of this SLOG are as follows:
CSC148's course topics primarily appears to be about modeling and solving real-world problems, and higher level program design. My first overall impression of the topics was that CSC148 is just the advanced form of CSC108. In my opinion, this should hold true for some topics such as exceptions, and design, as these are just expanding on what we learned in CSC108 (testCases, class design). On the other hand, recursion is a new topic and seems prone to infinite loops based on its description. ADTs has just been described this week and seems to be about the idea of modeling real world ideas as a data type on a computer. Finally, efficiency was not a factor we placed much emphasis on, having only talked about it briefly for sorting algorithms and thus will be something to learn.
The goals of this SLOG are as follows:
- Making me review my notes, including each week's subjects and concepts
- Hopefully increase my writing skills for another class' essays
- Provide yet-another-first-year-comp-sci-student™ perspective on the course's subjects
Subscribe to:
Posts (Atom)