The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Aditya Diwan, 2024-03-13 06:55:17

Data Structure Question and Answer

Data Structure Question and Answer

Unit 1 Chapter -1 Introduction To Data Structures


Unit 1 Chapter -1 Introduction To Data Structures Question & Answer: - Ques1: Difference between data and information. Ans: Ques2: Define Data structures. Ans: The Organized collection of data is known as data structure. OR In Other words, the possible way in which the data values such as integers, floats, chars are logically related among themselves are defined by data structures. OR The data structures deal with the study of how the data is organized in the memory. So, the data structure can also be defined as the logical (or)mathematical model of a particular organization of data. Data Structure = Organized data + Operations


Ques3: Why data structures are required? Ans: Data structures are essential for organizing and manipulating data in a systematic and efficient way. 1. Efficient Data Organization: Data structures provide efficient ways to organize and store data. Different structures are designed to optimize specific operations like searching, insertion, deletion and retrieval. 2. Algorithm Design: Efficient algorithm often relies on appropriate data structures. Choosing the right data structure can significantly impact the performance of algorithms. For example, a well-designed data structure can lead to faster sorting or searching algorithms. 3. Resource Management: Data structures help in managing and optimizing the use of resources such as memory. They enable efficient storage and retrieval of information, reducing the overall resource requirements of a program. 4. Abstraction: Data structures allow programmers to abstract complex details and focus on high-level problem-solving. They provide a way to represent and manipulate data in a more understandable and modular manner. 5. Flexibility and Adaptability: Different data structures are suited for different tasks. By understanding the characteristics and behaviours of various data structures, programmers can choose the most appropriate one for a specific problem or scenario. Ques4: Mention the application of data structure. Ans: Designing and using data structure is good programming skill, the data structure skills are not provided in general purpose programming languages. The programmer who wants to use this structure has to build them. The data structure is necessary for following reasons: 1.It provides a function that can be used to retrieve the individual data elements.


2. The data structure enables to solve the relationships between the data elements that are relevant to the solution of the problem. 3. Data structure helps to describe the operation that must be performed on the logically related data elements, the operations such as creating, displaying, inserting, deleting, retrieving, etc. 4. Data structure enables to devise the methods of representing the data elements in the memory of the computers, that will reduce the loss of fragmentation and also allows to select the memory configuration or storage structures. 5. Data structures give the freedom to the programmer to decide any type of language that best suits for a particular problem. 6. The algorithm can be improved by structuring the data in such a way that the resulting operations can be efficiently carried out. 7. The data structure describes the physical and logical relationship between the data items. It also provides a mode of access to each element in the data structure. 8. Data structure helps in selection of an appropriate mathematical model for writing a program. Ques5: Explain the classification of data structures. Ans: The data structures are classified into two categories as shown below: 1. Primitive Data Structures: The data structures which can be directly operated by machine level instruction are called primitive data structures. In C language the following are the primitive data structures 1. int 2. char 3. float 4. double 2. Non-Primitive Data Structures The data structures which are not primitive are called non primitive data structures, which means these are the data structures that cannot be manipulated directly by machine instructions


Non-Primitive data structures are classified into two categories 1. Linear data structure 2. Non-Linear data structure Linear Data Structures A linear data structure is one which establishes the relationship of adjacency between the elements in which all the elements are stored in memory linearly or sequentially (one after the other). Example: 1. Arrays 2. Linked List 3. Stack 4. Queues Non-Linear Data Structures Any data structure which establishes the relationships other than the adjacency relationship is called Non-Linear data structures. Example: 1. Trees 2. Graphs Ques6: Write operations on primitive data structures. Ans: Operations on Primitive Data Structures:- 1. Creation: The operation creates a data structure. Example: int; It results in creation of memory space for ‘i’ during the complication of declaration statement. 2. Deletion: It provides complimentary effect of creation operation. It leads to the efficient use of memory. It is used to destroy the data structure. This operation is not supported in many programming languages. In C language, we can use function free () for destroying the data structure.


3. Selection: It is for accessing of data within a data structure. It depends on type of data structure being used. Method of accessing is one of the important operations in data structure. 4. Update: This operation is used to change data in the structure. An assignment operation is a good example of an update operation. eg: int i = 0; i=5; //update operation. Ques7: Explain the classification non-primitive data structures. Ans: The data structures which are not primitive are called non-primitive data structures, which means these are the data structures that cannot be manipulated directly by machine instructions. Non-primitive data structures are classified into two categories: - 1. Linear data structure 2. Non-Linear data structure Linear Data Structures: A linear data structure is one which establishes the relationship of adjacency between the elements in which all the elements are stored in memory linearly or sequentially (one after the other). Example: 1. Arrays 2. Linked List 3. Stack 4. Queues Non-Linear Data Structures: Any data structure which establishes the relationships other than the adjacency relationship is called Non-Linear data structures. Example: 1. Trees 2. Graphs


In trees, children, parent, grandparent relationship can be established. The possible way in which the data items or atoms are logically related define different data structures, some of the data structures are as follows: (i)Arrays: Array is a finite ordered set of homogeneous elements, which are stored in adjacent cells in memory. The data are represented by a single name identified by a subscript. Linear arrays are called one-dimensional arrays because each element in an array is referred by one subscript. A Two-dimensional array is a collection of similar data types where an element is referred by two subscripts. Array can be multi-dimensional that can be 2 dimensional, 3-dimensional, 4-dimensional, N-dimensional. (ii)Stacks: It is a linear data structure in which data is inserted and deleted at one end called as top of stack, i.e., data is stored and retrieved in last in first out (LIFO) order. (iii)Linked Lists: A list is a linear sequence of data objects of the same type. The list may be such as singly, doubly and circular linked list. Top Of the Stack


The linked list is a linear collection of data items called as nodes, node is divided into two parts (1) Information field (2) Link field. The linked list structure i.e., each of its nodes have -data/information field. -a pointer pointing to next node of the list / link field. (iv) Trees: A tree is a finite set of vertices that has a vertex called as root and remaining vertices are collection of sub-trees. Ques8: What are linear data structures? Ans: Linear Data Structures: A linear data structure is one which establishes the relationship of adjacency between the elements in which all the elements are stored in memory linearly or sequentially (one after the other). Example: 1. Arrays 2. Linked List 3. Stack 4. Queues Ques9: What are non-linear data structures? Ans: Non-Linear Data Structures: Any data structure which establishes the relationships other than the adjacency relationship is called Non-Linear data structures. Example: 1. Trees 2. Graphs


Ques10: Explain non-linear data structures in detail. Ans: Non-Linear Data Structures: Any data structure which establishes the relationships other than the adjacency relationship is called Non-Linear data structures. There are two types of non-linear data structures: - 1. Trees -Hierarchy of nodes. -Root node at the top. - Nodes have parent-child relationships. - Binary trees have at most two children per node. - AVL trees and B-trees are specific types. 2. Graphs -Nodes connected by edges. -Edges can be directed or undirected. -Can be cyclic or acyclic. -Directed Acyclic Graphs (DAGs) have no cycles. -Weighted graphs assign values to edges. Ques11: What are the operations on data structures? Ans: The data appearing in the data structures are processed by the operations, the operations on data structures are classified as follows: 1. Inserting: Adding the new element to the existing data structure. 2. Deleting: Deleting the element from a data structure. 3. Traversing: Accessing each element exactly once is called traversing. 4. Searching: Searches a particular element in the list of elements. 5. Sorting: Arranging the elements in ascending or descending order. 6. Merging: Combining the two different lists into single list.


Ques12: What are fixed length records & variable length records? Ans: The records are classified according to their lengths: (1) Fixed length records (ii) Variable length records (1) Fixed Length Records: In this type of records, all records will contain equal number of fields with similar data items and the same amount of space allocated for each record. (2) Variable Length Records: In this type of records, each record will have equal number of fields with same data items but available in different lengths in size i.e., amount of space is varied for each record. Ques13: Define Abstract Data Type. Ans. An abstract data type is a type with associated operations, but whose representation is hidden. OR An abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data; and this is organized in such a way that the specification of values and operations on those values are separated from the representation of the values and the implementation of the operations. OR An abstract data type (ADT) is an object (or type) with a generic description but independent of implementation details. This description includes a specification of the components from which the object is made and also the behavioural details of the object.


Ques14: Explain Abstract Data Type Model Ans: An ADT model contains both public functions and private functions. The private function is accessible only to public functions and public functions are accessible to the application program (or) user by an interface call. The functions defined in an interface are implemented by the data structures like stack, queue, linked list, arrays etc Data are entered, accessed, modified and deleted through the external call using application programming interface. This interface can access only public functions. Ques15: Explain Abstract Data Type with example Ans: An abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data; and this is organized in such a way that the specification of values and operations on those values are separated from the representation of the values and the implementation of the operations.


Examples Of Abstract Data Type (ADT): - Stack ADT: We can define ADT stack as a collection of elements together with operations push, pop, isFull, isEmpty, top, etc., that have the usual properties. Queue ADT: We can define ADT queue as a collection of elements together with operations insert, delete, isFull, isEmpty, etc., that have the usual properties.


Ques16: Why ADTs are important? Ans: To manage the complexity of problems and the problem-solving process, computer scientists use abstractions to allow them to focus on the "big picture" without getting lost in the details. By creating models of the problem domain, we are able to utilize a better and more efficient problem-solving process. These models allow us to describe the data that our algorithms will manipulate in a much more consistent way with respect to the problem itself. Ques17: What is the relationship between ADT and data structures? Ans: Abstract data type is an interface and data structure is an implementation. An ADT gives the specification and data structure implements that specification. So, we can say that data structure is an implementation of ADT. Ques18: Define Algorithm and Algorithm Complexity Ans: ALGORITHM: - Informally, an algorithm is any well-defined computational procedure that takes some values as "input" and produces some value, or set of values, as "output". An algorithm is thus "a sequence of computational steps that transform the input into the output." We can also view an algorithm as a tool for solving a well specified "computational problem". The statement of the problem specifies in general terms the desired input/output relationship. The algorithm describes a specific computational procedure for achieving that input/output relationship. OR In simple terms we can say that "An algorithm is a step-by-step procedure for performing some tasks in a finite amount of time".


ALGORITHM COMPLEXITY: - Algorithmic complexity is concerned about how fast or slow particular algorithm performs and how much memory it requires. Complexity of algorithm is a function of size of input of a given problem instance which determines how much running time/memory space is needed by the algorithm in order to run to completion. OR In simple terms we can say that “Complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size (n)”. Ques19: Define Space and Time Complexity. Ans: In order to find the complexity of algorithm, two kinds of efficiencies are to be kept in mind. 1. Space Complexity: It indicates the amount of storage required for running the algorithm. i.e. "the amount of memory needed by the algorithm to run to completion". 2. Time Complexity: Time complexity of an algorithm is the amount of time it needs in order to run the program for completion. Ques20: Explain Time-Space Trade off. Ans: Time-Space tradeoff is a way of solving a problem or calculation in less time by using more storage space (or memory), or by solving a problem in very little space by spending a long time. Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy


Unit 1 Chapter - 2 Preliminaries


Unit 1 Chapter- 2 Preliminaries Question & Answer: - Ques1: Explain different mathematical functions and notations. Ans: Mathematical Notations and functions: • Floor and Ceiling Functions If x is a real number, then floor and ceiling of x is defined as follows: 1.)Floor(x): returns the largest integer that is smaller then or equal to x (i.e.: rounds down the nearest integer). 2.)Ceil(x): Returns the smallest integer that is greater than or equal to x (i.e.: rounds up the nearest integer). Example: floor (2.5) =2, floor(2.9)=2, floor(-7.2)= -8, ceil(2.5)=3, ceil(2.9)= 3, ceil(-7.2)= -7 • Remainder Function(Modular Arithmetic) If k is any integer and M is a positive integer, then: k(mod M) gives the integer remainder when k is divided by M. Example: 26(mod 7)=5, 30(mod 5)=0 • Integer and Absolute Value Functions If k is a real number, then integer function INT(x) will convert x into integer and the fractional part is removed. Example: int(5.34)=5, INT(-7.5)= -7 The absolute function ABS(x) or |x| gives the absolute value of x i.e. it gives the positive value of x even if x is negative. Example: ABS (-99) =99 or ABS [-99] =99 ABS (-3.33) =3.33 or ABS [-3.33]=3.33


• Summation symbol Here we introduce the summation symbol ∑(sigma). Consider a sequence of n-terms a1 + a2………an, then the sums a1 + a2 +… + an will be denoted as ∑ai 1≤i≤n Examples are sum of n natural numbers, square of n positive integers and many more. We can write an expression as a sum of series or sequence. We can also call summation of summation. Consider that a function f(n) denotes the summation of n positive integers. Here function f(n) computes the order of magnitude of an algorithm. f(n)= ∑ ai =1+2+….+ n= n(n+1)/2, and the order of complexity is 0(n2 ) 1≤i≤n • Factorial Function n! denotes the product of the positive integers from 1 to n. n! is read as ‘n factorial’, i.e. n! = 1*2*3*….*(n-2)*(n-1)*n Example: 4! = 1*2*3*4 = 24 5! = 5*4*3*2*1 = 120 • Permutations Let us consider a set of n elements. A permutation of this set means the arrangement of the elements of the set in some order. Example: Suppose the set contains a, b and c. the various permutations of these elements can be: abc, acb, bac, bca, cab, cba. If there are n elements in the set then there will be n! permutations of those elements. It means if the set has 3 elements, then there will be 3! = 1*2*3 = 6 permutations of the elements. • Exponents and Logarithms Exponent means how many times a number is multiplied by itself. If m is a positive integer, then: am= a* a * a *…. a(m times) and am = 1/am Example: 25= 2*2*2*2*2 = 32, 2-5= 1/25= 1/32


The concept of logarithms is related to exponents. If b is a positive number, then the logarithm of any positive number x to the base b is written as logbx. it represents the exponent to which b should be raised to get x i.e. y=logbx and by= x Ques2: Explain Algorithmic notations with an example. Ans: An algorithm can be described in many ways. A natural language such as English can be used to write an algorithm but generally algorithms are written in Englishlike pseudo code that resembles with high level languages such as ‘C’ and ‘C++’. The following algorithmic notations are used to write an algorithm. Algorithmic Notations Description Name of algorithm Every algorithm is given an identifying name. Example: Algorithm LINEARSEARCH Comments Every algorithm can have brief description of the tasks the algorithm performs and any assumptions that have been made. The description gives the name and types of the variables used in the algorithm. Comment begins with // and continues until the end of line. Comments are included only for clarity. Example: Algorithm: LINEARSEARCH //This algorithm is used to find the element in an array //by accessing elements sequentially. Data Type Data types such as integer, char, real, Boolean and other data structures such as array, pointer, structure are used. Array ith element can be described as A[i] and (i,j) element can be described as A[i,j] Example: Integer MAX, MIN, SUM Real SALARY, AVG Integer ARRAY[N] Variable Names The variable names are generally be in capital letters such as MAX, MIN, NAME, AGE etc.


Steps Each algorithm is made of a sequence of numbered steps and each step has an ordered sequence of statements, which steps describe the tasks to be performed. The statements in each step are executed in a leftto-right order. Example: Set A = 1, MAX=0; MIN:= 0 Assignment statement The assignment statement is indicated by placing equal (=) between the variable (in left hand side) and expression or value(s) (in right hand side). For example, addition of a and b is assigned in variable a. a= a+b Expression There are three expressions: arithmetic, relational and logical. The arithmetic expression used arithmetic operator such as /,*,+,-, relational expression used relational operator such as <,<=,>,>=,<>,= and logical expression used logical operator such as not, or, and. There are two Boolean values, true or false. Example: if(A>B) Input and output For input of data READ statement and for output of data WRITE statement are used. If more than one output data then we can use comma as separator among the variable names. Example: READ: X READ:X, Y WRITE:X,Y Goto statement The goto statement causes unconditional transfer of control to the step referenced. Thus statement goto step N will cause transfer of control to step N. Example: GOTO step 4; End statement The end statement is used to terminate an algorithm. It is usually the last step and algorithm name is written after the end. Example: END LINEARSEARCH Functions or procedures A function is used to return single value to the calling function. Transfer of control and returning of the value are accomplished by return(value) statement. A function begins as follows: Example: function name(parameters list).


Ques3: Explain different control structures with an example. Ans: Control Structures One of the most important concepts of programming is the ability to control a program so that different lines of code are executed or that some lines of code are executed many times. The mechanism that allows us to control the flow of execution are called control structures. There are four main categories: 1. Sequence Simply do one instruction then the next and the next. It just executes them in a given sequence or in order listed. Most lines of code in any program belongs to this category. In this figure, statement 1 is executed first, followed by statement 2, then finally statement 3. Sequence control structures 2. Selection This is where we select or choose between two [or] more flows. The choice is decided by asking some sort of questions. The questions are nothing but conditions. The answer determines the path (or which lines of code) will be executed. Statement 1 Statement 2 Statement 3


Example: if-then-else True If condition then Statement 1; else Statement 2; False Here the condition is a Boolean expression which evaluates to either true [or] false. If condition evaluates to true, then statement 1 will be executed, otherwise statement 2 will be executed S1 and S2 may single statement or group of statements. 3. Iteration • It is also known as repetition; it allows some code to be executed several times. • It executes the statements repeatedly for a certain number of times as long as the condition is true. • If the value of condition is true, then it executes all the statements in the body of the loop after which the control is transferred to the while statements for evaluation of condition again. • The execution of loop is terminated, when the condition is evaluated to FALSE. Then the control statement is transferred to the first statement of the while loop. Example: condition S1 S1


4. Branching A control structure that allows the flow of execution to jump to a different part of program. This category is rarely used in modular structured programming. Example: Go To statement Ques4: Explain time complexity of an algorithm. Ans: The amount of time needed to run the program is termed as time efficiency or time complexity. The total time taken by a program is the sum of the compile time and runtime. The compile time does not depend on the instance characteristics and it can be assumed as a constant factor so we concentrate on runtime of the program. Let this runtime is denoted by tp (instance characteristics). Then tp (n) = ta ADD(n) + ts SUB(n) + tm MUL(M) +----- where n indicates the instance characteristics and ta, ts, tm ---denote the time needed for an addition, subtraction, multiplication, and so on. ADD, SUB, MUL - -- represent the functions and they are performed when the code for the program is used on an instance characteristic ‘n’. Ques5: Explain space complexity of an algorithm. Ans: The total amount of time of computer memory required by an algorithm to complete its execution is called as space complexity of that algorithm. For any algorithm, memory is required for the following purposes: 1. Memory required to store program instructions. 2. Memory required to store constant variables. 3. Memory required to variable values. 4. And for few other things. Generally, when a program is under execution it uses the computer memory for three reasons. This are as follows: 1. Instruction Space: It is the amount of memory used to store compiled version of instructions.


2. Environmental Stack: It is the amount of memory used to store information of partial executed functions at the same time of function call. 3. Data Space: It is the amount of memory used to store all the variables and constants. Ques6: Write a note on operation count and step count. Ans: Operations counts Consider an algorithm ‘A’ with ‘n’ size of that input data. The time and space are the two main measures for the efficiency of the algorithm. In operation counts, the time is measured by counting the number of basic operations [or] key operations. The ‘basic operations’ are defined that time for the other operations is much less than [or] almost proportional to the time for the basic operations. The operation count method concentrates on certain important basic operations like multiplications, for loop where it takes considerably more time than any other operations in an algorithm.


Step counts In step counts method, we attempt to find the time spent in all the parts of the program. A step is any computational unit that is independent of the selected characteristics. There is another method to determine the step count of an algorithm is to build a table and list the total number of steps contributed by each statement. First determine the number of steps per execution [s/e] of the statement and total the number of times [frequency] each statement is executed.


Ques7: List out various asymptotic notations and their significance. Ans: 1. Big-oh Notation (O) The Big – oh – Notation gives an upper bound on function f(n). The upper bound of f(n) indicates that the function f(n) will be the worst - case that it does not consume more than this computing time. 2. Omega Notation (Ω) This notation is used to find the lower bound behavior of f(n). The lower bound implies that below this time the algorithm cannot perform bett er i.e., the algorithm will take at least this much of time (this indicates lower bound). It is represented mathematically by notation Ω for a given function g(n), we denote by Ω(g(n). The function g(n) is only a lower bound of f(n). For the statement f(n) = Ω(g(n) to be informative, g(n) should be as large as function of n as possible for which the statement f(n) = Ω(g(n) is true. 3. Theta Notation The theta notation can be used when the function f(n) can be bounded both from above and below by the same function g(n). For some functions, the lower bound and upper bound may be same. In finding the maximum and minimum element in an array, the computing time is O(n) and Ω(n). There exists a special notation to denote for functions having the same time complexity for lower bound and upper bound and this notation is called the theta – notation and which is denoted by ϴ. Ques8: Arrange the following complexities in ascending sequence. Give reasons. O(n log n) O(n3 ) O(n) O(n2 ) O(1) Ans: O(1) < O(n) < O(n log n) < O(n 2 ) < O(n3 ) Reason: let us assume that algorithm P has complexity O(n) and algorithm Q has complexity O(n2 ). We can asset that algorithm P is faster than algorithm Q for sufficiently large n. Since here (n < n2 ) and we can say that algorithm p is faster than algorithm Q. Ques9: Derive space complexity for finding sum of array elements.


Ans: Finding the sum of array elements. int square (int a) { return a*a; } In this program it requires 4 bytes of memory to store variable ‘a’ and another 4 bytes of memory is used to return value. That means, totally it requires 4 bytes of memory to complete its execution. If any algorithm requires a fixed amount of space for all input values then that space complexity is said to be Constant space Complexity. Therefore, S(P) = CP + SP S(P) = 8 + 0 S(P) = 4 Ques10: Explain the active areas of study of algorithm. Ans: Algorithm Definition: A step- by- step set of rules to solve a specific problem. 1. Time complexity: Measure of how runtime grows with input size (Big – O notation). 3.Space complexity: Measure of how memory usage grows with input size (Big – O notation). 4. Efficiency and Optimization Minimizing resource usage for better performance. 5. Algorithmic Paradigms: General approaches like Divide and Conquer, Dynamic programming, Greedy algorithms, Backtracking, and Randomized algorithms. Ques11: Briefly explain best case, average case and worst case time complexity of an algorithm. Ans: Let’s find the complexity function T(n) for certain cases.


• Worst case The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n, for which the algorithm runs the longest among all possible inputs of that size. Basically, it gives the maximum value of T(n) for any possible input. Let us denote the worst-case efficiency of an algorithm by Tworst(n). The worst-case occurs when the element to be searched is found at the last position or element to be searched is not found. In both cases, the algorithm makes n comparison. Therefore, the number of comparisons = n Tworst(n) = n • Best case The best-case efficiency of an algorithm is its efficiency for the best-case inputs of size n, for which the algorithm runs the fastest among all the possible inputs of that size. Eventually, it gives the minimum value of T(n) for any possible input. Let us denote the best -case efficiency of an algorithm by Tbest(n). The best-case occurs when the element to be searched is found at the first location. In that case, the algorithm makes only one comparison. Therefore, number of comparisons = 1 Tbest(n) = 1 • Average case It’s noticed that neither the worst-case nor the best-case yields the necessary information about the algorithm’s behaviour on random input. The average-case efficiency provides that information. To find the average-case efficiency. The following assumptions are made. 1. The probability of successful search is equal to P(0 ≤ P ≤ 1) 2. The probability of the first match occurring in the ith position of the array is P/n or every I, and in the case of unsuccessful search, the


number of comparisons is n with the probability of such a search being (1-p). therefore Ques12: What do you mean by Orders of Growth? Ans: The time complexity of an algorithm is generally some function of the instance characteristics. This function is very useful in determining how the time requirements vary as the instance characteristics change. Let T(n) be the complexity function with input size ‘n’. the values of T(n) increases when ‘n’ value increases and t(n) value decreases when ‘n’ value decreases. Therefore, the complexity function is directly proportional to the instance characteristics ‘n’.


Ques13: What are the units for measuring running time? Ans: The units for measuring running time are: ➢ Operation Counts ➢ Step Counts ➢ Asymptotic notations (mathematical analysis) Ques14: Given f(n) = 120n + 20, prove that f(n) = o(n2 ). Ans: Given, f(n) = 120n + 20, prove that f(n) = o(n2 ) |120 + 20| ≤ c. |n2 | Ɐ n ≥ n0 120n + 20 ≤ 121.n2 Ɐ n ≥ 1 The above inequality can be satisfied by setting C = 121 and n0 = 1 Therefore, f(n) = o(n2 ) is proved. Ques15: Given f(n) = 20n2 + 10n + 5, prove that f(n) = Ω(n). Ans: Given f(n) = 20n2 + 10n + 5 prove that f(n) = Ω(n) 20n2 + 10n + 5 ≥ c.n Ɐ n ≥ n0 Choose c=1 and n0 =1, we get 20n2 +10n + 5 ≥ 1.n The above inequality can be satisfied by setting c = 1 and n0 = 1 Therefore, f(n) = Ω(n) is proved. Ques16: Given that f(n) = 30n3 -2, prove that f(n) = O(n3 ) using limits of the ratio of two functions. Ans:


Ques17: Let f(n) = 100n +5. Express f(n) using Big – Omega and Big – Oh. Ans: 1. To express f(n) in terms of Big – Oh, we need to find a function h(n) such that 0 ≤ f(n) ≤ h(n) Ɐ n ≥ n0 Where n0 is some positive constant. In that case we can choose h(n) = 200n Ɐ n ≥ 1 0 ≤ 100n + 5 ≤ 200n Therefore, f(n) as O(n) with h(n) = 200n. 2. To express f(n) in terms of Big – Omega, we need to find function g(n) such that, 0 ≤ g(n) ≤ f(n) Ɐ n ≥ n0 g(n) = n Ɐ n ≥ 1 0 ≤ n ≤ 100n + 5 Therefore, f(n) as Ω(n) with g(n) = n. Ques18: Prove that 3n3 + 4n2 = O(n3 ). Ans: |0 ≤ 3n3 + 4n2 | ≤ c |n3 | Ɐ n ≥ n0 3n3 + 4n2 ≤ 3n3 + 4n3 Ɐ n ≥ 1 Common factor = n3 3n3 + 4n3 = 7n3


We have, 3n3 + 4n3 ≤ 7n3 Ɐ n ≥ 1 Choose c = 7 and n0 = 1 0 ≤ 3n3 + 4n2 ≤ 7n3 Therefore, 3n3 + 4n2 = O(n3 ) is proved. Ques19: Let f(n) = 100n + 5. Express f(n) using Big – Theta. Ans: c1 | g(n) | ≤ | f(n) | ≤ c2 | h(n) | Ɐ n ≥ n0 We can choose g(n) = n, h(n) = n 1. For lower bound: c1 . g(n) = c1 . n where c1 = 1 and n0 = 1. 0 ≤ 1.n ≤ 100n + 5 2. For upper bound: c2 . h(n) = c2 . n where c2 = 101 (any constant greater than 100) and n0 = 1. 0 ≤ 100n + 5 ≤ 101. n Therefore, f(n) = ϴ(n) Ques20: Explain time and space complexity with an example. Ans: • Time complexity The amount of time needed to run the program is termed as time efficiency or time complexity. The total time taken by a program is the sum of the compile time and runtime. Example: #include <stdio.h> 1. int main() { 2. int n, sum = 0; 3. scanf("%d", &n); 4. for (int i = 1; i <= n; i++) { 5. sum += i; 6. } 7. printf("Sum of first %d natural numbers = %d\n", n, sum); 8. return 0;


9. } The number of operations in the for loop is n, and each operation takes constant time. That's why the Time Complexity of the above example is considered as O(n). • Space complexity It indicates the amount of temporary storage required for running the algorithm. Example


Ques21: Find out the time required to run matrix multiplication using a) Operation counts b) Step counts Ans: a) Operation counts let us consider two matrices A and B. AB requires dot product of row ‘i’ in A and column ‘j’ in B. computing the dot product requires n multiplications and n -1 additions. Since there are n 2 elements, the dot product must be computed n 2 times. Thus, the total number of operations is n 2 (n + (n – 1)) = 2n3 – n 2 = O(n3 ). b) Step counts Consider that for each row.dot(column) you have to do the same thing, and you have to do this for each row.column pair - so it seems like each dimension would give you 21/7=3 steps, since you have 7 row.column pairs needing a total of 21 steps.


Ques22: Explain worst – case, best – case and average – case efficiencies? Explain with an example. Ans: 1. Worst – case The worst case occurs when the element to be searched is found at the last position or element to be searched is not found at any location. Example


2. Best- case The best case occurs when the element to be searched is found at the first location. Example 3. Average- case This gives expected value when the element is searched. Example


Ques23: Explain what are the basic steps that are required to be followed to analyze recursive and non – recursive algorithms. Explain with an example. Ans:


Ques24: Explain asymptotic notations and give an example for each. Ans: 1. Big- oh notation (O) Big- Oh notation gives an upper bound on function f(n). The upper bound of f(n) indicates that the function f(n) will be the worst- case that it does not consume more than this computing time. 2.Omega notation (Ω) This notation is used to find the lower bound behaviour of f(n). The lower bound implies that the algorithm will take at least this much of time. It is represented by Ω.


3. Theta- notation (ϴ) This notation can be used when the function f(n) can be bounded both from above and below by the same function g(n). for some functions, the lower and upper bound may be same. This will the average case which the computing time is O(n) and Ω(n). It is denoted by ϴ.


Ques25: Design an algorithm to obtain maximum of N elements and obtain the time complexity. Ans: Algorithm: start arr[] for (int i=1; i<n; ++i) if (arr[0] < arr[i]) { arr[0] = arr[i]; } } if the first element will be the largest element, then also loop will run till end of the array to verify. and if the last element is the largest then also for loop will be executed till the end of the for loop. So, the worst and best case both will be same that is O(n). Ques26: Calculate the time complexity for the following recursive algorithms. a. Factorial b. GCD c. Fibonacci Series Ans: a) Factorial factorial(n): if n is 0 return 1 return n * factorial(n-1) From the above analysis we can write: T(n) = T(n — 1) + 3 T(0) = 1 T(n) = T(n-1) + 3 = T(n-2) + 6 = T(n-3) + 9 = T(n-4) + 12 = ……. = T(n-k) + 3k as we know T(0) = 1


we need to find the value of k for which n - k = 0, k = n T(n) = T(0) + 3n , k = n = 1 + 3n that gives us a time complexity of O(n) b) GCD Algorithm: GCD(x,y) { // x >= y where x & y are integers if(y==0) return x else return (GCD(y,x%y)) } Time Complexity: 1. Best Case: O(1) if y is divisible of x, then Euclid GCD terminates in one call. 2.Worst Case: O(log n) when x, y are two consecutive Fibonacci numbers. c) Fibonacci Series Ques27: Write an algorithm to add two matrices and explain how its time complexity is obtained by introducing count statements. Ans:


In this example, the total count of basic operations is 3*m*n* + 2, where ‘m’ is the number of rows and ‘n’ is the number of columns in the matrices. The time complexity is O(m * n), where ‘m’ and ‘n’ are the dimensions of the matrices. Ques28: Write an algorithm for bubble sort and prove that T(n) = n2 . Ans:


Ques29: Write an algorithm for sequential search and explain best - case, worst – case, average – case efficiencies. Ans: Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy


Unit 1 Chapter-3 Single Dimensional Array


Unit 1 Chapter-3 Single Dimensional Array Questions & Answers: - Ques1: Define an Array Ans: An array is a collection of elements of the same type of data or an array is a sequence of data items of homogeneous value (same type) Ques2: What are the operations on Array? Ans: 1. Traversal: Processing each element in the array. 2. Search: Finding the location of the element with a given value in the array. 3. Insertion: Adding a new element into an array. 4. Deletion: Removing an element from an array. 5. Sorting: Arranging the elements in some type of order. 6. Merging: Combining two arrays into single array Ques3: Explain Array as Abstract data type. Ans: An array is a fixed-size sequence of elements of the same type. An array is a fundamental abstract data type (ADT). The basic operations include direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position.


Ques4: How arrays are represented in memory? Explain with diagram. Ans: In memory, all the data items are stored in contiguous memory locations one after the other. For example: - Int value [5] = {85,98,79,88,90}; Ques5: Write an algorithm to traverse an array. Ans: Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses array A and applies the operation PROCESS to each element of the array 1. Repeat for I =LB to UB Apply PROCESS to A[I] [End of For Loop] 2. Exit Explanation Here A is a linear array stored in memory with lower bound LB and upper bound UB. The for loop iterates from LB to UB and visits each element of the array. During this visit, it applies the operation PROCESS to the elements of the array A. the PROCESS operation could be any operation like adding a number to A[I], Modifying the value of A[I] etc., 85 98 79 88 90 VALUE [0] VALUE [1] 1200-1203 1204-1207 1212-1215 1208-1211 1216-1219 Array name→ VALUE [2] VALUE [3] VALUE [4] Data→ Address→


Ques6: Write an algorithm to insert an element into an array Ans: Algorithm to insert an element into an array Here A is a linear array with N elements, LOC is the location where ITEM is to be inserted 1. Set I=N [initialize counter] 2. Repeat While (I>=LOC) 3. Set A[I+1] =A[I] [Move elements to next location] 4. Set I=I-1 [Decrease counter by 1] [End of while loop] 5. Set A[LOC] = ITEM [Insert element at LOC position] 6. Set N=N+1 [Increment N by 1] 7. Exit Ques7: Write an element to delete an element from an array Ans: Algorithm to delete an element from an array Here A is a linear array with N elements.LOC is the location where ITEM is to be deleted 1. Set ITEM=A[LOC] [Assign the element to be deleted to ITEM] 2. Repeat for I=LOC to N 3. Set A[I]=A[I+1] [Move the Ith element to previous locations [End of for loop] 4. Set N=N-1 [Decrement N by 1] 5. Exit


Ques8: Write a program to demonstrate traverse, insert, and delete operations on array. Ans:


Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy


Unit 1 Chapter -4 Multi Dimensional Arrays


Click to View FlipBook Version