Saturday, December 3, 2011

Complexity and Big-O Notation

  • Complexity and Big-O Notation
Be careful to differentiate between:

Performance: how much time/memory/disk/... is actually used when a program is run. This depends on the machine, compiler, etc. as well as the code.
Complexity: how do the resource requirements of a program or algorithm scale, i.e., what happens as the size of the problem being solved gets larger.

Complexity affects performance but not the other way around


Big-O Notation

We express complexity using big-O notation. For a problem of size N:

constant-time method is "order 1": O(1)
linear-time method is "order N": O(N)
quadratic-time method is "order N squared": O(N2)

How to Determine Complexities

In general, how can you determine the running time of a piece of code? The answer is that it depends on what kinds of statements are used.

1-Sequence of statements
statement 1;
statement 2;
  ...
statement k;

If each statement is "simple" (only involves basic operations) then the time for each statement is constant and the total time is also constant: O(1).

2-if-then-else statements

Here, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case time is the slowest of the two possibilities: max(time(sequence 1), time(sequence 2)). For example, if sequence 1 is O(N) and sequence 2 is O(1) the worst-case time for the whole if-then-else statement would be O(N)

3-for loops

for (i = 0; i < N; i++) {
    sequence of statements
}
The loop executes N times, so the sequence of statements also executes N times. Since we assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N) overall.


4-Nested loops
First we'll consider loops where the number of iterations of the inner loop is independent of the value of the outer loop's index. For example:

for (i = 0; i < N; i++) {
    for (j = 0; j < M; j++) {
        sequence of statements
    }
}
The outer loop executes N times. Every time the outer loop executes, the inner loop executes M times. As a result, the statements in the inner loop execute a total of N * M times. Thus, the complexity is O(N * M). In a common special case where the stopping condition of the inner loop is j < N instead of j < M (i.e., the inner loop also executes N times), the total complexity for the two loops is O(N2).



Best-case and Average-case Complexity

Some methods may require different amounts of time on different calls, even when the problem size is the same for both calls. For example, consider the add method that adds an item to the end of the list. In the worst case (the array is full), that method requires time proportional to the number of items in the list (because it has to copy all of them into the new, larger array). However, when the array is not full, add will only have to copy one value into the array, so in that case its time is independent of the length of the list; i.e., constant time.


When do Constants Matter?

Recall that when we use big-O notation, we drop constants and low-order terms. This is because when the problem size gets sufficiently large, those terms don't matter. However, this means that two algorithms can have the same big-O time complexity, even though one is always faster than the other. For example, suppose algorithm 1 requires N2 time, and algorithm 2 requires 10 * N2 + N time. For both algorithms, the time is O(N2), but algorithm 1 will always be faster than algorithm 2. In this case, the constants and low-order terms do matter in terms of which algorithm is actually faster.



http://pages.cs.wisc.edu/~vernon/cs367/notes/3.COMPLEXITY.html




  • Algorithmic Complexity


Algorithmic complexity is concerned about how fast or slow particular algorithm performs. We define complexity as a numerical function T(n) - time versus the input size n



Asymptotic Notations

The goal of computational complexity is to classify algorithms according to their performances. We will represent the time function T(n) using the "big-O" notation to express an algorithm runtime complexity.



Constant Time: O(1)

An algorithm is said to run in constant time if it requires the same amount of time regardless of the input size. Examples:

array: accessing any element
fixed-size stack: push and pop methods
fixed-size queue: enqueue and dequeue methods



Linear Time: O(n)

An algorithm is said to run in linear time if its time execution is directly proportional to the input size, i.e. time grows linearly as input size increases. Examples:

array: linear search, traversing, find minimum
ArrayList: contains method
queue: contains method


Logarithmic Time: O(log n)

An algorithm is said to run in logarithmic time if its time execution is proportional to the logarithm of the input size. Example:
binary search

Quadratic Time: O(n2)

An algorithm is said to run in logarithmic time if its time execution is proportional to the square of the input size. Examples:
bubble sort, selection sort, insertion sort

Definition of "big Omega"
We need the notation for the lower bound.


Definition of "big Theta"
To measure the complexity of a particular algorithm, means to find the upper and lower bounds

Analysis of Algorithms

The term analysis of algorithms is used to describe approaches to the study of the performance of algorithms. In this course we will perform the following types of analysis:
the worst-case runtime complexity of the algorithm is the function defined by the maximum number of steps taken on any instance of size a.
the best-case runtime complexity of the algorithm is the function defined by the minimum number of steps taken on any instance of size a.
the average case runtime complexity of the algorithm is the function defined by an average number of steps taken on any instance of size a.
the amortized runtime complexity of the algorithm is the function defined by a sequence of operations applied to the input of size a and averaged over time.


Example. Let us consider an algorithm of sequential searching in an array.of size n.
Its worst-case runtime complexity is O(n)
Its best-case runtime complexity is O(1)
Its average case runtime complexity is O(n/2)=O(n)


http://www.cs.cmu.edu/~adamchik/15-121/lectures/Algorithmic%20Complexity/complexity.html

  • Sorting: Ep 07a - Big O Notation
http://www.youtube.com/watch?v=kRy8OlBAALk


  • Big-O notation.mp4

http://www.youtube.com/watch?v=WoKlmaP3dto



  • Algorithms: Big O, Growth Functions, and Worst Case Analysis (1/3)

http://www.youtube.com/watch?v=aPxauBqARGU&feature=relmfu



  • Big-Oh, Omega and Theta notation

http://www.youtube.com/watch?v=pe250F4ttmE





  • When solving a computer science problem there will usually be more than just one solution. These solutions will often be in the form of different algorithms, and you will generally want to compare the algorithms to see which one is more efficient. 

This is where Big O analysis helps – it gives us some basis for measuring the efficiency of an algorithm.
it measures the efficiency of an algorithm based on the time it takes for the algorithm to run as a function of the input size.

Basically, Big-O will want to express how many times the 'n' input items are 'touched'.
The word 'touched' can mean different things in different algorithms - in some algorithms it may mean the number of times a constant is multiplied by an input item, the number of times an input is added to a data structure, etc.

http://www.programmerinterview.com/index.php/data-structures/big-o-notation/




  •  Big Omega 

 Big O describes the upper (worst case) bound, and Big Omega describes the lower (best case) bound.
 Insertion Sort has an upper bound of O(n^2) and a lower bound of Omega(n)
 http://www.linuxquestions.org/questions/programming-9/big-o-big-omega-and-big-theta-106102/




  •  asymptotic bounds

 Big O, Big Omega, and Big Theta
 http://www.youtube.com/watch?v=6Ol2JbwoJp0
 http://xoax.net/comp_sci/crs/algorithms/lessons/Lesson6/



  •  Algorithms Lesson 7: Analyzing Bubblesort 

 http://www.youtube.com/watch?v=ni_zk257Nqo


 http://www.cs.auckland.ac.nz/compsci220s1t/lectures/lecturenotes/GG-lectures/BigOhexamples.pdf

 http://www.programmerinterview.com/index.php/data-structures/big-o-notation/

 http://www.cforcoding.com/2009/07/plain-english-explanation-of-big-o.html



  • This measure of efficiency is called asymptotic complexity,
and is used when dis-regarding certain terms of a function to express the efficiency of an algorithm
or when calculating a function is difficult or impossible and only approximations can be found.


The most commonly used notation for specifying asymptotic complexity—that is, for
estimating the rate of function growth—is the big-O notation

Given two positive-valued functions f and g,consider the following definition:
f(n) is O(g(n)) if there exist positive numbers cand Nsuch that f(n) =cg(n) for all n=N.

This definition reads:
f is big-O of g if there is a positive number c such that f is
not larger than cg for sufficiently large ns;
that is, for all ns larger than some number N.

The relationship between f and g can be expressed by stating either that g(n) is
an upper bound on the value of f(n) or that, in the long run,f grows at most as fast
as g.

Algorithms: Big O, Growth Functions, and Worst Case Analysis (1/3)

Big O Part 1 – Linear Complexity
describes the impact of increasing  the input data on the time taken for a program to run
big o space complexity
describes the impact of increasing the input data on the extra memory needed by the program


big o describes how the time is taken or how memory is used
big o describes complexity
common sense tells us that a program takes longer when there's more data to work on

linear search or sequential search
brute force approach
an item is searched in an unordered list
time is directly proportional to the amount of data  

linear search complexity
big o time complexity is linear, O(n)

Big O Part 2 – Constant Complexity
peek operations, peek over top item wo removing it
LIFO data structure
top is popped, the item is not removed, it is overwritten

no matter how much data is in the stack the time taken remains same
increasing the amount of data makes no difference to the time taken by push or pop operations
stack operations complexity
big o complexity is constant
O(1)

Big O Part 3 – Quadratic Complexity
the dominant term
n^3 is dominant to n^2

bubble sort
comparing,swapping
bubble sort complexity
for n data items, a simple implementation performs (n-1)*(n-1) operations
the dominant term is n^2
big o time complexity is quadratic, O(n^2)

enhanced bubble sort
for n data items, enhanced bubble sort algorithm performs (n-1)+(n-2)+(n-3).....+2+1
(n^2-n)/2
the dominant term is n^2, still O(n^2)
the big o complexity of enhanced bubble sort is still O(n^2)

alternative enhanced bubble sort

best vs worst case scenario
best case scenario, data is already sorted, O(n)
worst case scenario, data is in reverse order, O(n^2)

  • DAA - Analysis of Algorithms

In theoretical analysis of algorithms, it is common to estimate their complexity in the asymptotic sense, i.e., to estimate the complexity function for arbitrarily large input. The term "analysis of algorithms" was coined by Donald Knuth.

Algorithm analysis is an important part of computational complexity theory, which provides theoretical estimation for the required resources of an algorithm to solve a specific computational problem. Most algorithms are designed to work with inputs of arbitrary length. Analysis of algorithms is the determination of the amount of time and space resources required to execute it.

Usually, the efficiency or running time of an algorithm is stated as a function relating the input length to the number of steps, known as time complexity, or volume of memory, known as space complexity.

The Need for Analysis

In this chapter, we will discuss the need for analysis of algorithms and how to choose a better algorithm for a particular problem as one computational problem can be solved by different algorithms.

By considering an algorithm for a specific problem, we can begin to develop pattern recognition so that similar types of problems can be solved by the help of this algorithm.

Algorithms are often quite different from one another, though the objective of these algorithms are the same. For example, we know that a set of numbers can be sorted using different algorithms. Number of comparisons performed by one algorithm may vary with others for the same input. Hence, time complexity of those algorithms may differ. At the same time, we need to calculate the memory space required by each algorithm.

Analysis of algorithm is the process of analyzing the problem-solving capability of the algorithm in terms of the time and size required (the size of memory for storage while implementation). However, the main concern of analysis of algorithms is the required time or performance. Generally, we perform the following types of analysis −

    Worst-case − The maximum number of steps taken on any instance of size a.

    Best-case − The minimum number of steps taken on any instance of size a.

    Average case − An average number of steps taken on any instance of size a.

    Amortized − A sequence of operations applied to the input of size a averaged over time.

In this context, if we compare bubble sort and merge sort. Bubble sort does not require additional memory, but merge sort requires additional space. Though time complexity of bubble sort is higher compared to merge sort, we may need to apply bubble sort if the program needs to run in an environment, where memory is very limited.

https://www.tutorialspoint.com/design_and_analysis_of_algorithms/analysis_of_algorithms.htm

  • Asymptotic Notations and Apriori Analysis
Complexity of an algorithm is analyzed in two perspectives: Time and Space.

Time Complexity

It’s a function describing the amount of time required to run an algorithm in terms of the size of the input. "Time" can mean the number of memory accesses performed, the number of comparisons between integers, the number of times some inner loop is executed, or some other natural unit related to the amount of real time the algorithm will take.

Space Complexity

It’s a function describing the amount of memory an algorithm takes in terms of the size of input to the algorithm. We often speak of "extra" memory needed, not counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units to measure this.

Space complexity is sometimes ignored because the space used is minimal and/or obvious, however sometimes it becomes as important an issue as time.

Asymptotic Notations
Execution time of an algorithm depends on the instruction set, processor speed, disk I/O speed, etc. Hence, we estimate the efficiency of an algorithm asymptotically.
Different types of asymptotic notations are used to represent the complexity of an algorithm. Following asymptotic notations are used to calculate the running time complexity of an algorithm.

    O − Big Oh

    Ω − Big omega

    θ − Big theta

    o − Little Oh

    ω − Little omega

https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_asymptotic_notations_apriori.htm

JSP Servlet interview questions

  • What is a JSP?


Java Server Pages (JSP) is a platform independent presentation layer technology
JSPs are normal HTML pages with Java code pieces embedded in them
A JSP compiler is used in the background to generate a Servlet from the JSP page
The goal of JSP is the simplified creation and management of dynamic Web pages
Make use of Java as a server-side scripting language



  • What are the lifecycle of JSP?

When presented with JSP page the JSP engine does the following 7 phases.
Page translation: -page is parsed, and a java file which is a servlet is created.
Page compilation: page is compiled into a class file
Page loading : This class file is loaded.
Create an instance :- Instance of servlet is created
jspInit() method is called
_jspService is called to handle service calls
_jspDestroy is called to destroy it when the servlet is not required.



  • What is difference between custom JSP tags and JavaBeans?


You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files
JavaBeans are Java utility classes you defined.
Beans have a standard format for Java classes
Custom tags can manipulate JSP content; beans cannot



  • What are the implicit objects?

Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
–request
–response
–pageContext
–session
–application
–out
–config
–page
–exception



  • How can I enable session tracking for JSP pages if the browser has disabled cookies?

We know that session tracking uses cookies by default to associate a session identifier with a unique user.
if cookies are disabled, you can still enable session tracking using URL rewriting.
URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie

  • What are the common mechanisms used for session tracking?

Cookies
SSL sessions
URL- rewriting
HttpSession
Hidden form fields



  • Explain the life cycle methods of a Servlet.

public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
First the servlet is constructed, then initialized wih the init() method.
Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet
The servlet is removed from service, destroyed with the destroy() methid, then garbaged collected and finalized.





  • What is the difference between doGet() and doPost()?

A doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this limitation.
In GET your entire form submission can be encapsulated in one URL, like a hyperlink.
query length is limited to 255 characters, not secure, faster, quick and easy.
The data is submitted as part of URL
A request string for doGet() looks like the following:
http://www.allapplabs.com/svt1?p1=v1&p2=v2&...&pN=vN

doPost() method call doesn't need a long text tail after a servlet name in a request.
All parameters are stored in a request itself, not in a request string, and it's impossible to guess the data transmitted to a servlet only looking at a request string.
In POST data is submitted inside body of the HTTP request.
The data is not visible on the URL and it is more secure.


  • What is the difference between JSP and Servlets ?

JSP is used mainly for presentation only.
A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP.
But a servlet can support any protocol like HTTP, FTP, SMTP etc


  • What is session?

The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests. The session is stored on the server

What is servlet mapping?
The servlet mapping defines an association between a URL pattern and a servlet.
The mapping is used to map requests to Servlets


  • Explain ServletContext.

Every web application has one and only one ServletContext and is accessible to all active resource of that application.
The servlet context is an object that contains a information about the Web application and container.
 Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use



  • What is a servlet ?

servlet is a java program that runs inside a web container.

http://faq.programmerworld.net/programming/jsp-interview-questions-answers.html
http://www.allapplabs.com/interview_questions/servlet_interview_questions.htm
http://www.javacertificate.net/jsp_iqns.htm




  • How can I pass variable from one servlet to another?


Add the string into HttpSession instance.Other servlet will be able retrieve it from there.

add the string to http servlet request
HttpServletRequest.getSession() .setAttribute(attrName, value)
HttpServletRequest.getSession() .getAttribute(attrName)

If it is a short lived value, the request is a better place for this information but can expose it to cross site scripting security issues

The session is good if it is state or authentication information.

If it is simply data that needs to be shared, then either a database field or working cache may be the answer

unlike the request, the session lives until the session times out,
so if it is a value that is only needed between two servlets once, the value will continue to reside in the session until it is manually removed or the session is terminated.


If the user name has been entered from a form which is then submitted as an HTTP request, you don't have to do anything except forward the request/response to the second servlet. The user name is still held as a request parameter and can be retrieved by the second servlet using the request's getParameter() method.

if the variable is created by the first servlet, it can be made available to a second ( and subsequent) servlet(s) by attaching it to the request attribute table using the setAttribute() method

Attaching it to the session attribute table is only necessary if the life of the variable needs to extend across more than one request

if you're simply moving the variable from one servlet to another, there's less housekeeping involved if you use the request object to transport it.


  • JavaServer Pages (JSP) Technology Quizzes

http://java.sun.com/products/jsp/learning/quizzes/index.html



  • Jsp Interview Questions -1

http://www.scribd.com/doc/57136912/89-Jsp-Interview-Questions-1



  • M.C.A Computer Aplications Enterprise Java (MC335) : October 2008 Question paper

http://discuss.itacumens.com/index.php?topic=56229.0

Friday, December 2, 2011

hardlink vs symbolic link

Hardlink or hardlinks cannot be created for directories (folders). Hard link can only be created for a file.


Symbolic links or symlinks can link to a directory (folder).

Removing the original file that your hard link points to does not remove the hardlink itself; the hardlink still provides the content of the underlying file

Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless


If you remove the hard link or the symlink itself, the original file will stay intact




  • How to create a hard link or hardlink?


use the ln command to create a hard link.where fileA is the original file and fileB is the name you want to give to the hardlink

ln fileA fileB

both files fileA and fileB have the same inode number
both files have the same file permissions and
the same size


ln Command Syntax

The syntax is as follows for Unix / Linux hard link command:
ln {source} {link}

Where,

    source is an existing file.
    link is the file to create (a hard link).


# ln a.txt harda.txt

1898766 -r-xr-xr-x 2 root     root        7 Dec  1  2011 a.txt
1898766 -r-xr-xr-x 2 root     root        7 Dec  1  2011 harda.txt
1898831 lrwxrwxrwx 1 root     root        5 Nov  6 15:38 symboa.txt -> a.txt



  • How to create a symlink?


where fileA is the original file and fileB is the name you want to give to the symbolic link

ln -s fileA fileB

The inode is different for the original file and for the symbolic link

the symbolic link has different permissions than the original file (because it is just a symbolic link).

The content of the symlink is just a string pointing to the original file

The size of the symlink is not the same as the size of the original file.

The symbolic link is a separate entity and as such occupies some space on your hard drive



ln -s a.txt symboa.txt

ls -li
1898766 -r-xr-xr-x 1 root     root        7 Dec  1  2011 a.txt
1898831 lrwxrwxrwx 1 root     root        5 Nov  6 15:38 symboa.txt -> a.txt

The inode is different for the original file and for the symbolic link
the symbolic link has different permissions than the original file (because it is just a symbolic link).
The size of the symlink is not the same as the size of the original file



  • Can I make a symlink to a hard link?


Yes. The hard link functions the same way like the original file; therefore, you can make symlinks to it.


http://www.maxi-pedia.com/difference+between+symbolic+link+and+hard+link