Listen to your teachers when they tell you WHAT to do. But more importantly, think about it later and ask yourself WHY they told you to do it.
--Your friends at LectureNotes

Note for Data Structure and Algorithms - DSA by Prakash Poudel

  • Data Structure and Algorithms - DSA
  • Note
  • Computer Science Engineering
  • B.Tech
  • 37 Offline Downloads
  • Uploaded 3 months ago
Prakash Poudel
Prakash Poudel
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

DATA STRUCTURES AND ALGORITHMS Student Handbook What is data? Facts and statistics collected together for reference or analysis is called data. It refers to the quantities, characters, or symbols on which operations are performed by a computer, which may be stored and transmitted in the form of electrical signals and recorded on magnetic, optical, or mechanical recording media. Data can exist in a variety of forms; as numbers or text on pieces of paper, as bits and bytes stored in electronic memory, or as facts stored in a person's mind. Strictly speaking, data is the plural of datum, a single piece of information. In practice, however, people use data as both the singular and plural form of the word. What is data structure? Data structure refers to the Interrelationship among data elements that determine how data is recorded, manipulated, stored, and presented by a database. It is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way. Data Structures is about rendering data elements in terms of some relationship, for better organization and storage. In simple language, Data Structures are structures programmed to store ordered data, so that various operations can be performed on it easily. Anything that can store data can be called as a data structure hence Integer, Float, Boolean, Char etc., all are data structures. They are known as Primitive Data Structures. Then we also have some complex Data Structures, which are used to store large and connected data. Some examples of Abstract Data Structure are:  Linked List  Tree  Graph  Stack, Queue etc. All these data structures allow us to perform different operations on data. We select these data structures based on which type of operation is required. We will look into these data structures in more details in our later lessons. © Er. PRAKASH POUDEL JIGYASU Page 1

Text from page-2

DATA STRUCTURES AND ALGORITHMS Student Handbook Why data structure is such an important subject? Sometimes it is difficult for students to view where they can apply knowledge that they get during studies. When we (software engineers, developers and etc.) were in bachelor degree we learnt a lot of different kind of data structures. In this hand book I would like to give some reasons, why it is important to learn complex data structures. Data structure is a particular way of storing and organizing information in a computer. So that it can be retrieved and used most productively. Different kinds of data structures are meant for different kinds of applications, and some are highly specialized to specific tasks. Data structures are important for the following reasons: 1. Data structures are used in almost every program or software system. 2. Specific data structures are essential ingredients of many efficient algorithms, and make possible the management of huge amounts of data, such as large integrated collection of databases. 3. Some programming languages emphasize data structures, rather than algorithms, as the key organizing factor in software design. 4. Use of appropriate data structure enables a computer system to perform its task more efficiently, by influencing the ability of computer to store and retrieve data from any location in its memory. 5. Almost every company that is hiring a software engineer would ask about data structures. 6. Choosing the right data structure for the algorithm is one of the key components in the world of software engineering. What is Abstract Data Type (ADT)? An abstract data type, sometimes abbreviated ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented. This means that we are concerned only with what the data is representing and not with how it will eventually be constructed. By providing this level of abstraction, we are creating an encapsulation around the data. The idea is that by encapsulating the details of the implementation, we are hiding them from the user’s view. This is called information hiding. Abstract data types are mathematical models of a set of data values or information that share similar behavior or qualities and that can be specified and identified independent of specific implementations. Abstract data types, or ADTs, are typically used in algorithms. An abstract data type is defined in term of its data items or its associated operations rather than by its implementation. A stack or a queue is an example of an ADT. It is important to understand that both stacks and queues can be implemented using an array. It is also possible to implement stacks and queues using a linked list. This demonstrates the "abstract" nature of stacks and queues: how they can be considered separately from their implementation. Figure shows a picture of what an abstract data type is and how it operates. The user interacts with the interface, using the operations that have been specified by the abstract data type. The abstract data type is the shell that the user interacts with. The implementation is hidden one level deeper. The user is not concerned with the details of the implementation. © Er. PRAKASH POUDEL JIGYASU Page 2

Text from page-3

DATA STRUCTURES AND ALGORITHMS Student Handbook There are two parts to each ADT: 1. The public or external part, which consists of:  the conceptual picture (the user's view of what the object looks like, how the structure is organized)  the conceptual operations (what the user can do to the ADT) 2. The private or internal part, which consists of:  the representation (how the structure is actually stored)  the implementation of the operations (the actual code) To best describe the term Abstract Data Type, it is best to break the term down into "data type" and then "abstract". Data type particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it. The word Abstract in our context stands for "considered apart from the detailed specifications or implementation". The benefits of using ADTs include:    Code is easier to understand (e.g., it is easier to see "high-level" steps being performed, not obscured by low-level code). Implementations of ADTs can be changed (e.g., for efficiency) without requiring changes to the program that uses the ADTs. ADTs can be reused in future programs. Introduction of Recursion Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. “A function that calls itself directly (or indirectly) to solve a smaller version of its task until a final call which does not require a self-call is a recursive function.” void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } Technically, a recursive function is a function that makes a call to itself. In any recursive process it is important to have a terminal condition (which we call the base condition) to assure that the recursive process will end at some point. The important thing to understand about recursion is that, it is the same algorithm we keep applying until we find a base case or termination condition. Recursion is a way to solve problems using a strategy call divide and conquer. Many problems can be solved recursively, For example, games of all types from simple ones like the Towers of Hanoi problem to complex ones like chess. © Er. PRAKASH POUDEL JIGYASU Page 3

Text from page-4

DATA STRUCTURES AND ALGORITHMS Student Handbook Recursive function can be categorized as: 1. Linear Recursion: This recursion is the most commonly used. In this recursion a function call itself in a simple manner and by termination condition it terminates. This process called 'Winding' and when it returns to caller that is called 'Un-Winding'. Termination condition also known as Base condition. 2. Binary Recursion: Binary Recursion is a process where function is called twice at a time in place of once at a time. Mostly it's using in data structure like operations for tree as traversal, finding height, merging, etc. 3. Tail Recursion: In this method, recursive function is called at the last. So it's more efficient than linear recursion method. Means you can say termination point will come (100%) only you have to put that condition. Recursion is of two types depending on whether a function calls itself from within itself, or whether two functions call one another mutually. The former is called direct recursion and the latter is called indirect recursion. Thus the two types of recursion are: 1. Direct recursion 2. Indirect recursion Int abc() { .....; .....; .....; abc(); } (b) Direct Recursion Int abc() { .....; xyz(); } Int xyz() . . . . .; abc(); } (a) Indirect Recursion Advantages  Reduce unnecessary calling of function.  Through Recursion one can solve problems in easy way while its iterative solution is very big and complex. Disadvantages   Recursive solution is always logical and it is very difficult to trace.(debug and understand). In recursive we must have an If statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return.  Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC.  Recursion uses more processor time.  Iteration, in the context of computer programming, is a process wherein a set of instructions or structures are repeated in a sequence a specified number of times or until a condition is met. When the first set of instructions is executed again, it is called iteration. When a sequence of instructions is executed in a repeated manner, it is called a loop. © Er. PRAKASH POUDEL JIGYASU Page 4

Lecture Notes