design & analysis of algorithms

7
Spring 2013 Masters in Computer Application (MCA) - Semester 4 MCA4040 - ANALYSIS AND DESIGN OF ALGORITHM (Book ID: B1649) (60 Marks) 1. Explain time complexity and space complexity of an algorithm. Time complexity is the amount of computer time an algorithm requires to run to completion. In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the string representing the input. The time complexity of an algorithm is commonly expressed using big O notation, which excludes coefficients and lower order terms. When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n, the asymptotic time complexity is O(n3). Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Thus the amount of time taken and the number of elementary operations performed by the algorithm differ by at most a constant factor. Since an algorithm's performance time may vary with different inputs of the same size, one commonly uses the worst-case time complexity of an algorithm, denoted as T(n), which is defined as the maximum amount of time taken on any input of size n. Time complexities are classified by the nature of the function T(n). For instance, an algorithm with T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2n) is said to be an exponential time algorithm. Space complexity of an algorithm is the amount of memory it needs to run to completion. Analysis of space complexity of an algorithm or program is the amount of memory it needs to run to completion. Some of the reasons for studying space complexity are:

Upload: suidhi

Post on 22-Apr-2017

219 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Design & Analysis of Algorithms

Spring 2013Masters in Computer Application (MCA) - Semester 4MCA4040 - ANALYSIS AND DESIGN OF ALGORITHM

(Book ID: B1649)(60 Marks)

1. Explain time complexity and space complexity of an algorithm.Time complexity is the amount of computer time an algorithm requires to run to

completion. In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the string representing the input. The time complexity of an algorithm is commonly expressed using big O notation, which excludes coefficients and lower order terms. When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n, the asymptotic time complexity is O(n3).

Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Thus the amount of time taken and the number of elementary operations performed by the algorithm differ by at most a constant factor.Since an algorithm's performance time may vary with different inputs of the same size, one commonly uses the worst-case time complexity of an algorithm, denoted as T(n), which is defined as the maximum amount of time taken on any input of size n. Time complexities are classified by the nature of the function T(n). For instance, an algorithm with T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2n) is said to be an exponential time algorithm.

Space complexity of an algorithm is the amount of memory it needs to run to completion. Analysis of space complexity of an algorithm or program is the amount of memory it needs to run to completion. Some of the reasons for studying space complexity are:

1. If the program is to run on multi user system, it may be required to specify the amount of memory to be allocated to the program.

2. We may be interested to know in advance that whether sufficient memory is available to run the program.

3. Here may be several possible solutions with different space requirements.4. Can be used to estimate the size of the largest problem that a program can

solve.

2. What do you mean by asymptotic notations? Explain three major asymptotic notations in detail.

Asymptotic complexity is a way of expressing the main component of the cost of an algorithm, using idealized units of computational work. Consider, for example, the algorithm for sorting a deck of cards, which proceeds by repeatedly searching through the deck for the lowest card. The asymptotic complexity of this algorithm is the square of the number of cards in the deck. This quadratic behavior is the main term in the complexity formula, it says, e.g., if you double the size of the deck, then the work is roughly quadrupled.

Three major asymptotic notations are

Page 2: Design & Analysis of Algorithms

The O NotationThe O(pronounced as: big-oh) is the formal method of expressing the upper

bound of an algorithm's running time. It's a measure of the longest amount of time it could possibly take for the algorithm to complete.Big-Omega Notation

For non-negative functions, f(n) and g(n), if there exists an integer n_{0} and a constant c > 0 such that for all integers n>n_{0}, f(n) ≥ cg(n), then f(n) is omega of g(n). This is denoted as "f(n) = Ω(g(n))".

This is almost the same definition as Big Oh, except that "f(n) ≥ cg(n)", this makes g(n) a lower bound function, instead of an upper bound function. It describes the best that can happen for a given data size.Theta Notation

For non-negative functions, f(n) and g(n), f(n) is theta of g(n) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). This is denoted as "f(n) = Θ(g(n))".This is basically saying that the function, f(n) is bounded both from the top and bottom by the same function, g(n).The theta notation is denoted by Q.Little-O Notation

For non-negative functions, f(n) and g(n), f(n) is little o of g(n) if and only if f(n) = O(g(n)), but f(n) ≠ Θ(g(n)). This is denoted as "f(n) = o(g(n))".Little Omega Notation

For non-negative functions, f(n) and g(n), f(n) is little omega of g(n) if and only if f(n) = Ω(g(n)), but f(n) ≠ Θ(g(n)). This is denoted as "f(n) = ω(g(n))".

Much like Little Oh, this is the equivalent for Big Omega. g(n) is a loose lower boundary of the function f(n); it bounds from the bottom, but not from the top.

3. Write short note on:a) Selection sort algorithmb) Bubble sort algorithm.Selection Sort

In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.Bubble Sort

Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a

Page 3: Design & Analysis of Algorithms

comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.Performance

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is O(n).

4. Explain depth- first search and breadth- first search algorithms.Depth-firth algorithm

Depth-first search selects a source vertex s in the graph and paint it as "visited." Now the vertex s becomes our current vertex. Then, we traverse the graph by considering an arbitrary edge (u, v) from the current vertex u. If the edge (u, v) takes us to a painted vertex v, then we back down to the vertex u. On the other hand, if edge (u, v) takes us to an unpainted vertex, then we paint the vertex v and make it our current vertex, and repeat the above computation. Sooner or later, we will get to a “dead end,” meaning all the edges from our current vertex u takes us to painted vertices. This is a deadlock. To get out of this, we back down along the edge that brought us here to vertex u and go back to a previously painted vertex v. We again make the vertex v our current vertex and start repeating the above computation for any edge that we missed earlier. If all of v's edges take us to painted vertices, then we again back down to the vertex we came from to get to vertex v, and repeat the computation at that vertex. Thus, we continue to back down the path that we have traced so far until we find a vertex that has yet unexplored edges, at which point we take one such edge and continue the traversal. Breadth-firth algorithm

Breadth-first search starts at a given vertex s, which is at level 0. In the first stage, we visit all the vertices that are at the distance of one edge away. When we visit there, we paint as "visited," the vertices adjacent to the start vertex s - these vertices are placed into level 1. In the second stage, we visit all the new vertices we can reach at the distance of two edges away from the source vertex s. These new vertices, which are adjacent to level 1 vertices and not previously assigned to a level, are placed into level 2, and so on. The BFS traversal terminates when every vertex has been visited.

To keep track of progress, breadth-first-search colors each vertex. Each vertex of the graph is in one of three states:1. Undiscovered;2. Discovered but not fully explored; and3. Fully explored.

5. Differentiate between bottom-up and Top-down heap construction.Top down design proceeds from the abstract entity to get to the concrete design.

Bottom up design proceeds from the concrete design to get to the abstract entity. Top down design is most often used in designing brand new systems, while bottom up design is sometimes used when one is reverse engineering a design; i.e. when one is trying to

Page 4: Design & Analysis of Algorithms

figure out what somebody else designed in an existing system. Bottom up design begins the design with the lowest level modules or subsystems, and progresses upward to the main program, module, or subsystem. With bottom up design, a structure chart is necessary to determine the order of execution, and the development of drivers is necessary to complete the bottom up approach. Top down design, on the other hand, begins the design with the main or top-level module, and progresses downward to the lowest level modules or subsystems. Real life sometimes is a combination of top down design and bottom up design. For instance, data modeling sessions tend to be iterative, bouncing back and forth between top down and bottom up modes, as the need arises.

6. Explain the two types of collision resolution in Hashing.Two types of collision resolution in hashing are

Open Addressing In an open addressing hashing system, if a collision occurs, alternative locations are

tried until an empty location is found. The process starts with examining the hash location of the identifier. If it is found occupied, some other calculated slots are examined in succession till an empty slot is found. The same process is carried out for retrieval.Features :a) All the identifiers are stored in the hash table itself b) Each slot contains an identifier or it is emptyc) Requires a bigger table for open addressingd) Three techniques are commonly used for open addressing : linearprobing, quadratic

probing, rehashing.e) There is a possibility of the table becoming fullf) Load factor can never exceed 1.g) Probing for insertionChaining

The main problem in using the linear probing and its variants is that, it results in series of comparisons, which is not a very big improvement over the linear search method. In chaining, what is done is that when an overflow occurs, the identifier X is placed in the next vacant slot ( like linear probing ) but it is chained to the identifier occupying its bucket so that X can be easily located. Chaining can be done if another field is provided in the hash table, which stores the hash address of the next identifier having the same hash address i.e. synonym. Hence identifiers can be easily located. Chaining has three variants :1. Chaining without replacement2. Chaining with replacement3. Chaining using linked lists