Friday, October 31, 2014

CSC165 Counting steps, worst-case, formal big-Oh



Week #8
Topic: counting steps, worst-case, formal big-Oh

The concept of big-Oh is very abstract, but I realized a few things that were really helpful to understand it. I would like to share some of them here.

Working with while loops

A while loop is a loop of the following form:

i = 1    # starting position
while (condition) : # when condition is true the loop executes
            body # The expressions to be repeated
            i += 1 # Increment of the loop

When working with big-Oh problems, we focus our attention on the condition. Let’s try a few examples:

1) Condition:  i  <  len(A). In this case, the loop will execute len(A) – 1 times

2) Condition:  i  <= len(A). In this case, the loop will execute len(A) times

The same works for i > len(A) and  i  >= len(A) when we are talking about a decrement, instead of an increment.

Nested Loops

a =  [[1, 2], [2, 3]]

1          i = 0
2          while i < len(a):
3                      j = 0
4                      while  j < len(a[i]):
5                                  print(a[i][j]|)
6                                  j += 1
7                      i += 1 # Increment of the loop

In this case, line 1 executes one time, line 2 executes len(a) +1 times, lines 3 and 7 execute
len(a) times, lines 5 and 6 execute (len(a) – 1)(len(a)) times because i and j start at 0, and line 4 executes (len(a))(len(a)+1) times.

This looks tricky at the beginning but the rule is that for any condition, we have to take the starting point (0), and subtract it from the first value that makes the condition false (len(a)). The result of this subtraction is going to be the number of times the code within the loop executes. As for the line with the while statement, this line executes one more time because it checks for the last possible value (len(a)).

I found this way and especially this example easier to understand, so I took them then as my starting point, and from there I was able to understand the concept of big Oh.

Monday, October 27, 2014

CSC 165 Sorting algorithm complexity, big-Oh

Week #7
Topic: Sorting algorithm complexity, big-Oh


Something really useful about this week was that we were presented with the different inferences that we are allowed to use when trying to prove conjectures. Existential instantiation was one of those inferences that did not make sense to me, but now I understand that what we are doing is a replacement of variables. Basically, we do not use the main variable during the process, just in our conclusion. I thought of this specific inference as not useful, but rather fancy; however, I realized that it adds a sense of order to our proof.

Speaking of order, sorting algorithms was another topic that caught my attention this week. There is a considerable number of algorithms and it seems to me that we should focus our attention in the most efficient ones. Thus, I surfed the Internet and found out that Merge sort, Heapsort and Quicksort are considered the most efficient algorithms.

The following video gives us a sense of how different, different sorting algorithms can be. https://www.youtube.com/watch?v=kPRA0W1kECg

And this video talks about Tim sort, which is the sorting algorithm used in python.

When looking at these videos it came to my attention the term binary search threes, and I found a very useful article that includes the history of these threes and how to make them more efficient. You can find them through the U of T library website by searching for efficient algorithms. This link gives you direct access:
 http://getit.library.utoronto.ca/index.php/access?http://myaccess.library.utoronto.ca/login?url=http://books.scholarsportal.info/viewdoc.html?id=/ebooks/ebooks0/springer/2010-04-28/1/9783642034565

Videos:

Algorithms


Tim Sort








Friday, October 17, 2014

CSC165 Cases, multiple quantifiers, limits

Week #6
Topic: Cases, multiple quantifiers, limits

I found really interesting both the proof by cases and the proof about limits; therefore, I would like to use this space to practice and enhance the knowledge about these two interesting proofs.

Proof by cases
I realized that in this kind of proofs we are considering all the natural numbers by dividing them in odd and even numbers.
In other words: = {x: k , x = 2k + 1} U {x: k , x = 2k}

Now, let’s try to proof that n , 3n2 + n +15 is odd

Definitions: n is odd when k , n = 2k + 1 and n is even when k , n = 2k

Assume n #Typical

            Case1: Assume n is odd, n = 2k + 1
                         Then 3n2 + n +15 = 3(2k + 1)2 + (2k + 1) + 1
                                                          = [3(4k2 + 4k +1)] + 2k + 2
                                                          = 12k2 + 12k + 3 +2k + 2
                                                          = 12k2 +14k + 5
                                                          = 12k2 +14k + 4 + 1 # 5 = 4 + 1
                                                          = 2(6k2 +7k + 2) + 1
                        Then k0 , n = 2 k0 + 1 # k0 = 6k2 +7k + 2
                        Then 3n2 + n +15 is odd #by definition

            Case2: Assume n is even, n = 2k
                         Then 3n2 + n +15 = 3(2k)2 + (2k) + 1
                                                          = [3(4k2)] + 2k + 1
                                                          = 12k2 + 2k + 1
                                                          = 2(6k2 + k) + 1
                        Then k0 , n = 2 k0 + 1 # k0 = 6k2 + k
                        Then 3n2 + n +15 is odd #by definition
            Then 3n2 + n +15 is odd #since in both cases this was true

Conclude n , 3n2 + n +15 is odd

Now, let’s try to prove that  the limit of 3x - 1 as x approaches 2 is 5

In this case, we apply a concept very similar to the delta-epsilon proof.

e , d , x , |x - |<d |(3x - 1) - 5| < e

Assume e #Generic x
            Pick  d = e/3
                        Assume |x - 2|<d
                        Then |(3x - 1) - 5| = |3x - 6| = |3 (x - 2)| < 3d = e   # |x - 2|<d
   # |(3x - 1) - 5| < e
                        Then x , |x - 2|<d |(3x - 1) - 5| < e
            Then d , x , |x - 2|<d |(3x - 1) - 5| < e

Conclude  e , d , x , |x - 2|<d |(3x - 1) - 5| < e