Data Structures

Share Embed Donate


Short Description

data structures...

Description

DATA STRUCTURES

Data structure :- It is a represen representatio tation n of the logical logical relation relationship ship existing existing between between individual elements of data. It is a way of organizing all the data items that considers not only the elements stored but also their relationship to each other.  Data Structure specifies 4 things :1. Orga Organi niza zati tion on of of data data.. 2. Acce Access ssin ing g of meth method ods. s. 3. Degre Degree e of of asso associa ciativ tivity ity.. 4. Processi Processing ng alter alternati natives ves for informat information. ion.  Structure of input and output data can be used to derive the structure of a program. Data Data struct structure ure affec affects ts the desig design n of both both struc structur tural al and and functi function onal al aspec aspects ts of the the program. Study of data structures involves two complementary goals:1. To iden identi tify fy and and deve develo lop p usef useful ul math mathem emat atic ical al enti entiti ties es and and oper operat atio ions ns and and to determine what classes of problems can be solved by using these entities and operations. 2. To determin determine e representat representations ions for abstract abstract entities entities and to implemen implementt the abstract abstract operations on these concrete representations. efficiency. Efficiency is  One important consideration in any implementation is its efficiency. usually measured by 2 factors, Time and Space. Space.  Operations on Data structures:1. Create:Create :- This operation results on reserving the memory for the program Execution. This can be done by declaration statement. Creation can be done at compile-time or run-time. 2. Destroy:Destroy:- It destroys the memory space allocated for specified data structure. malloc() and free() functions of C language are used for create and destroy operations as far as dynamic memory allocation and deallocation is concerned . 3. Selection:Selection :- It deals with accessing a particular data within a data structure . 4. Update:Update :- It updates or modifies the data in data structure. 5. Searching:Searching :- It finds the presence of the desired data item in the list of data items. 6. Sorting:Sorting: - This is the process of arranging all data items in a data structure in a particular order. 7. Merging:Merging :- This is a process of combining the data items of two different sorted lists into a single sorted list. .

1

Data Structure

Primitive

integers

floats

character

Non-primitive

pointers

Arrays

Lists

Linear

Stacks

Queues

Files

Non-Linear  

Trees

Graphs

STACKS:STACKS:It is a non-primitive non-primitive Linear data structure. It is an ordered list in which addition of new data item or deletion of an already existing data item is done at one end. And this end is called the Top of the stack. It is also called as LIFO Data structure. When stack is created, the stack base remains fixed while the stack top changes as elements are added and removed. 

Representation of stacks can be done in 2 ways:1. Static:- It is an array representation. 2. Dynamic:- It is a linked list representation. Operations on stack:1. Push(s,i); This adds a new data item ‘I’ on to stack ’s’. The number of data items on stack should not exceed MAXSIZE. 2. i=pop(s); It deletes an item ‘I’ from stack s. The number of data items should not be deleted beyond 0. 3. i=stacktop(s); It returns the top element of the stack. 4. isempty(s); It returns true if the stack is empty. Otherwise it returns false. 5. isfull(s); It returns true if the stack is full, else it returns false.

2

Data Structure

Primitive

integers

floats

character

Non-primitive

pointers

Arrays

Lists

Linear

Stacks

Queues

Files

Non-Linear  

Trees

Graphs

STACKS:STACKS:It is a non-primitive non-primitive Linear data structure. It is an ordered list in which addition of new data item or deletion of an already existing data item is done at one end. And this end is called the Top of the stack. It is also called as LIFO Data structure. When stack is created, the stack base remains fixed while the stack top changes as elements are added and removed. 

Representation of stacks can be done in 2 ways:1. Static:- It is an array representation. 2. Dynamic:- It is a linked list representation. Operations on stack:1. Push(s,i); This adds a new data item ‘I’ on to stack ’s’. The number of data items on stack should not exceed MAXSIZE. 2. i=pop(s); It deletes an item ‘I’ from stack s. The number of data items should not be deleted beyond 0. 3. i=stacktop(s); It returns the top element of the stack. 4. isempty(s); It returns true if the stack is empty. Otherwise it returns false. 5. isfull(s); It returns true if the stack is full, else it returns false.

2



Algorithms: Assume that the top value is initialized as top= -1. -1. 1. Push operation:operation:push(int stack[],int maxsize, int top) { int item; if(top==maxsize-1)) { printf(“stack is full”); return; } else { printf(“enter the element to be inserted”); scanf(“%d”,&item); top=top+1; stack[top]=item; } } 2. Pop Ope Opera rati tion on::pop(int stack[], int top) { int item; if(top==-1) { printf(“stack is empty”); return; } else { item=stack[top]; top=top-1; } return(item); }



Applications of stack:stack :1. Recursio rsion n. 2. Keep Keepin ing g track track of of funct function ion call calls. s. 3. Evaluat Evaluation ion of Expres Expression sions s such as infix, infix, prefix prefix,, postfix. postfix. 4. Reve Reversi rsing ng the charac character ters. s. 5. Servi Servicin cing g hardwa hardware re interr interrup upts. ts. 6. Solving Solving combi combinato natorial rial problem problems s using backt backtrack racking. ing.

3

Infix, prefix, postfix expressions:An Expression in any programming language is a meaningful combination of  operands and operations. Operands may be of int, float, or double. The operators may be arithmetic, logical , relational, or bitwise.  There are 3 ways of writing Arithmetic expressions:1.Infix:- Operator is placed between the two operands. Eg: a+b 2.Prefix- Operator is placed before its two operands. Eg: +ab 3.Postfix: Operator is placed after its two operands. Eg: ab+  Order of precedence of operators(Highest to lowest): 1. Exponentiation  Right to Left evaluation.  Left to Right 2. * /  Left to Right 3. + By using parenthesis, we can override the default precedence.  The prefix form of a complex expression is not the mirror image of the postfix form.  Parenthesis are never needed in prefix and postfix expressions.  Converting an infix expression into postfix expression: Steps: 1. Expressions within innermost parentheses must first be converted to postfix, so that they can be treated as single operands. IN this fashion, parentheses can be successfully eliminated until the entire expression is converted. 2. From the given infix expression, if the scanned symbol is an operand, then it is immediately placed on the postfix string. 3. If an operator is scanned, and the stack is found to be empty, then the operator is placed on the stack. 4. If the precedence of the new symbol is greater than the precedence of the symbol on the top of the stack, then the new symbol is pushed on to the stack. 5. If the precedence of the new operator is less than the top of the stack, the operator at the top of the stack is popped and sent to postfix string, and then incoming symbol is compared with the new top symbol, and so on. 6. As the parenthesis changes the order of operations, a) when the ‘(‘ parentheses is scanned, it is pushed on to the stack. b) When its associated right parentheses is found, all the operators between the two parentheses are placed on the postfix string, because they are to be executed before any operators appearing after the parentheses.  Postfix Evaluation: Each operator in a postfix string refers to the previous two operands in the string. Of course, one of these two operands may itself be the result of applying a previous operator. Steps: 1. If an operand is read, it is pushed onto the stack. 2. If an operator is read, then the top two elements of the stack will be the two operands. These two operands are popped, and the indicated operation is performed on them, and the result is pushed back onto the stack, so that it will be available for use as an operand of the next operator.

4

QUEUES:  It is a non-primitive Linear data structure.  It is an ordered collection of items from which items are inserted at Rear end, and deleted at front end.  It is also called as FIFO data structure, because the first element inserted into the \ Queue is the first element to be removed.  Operations:1. insert(q, x); It inserts item ‘x’ at the rear end of the queue q. 2. x=remove(q); It deletes the element from the front end if the queue q, and sets ‘x’ to its contents. 3. empty(q): It returns true if the Queue is empty. Otherwise it returns false. Representation of Queue using C can be done in 2 ways :1. Static implementation. 2. Linked list representation.  Algorithms: If initially Front=Rear=0 1. insert():- This operation inserts a new item at the rear end of the Queue. Qinsert() { if(rear==maxsize-1) { printf(“queue is full”); return; } a[rear]=item; rear=rear+1; } 2. delete():- It deletes and returns the data item at the front of the queue. Qdelete() { if(front==0) { printf(”Queue is empty’); return; } item=q[front]; if(front==rear) front=rear=0; else front=front+1; return; } 3. display(): Displays the elements of the queue. 

5

Qdisplay() { int i; if(front==0) { printf(“Queue is empty”); return; } else { for(i=front; i For any pair of nodes, there exists only one path between them, and the insertion of any edge to a spanning tree forms a unique circle.

28

-->The cost of a spanning tree is the sum of the weights of the tree edges. --> A minimum cost spanning tree is formed when the edges are picked to minimize the total cost. -->If a depth first search is used, those edges traversed by the algorithm form the edges of the tree, referred to as depth First Spanning Tree. --> If Breadth first search is used, the spanning tree is formed from those edges traversed during the search producing a Breadth First Spanning Tree . -->The most efficient graph representation for this algorithm is an edge list sorted by increasing order of weight. Eg: A

B

C

D

E

F

. A

A

B

C

D

E

B

C

D

E

F F

DFS

BFS

29

UNIT -5 ASSIGNMENT

1. What is a linked list? What are the different types of linked lists available? 2. Write a C program to form a list containing the union of elements of two lists and another list containing the intersection of the elements of the same two lists? 3. Explain inorder,preorder, postorder traversals with examples? 4. What are the different methods available for representing a graph? 5. Write an algorithm for deleting a node from a Binary search tree? 6. Write a full binary tree of depth 4 with sequential node numbers? 7. Explain with example for Breadth First Search and Depth first search of a graph? 8. Write a program to create a Linear linked list and print out the list? 9. Write algorithms for inserting and deleting nodes from Circular linked list? 10.Write algorithms for inserting and deleting nodes from Doubly linked list? 11. What are spanning trees? Explain the different types of spanning trees available?

30

UNIT-6

 Sorting And Searching Techniques There are several applications in which we store huge amount of data and search for a piece of information within it. We can present the methods that locate the desired thing rather than the way the elements are organized. Basic searching techniques:- Some searching techniques are very faster and few of them are slower in finding the desired element in a file. A file is a meaningful collection of records. There is a key associated with each record which distinguishes them. Search Algorithm:Procedure for searching a desired record with in a given key element is as follows:1. Accept a key element. 2. Search begins and tries to locate the record matching with the given key element. 3. If the desired record is found in a file, then searching process signals “Successful” message and returns the entire record or pointer to that record. 4. Otherwise, it signals an “Unsuccessful” message. Searching Techniques: 1. Linear Searching(sequential): This is the simplest of all the searching techniques. An unordered search table is scanned from its first record until the desired record is found. Linear-search(K,N,X) . An unordered vector K is given. It consists of N+1 elements. And it searches for X. The function returns the index of the vector element if the search is successful, and returns zero otherwise.

31

1.[Initialize search] I1 K[N+1] X 2. [Search the vector] Repeat while K[I] X I  I +1 3. [Successful Search] if I= N+1 then write ‘ Unsuccessful ‘ return(0) else write ‘successful ‘ return(1) The first step of the algorithm initializes the key value of the sentinel record to x. In the second step, a sequential search is then performed on the n+1 records. If the index of the record found denotes record R n+1 then the search has failed, otherwise, the search is successful; and I contains the inex of the desired element.  the average and worst search time for Linear method is O(n).

2. Binary searching: The entries in the table are stored in alphabetically or  numerically increasing order. The appropriate middle entry of te table is located, and its key value is examined. If its value is too high, then the key value of the middle entry of the first half of the table is examined and the procedure is repeated on the first half  until the required item is found. function Binary-search(K,N,X) K: It consists of N elements in ascending order. Low : It is the lower limit of the search interval. Middle: It is the upper limit of the search interval. High : It is the upper limit of the search interval. 1. [Initialize] Low  1 High  N 2. [Perform Search] Repeat thru step 4 while Low K[MIDDLE] then Low  MIDDLE +1 else Write ‘ Successful Search ‘ return (MIDDLE). 5. [Unsuccessful Search] write ‘ Unsuccessful search ‘ return. 

the average and worst search time for Binary search method is O(log2n).

 Sorting Techniques Sorting:Process of re-arranging a given set of elements in a specific (ascending or  descending) order. Uses:Used as an aid in searching. As a means for matching entries in files. Applications in operations research and job scheduling. Comparison No. of records Less More More

key size --short long

Sorting method   Selection sort or Bubble sort radix sort quick sort or heap sort or merge sort

If records are initially almost sorted, avoid quick sort method. Internal sorting Methods to be used when the file to be sorted is small enough so that the entire sort can be carried out in main memory. Requires excessive data movement. : Insertion sort Quick sort Heap sort Radix sort Limitation Slows down the sorting process when records are large. 33

External sorting  They are the methods that can be used on larger files.  Most popular method is Merge sort.

Merge sort method It consists of two distinct phases: 1. Segments of the input file are sorted using an internal sort method. The sorted segments (runs) are written out onto external storage as they are generated. 2. The runs generated in phase 1 are merged together following the merge tree pattern until only one run is left.

Exchange / Bubble Sort 1. Repeat through step 4 a total of n-1 times. 2. Repeat step 3 for elements in unsorted portion of the array. 3. If the current element in the array > next element in the array then exchange elements. 4. If no exchanges were made then return else reduce the size of the unsorted array by one. Procedure Bubble-Sort (K, N) K Array of N elements PASS Pass counter variable LAST Position of the last unsorted element I Index EXCHS Variable to count the number of exchanges made on any pass. 1. [Initialise] LAST ← N-1; PASS ← 1; 2. [Initialise exchanges counter for this pass] EXCHS ← 0 3. [Perform pair-wise comparisons on unsorted elements]. Repeat for I = 1, 2, …, LAST if K[I] > K[I+1] then K[I] ↔ K[I+1] EXCHS ← EXCHS + 1 4. [Any exchanges made on this pass?] if EXCHS = 0 then return else LAST ← LAST – 1; (reduce size of unsorted list) PASS ← PASS + 1; 34

if PASS = N – 1 return else go to Step 2. The time complexity for bubble sort method is: Best case O(n) Average Case O(n 2) Worst Case O(n 2) Selection Sort Beginning with the first element in the array, a search is performed to locate the element which has the smallest key. When this element is found, it is interchanged with the first element in the array. This interchange places the element with the smallest key, in the first position of the array. A search for the second element with smallest key is then carried out by examining the keys of the elements from the second element onwards. The element, which has the smallest key, is interchanged with the element located in the record position of the array. The process continues until all records have been sorted in ascending order. Procedure Selection_Sort (K, N) K Array of N elements PASS Pass index variable MIN_INDEX Position variable of the smallest element I Index 1. PASS = 1 2. [Initialise minimum index] MIN_INDEX ← PASS 3. [Make a pass and obtain element with smallest value] repeat for I = PASS + 1, PASS + 2, …, N if K[I] < K[MIN-INDEX] then MIN-INDEX ← I 4. [Exchange elements] if MIN-INDEX ≠ PASS then K [PASS] ↔ K[MIN-INDEX] PASS ← PASS + 1; if PASS ≠ N then go to Step 2 else return; The time complexity for Selection Sort Method is: Best Case O(n 2) Average Case O(n 2) Lowest Case O(n 2) Quick / Partition Exchange Sort 35

At each step, a particular element is placed in its final position within the list. All elements, which precede this element, have smaller  keys/values, while all elements that follow it have larger keys. This technique partitions the list into two sub-lists. The same process can then be applied to each of these sub-lists and repeated until all elements are placed in their final positions. It performs well on larger list of elements. Procedure Quick_Sort(K, LB, UB) K Array of n elements LB lower bound of the current sub-list. UB upper bound of the current sub-list. I, J Index variable KEY key value FLAG logical variable 1. [Initialise] FLAG ← true 2. [Perform Sort] if LB < UB then I ← LB J ← UB + 1 KEY ← K[LB] Repeat while (FLAG) I ←I + 1 Repeat while K[I] < KEY (Scan the keys from left to right) I ←I + 1 J ←J – 1 Repeat while K[J] > KEY (Scan the keys from right to left) J ←J – 1 if I < J then K[I] ↔ K[J] (interchange elements) else FLAG ← false K[LB] ↔ K[J] (interchange elements) Call Quick_Sort (K, LB, J – 1) (Sort first sub-list) Call Quick_Sort (K, J + 1, UB) (Sort second sub-list) 3. [Finished] Return The time complexity of Quick Sort method is: Best Case O(nlog 2n) Average Case O(nlog 2n) 2 Worst Case O(n )

36

Heap Sort:Heap sort method consists of two phases, namely, construction phase and a traversal phase. The construction Phase consists of successively inserting a new element in a tree structure. The tree obtained from this phase can be traversed in inorder. In a list of elements, heap satisfies the property K j ≤ Ki for 2 ≤ j ≤ n and i =   j/2 . The binary tree is allocated sequentially such that the indices of the left and right sons (if they exist) of element i are 2i and 2i + 1, respectively i.e. the index of the parent of element j if (if it exists) is   j/2 . The element with the largest key is at the root of the tree (also called the top of  the heap). The starting point is to have a heap initially with one element tree and then, insert a new element into the existing heap such that a new heap is formed after performing the insertion. Insertions are performed repeatedly until all elements in the original list form a heap. Procedure CREATE-HEAP (K, N) K Array of N elements Q, I, J Index variable KEY key of the element 1. [Build Heap] repeat thru step 7 for Q = 2, 3, …, N 2. [Initialise construction phase] I ←Q KEY ← K[Q] 3. [Obtain parent of new element] J ← TRUNC (I / 2) 4. [Place new element in existing heap] repeat thru step 6 while I > 1 and KEY > K[J] 5. [Interchange element] K[I] ↔ K[J] 6. [Obtain next parent] I ←J J ← TRUNC (I / 2) if J < 1 then J ←1 7. [Copy new element into its proper place] K[I] ← KEY 8. [Finished] Return Procedure Heap-Sort (K, N) K Array of N elements Q Pass Index variable I Index variable 37

J KEY

Index variable Integer variable

1. [Create the initial heap] Call CREATE_HEAP (K, N) 2. [Perform Sort] repeat thru step 10 for Q = N, N – 1, …, 2 3. [Exchange element] K[1] ↔ K[Q] 4. [Initialise pass] I ←1 KEY ← K[1] J ←2 5. [Obtain index of largest son of new element] if J + 1 < Q then if K[J+1] > K[J] then J ← J +1 6. [Reconstruct the new heap] repeat thru step 10 while (j ≤ Q – 1 and K[J] > KEY) 7. [interchange element] K[I] ↔ K [J] 8. [Obtain next left son] I ←J J ←2 * I 9. [Obtain index of next largest son] if J + 1 < Q then if K[J+1] > K [J] then J ← J +1 else if J > N then J ← N 10. [Copy element into its proper place] K[I] ← KEY 11. [Finished] Return The time complexity of heap sort is: Best Case O(nlog 2n) Average Case O(nlog 2n) Worst Case O(n 2)

Algorithm Selection_sort Bubble_sort Merge_sort Quick_sort Heap-sort

Average Case n 2/4 n 2/4 O(nlog 2n) O(nlog 2n) O(nlog 2n)

Worst case Space usage 2 n /4 In space 2 n /2 In space O(nlog 2n) Extra n entries. 2 n /2 Extra log 2n entries O(nlog 2n) In space

38

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF