Pages

Sunday, 18 March 2012

Optimization of News Feed in Facebook


The Facemash algorithm that I had posted here was just a “trailer” of this actual post for starting understanding Facebook.

Facebook used basically a data structure,called graph.Graph includes two basic things:

1.Nodes with their corresponding value

2.Edges between nodes with their corresponding weight

Whatever you observe in Facebook mainly is always the News Feed, which is the most important feature of Facebook.

So it is always needed  to carefully choose which posts to publish on the first page of your News Feed. So the main component of your News Feed and at last of your Facebook page is optimization of News Feed.

Below photo shows formula used by Facebook to optimize the News Feed of Facebook profile.



As shown in above photo, the friends and relationships and post by any one of them are based on three parameters:

1.Weight of edge type

2. Time from which they were friends and how ago the post was posted on Facebook

3. How closer the friends i.e. in same school or just a fan of other friend

Thus,the sum of product of all your friends and “weight” of your relationships with them is what you see in the News Feed of your Facebook profile.

So the better you are friends (it can be known by a simple Facebook app,whose making we will discuss later)  the more are the chances that your post will be on the News Feed of other one. Similarly , the more you interact,more is the chance is there that you are featured on the News Feed of your other friend.


 Four Your Information(FYI):

Depending on your number of friends and their activity, a click to your News Feed can call up as many as 10,000 stories that account for about 8 megabytes of space. All this occurs in about seven-tenths of a second.

Sunday, 11 March 2012

Algorithm for facemash


You must be remembering this scene from The Social Network movie,when Eduardo Saverin writes an algorithm for making facemash.com.

Actually,the algorithm is a simple algorithm to rank the players of chess,named Elo rating System.According to wiki:


The Elo rating system is a method for calculating the relative skill levels of players in two-player games such as chess.

Algorithm:

According to this algorithm following represents the expected score of for example,Player A








Similarly the expected score for example,Player B is:


Where RA is current rating of Player A and RB is current rating of player B.

When player A will play the match whatever score he has,it will be compared with the expected score and that will give a new rating for player A. This will be done using the below formula;




Where SA is the actual score of player A. K is a constant that has two values for Master players K=16 and for weaker players K will be 32.

Let us take an example of rating girls as done in the Social Network movie. For example we have the following table:


Girl Current Rating Result Score


A


80




B


90
Lost 0


C


30
Won 1


D


50
Draw 0.5


E


40
Won 1


Now as you can see clearly that A was compared with 4 girls and she has scored 2.5 points. If you calculate the expected score using the above formula it will be as follows :
                    
                     2.157(0.485+0.572+0.543+0.557).

Now using the second formula given above we will calculate the new Rating for player A  using K = 16  will be

                     R’A = 80 + 16(2.5-2.157) = 85.2

That's all about the algorithm.Anything more should be reflected in comments.




Friday, 2 March 2012

Build a Server on your computer using Python

To make a "home-made" server on your computer is a very easy thing,but its tough to make it with satisfying the following requirements :

1.Without the use of Apache,IIS,Nginx,Lighttpd

2.Using open-source

3.With lot of in-built facilities

This seems like dreamy but you have another way of doing it using the world's best open-source scripting-cum-programming language Python it makes it very easy.By the way,Facebook runs mostly on Python.

If you find that you do not know much about Python,no problem.Just write the four lines below,and a "home-made" server would be built.This will help in making your computer an efficient server.

Initially,you must have Python installed.You can download it from following links,copy the link and paste in the browser (prefer to install the version 3.2):

http://python.org/download/

After the installation,do following.

Open a notepad file and type the following code there:

from http.server import HTTPServer, CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

Save it and remember the location of it.

Thereby,

For Windows user

1.Go to Start -->All Programs-->Python 3.2-->IDLE

2.Press Ctrl+O to open the file saved above.

3.Press F5.






















Above window with message "Staring simple_httpd on port 8080" shows success of our work.

For Unix And Mac OS X Users

You need to do two things to prepare your CGI script for execution:

1. Set the executable bit for your CGI using the chmod +x command.
2. Add the following line of code to the very top of your program:
#! /usr/local/bin/python3

Thereby do another thing:

1. Open Terminal (or Shell).
2. Type python <filename of  file created above with full path>

That's it,server is running on your *NIX OS.


That's all how a simple "home-made" Python server is made and run,simply with above 4 steps.Send your queries below in the comments.