Data Structures Lecture Notes: Introduction to Algorithms and Data Structures, Exercises of Data Structures and Algorithms

Algorithms: Definition, Properties, Performance Analysis-Space Complexity, Time Complexity,. Asymptotic Notations.Data structures: Introduction, Data ...

Typology: Exercises

2022/2023

Uploaded on 05/11/2023

lalitdiya
lalitdiya 🇺🇸

4.3

(26)

240 documents

1 / 97

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DATA STRUCTURES
LECTURE NOTES
Dr.K VENKATA NAGENDRA
Mr.G.RAJESH
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61

Partial preview of the text

Download Data Structures Lecture Notes: Introduction to Algorithms and Data Structures and more Exercises Data Structures and Algorithms in PDF only on Docsity!

DATA STRUCTURES

LECTURE NOTES

Dr.K VENKATA NAGENDRA

Mr.G.RAJESH

DATA STRUCTURES(18CS202)

OBJECTIVES:

The course should enable the students to :

  1. Demonstrate familiarity with major algorithms and data structures.
  2. Choose the appropriate data structure and algorithm design method for a specified application.
  3. Determine which algorithm or data structure to use in different scenarios.
  4. To improve the logical ability. UNIT-I INTRODUCTION TO ALGORITHMS AND DATA STRUCTURES Classes: Algorithms: Definition, Properties, Performance Analysis-Space Complexity, Time Complexity, Asymptotic Notations. Data structures: Introduction, Data Structures types, DS Operations. UNIT-II STACKS AND QUEUES Classes: Stacks: Introduction, Stack Operations, Applications: Infix to Postfix Conversion, Evaluation of Postfix Expression. ueues: Introduction, Operations on queues, Circular queues, Priority queues. UNIT-III LINKED LISTS AND APPLICATIONS Classes: Linked lists: Introduction, Singly linked lists, Circular linked lists, Doubly linked lists, Multiply linked lists, Applications: Polynomial Representation.Implementation of Stack and Queue using linked list. UNIT-IV SORTING AND SEARCHING Classes: Sorting: Introduction, Selection sort, Bubble sort, Insertion sort, Merge sort, Quick sort, Heap Sort. Searching: Introduction, Linear search, Binary search, Fibonacci search. UNIT-V TREES AND BINARY TREES Classes: Trees: Introduction, Definition and basic terminologies, Representation of trees. Binary Trees: Basic Terminologies and Types, Binary Tree Traversals, Binary Search Trees. Text Books:
  5. G.A.V PAI, Data Structures and Algorithms, Concepts, Techniques and Applications, Volume1, 1stEdition, Tata McGraw-Hill, 2008.
  6. Richard F. Gilberg& Behrouz A. Forouzan, Data Structures, Pseudo code Approach with C, 2ndEdition, Cengage Learning India Edition, 2007. Reference Books:
  7. Langsam,M. J. Augenstein, A. M. Tanenbaum, Datastructures using C and C++, 2nd Edition, PHI Education, 2008.
  8. Sartaj Sahni, Ellis Horowitz, Fundamentals of at Structures in C, 2nd Edition, Orientblackswan, 2010. Web References:
  9. https://www.geeksforgeeks.org/data-structures/
  10. https://www.programiz.com/dsa
  11. https://www.w3schools.in/data-structures-tutorial/intro/ Outcomes: At the end of the course students able to
  12. Apply Concepts of Stacks, Queues, Linked Lists.
  13. Develop Programs for Searching and Sorting, Trees.
  14. Interpret concepts of trees.
  15. Develop programs for Sorting and Searching.

Iteration: Iteration type algorithms are used in solving the problems which involves repetition of statement. In this type of algorithms, a particular number of statements are repeated ‘n’ no. of times. Example1: Step 1 : start Step 2 : read n Step 3 : repeat step 4 until n> Step 4 : (a) r=n mod 10 (b) s=s+r (c) n=n/ Step 5 : write s Step 6 : stop Performance Analysis an Algorithm: The Efficiency of an Algorithm can be measured by the following metrics. i. Time Complexity and ii. Space Complexity. i.Time Complexity: The amount of time required for an algorithm to complete its execution is its time complexity. An algorithm is said to be efficient if it takes the minimum (reasonable) amount of time to complete its execution. ii. Space Complexity: The amount of space occupied by an algorithm is known as Space Complexity. An algorithm is said to be efficient if it occupies less space and required the minimum amount of time to complete its execution.

  1. Write an algorithm for roots of a Quadratic Equation? // Roots of a quadratic Equation Step 1 : start Step 2 : read a,b,c Step 3 : if (a= 0) then step 4 else step 5 Step 4 : Write “ Given equation is a linear equation “ Step 5 : d=(b * b) _ (4 *a *c) Step 6 : if ( d>0) then step 7 else step Step 7 : Write “ Roots are real and Distinct” Step 8: if(d=0) then step 9 else step 10 Step 9: Write “Roots are real and equal” Step 10: Write “ Roots are Imaginary” Step 11: stop

2. Write an algorithm to find the largest among three different numbers entered by user Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b If a>c Display a is the largest number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the greatest number. Step 5: Stop 3.Write an algorithm to find the factorial of a number entered by user. Step 1: Start Step 2: Declare variables n,factorial and i. Step 3: Initialize variables factorial← i← Step 4: Read value of n Step 5: Repeat the steps until i=n 5.1: factorial←factorial*i 5.2: i←i+ Step 6: Display factorial Step 7: Stop 4.Write an algorithm to find the Simple Interest for given Time and Rate of Interest. Step 1: Start Step 2: Read P,R,S,T. Step 3: Calculate S=(PTR)/ 100 Step 4: Print S Step 5: Stop

ASYMPTOTIC NOTATIONS

Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-time performance. Using asymptotic analysis, we can very well conclude the best case, average case, and worst case scenario of an algorithm. Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work in a constant time. Other than the "input" all other factors are considered constant. Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation. For example, the running time of one operation is computed as f (n) and may be for another operation it is computed as g (n^2 ). This means the first operation running time will increase linearly with the increase in n and the running time of the second operation will increase exponentially when n increases. Similarly, the running time of both operations will be nearly the same if n is significantly small.

θ( f (n)) = { g (n) if and only if g (n) = Ο( f (n)) and g (n) = Ω( f (n)) for all n > n 0. } DATA STRUCTURES Data may be organized in many different ways logical or mathematical model of a program particularly organization of data. This organized data is called “Data Structure”. Or The organized collection of data is called a ‘Data Structure’. Data Structure involves two complementary goals. The first goal is to identify and develop useful, mathematical entities and operations and to determine what class of problems can be solved by using these entities and operations. The second goal is to determine representation for those abstract entities to implement abstract operations on this concrete representation. Primitive Data structures are directly supported by the language ie; any operation is directly performed in these data items. Ex: integer, Character, Real numbers etc. Non-primitive data types are not defined by the programming language, but are instead created by the programmer.

Data Structure=Organized data +Allowed operations

Linear data structures organize their data elements in a linear fashion, where data elements are attached one after the other. Linear data structures are very easy to implement, since the memory of the computer is also organized in a linear fashion. Some commonly used linear data structures are arrays, linked lists, stacks and queues. In nonlinear data structures, data elements are not organized in a sequential fashion. Data structures like multidimensional arrays, trees, graphs, tables and sets are some examples of widely used nonlinear data structures. Operations on the Data Structures: Following operations can be performed on the data structures:

  1. Traversing
    1. Searching
    2. Inserting
    3. Deleting
    4. Sorting
    5. Merging 1. Traversing- It is used to access each data item exactly once so that it can be processed. 2. Searching- It is used to find out the location of the data item if it exists in the given collection of data items. 3. Inserting- It is used to add a new data item in the given collection of data items. 4. Deleting- It is used to delete an existing data item from the given collection of data items. 5. Sorting- It is used to arrange the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data. 6. Merging- It is used to combine the data items of two sorted files into single file in the sorted form.

Representation of Stack (or) Implementation of stack: The stack should be represented in two ways:

  1. Stack using array
  2. Stack using linked list 1. Stack using array: Let us consider a stack with 6 elements capacity. This is called as the size of the stack. The number of elements to be added should not exceed the maximum size of the stack. If we attempt to add new element beyond the maximum size, we will encounter a stack overflow condition. Similarly, you cannot remove elements beyond the base of the stack. If such is the case, we will reach a stack underflow condition. 1.push(): When an element is added to a stack, the operation is performed by push(). Below Figure shows the creation of a stack and addition of elements using push(). Initially top=- 1 , we can insert an element in to the stack, increment the top value i.e top=top+1. We can insert an element in to the stack first check the condition is stack is full or not. i.e top>=size- 1. Otherwise add the element in to the stack. void push() { int x; if(top >= n-1) { printf("\n\nStack Overflow.."); return; } else { printf("\n\nEnter data: "); scanf("%d", &x); stack[top] = x; top = top + 1; printf("\n\nData Pushed into the stack"); } } Algorithm: Procedure for push(): Step 1: START Step 2: if top>=size-1 then Write “ Stack is Overflow” Step 3: Otherwise 3.1: read data value ‘x’ 3.2: top=top+1; 3.3: stack[top]=x; Step 4: END

2.Pop(): When an element is taken off from the stack, the operation is performed by pop(). Below figure shows a stack initially with three elements and shows the deletion of elements using pop(). We can insert an element from the stack, decrement the top value i.e top=top- 1. We can delete an element from the stack first check the condition is stack is empty or not. i.e top==- 1. Otherwise remove the element from the stack. Void pop() { If(top==-1) { Printf(“Stack is Underflow”); } else { printf(“Delete data %d”,stack[top]); top=top-1; } } Algorithm: procedure pop(): Step 1: START Step 2: if top==-1 then Write “Stack is Underflow” Step 3: otherwise 3.1: print “deleted element” 3.2: top=top-1; Step 4: END 3.display(): This operation performed display the elements in the stack. We display the element in the stack check the condition is stack is empty or not i.e top==-1.Otherwise display the list of elements in the stack.

display(); break; } case 4: { printf("\n\t EXIT POINT "); break; } default: { printf ("\n\t Please Enter a Valid Choice(1/2/3/4)"); } } } while(choice!=4); return 0; } void push() { if(top>=n-1) { printf("\n\tSTACK is over flow"); } else { printf(" Enter a value to be pushed:"); scanf("%d",&x); top++; stack[top]=x; } } void pop() { if(top<=-1) { printf("\n\t Stack is under flow"); } else { printf("\n\t The popped elements is %d",stack[top]); top--; } } void display() { if(top>=0) {

printf("\n The elements in STACK \n"); for(i=top; i>=0; i--) printf("\n%d",stack[i]); printf("\n Press Next Choice"); } else { printf("\n The STACK is empty"); } }

2. Stack using Linked List: We can represent a stack as a linked list. In a stack push and pop operations are performed at one end called top. We can perform similar operations at one end of list using top pointer. The linked stack looks as shown in figure. Applications of stack:

  1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
  2. Stack is used to evaluate a postfix expression.
  3. Stack is used to convert an infix expression into postfix/prefix form.
  4. In recursion, all intermediate arguments and return values are stored on the processor’s stack.
  5. During a function call the return address and arguments are pushed onto a stack and on return they are popped off. Converting and evaluating Algebraic expressions: An algebraic expression is a legal combination of operators and operands. Operand is the quantity on which a mathematical operation is performed. Operand may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator is a symbol which signifies a mathematical or logical operation between the operands. Examples of familiar operators include +, - , *, /, ^ etc.

Evaluation of postfix expression: The postfix expression is evaluated easily by the use of a stack.

  1. When a number is seen, it is pushed onto the stack;
  2. When an operator is seen, the operator is applied to the two numbers that are popped from the stack and the result is pushed onto the stack.
  3. When an expression is given in postfix notation, there is no need to know any precedence rules; this is our obvious advantage.

Representation of Queue (or) Implementation of Queue: The queue can be represented in two ways:

  1. Queue using Array
  2. Queue using Linked List 1.Queue using Array: Let us consider a queue, which can hold maximum of five elements. Initially the queue is empty. Now, insert 11 to the queue. Then queue status will be: Next, insert 22 to the queue. Then the queue status is: Again insert another element 33 to the queue. The status of the queue is: Now, delete an element. The element deleted is the element at the front of the queue.So the status of the queue is: Again, delete an element. The element to be deleted is always pointed to by the FRONT pointer. So, 22 is deleted. The queue status is as follows: Now, insert new elements 44 and 55 into the queue. The queue status is:

Next insert another element, say 66 to the queue. We cannot insert 66 to the queue as the rear crossed the maximum size of the queue (i.e., 5). There will be queue full signal. The queue status is as follows: Now it is not possible to insert an element 66 even though there are two vacant positions in the linear queue. To overcome this problem the elements of the queue are to be shifted towards the beginning of the queue so that it creates vacant position at the rear end. Then the FRONT and REAR are to be adjusted properly. The element 66 can be inserted at the rear end. After this operation, the queue status is as follows: This difficulty can overcome if we treat queue position with index 0 as a position that comes after position with index 4 i.e., we treat the queue as a circular queue. Queue operations using array: a.enqueue() or insertion(): which inserts an element at the end of the queue. void insertion() { if(rear==max) printf("\n Queue is Full"); else { printf("\n Enter no %d:",j++); scanf("%d",&queue[rear++]); } } Algorithm: Procedure for insertion(): Step-1:START Step-2: if rear==max then Write ‘Queue is full’ Step-3: otherwise 3.1: read element ‘queue[rear]’ Step-4:STOP b.dequeue() or deletion(): which deletes an element at the start of the queue. void deletion() { if(front==rear) { printf("\n Queue is empty"); } else { printf("\n Deleted Element is %d",queue[front++]); x++; } } Algorithm: procedure for deletion(): Step-1:START Step-2: if front==rear then Write’ Queue is empty’ Step-3: otherwise 3.1: print deleted element Step-4:STOP