Monday, 15 June 2015

Friday, 9 January 2015

11 Useful Cloud Computing Tutorials

Cloud computing is the future, you may have heard someone say this before. Here are 11 tutorials that will get you through various aspects of dealing with the cloud.


 



 1. Using Google Weather API In A C# Application http://www.dreamincode.net/forums/topic/153911-c%23using-google-weather-api-in-a-c%23-application/
This one teaches you the basics of getting the weather with Google weather API.
2. Using Tropo & Python To Have The Phone Tell Us The Weather
http://www.dreamincode.net/forums/topic/305319-using-tropo-python-to-have-the-phone-tell-us-the-weather/ 
According to the user, this tutorial teaches you to set up an automatic phone system and, using Python, design it to answer the phone and tell you the weather in your area based on the zip code you give the program.
3. FaceBook FBML Ajax / PHP Interaction
http://www.dreamincode.net/forums/topic/188887-facebook-fbml-ajax-php-interaction/ 
Did you think HTML is the only markup language that you need to know about? Well, think again. This tutorial deals with three languages, FBML (Facebook Markup Language), PHP and Ajax.
4. Faces In The Cloud: High-Throughput Data Processing W / Message Queues
http://www.dreamincode.net/forums/topic/248319-faces-in-the-cloud-high-throughput-data-processing-w-message-queues/ 
Want to set up a grid computing cluster that can leverage a high-performing message queue for arbitration? Here is a user teaching you just how to do so.
5. Scraping Tweets Using Python And Flask
http://www.dreamincode.net/forums/topic/248319-faces-in-the-cloud-high-throughput-data-processing-w-message-queues/ 
Most scripting languages are good at scraping information of a web page, but Python is exceptionally good at it. So, this tutorial takes the Python programming language and the Flask micro-framework and teaches you how to scrape tweets.
6. Icon Using Python And Google Translate
http://www.dreamincode.net/forums/topic/236280-scraping-tweets-using-python-and-flask/ 
This tutorial covers three things. First, it teaches you how to use Python to write a simple script for translating text to be used in other applications. Second, you will learn to scrape HTML using BeautifulSoup and lastly, using the Google AJAX service for translation purposes.
7. Google Data Java Client API
http://www.dreamincode.net/forums/topic/188428-javagoogle-data-java-client-api/ 
The Google Data Protocol is an application package interface designed by Google, which in turn allows developers to interact with various Google functions, like docs etc.
8. [PHP] Building Automated Twitter Bot With Twitters API Functionality
http://www.dreamincode.net/forums/topic/108559-php-building-automated-twitter-bot-with-twitters-api-functionality/ 
This tutorial teaches you how to make a bot for Twitter using the PHP programming language.
9. Facebook PHP API And XFBML On Iframe
http://www.dreamincode.net/forums/topic/153919-facebook-php-api-and-xfbml-on-iframe/ 
You need to understand PHP, HTML and JavaScript for this one. It teaches you how to make a Facebook application using PHP and XFBML.
10. Using The Python Twitter API
http://www.dreamincode.net/forums/topic/150776-using-the-python-twitter-api/ 
This tutorial teaches you how to write a basic Twitter client using the Python Twitter API.
11. [C#] Twitter API: Get User Timeline
http://www.dreamincode.net/forums/topic/114928-c%23-twitter-api-get-user-timeline/ 
This user generated tutorial gets you started with building a .Net library for the Twitter API.

Thursday, 11 December 2014

Algorithms- What does ‘Space Complexity’ mean?

Space Complexity:

Following are the correct definitions of Auxiliary Space and Space Complexity.

Auxiliary Space is the extra space or temporary space used by an algorithm.

Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input.

For example, if we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criteria than Space Complexity. Merge Sort uses O(n) auxiliary space, Insertion sort and Heap Sort use O(1) auxiliary space. Space complexity of all these sorting algorithms is O(n) though.

Algorithms- Solving Recurrences

Many algorithms are recursive in nature. When we analyze them, we get a recurrence relation for time complexity. We get running time on an input of size n as a function of n and the running time on inputs of smaller sizes. For example in Merge Sort, to sort a given array, we divide it in two halves and recursively repeat the process for the two halves. Finally we merge the results. Time complexity of Merge Sort can be written as T(n) = 2T(n/2) + cn. There are many other algorithms like Binary Search, Tower of Hanoi, etc.
There are mainly three ways for solving recurrences.
1) Substitution Method: We make a guess for the solution and then we use mathematical induction to prove the the guess is correct or incorrect.
For example consider the recurrence T(n) = 2T(n/2) + n

We guess the solution as T(n) = O(nLogn). Now we use induction
to prove our guess.

We need to prove that T(n) <= cnLogn. We can assume that it is true
for values smaller than n.

T(n) = 2T(n/2) + n
    <= cn/2Log(n/2) + n
    =  cnLogn - cnLog2 + n
    =  cnLogn - cn + n
    <= cnLogn
2) Recurrence Tree Method: In this method, we draw a recurrence tree and calculate the time taken by every level of tree. Finally, we sum the work done at all levels. To draw the recurrence tree, we start from the given recurrence and keep drawing till we find a pattern among levels. The pattern is typically a arithmetic or geometric series.
For example consider the recurrence relation 
T(n) = T(n/4) + T(n/2) + cn2

           cn2
         /      \
     T(n/4)     T(n/2)

If we further break down the expression T(n/4) and T(n/2), 
we get following recursion tree.

                cn2
           /           \      
       c(n2)/16      c(n2)/4
      /      \          /     \
  T(n/16)     T(n/8)  T(n/8)    T(n/4) 
Breaking down further gives us following
                 cn2
            /            \      
       c(n2)/16          c(n2)/4
       /      \            /      \
c(n2)/256   c(n2)/64  c(n2)/64    c(n2)/16
 /    \      /    \    /    \       /    \  

To know the value of T(n), we need to calculate sum of tree 
nodes level by level. If we sum the above tree level by level, 
we get the following series
T(n)  = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + ....
The above series is geometrical progression with ratio 5/16.

To get an upper bound, we can sum the infinite series. 
We get the sum as (n2)/(1 - 5/16) which is O(n2)
3) Master Method:
Master Method is a direct way to get the solution. The master method works only for following type of recurrences or for recurrences that can be transformed to following type.
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
There are following three cases:
1. If f(n) = \Theta \left(n^{{c}}\right) where c < \log _{b}a then T(n) = \Theta \left(n^{{\log _{b}a}}\right)
2. If f(n) = \Theta \left(n^{{c}}\right) where c = \log _{b}a then T(n) = \Theta \left(n^{{c}}logn\right)
3.If f(n) = \Theta \left(n^{{c}}\right) where c > \log _{b}a then T(n) = \Theta \left(f(n)\right)
How does this work?
Master method is mainly derived from recurrence tree method. If we draw recurrence tree of T(n) = aT(n/b) + f(n), we can see that the work done at root is f(n) and work done at all leaves is \Theta \left(n^{{c}}\right) where c is \log _{b}a. And the height of recurrence tree is \log _{b}n
Master Theorem
In recurrence tree method, we calculate total work done. If the work done at leaves is polynomially more, then leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done at leaves and root is asymptotically same, then our result becomes height multiplied by work done at any level (Case 2). If work done at root is asymptotically more, then our result becomes work done at root (Case 3).
Examples of some standard algorithms whose time complexity can be evaluated using Master Method
Merge Sort: T(n) = 2T(n/2) + \Theta(n). It falls in case 2 as c is 1 and \log _{b}a is also 1. So the solution is \Theta(nLogn)
Binary Search: T(n) = T(n/2) + \Theta(1). It also falls in case 2 as c is 0 and \log _{b}a is also 0. So the solution is \Theta(Logn)
Notes:
1) It is not necessary that a recurrence of the form T(n) = aT(n/b) + f(n) can be solved using Master Theorem. The given three cases have some gaps between them. For example, the recurrence T(n) = 2T(n/2) + n/Logn cannot be solved using master method.
2) Case 2 can be extended for f(n) = \Theta \left(n^{{c}}\log ^{{k}}n\right).
If f(n) = \Theta \left(n^{{c}}\log ^{{k}}n\right) for some constant k >= 0 and c = \log _{b}a, then T(n) = \Theta \left(n^{{c}}\log ^{{k+1}}n\right)

Algorithms- Analysis of Loops

We have discussed Asymptotic Analysis,  Worst, Average and Best Cases  and Asymptotic Notations in previous posts. In this post, analysis of iterative programs with simple examples is discussed.
1) O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function.
   // set of non-recursive and non-loop statements
For example swap() function has O(1) time complexity.
A loop or recursion that runs a constant number of times is also considered as O(1). For example the following loop is O(1).
   // Here c is a constant   
   for (int i = 1; i <= c; i++) {  
        // some O(1) expressions
   }

2) O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. For example following functions have O(n) time complexity.
   // Here c is a positive integer constant   
   for (int i = 1; i <= n; i += c) {  
        // some O(1) expressions
   }

   for (int i = n; i > 0; i -= c) {
        // some O(1) expressions
   }

3) O(nc): Time complexity of nested loops is equal to the number of times the innermost statement is executed. For example the following sample loops have O(n2) time complexity
  
   for (int i = 1; i <=n; i += c) {
       for (int j = 1; j <=n; j += c) {
          // some O(1) expressions
       }
   }

   for (int i = n; i > 0; i += c) {
       for (int j = i+1; j <=n; j += c) {
          // some O(1) expressions
   }
For example Selection sort and Insertion Sort have O(n2) time complexity.

4) O(Logn) Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount.
   for (int i = 1; i <=n; i *= c) {
       // some O(1) expressions
   }
   for (int i = n; i > 0; i /= c) {
       // some O(1) expressions
   }
For example Binary Search(refer iterative implementation) has O(Logn) time complexity.

5) O(LogLogn) Time Complexity of a loop is considered as O(LogLogn) if the loop variables is reduced / increased exponentially by a constant amount.
   // Here c is a constant greater than 1   
   for (int i = 2; i <=n; i = pow(i, c)) { 
       // some O(1) expressions
   }
   //Here fun is sqrt or cuberoot or any other constant root
   for (int i = n; i > 0; i = fun(i)) { 
       // some O(1) expressions
   }
See this for more explanation.

How to combine time complexities of consecutive loops?
When there are consecutive loops, we calculate time complexity as sum of time complexities of individual loops.
   for (int i = 1; i <=m; i += c) {  
        // some O(1) expressions
   }
   for (int i = 1; i <=n; i += c) {
        // some O(1) expressions
   }
   Time complexity of above code is O(m) + O(n) which is O(m+n)
   If m == n, the time complexity becomes O(2n) which is O(n).   

How to calculate time complexity when there are many if, else statements inside loops?
As discussed here, worst case time complexity is the most useful among best, average and worst. Therefore we need to consider worst case. We evaluate the situation when values in if-else conditions cause maximum number of statements to be executed.
For example consider the linear search function where we consider the case when element is present at the end or not present at all.
When the code is too complex to consider all if-else cases, we can get an upper bound by ignoring if else and other complex control statements.

How to calculate time complexity of recursive functions?
Time complexity of a recursive function can be written as a mathematical recurrence relation. To calculate time complexity, we must know how to solve recurrences. We will soon be discussing recurrence solving techniques as a separate post.