Data Structure for Professions

85
1 ! " # $ # " %& ! # % ’())*+ &, -. /0 $1

Transcript of Data Structure for Professions

1

! "

#

$

#

"

%&

! #

%

'())*+&,

-.

/0

$1

2

SYLLABUS

BRAHMAPUR UNIVERSITY, ORISSA (INDIA)

UNIT -1 Data, Data structure, algorithmic notation, Complexity (Fundamental

idea only), Operations on data structure, linear array (representation,

traversal, insertion, deletion, reverses), two-dimensional arrays

(representation only). Record: record structure, representation of

records in memory, Parallel arrays, matrices.

UNIT-2

Stack, Operations on stack, few applications of stack, linear queue,

circular queue, priority queue; Single linked list. Memory

representation of linked list, traversing a linear list, searching a linked

list. Insertion into and deletion from linked list, reversing a linked list.

Circular list, double linked list.

UNIT – 3

Tree, Binary tree, Representation of binary tree in memory, Binary

tree traversal, Binary search tree, Threaded binary tree, AVL tree

(Idea only).

UNIT – 4

Searching: Linear and binary searching. Sorting: selection, Bubble,

Insertion, Quick, Heap and merge sort.

3

CONTENTS

UNIT - I

Introduction.....................................................................................................................04 Data...................................................................................................................................02 Data types ...........................................................................................................02 Abstract Data Types..........................................................................................03 Algorithm ........................................................................................................................05 Definition of an algorithm................................................................................05 Complexity ......................................................................................................................07 Asymptotic Analysis.........................................................................................07 Tradeoff between Space & Time Complexity................................................07 Algorithm Performance.................................................................................................08 Classification of Data Structure ....................................................................................08 Operation of data structure...........................................................................................09 Array ................................................................................................................................10 ADT of an array .................................................................................................11 Operation of linear array..................................................................................11 Multi-dimensional arrays.................................................................................13 Representation of arrays...................................................................................15 Application of arrays ........................................................................................17 Parallel arrays ....................................................................................................17 Concepts of records........................................................................................................18 Check your progress (Unit – 1).....................................................................................20

UNIT – II

Introduction of Stack......................................................................................................22 Abstract data type – Stack ................................................................................24 Representation of stack.....................................................................................24 Operations on Stack ..........................................................................................25 Stack Terminology.............................................................................................25 Algorithms for stack operations......................................................................25

Application of stack ..........................................................................................27 Polish Notations.................................................................................................27 Notations Conversions .....................................................................................28 Operator precedence.........................................................................................29 Evaluation of a postfix expression ..................................................................35 Evaluation of a prefix expressions ..................................................................36

Queue ...............................................................................................................................38 Implementation of queues ...............................................................................38 Circular queue....................................................................................................39 Priority queue ....................................................................................................41 Double ended queue .........................................................................................41

Linked Lists .....................................................................................................................42 Types of linked lists...........................................................................................43 Traversing a linked list .....................................................................................43 Application of linked lists ................................................................................47

Check your progress (Unit – II) ...................................................................................48

4

UNIT – III Introduction.....................................................................................................................50 What is Tree?...................................................................................................................50 Tree terminology ............................................................................................................51 Binary tree........................................................................................................................52 Properties of a binary tree ................................................................................53 Strictly binary tree ..........................................................................................................53 Extended binary tree ......................................................................................................54 Array representation of binary tree .............................................................................55 Linked list representation of binary tree .....................................................................56 Creation of binary tree ...................................................................................................57 Operations of binary tree...............................................................................................58 Traversal of a binary tree...............................................................................................58 AVL tree (Balanced Tree) ..............................................................................................60 Check your progress (Unit – III) ..................................................................................62

UNIT – IV

Searching & Sorting .......................................................................................................64 Linear or Sequential Search...........................................................................................65 Binary Search...................................................................................................................66 Bubble sort.......................................................................................................................68 Selection sort ...................................................................................................................69 Quick sort ........................................................................................................................71 Check your progress (Unit – IV) ..................................................................................72

UNIT – V

Graphs..............................................................................................................................74 Undirected Graph...........................................................................................................75 Directed Graph or Digraph...........................................................................................76 Weighted graph ..............................................................................................................77 Traversal of graphs.........................................................................................................78 Depth-first search (DFS) ...............................................................................................78 Breadth-first search (BFS) .............................................................................................80 Check your progress (Unit – V)

Appendixes Suggested Readings...........................................................................................................82

5

U N I T - I

6

! ! ! !

""""# $ # $ # $ # $

UNIT -I

OBJECTIVES

After going through this unit, you should be able to:

• Understand the concepts of algorithms.

• To understand various asymptotic notations such as ‘Big O’ Theta & Omega (big

O, theta, Omega) notations for analysis of algorithm.

• Understand various principles for the complexity of the algorithms.

• String processing linear array, matrices etc

1. INTRODUCTION

1.1. DATA

Data is simply a value or set of values. Data items that are divided into sub-items are called group of items. For example, a student’s name may be divided into three sub-items, first name, middle name and last name but the student would normally be treated as a single item.

7

1.2. DATA TYPE

A data type specifies a collection of values and a set of operations on those values. The term abstract data type refers to the basic mathematical concept that defines the data types.

1.3. ABSTRACT DATA TYPES (ADT) ADT defines a data type without knowing the actual implementation of a

programming language. The actual implementation can be done by the programmer or user. Abstraction refers to the act of representations essential features without including the background details or any explanations. An Abstract Data type consists of two parts:

(a) Value definition (b) Operator definition

The Value Definition defines the collection of the values for the ADT and consists of

two parts. (i) A definition clause (ii) A condition clause

The definition clause is necessary while the condition clause is optional. The Operator Definitions are divided into three parts as: header, the optional condition and the post condition.

2. ALGORITHM

A common person’s belief is that a computer can do anything. This is far from truth. In realty, a computer can perform only certain predefined instructions which are designed by human. Roughly, a sequence of instructions designed is known as algorithm and the same algorithm designed by a specific computer language is called a program. Analysis of algorithm has been an area of research in computer science; evolution of very high speed computers has not diluted the need for the design of time-efficient algorithms. Analysis of algorithm is a field of computer science whose overall goal is understands the complexity of algorithms. To analyze an algorithm is to determine the amount of resource (i.e. time for processing and space for storing information) for proper utilization. Most algorithms are designed to work with inputs of arbitrary length. Algorithm analysis is an important part of a broader computational complexity theory, which provides theoretical estimates for the resources needed by any algorithms which solve a given computational problem. These estimates provide an insight into reasonable directional of search for efficient algorithms.

2.1. DEFINITION OF AN ALGORITHM

Algorithm is defined as a step by step representation of a problem. An algorithm is a well-defined sequence of computational steps that transform the input into the output for

8

solving a particular problem. We can design an algorithm as a tool for solving a specific computational problem. The time and space it uses are two measures of the efficiency of an algorithm. Algorithm should have the following five characteristic features: Input: There are some input data which are externally supplied to the algorithm Output: There will be at least one output as a result. Definiteness: Each instruction or step of the algorithm must be precise and unambiguous and clear design. Effectiveness: The steps of an algorithm must be sufficiently clear idea that it can be carried out by a person mechanically using pen and paper or any other form. Termination: The algorithm should be terminated after a particular time period and algorithm should be designed like that it will terminate automatically after executing the algorithmic steps. The first step in the analysis of an algorithm into characterizes the data that is to be used as input to the algorithm and to decide what type of analysis is appropriate. Ideally, we would like to be able to derive, for any given distribution of the probability of occurrence of the possible inputs, the corresponding distribution of possible running times of the algorithms. The second step in the analysis of an algorithm is to identify abstract operations upon which the algorithm is based, in order to separate the analysis from the implementation. Thus, for example, we separate the study of how many comparisons a sorting algorithm makes from the determination of how many microseconds a particular computer takes to execute a particular code fragment. In the third step in the analysis of an algorithm, we proceed to the mathematical analysis itself, with the goal of finding average and worst case values for each of the fundamental quantities. It is not difficult to find an upper bound on the running time of a program – the challenge is to find the best upper bound, one which could actually be achieved if the worst input were encountered. This gives the worst case: the average case normally requires a rather sophisticated mathematical analysis. Once such analyses have been successfully performed on the fundamental quantities, the time associated with each quantity can be determined and expression for the total running time obtained. In principle, the performance of an algorithm often can be analyzed to an extremely precise level of details, limited only by uncertainty about the performance of the computer or by the difficulty of determining the mathematical properties of some of the abstract quantities. Analysis of an algorithm is a cyclic process of analyzing, estimating and refining the analysis until an answer to the desired level of details is reached. Data structure mainly specifies (or dictates) the following four things: 1. Organizing of data 2. Accessing methods 3. Degree of associatively

9

4. Processing alternatives for information Data structures are the building blocks of a program. And hence the selection of a particular data structure stresses on the following two things. The data structure must be rich enough in structure to reflect the relationship existing between the data. And the structure should be simple so that we can process data effectively whenever required. The identification of the inherent data structure is vital in nature. And the structure of input and output data can be use to derive the structure of a program. Data structure affects the design of both structural and functional aspects of a program.

Algorithm + Data structure = Program

2.2. COMPLEXITY

Complexity refers to the rate at which the required storage or consumed time grows as a function of the problem size. The absolute growth depends on the machine used to execute the program, the compiler used to construct the program, and many other factors. We would like to have a way of describing the inherent complexity of a program (or piece of a program), independent of machine or compiler considerations. This means that we must not try to describe the absolute time or storage needed. We must instead concentrate on a “proportionality” approach, expressing the complexity in terms of its relationships to some known function. This type of analysis is known as “Asymptotic analysis”. It may be noted that we are dealing with complexity of an algorithm not that of a problem. For example, the simple problem could have high order of time complexity and vice-versa.

2.3. Asymptotic Analysis

Asymptotic analysis is based on the idea that as the problem size grown; the complexity can be described as a simple proportionality to some known function. This idea is incorporated in the “Big O”, “Omega” and “Theta” notation for asymptotic performance. The notation like “Little Oh” is similar in sprit to Big Oh; but is rarely used in computer science for asymptotic analysis.

2.4. Tradeoff between space & time complexity

We may sometime seek a tradeoff between space and time complexity. For example we may have to choose a data structure that requires a lot of storage in order to reduce the computation time. Therefore, the programmer must make a judicious choice from an informed point of view. The programmer must have some verifiable basis based on which a data structure or algorithm can be selected complexity analysis provides such a basis.

10

In the theoretical analysis of algorithms it is common to estimate their complexity in asymptotic sense i.e. to estimate the complexity function for reasonably large length of input ‘n’. Big O notation, Omega notation Ω and theta notation Φ are used for this purpose. In order to measure the performance of an algorithm underlying the computer program, our approach would be based on the concept called asymptotic measure of

complexity of algorithm. There are notations like big O, Ω, Φ for asymptotic measure of growth functions of algorithms. The most common being big-O notation. The asymptotic analysis of algorithms is often used because time taken to execute an algorithm varies with the input n and the other factor, which may differ from computer to computer, and from run time to run time. The essence of these asymptotic notations is to bind the growth function of time complexity with a function for sufficiently large input.

3. ALGORITHM PERFORMANCE

The performance evaluation of an algorithm is obtained by totaling the number of occurrence of each operation when running the algorithm. The performance of an algorithm is evaluated as a function of the input size n and is to be considered modulo a multiplicative constant.

3.1. ΦΦΦΦ - Notation

This notation bounds a function to with in constant factors. We say f(n) = Φ(g(n)) if there exists positive constants n0, C1 and C2 such that to the right of n0 the value of f(n) always lines between C1g(n) and C2g(n) inclusive.

3.2. O-Notation (Upper Bound)

This notation gives an upper bound for a function to within a constant factor. We write f (n) = O(g(n)) if there are positive constants n0 and c such that to the right of n0, the value of f(n) always lies on or below Cg(n).

3.3. ΩΩΩΩ-Notation (Lower Bound)

This notation gives a lower bound for a function to within a constant factor. We write f(n)= Ωg(n) if there are positive constants n0 and C such that to the right of n0, the value of f(n) always lies on or above Cg(n).

4. CLASSIFICATION OF DATA STRUCTURE

Data structures are normally divided into two broad categories.

Primitive data structure

Non-primitive data structure

11

Primitive data structures are directly operated upon by the machine instructions. These in general, have different representations on different computers. Integers, floating-point numbers, character constants, string constants, pointers etc fall in this category. Integers are used to represent natural numbers and it occupies 2-bytes of memory in c-programming compilers. Floating point numbers are used for the rational numbers and occupy 4-bytes of memory. Non-primitive data structures are more sophisticated data structure. These are derived from the primitive data structures. The non-primitive data structures emphasize on structuring of a group of homogeneous (same type) or heterogeneous (different type) data items. Arrays, lists and files are the examples of non-primitive data structure.

5. OPERATIONS OF DATA STRUCTURE

The most commonly used operations on data structure are broadly categorized into four types.

CREATE

DESTORY or DELETE

SELECTION

UPDATION The create operation results in reserving memory for the program elements. This can be done by declaration statements. The creation of data structure may take place either during compile-time or during run-time. On the other hand, Destroy operation destroys the

Primitive Data Structure Non-Primitive Data Structure

Integer

Float

Char

Pointer

Arrays Lists Files

Linear Lists Non-linear Lists

Stacks Queues Graph Trees

Data Structure

12

memory space allocated for the specified data structure. melloc ( ) and free( ) function of C language are used for these two operation respectively (as far as dynamic memory allocation and deallocation is concerned). The selection operation deals with accessing a particular data within a data structure. And the last operation Update as the name implies, it updates or modifies the data in the data structure. Probably new data may be entered or previously stored data may be deleted. Others operations performed on the data structure include:

Searching

Sorting

Merging Searching operation finds the presence of the desired data items in the list of data items. It may also find the locations of all elements that satisfy certain conditions. Sorting is the process of arranging all data items in a data structure in a particular order say for example, either ascending order or in descending order. And finally, merging is a process of combining the data items of two different sorted lists into a single sorted list.

6. ARRAY

This is the simplest type of data structure, is a linear (one dimensional) array. Programs very often deal with multiple data items processing common characteristic. In such cases, it is often convenient to place the data items in an array, so that they all share the common name. An array is a finite collection of homogeneous (i.e. similar) data elements. Following are some of the examples:

An array of integers to store the roll numbers of the students.

An array of characters to store the name of an organization.

An array of doubles to store the salary of different employees.

6.1. Array Size: Number of elements in an array is called the size of the array. It is other wise known as length or dimension. 6.2. Type: Before using an array its type and dimension must be declared. Type of an array represents the kind of data type it is meant for. For example array of integer, array of floats, array of characters etc. 6.3. Base: however, big an array its elements are always stored in contiguous memory locations. Base of an array is the address of memory location where the first element of an array is located.

13

6.4. Index: All the array elements can be referenced by a subscript like a[i]. This subscript is known as index. Index is always an integer value. Array elements are numbered, starting with 0 indexes is called Lower Bound and the last element index is SIZE-1 known as Upper Bound. When an index is incremented it point to the next element of an array. In general, the size of an array can be obtained from the index set by the formula SIZE = UB-LW+1. 6.5. ADT FOR AN ARRAY

Each array has a specific data type and constant size. We use eltype for data type of the elements of the array and up for the upper bound (of index). The definition is as follows: Abstract typedef <<eltype,up>> array(up,eltype) Condition type(up) = int;

6.6. OPERATIONS ON LINEAR ARRAY

TRAVERSAL: Traversing means to access all the elements of the array, starting from first element up to the (upper bound) last element in the array one-by-one. This operation is used to visit all the elements of an array. Algorithm for traversal of array

ALGORITHM Let LB be the lower bound and UB be the upper bound of linear array a. 1. (Initialize counter), set I at lower bound LB 2. Repeat for I = LB to UB (Visit element) Display a[i] 3. Exit

INSERTION: Insertion of a new element in an array can be done in two ways: a) Insertion at the end of the array. b) Insertion at the required position Inserting an element at the end of an array can be easily done provided the memory space allocated for the array is large enough to accommodate the additional element. For inserting the element at required position, elements must be moved downwards to new locations. To accommodate the new element and keep the order of the elements. For inserting an element into a linear array insert (a, len, pos, num) where ‘a’ is linear array ‘len’ be total no. of elements with in array ‘pos’ is the position at which number ‘num’ will be inserted.

14

ALGORITHM 1. (Initialize the value of I). Set I = len Repeat for I = len down to pos. 2. (Shift the elements shown by 1 position) Set a[i+1] = a[i] (End of loop) 3. (Insert the element at required position) Set a(pos) = num 4. (Reset len) set len = len+1 5. Display the new list of array. 6. End.

For example: Let we have an array a[6] = 1, 5, 7, 22, 6, 90; Suppose we want to insert 20 in array a, at location with index 4, it means the elements 22 onwards must be shifted downwards.

DELETION: For deleting an element from the array, the logic is straight forward. Deleting an element at the end of an array presents no difficulties, but deleting element somewhere in the middle of the array would require shifting all the elements to fill the space emptied by the deletion of the element. The following algorithm deletes the element stored at position pos from a linear array a and assign it to a variable item.

a[0] 1

a[1] 5

a[2] 7

a[3] 6

a[4] 22

a[5] 90

a[6] 0

a[7] 0

a[8] 0

a[9] 0

a[0]

a[0] 1

a[1] 5

a[2] 7

a[3] 6

a[4]

a[5] 22

a[6] 90

a[7] 0

a[8] 0

a[9] 0

a[0]

a[0] 1

a[1] 5

a[2] 7

a[3] 6

a[4] 20

a[5] 22

a[6] 90

a[7] 0

a[8] 0

a[9] 0

a[0]

Position for 20

Original Array Elements shifted down words

Array after insertion

15

Delete (a, pos, n)

ALGORITHM 1. Set item = a[pos]; 2. Repeat for j = pos to n – 1 (Shifting elements 1 position upwards) Set a[j] = a [j +1] (End loop) 3. Reset n = n-1 4. Display the new list of element of arrays. 5. End For example a[5] = 90, we want to delete the element from the array. So, for this purpose the elements following it were moved upwards by one location.

MULTI-DIMENSIONAL ARRAYS Multidimensional arrays are arrays of arrays are declared. int int_array[10]; //A normal one-dimensional array. int int_array2d[10][10]; //A two-dimensional array. Two-dimensional arrays are as a grid. We have ten rows and ten columns, for a total of 100 elements. The first number in brackets is the number of rows; the second number in brackets is the number of columns. So, the upper left corner of any grid would be element [0][0]. The element to its right would be [0][1], and so on. Here is a little illustration to help.

a[0] 15

a[1] 7

a[2] 20

a[3] 13

a[4] 66

a[5] 90

a[6] 5

a[7] 10

a[8] 50

a[9] 8

(A)

a[0] 15

a[1] 7

a[2] 20

a[3] 13

a[4] 66

a[5]

a[6] 5

a[7] 10

a[8] 50

a[9] 8

(B)

a[0] 15

a[1] 7

a[2] 20

a[3] 13

a[4] 66

a[5] 5

a[6] 10

a[7] 50

a[8] 8

a[9]

(C)

16

[0][0] [0][1] [0][2]

[1 ][0] [1][1] [1][2]

[2][0] [2][1] [2][2]

INITIALIZING A TWO DIMENSIONAL ARRAY A two dimensional array can be initialized like this: int array[3][3] = 1, 2, 3, 4, 5, 6, 7, 8, 9 ;

The results are the same; it's just not as obvious. Also, like a normal array, you don't have to specify the numbers in the brackets when you initialize an array, the compiler will figure it out for you. Well, it’s not very sophisticated, but maybe later (much later) we will make a more advanced database program. The only thing we haven't used before is the two dimensional array. We used an array of strings as the basis of our name database. The array, name was declared as described above. If you thought two-dimensional arrays where fun, check this out. In C we can have as many dimensions as we please by adding another set of brackets. Like this int array3D[3][3][3] ; .

A two-dimensional array is a grid, and a three dimensional array is as a cube. The above declaration would give you 27 integers (3 x 3 x 3). Just like other arrays, you can initialize it, and leave out the numbers. Here is an example: int array3D[ ][ ][ ] = 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27; In the above example, element array3D [1][2][0] would be 16. The one in the first spot gets you to the second "grid" (remember the first "grid" would be 0). The two in the second spot gets you to the third row, and the 0 in the last spot indicates the first element of that row. ACCESSING TWO-DIMENSIONAL ARRAY ELEMENTS Elements .of two-dimensional arrays can be accessed as follows: a(0][0] =.10 ; //assigns 10 to element at first row and first column a[0][1] = 3 ; //assigns 3 to element at first row and second column a[2][2] = 45 ; //assigns 45 to the element at second row and second column Example 3.8. The following program illustrates the working of two-dimensional arrays.

17

#include<stdio.h> . # include<conio.h> main( ) int a[3][3], i, j ; printf ("Enter the array elements") ; for (i = 0 ; i <= 2 ; i++) for(i = 0 ; j <= 2 ; j++); scanf ("%d", &a[ i ][ j ]) ; printf ("The elements of the array are") ; for (i = 0 ; i <= 2 ; i++) for (j = 0 ; j <= 2 ; j++) printf(“%d”,a[[i][j]); printf (''\n'') ; //End of the main program Out put of the program Enter the array elements: 4 6 8 7 2 1 1 1 3 The elements of the array are 4 6 8 7 2 1 1 1 3

REPRESENTATION OF ARRAY It is not common to find a large number of programs which process the elements of an array in sequence. But, does it mean that the elements of an array are also stored in sequence in memory. The answer depends on the operating system under which the program is running. However, the elements of an array are stored in sequence to the extent possible. If they are being stored in sequence, then how are they sequenced. Is it that the elements are stored row wise or column wise? Again it depends on the operating system. The former is called row major order and the later is called column major order.

18

Row Major Representation

The first method of representing a two-dimensional array in memory is the row major representation. Under this representation, the first row of the array occupies the first set of memory location for the array; the second row occupies the next set, and so forth. The schematic of row major representation of an array is shown in the figure below. Let us consider the following two-dimensional array:

a b c d e e e h i j k l

To make its equivalent row major representation, we perform the following process: Move the elements of the second row starting from the first element to the memory location adjacent to the last element of the first row. When this step is applied to all the rows except for the first row, we have a single row of elements. This is known as row major representation. By application of above mentioned process, we get the row major order elements are a, b, c, d, e, f, g, h, i, j, k, l

Column major representation Under this type of representation, the first column of the array occupies the first set of the memory locations reserved for the array. The second column occupies the next set and so on. Consider the following two-dimensional array:

a b c d e f g h i j k l

To make its equivalent column major representation, we can perform the following process: Transpose the elements of the array. Then, the representation will be same as that of the row major representation. By application of the process, the column major representation of the elements is a, e, i, b, f, j, c, g, k, d, h, l

Row 0 Row 1 Row 2 Row 3 . . . . . . Row n

Col 0 Col 1 Col 2 Col 3 . . . . . . Col n

19

APPLICATIONS OF ARRAY Arrays are simple, but reliable to use in more situations than we can count. Arrays are used to those problems when the number of items to be solved is fixed. They are easy to traverse, search and sort. It is easy to manipulate an array rather than other subsequent data structures. Arrays are used in those situations where in the size of array can be established before hand. Also they are used in situations where the insertion and deletion are minimal or not present. Insertion and deletion operation will lead to wastage of memory or will increase the time complexity of the program due to the reshuffling of elements. PARALLEL ARRAY In computing, a parallel array is a simple data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory. For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index. Here's a simple example in C using parallel arrays: int ages[] = 0, 17, 2, 52, 25; char *names[ ] ="Sachi", "Bishnu", "Kamala", "Kiran", "Rabi"; int parent[ ] = 0 /*Sachi*/, 3 /*Bishnu*/, 1 /*Kamala*/, 0 /*Kiran*/, 3 /*Rabi*/; for(i = 1; i <= 4; i++) printf("Name: %s, Age: %d, Parent: %s \n", names[i], ages[i], names[parent[i]]);

Advantage & Disadvantage Parallel arrays have a number of practical advantages over the normal approach: They can be used in languages which support only arrays of primitive types and not of

records (or perhaps don't support records at all). Parallel arrays are simple to understand and use, and are often used where declaring a

record is more trouble than it's worth. They can save a substantial amount of space in some cases by avoiding alignment issues.

For example, one of the fields of the record can be a single bit, and its array would only need to reserve one bit for each record, whereas in the normal approach many more bits would "pad" the field so that it consumes an entire byte or a word.

20

If the number of items is small, array indices can occupy significantly less space than full pointers, particularly on architectures with large words.

Sequentially examining a single field of each record in the array is very fast on modern machines, since this amount to a linear traversal of a single array, exhibiting ideal locality of reference and cache behavior.

However, parallel arrays also have several strong disadvantages, which serves to explain why they are not generally preferred: They have significantly worse locality of reference when visiting the records

sequentially and examining multiple fields of each record, which is the norm. They obscure the relationship between fields of a single record. They have little direct language support (the language and its syntax typically express

no relationship between the arrays in the parallel array.) They are expensive to grow or shrink, since each of several arrays must be reallocated. CONCEPTS OF RECORDS The structures of files change from operating system to operating system. A file may be defined as a collection of records. Though, a text file doesn't conform to this definition at the first glance, it is also a collection of records and records are words in the file. Consider a file consisting of information about students. We may name such a file as Student file. The typical records of such file are shown below:

A file should always be stored in such a way that the basic operations on it can be performed easily. In other words, queries should be able to be executed with out much hassle. TERMINOLOGY The following are the definitions of some important terms about a record: Field: It is an elementary data item characterized by its size, length and type. For example, Name : a character type of size 10 Age : a numeric type Record: It is a collection of related fields that can be treated as a unit from an application point of view.

Roll no Name Address State Country Program BU1233 S.Patnaik Brahmapur Orissa India MCA BU1355 M.Mohapatro BBSR Orissa India BBA

21

For example: A university could use a student record with the fields, namely, University enrolment no. and names of courses File: Data is organized for storage in files. A file is a collection of similar related records. It has an identifying name. For example, "STUDENT" could be a file consisting of student records for all the students in a university. . Index: An index file corresponds to a data file. Its records contain a key field and a pointer to that record of the data file, which has the same value of the key field. The data stored in files is accessed by software, which can be divided into the following two categories: User Programs: These are usually written by a programmer to manipulate retrieved data in the manner required by the application. . File Operations: these deals with the physical movement of data, in and out of files. User programs effectively use file operations through appropriate programming language syntax. The File Management System manages the independent files and acts as the software interface between the user programs and the file operations. . File operations can be categorized as CREATION of the file INSERTION of records into the file UPDATION of previously inserted records RETRIEVAL of previously inserted records DELETION of records DELETION of the file

22

CHECK YOUR PROGRESS What do you understand by aggregate data types? Discuss with the help of suitable

examples. Write an algorithm to find out the value of x y. What is an array? Differentiate between one-dimensional array and two-dimensional

array. What are the limitations of an array? How a two-dimensional array is represented in memory. What do you mean by complexity? Define time and space complexity. What do you mean by data structure? Differentiate between primitive and non-primitive

data structure. What is an algorithm? Differentiate between algorithm and flow chart. What is a parallel array? What are its limitations?

23

U N I T – I I

24

% & % & % & % &

& & & & &&&&

"""" $ $ $ $

UNIT -2

OBJECTIVES

After going through this unit, you should be able to:

• Understand the concepts of stack, queue and linked list;

• Implement the stack using array and linked list;

• Implement the queue and double linked list

• Application of stack and queue, circular linked list and double linked list etc.

INTRODUCTION

One of the most useful concepts in computer science is the stack. A stack is a linear structure in which items may be inserted or removed only at one end called the top of the stack. In other words, a stack is a non-primitive linear data structure. It is an ordered list in which addition of new data items and deletion of already existing item is done from only one end.

As all the deletion and insertion in a stack is done from top of the stack, the last added element will be the first to be removed from the stack. This process is known as Last-in-First-out (LIFO). The most recently accessible element in the stack is the top most element, where as the least accessible element is the bottom of the stack.

Example 1: A common model of a stack is plates in a marriage party or coin stacker. Fresh plates are "pushed" onto to the top and "popped" off the top.

25

Example 2: Some of you may eat biscuits (or pop pins). If you assume only one side of the cover is torn and biscuits are taken off one by one. This is what is called popping and similarly, if you want to preserve some biscuits for some time later, you will put them back into the pack through the same torn end called pushing.

Whenever a stack is created, the stack base remains fixed, as a new element is added to the stack from the top, the top goes on increasing; conversely as the top most element of the stack is removed the stack top is decrementing. For example,

Fig. below figure shows various stages of stack top, during insertion and during deletion of elements.

ABSTRACT DATA TYPE – STACK

Push Pop

Plates at the counter of a canteen Disk on a peg

0

1 2

3 4

5 6

Stack is empty Top

0

1 2

3 4

5 6

Inserting first element

20 Top = 0 0

1 2

3 4

5 6

20

Top = 0

Inserting second element

13 0

1 2

3 4

5 6

20

Top = 0

Inserting second element

13 54

0

1

2

3 4 5 6

20

Top = 0

Stack initially

13 54

(Stack top increases during insertion)

0

1

2

3 4 5 6

20

Top = 0

Element 54 is deleted

13 0

1

2

3 4 5 6

20 Top = 0

Element 13 is deleted

0

1

2

3 4 5 6

20 Top = 0

Element 20 is deleted

(Stack top decreases during insertion)

26

Conceptually, the stack abstract data type depicts the information kept in a pile on a desk. Informally, we first consider materials on a desk, where we may keep separate stack files bills that need paying, magazines that we plan to read and notes we have taken.

We can perform several operations that involve a stack; Start a new stack. Place new information on the top of the stack, Take the top item off the stack, Read the item on the top, and Determine whether a stack is empty

When discussing these operations, it is conventional to call the addition of an item to the pop of the stack as a PUSH operation and the deletion of an item from the top as a POP operation.

REPRESENTATION OF STACK

There are various ways to represent a stack, such as railway shunting method, vertical vector method and horizontal vector method etc.

Input (Insertion) Output (Deletion)

(Railway shunting System representation) TOS Free Space (Vertical Representation of stack)

Insertion

Deletion

(Horizontal representation of stack)

27

A pointer TOS keeps track of the top most information in the stack. Initially when the stack is empty, TOS has a value zero (if implementing through C/C++ this may be -1) and when the stack contains single information TOS has a value one and so on. Before an item is pushed onto the stack, the pointer is incremented by one. The pointer is decrement by one each time a deletion is made from the stack.

OPERATIONS ON STACK

The basic operations that can be performed on stack are as follows:

1. PUSH: The process of adding a new element to the top of stack is called PUSH operation. Pushing an element in the stack invoke adding of element, as the new element will be inserted at the top after every push operation the top is incremented by one.. 2. POP: The process of deleting an element from the top of stack is called POP operation. After every pop operation the stack is decremented by one. If there is no element on the stack and the pop is performed then this will result into STACK UNDERFOW condition.

STACK TERMINOLOGY

1. Context: The environment in which a function executes: includes argument values, local variables and global variables. The entire context except the global variables is stored in a stack frame. 2. Stack frames: The data structure containing all the data (arguments, local variables, return address, etc) needed each time a procedure or function is called. 3. Max size: This term is not a standard one; we use this term to refer the maximum size of stack. 4. TOP: This term refers to the top of stack (TOS). The stack top is used to check stack overflow or underflow conditions. Initially Top stores - 1. This assumption is taken so that whenever an element is added to the stack the Top is first incremented and then the item is inserted into the location currently indicated by the Top. 5. Stack: It is an array of size MAXSIZE. 6. Stack Empty or Underflow: This is the situation when the stack contains no element. At this point the top of stack is present at the bottom of the stack. 7. Stack overflow: This is the situation when the stack becomes full, and no more elements can be pushed onto the stack. At this point the stack top is present at the highest location of the stack.

ALGORITHMS FOR STACK OPERATIONS

Push Operation

Push means to insert an item onto the stack. Inserts an item to the top of a stack, which is represented by vector A and containing Size number of items, with a pointer TOS denoting the position of top most item in the stack.

28

Step 1: [Check for the stack overflow] If Tos >= size Print “Stack overflow” and Exit. Step 2: [Increment the pointer value by one] Tos = Tos +1 Step 3: [Perform insertion] S [Tos] = value Step 4: Exit

Pop Operation

The pop operation deletes or removes the topmost item from the stack. After removal of top most information, new value of the pointer TOS becomes the previous value of TOS that is TOS = TOS -1 and freed position is allocated to free space.

Step 1: [Check whether the stack is empty or not] If TOS = 0 Print “Stack is under flow” and exit. Step 2: [remove the TOS information] Value = S[TOS] TOS = TOS -1 Step 3: [returns the former information of the stack Return value Step 4: Exit

Peep Operation If one is interested only about information stored at some location in a stack then

peep operation is required. In this operation we simply move the pointer to the desired location and then fetch the information associated with that location.

Step 1: [Check for the stack overflow] If TOS - i = 0 Output "Stack overflow" and exit

Step 2: [Return the ith element from the top of stack] Return (S [TOS – i]

29

Update Operation

Update operation is required when the content of some location in a stack is to be changed. Suppose on want to update information at the ith location in the stack S, and then we move TOS pointer to the ith location from the top of the stack and input the new value of that location.

Step 1: [Check the underflow condition] If TOS – I = 0 Print “Stack underflow” and return. Step 2: [Change the element] S[TOS – i] = value Step 3: Return the value. Step 4: Exit

APPLICATION OF STACK

Stacks are frequently used in evaluation of arithmetic expressions. An arithmetic expression consists of operands and operators. Polish notations are evaluated by stacks. Conversion of different notations (Prefix, Postfix, Infix) into one another are performed using stack. Stacks are widely used inside computer when recursive functions are called. The computer evaluates an arithmetic expression written in infix notation in two steps. First, it converts the infix expression to postfix expression and then it evaluates the prefix expression. In each step, stack is used to accomplish the tasks.

POLISH NOTATION

The process of writing the operators of an expression either before their operands or after the operands is called the Polish notations. The Polish mathematician Jan Luksiewicz discovers the polish notation. The fundamental property of polish notation is that the order in which the operations are to be performed is completely by the positions of the operators and operands in the expression. So, accordingly, one never needs parenthesis or brackets while writing an expression in polish notations. The polish notations are classified into three categories as follows: Infix notation Prefix notation Postfix notation

30

Infix Notation

When the operators exist between two operands then the expression is called infix expression. For example, let a + b * c is an expression, by inspection we find that the operator + exists between the operands a and b, and the operator * exists between the operands b and c. Hence, we can say that a + b * c is an infix expression.

Prefix Notation

When the operators are written before their operands then the resulting expression is called the prefix notation.

Suppose we want to change the infix expression a + b * c into its prefix form. First of all we think the precedence of the operators present in the infix expression.

Here, the given infix expression there are two operands + and *. The precedence of the * is higher than that of the + operator. The steps which are required to change infix expression a + b * c into its equivalent prefix form are as follows: Step 1: a + * b c [Putting * before b & c] Step 2: +a * b c [Putting + before a & b]

Prefix Notation

When the operators come after their operands the resulting expression is called the reverse polish notation or postfix expression. For example, suppose a + b * c is in infix expression then after changing this expression into post form we get a b c * +. Thus, we can say that post expression in not a mirror image of the prefix expression but a different one. Notation Conversions

Let an expression A + B * C is given in, which is in infix notation. To calculate this expression for values 4, 3, 7 for A, B, C respectively we must follow certain rule (called BODMAS in general mathematics) in order to have right result.

For example:

A + B * C = 4 + 3 * 7 = 7 * 7 =49

Is this the right result? No, this is because the multiplication is to be done before addition, because it has higher precedence over addition. This means that for an expression to be calculated we must have the knowledge of precedence of operators. The error in the above calculation occurs, as there are no braces to define the precedence of operators.

Thus expression A + B * C is interpreted as, A + (B * C). This is an. alternative for us to convey the computer that multiplication has higher precedence over addition, as there is no other way to specify this.

31

OPERATOR PRECEDENCE

Exponential operator Highest Precedence

Multiplication/Division *, / Next precedence

Addition/Subtraction +, - Least precedence

Converting infix expression to postfix form

A + B * C Infix Form

A + (B * C) Parenthesized expression

A + (BC) Convert the multiplication

A (BC*) + Convert the addition

ABC* + Postfix form

The rules to be remembered during infix to postfix conversion are:

(i) Parenthesize .the expression starting from left to right.

(ii) During parenthesizing the expression, the operands associated with operator having higher precedence are first parenthesized. For example in above expression B * C is parenthesized first before A + B.

The sub-expression (part of expression), which has been converted into postfix, is to be treat Notation Conversions

Let an expression A + B * C is given in, which is in infix notation. To calculate this expression for values 4, 3, 7 for A, B, C respectively we must follow certain rule (called BODMAS in general mathematics) in order to have right result.

For example:

A + B * C = 4 + 3 * 7 = 7 * 7 =49

Is this the right result? No, this is because the multiplication is to be done before addition, because it has higher precedence over addition. This means that for an expression to be calculated we must have the knowledge of precedence of operators. The error in the above calculation occurs, as there are no braces to define the precedence of operators.

Thus expression A + B * C is interpreted as, A + (B * C). This is an. alternative for us to convey the computer that multiplication has higher precedence over addition, as there is no other way to specify this.

OPERATOR PRECEDENCE

Exponential operator Highest Precedence

Multiplication/Division *, / Next precedence

Addition/Subtraction +, - Least precedence

32

Converting infix expression to postfix form: A + B * C Infix Form

A + (B * C) Parenthesized expression

A + (BC) Convert the multiplication

A (BC*) + Convert the addition

ABC* + Postfix form

The rules to be remembered during infix to postfix conversion are:

(iii) Parenthesize .the expression starting from left to right.

(iv) During parenthesizing the expression, the operands associated with operator having higher precedence are first parenthesized. For example in above expression B * C is parenthesized first before A + B.

(v) The sub-expression (part of expression), which has been converted into postfix, is to be treated as single operand.

(vi) Once the expression is converted to postfix form remove the parenthesis.

Let us consider some solved problems for converting infix expression to postfix, we will follow the above said 4 rules.

EXAMPLE 1. Give postfix form for A + [ (B + C) + (D + E) * F ]/G

SOLUTION. Evaluation order is

A + [(BC +) + (DE +) * F/G]

A + [(BC +) + (DE + F *)/G]

A + [(BC + (DE + F * +)/G]

A + [BC + DE + F *+ G/]

ABC+DE+F *+G/+ [Postfix form]

EXAMPLES 2. Give postfix form for A + B - C

SOLUTION. (A + B) - C

(AB +) – C

Now as said-after converting a part of expression to postfix, treat it as single unit, therefore (AB+) now can be treated as T.

T - C

TC

Now as the final conversion is accomplished replace the value of T.

AB + C - [Postfix Form]

33

EXAMPLE 3 Convert the expression A * B + C/D to postfix form.

SOLUTION. In this particular example the * operator is encountered during left to right evaluation, which shares an equal precedence to operator. When this happens (two operators with same precedence comes in same expression), the operator that is encountered first while evaluating from left to right is first parenthesized. Hence the above expression will be interpreted as:

(A * B) + C/D

(AB *) + C/D

(T) + C/D

(T) + (C/D)

(T) + (CD/)

T+S [S = CD/]

TS+

AB * CD/+ [Postfix Expression]

EXAMPLE 4 Give positive form for A * B + C

SOLUTION. (A * B + C)

(AB *) + C

(T)+C

TC+

AB * C+ [Postfix Expression]

EXAMPLE 5 Give postfix form for A + HIC - D.

SOLUTION. A + (B/C) - D

A + (BC/) - D

A+T-D

(A + T) - D

(AT+) - D

S-D

SD-

AT+D

ABC / +D –

34

EXAMPLE 6 Convert the expression (A + H) I (C - D) to postfix form.

SOLUTION. In this expression the brackets are already specified. Therefore the conversion looks like:

(AB+) / (CD-)

T/S [Where T = (A+B) and S = (CD-)

TS/

AB + CD-/ [Postfix notation]

EXAMPLE 7 Give postfix form for (A + B) * C/D

SOLUTION. (AB+)*C/D

T* C/D [Where t = (A+B)]

(T * C)/D

(TC *)/D

S/D

SD/

TC * D/

AB + C * D/ [Postfix expression]

EXAMPLE 8 Give postfix form for (A + B) * C/D + E /\ F/G

SOLUTION. (AB+) * C/D + E /\ F/G

T * C/D + (E /\ F) / G [^ has the highest precedence]

T * C/D + (EF /\) / G

T * C/D + S/G [Where s = (EF^)]

(T * C)/ D +S/G

(TC *)/D + S/G

Q/D + S/G [Where Q = (TC *)]

(Q/D) + S/G

(QD/) + S/G

P + S/G

P + (S/G)

P + (SG/)

P+O

PO+

35

Now we will expand the expression PO+

PO+

QD/O+

TC * D/O+

AB + C * D/SG/+

AB + C * D / EF /\ G / + [Postfix expression]

EXAMPLE 9. A + [(B + C) + (D + E) * FJ/G. Give postfix form.

SOLUTION. A + [(BC+) + (DE+) * F]/G

A + [T + S * F]/G

A + [T -(S * F)]/G

A + [T + (SF*)]/G

A + [T + Q]/G

A + (TQ+)/G

A + P/G

A + (PG/)

A+N

AN+

Expanding the expression AN+

APG/+

ATQ + G/+

ABC + Q + G/+

ABC + SF * + G/ +

ABC + DE + F * G/ + [Postfix expression]

EXAMPLE 10 A + (B * C - (D / E /\ F) * G) * H. Give postfix form.

SOLUTION. A + (B * C - (D (EF /\) * G))* H [/ has the highest priority]

A + (B * C - (D/T) * G))* H [where T = EF/]

A + (B * C – ((D/T) * G))* H

A + (B * C – ((DT/) * G))* H

A + (B * C - (S * G))* H

A + (B * C - (SG *)) * H

A + (B * C - Q) * H

A + ((BC *) - Q) * H

36

A + (P - Q) * H [Where p = (BC*)

A + (PQ-) * H

A+O*H [Where O = (PQ-)

A + (O * H)

A + (OH *)

A+N

AN+

Expanding the expression AN+

AOH * +

APQ-H * +

ABC * Q - H * +

ABC * SG * - H *+

ABC * DT/G * - H * +

ABC * DEF 1\ IG * - H * + [Postfix expression]

EXAMPLE 11 Give prefix form for A * B + C

SOLUTION. (A * B) + C

(* AB) + C

T+C [Where t = (*AB)

+TC

Expanding expression + TC

+ * ABC [Prefix expression.]

EXAMPLE 12 Give prefix form for A/B /\c + D

SOLUTION. A/ (B / C) + D (/has highest priority)

A/(/BC) + D

A/T + D

(A/T) + D

(/AT) + D

S+D

+SD

Expanding expression +SD

+/ ATD

+ / A /\ BCD [Prefix expression]

37

EXAMPLE 13 Give prefixes form for (A - B/C) * (D * E - F)

SOLUTION. (A - (B/C)) * ((D * E) - F)

(A - (/BC) * ((*DE)) - F)

(A - T) * (S - F) [Where (T = /BC) and 5 = (*DE)]

(-AT) * (-SF)

Q* P

*QP

Expanding expression *QP

*AT – SF

*-A/BC – SF

*-A/BC- * DEF [Prefix expression] ALGORITHM FOR CONVERTING INFIX EXPRESSION TO POSTFIX FORM ALGORITHM POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.

1. Push “(“ onto STACK, and add ”)” to the end of Q. 2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the

STACK is empty. 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator ⊗ is encountered, then:

(a) Add ⊗ to STACK [End of If structure.]

(b) Repeatedly pop from STACK and add P each operator (on the, top of STACK) which has the same precedence as or higher precedence than ⊗

6. If a right parenthesis is encountered, then: (a) Repeatedly pop from STACK and add to P each operator (on the top of

STACK until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to P.]

[End of If structure.] [End of Step 2 loop.]

7. Exit.

38

EVALUATION OF A POSTFIX EXPRESSION

This algorithm finds the value of an arithmetic expression P written in postfix notation. This algorithm, which uses a STACK to hold operands, evaluates P.

ALGORITHM Step 1: Add a right parenthesis “)” at the end of P. Step 2: Scan P from left to right and repeat step 3 and 4 foe each element of P until

the “)” is encountered. Step 3: If an operand is encountered, put it on STACK. Step 4: If an operator X is encountered, then

i. Remove the two top elements of STACK, where A is the TOP element and B is the next TOP.

ii. Evaluate B X A. iii. Place the result of (b) back to the STACK.

Step 5: Set value equal to the top of the element. Step 6: Exit. CONVERTING INFIX EXPRESSION TO PREFIX EXPRESSION

This algorithm is bit tricky, in this we first reverse the input expression a + b * c will become ‘c * b + a’ and then we do the conversion and then again we reverse to get the result. Doing this has an advantage that except for some minor modification algorithm for infix to prefix remains almost same as the one for infix to postfix.

ALGORITHM 1. Reverse the input string. 2. Examine the next element in the input. 3. If it is operand, add it to the output string. 4. If it is closing parenthesis, push it on stack. 5. If it is an operator, then

a. If stack is empty, push-operation on stack. b. If the top of stack is closing parenthesis push operator on stack. c. If it has same or higher priority then the top of stack, push operator on S. d. Else pop the operator from the stack and add it to output string, repeat S.

6. If it is a opening parenthesis, pop operator from stack and add them to S until a closing parenthesis is encountered. POP and discard the closing parenthesis.

7. If there is more input go to step 2. 8. If there is no more input, un-stack the remaining operators and add them. 9. Reverse the output string.

39

EVALUATION OF A PREFIX EXPRESSION The evaluation of an expression into pre-fix expression is little bit tricky and easy. Take the prefix expression P as a string for the prefix expression. ALGORITHM Step 1: Add a parenthesis “(“ at the beginning of the string P. Step 2: Scan P from right to left and repeat step 3 and 4 until the “(“ is encountered. Step 3: If any operand (i.e. any variable or number) is encountered, push it into the STACK. Step 4: If an operator X (one of ^, *, /, +, -) is encountered then

a) Remove the two top elements of the STACK (call them A and B) [here the top most element is A]

b) Evaluate A X B and place the result of B back to the STACK. Step 5: Print the top element in the STACK as the value of the expression P.

ALGORITHM FOR CONVERTING INFIX EXPRESSION TO POSTFIX FORM ALGORITHM POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.

1. Push “(“ onto STACK, and add ”)” to the end of Q. 2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the

STACK is empty. 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK.

5. If an operator ⊗ is encountered, then: a. Add ⊗ to STACK b. [End of If structure.]

6. (b) Repeatedly pop from STACK and add P each operator (on the, top of STACK)

which has the same precedence as or higher precedence than ⊗

7. If a right parenthesis is encountered, then: a. Repeatedly pop from STACK and add to P each operator (on the top of

STACK until a left parenthesis is encountered. b. Remove the left parenthesis. [Do not add the left parenthesis to P.]

8. [End of If structure.] 9. [End of Step 2 loop.] 10. Exit.

40

CONVERTING INFIX EXPRESSION TO PREFIX EXPRESSION This algorithm is bit tricky, in this we first reverse the input expression a + b * c will

become ‘c * b + a’ and then we do the conversion and then again we reverse to get the result. Doing this has an advantage that except for some minor modification algorithm for infix to prefix remains almost same as the one for infix to postfix.

ALGORITHM 1. Reverse the input string. 2. Examine the next element in the input. 3. If it is operand, add it to the output string. 4. If it is closing parenthesis, push it on stack. 5. If it is an operator, then

a) If stack is empty, push-operation on stack. b) If the top of stack is closing parenthesis push operator on stack. c) If it has same or higher priority then the top of stack, push operator on S. d) Else pop the operator from the stack and add it to output string, repeat S.

6. If it is an opening parenthesis, pop operator from stack and add them to S until a closing parenthesis is encountered. POP and discard the closing parenthesis.

7. If there is more input go to step 2. 8. If there is no more input, un-stack the remaining operators and add them. 9. Reverse the output string.

QUEUE

A queue is a non-primitive linear data structure. It is a homogeneous collection of elements in which new elements are added at one end called REAR END, and the existing elements are deleted from the other end called the FRONT END. Like, people stand in a queue to get particular services; various processes will wait in a queue for their turn to avail a service. In computer science, it is also called a FIFO (First-in-First out) list. Another example of queue is time-sharing computer system where many users share the system simultaneously. The procedure, which is used to design such type of system, is round robin technique. The railway reservation counter is also an example of queue where the people collect their tickets on the first-come-first-serve basis.

IMPLEMENTATION OF QUEUES Two common ways in which queues may be implemented are as follows: By using arrays. Pointers (One way linear linked list)

41

(A simple queue) A queue has two pointers front and rear, pointing to the front and rear elements of the queue, respectively. Consider a queue Q consisting of n elements and an element value, which we have to insert into the Q. the value NULL (0) of front pointer, implies an empty queue. Algorithm to insert an element in a queue Step 1: [Check overflow condition] If rear >= size Print “Overflow” and return Step 2: [Increment rear pointer] Rear = rear +1 Step 3: [Insert an element] Q [rear] = value Step 4: [Set the front pointer] If front = 0 Front =1 Step 5: return Algorithm to delete a node from the queue Step 1: [Check underflow condition] If front = 0 Output “underflow” and return Step 2: [Remove an element] Value = Q [front] Step 3: [Check for empty queue] If front = rear Front = 0 Rear = 0 Else Front = front + Step 4: return (value)

Insertion

Rear

Deletion

Front

42

CIRCULAR QUEUE

If queue is represented using array when the rear pointer reaches at the end, insertion will be denied even if room is available at the front. One way to remove this is using a Circular Queue. Physically, a circular queue is same as ordinary queue or array. Let we have an array Q that contains n elements in which Q[1] comes after Q[n] in the array. When this technique is used to construct a queue then the queue is called circular queue. In a circular queue when rear = n, if we insert an element then this element is assigned to Q [1]. That is instead of increasing rear to n + 1, we reset rear to 1. Similarly, if front = n and an element of the queue is removed, we reset front to 1 instead of n + 1. Suppose queue contains only one element that is front = read = 1 and suppose that the element is removed then the front and rear pointers are now assigned NULL to indicate that the queue is empty. Physically a circular queue is same as an ordinary array. Say arr [n], but logically it implies that arr [0] comes after arr [n – 1] or after arr [n-1], arr [0] appears. Consider a circular queue with 3 items in array 5 elements at position 2, 3 and 4.

q.front

q.rear

33

44

55

arr [0]

arr [1]

arr [2]

arr [4]

arr [4]

43

Although the array is not full, its last element (position 4) is occupied. If we want to insert an item 66, we place it array position 0. However, if it increment q.rear then it will become 5, so we will have to set again to 0 i.e.

Now, since q.front = 2 the first item of the queue is in q.arr[2], followed by the other in the queue q.arr[3], q.arr[4] and q.arr[0]. The following figure shows the deletion of item 33 and insertion of item 77.

(Representation of a circular queue)

PRIORITY QUEUE

Many application-involving queues require priority rather than the simple FIFO strategy. Each queue elements has an associated priority value and the elements are served on a priority basis instead of using the order of arrival.

For elements of same priority, the FIFO order is used. For example in a multi user operating system, there will be several programs competing for use of the central processor at one time. The programs have a priority is given first use of the central processor.

q.front

q.rear

33

44

55

arr [0]

arr [1]

arr [2]

arr [4]

arr [4]

66

q.front q.rear

77 44

55

arr [0]

arr [1]

arr [2]

arr [4]

arr [4]

66

44

Scheduling of jobs writhing a time-sharing system is another application of queues. In such a system many user may request processing at a time and computer time divided among these requests. The simplest approach set up one queue that store all requests for processing. Computer processes the request at the front of the queue and finished it before starting on the next. Same approach is also used when several users want to use the same output device, say printer.

In a time-sharing system, another common approach used is a job only for a specified maximum length of time. If the program is fully processed within the time, then the computer goes on to the next process. If the program is not completely processed within the specified time, the intermediate values are stored and the remaining part of the program is put on the queue. This approach is useful in handling a mix of long and short jobs.

DOUBLE ENDED QUEUE

It is also a homogeneous list of elements in which insertion and deletion operation are preformed from both the ends. That is we can insert elements from the rear end or from the front end. Hence it is called double-ended queue. If is commonly referred as deque. There are two types of deques. These tow types are due to the restriction put to perform either the insertion or deletion only at one end. They are:

Input-restricted deques.

Output-restricted deques.

Since, both insertion and deletion are performed from either end, it is necessary design

algorithm to perform four operations.

Insertion of an element at the rear end of the queue.

Deletion of an element from the FRONT end of the queue.

Insertion of an element at the FRONT end of the queue.

Deletion of an element from the REAR end of the queue.

LINKED LIST

In computer science, a linked list is one of the fundamental data structures used in computer programming. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes.

10 20 30 40 50 60 Deletion Insertion

Insertion Deletion

FRONT REAR

A double ended queue

45

A linked list is a self-referential data type because it contains a pointer or link to another data of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access. Several different types of linked list exist: singly linked lists, doubly linked lists, and circularly linked lists. The simplest kind of-linked list (or slist for short), which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node.

Advantages of Linked List Insertion and deletion can be handled efficiently without having to restructures the list. Not necessary to know the number of elements and allocate memory for them before

hand. In linked lists memory can be allocated as and when necessary. Last node does not point to any node (link contains NULL). Head is pointer, which stores the address of the first node.

TYPES OF LINKED LIST

Doubly-linked list A more sophisticated kind of linked list is a doubly-linked list or two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

Circularly linked list In a circularly linked list, the first and final nodes are linked together. This can be done for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and follow the list in either direction until we return to the original node. Viewed another way, circularly linked lists can be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest, and in cases where we have one object in a list and wish to see all other objects in the list. The pointer pointing to the whole list is usually called the end pointer.

A singly linked list containing three integer values

An example of a doubly linked list.

46

Singly-circularly-linked list In a singly-circularly-linked list, each node has one link, similar to an ordinary singly linked list, except that the next link of the last node points back to the first node. As in a singly linked list, new nodes can only be efficiently inserted after a node we already have a reference to. For this reason, it's usual to retain a reference to only the last element in a singly-circularly-linked list, as this allows quick insertion at the beginning, and also allows access to the first node through the last node's next pointer.

Doubly-circularly-linked list In a doubly-circularly-linked list, each node has two links, similar to a doubly linked list, except that the previous link of the first node points to the last node and the next link of the last node points to the first node. As in doubly linked lists, insertions and removals can be done at any point with access to any nearby node.

TRAVERSING A LINKED LIST Let LIST be a linked list in memory, where each node contains the INFO and LINK parts with HEAD pointing to the first node and NULL indicating the end of the LIST. This algorithm traverses LIST applying an operation PROCESS to each element of LIST. The variable PTR point to the node currently, being processed.

ALGORITHM Step 1: Set PTR = HEAD [Initializes the pointer PTR] Step 2: repeat Steps 3 and 4 while PTR != NULL Step 3: Print INFO [PTR] Step 4: set PTR = LINK [PTR] Step 5: END

Insertion of a Node at the Beginning of the Linked List

This algorithm inserts as the first node in the list.

47

ALGORITHM INSERTBEG (HEAD, ITEM) Step 1: Create a node called NEWNODE If NEWNODE = NULL then PRINT “Insufficient memory” or OVERFLOW. EXIT [End of if] Step 2: Set INFO (NEWNODE) = ITEM [Copies the item into the new node] Step 3: Set LINK (NEWNODE) = HEAD [new node now points to the original first node] Step 4: Set HEAD = NEWNODE Step 5: EXIT

Insertion of a node at the end of the linked list

This algorithm inserts an item as the last node in the linked list.

ALGORITHM

INSERTEND (HEAD, ITEM) Step 1: Create a node called NEWNODE If NEWNODE = NULL then PRINT “Insufficient memory” or OVERFLOW. EXIT [End of if] Step 2: Set INFO (NEWNODE) = ITEM [Copies the data into the new node] Step 3: Set LINK (NEWNODE) = HEAD [newnode now points to the original first node] Step 4: Set PTR = HEAD Step 5: If HEAD = NEWNODE then Set HEAD = NEWNODE and Exit [NEWNODE is inserted at the beginning of the list.] Step 6: Repeat while PTR != NULL [Traverse the list] Set PTR = LINK [PTR] [End of the loop] Step 7: Set LINK [PTR] = NEWNODE [NEWNODE now inserted at the end of the linked list.] Step 8: END

48

Deletes the First Node from a List

ALGORITHM DELBEG (HEAD) Step 1: [If list is empty] If HEAD = NULL then PRINT “list is empty or Underflow” EXIT [End of if] Step 2: [If list contains 1 or more nodes] Set PTR = HEAD Set HEAD = LINK (PTR) [Removes the first node] Step 3: END

Deletion of a Node at the Last of the Linked List The last node can be deleted only when we knows the location of the next-to-last

node. Accordingly, traverse the list using a pointer variable PTR, and keep track of the preceding node using a pointer variable PREV. PTR points to the last node when LINK[PTR = NULL, and such case PREV points to the last but not one node. The case that LIST has only one node is treated separately, since PREV can be defined only when the list has two or more nodes. The following algorithm is as follows:

49

ALGORITHM

DELEND (HEAD) Step 1: [If List is empty] If HEAD = NULL then Print “LIST is empty or Underflow” Exit [End of if] Step 2: [List contains only one node If LINK (HEAD) = NULL then Set HEAD = NULL and EXIT [End of if] Step 3: [List contains 2 or more node] Repeat while LINK [PTR] != NULL Set PREV = PTR Set PTR = LINK [PTR] [End of loop] Step 4: Set LINK [PREV] = LINK [PTR] Step 5: Remove the node PTR Step 6: EXIT

Print the Information at Each Node of a Linked List

This algorithm is to print the information at each node of the list.

ALGORITHM

PRINT (HEAD)

Step 1: Set PTR= HEAD

Step 2: Repeat Steps 3 and 4 while PTR != NULL

Step 3: PRINT INFO [PTR]

Step 4: Set PTR = LINK [PTR]

[End of loop]

Step 5: END

50

Searching for a given item of a linked list

This algorithm finds whether the ITEM appears in the LIST, or not.

ALGORITHM SEARCH (HEAD, ITEM) Step 1: Set PTR = HEAD Step 2: Repeat while PTR != NULL if ITEM = INFO[PTR] then PRINT “Element is present in the list” Return else Set PTR= LINK[PTR] [End of if] [End of loop] Step 3: PRINT “No such elements are present in the list” Step 4: END

APPLICATIONS OF LINKED LISTS Linked lists are used as a building block for many other data structures, such as

stacks, queues and their variations. The "data" field of a node can be another linked list. By this device, one can construct

many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style.

Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary search trees even on small data sets.

However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set.

51

CHECK YOUR PROGRESS

(Short Type questions)

1. Can we use double linked list as a circular linked list? If yes, explain.

2. Write the difference between double linked list and circular linked list.

3. Explain the difference between a sequential and linked representation of a list.

4. State true or false (& explain why?)

a. An array is a random access structure.

b. A linked list is a random access structure.

c. A sequential list is a random access structure.

d. A sequential list is always stored in a statically allocated structure.

5. Write a function to delete a node from a linked list.

6. Define stack? What are the different types of operations of the stack?

7. Explain the application of the linked list.

8. What is circular linked list? Explain its operations.

52

U N I T – I I I

53

' ' ' '

& ' !& ' !& ' !& ' !

""""( ) ( ) ( ) ( )

UNIT -3

OBJECTIVES

After going through this unit, you should be able to:

To define a tree as abstract data type.

Learn the different properties of a Tree and a Binary Tree.

Implement the tree an binary tree, and

Give some application of tree etc.

INTRODUCTION Tree is a hierarchical relationship among data items. Indicative of this hierarchy is the parent-child terminology used to express the relationship between items on successive levels of the tree.

WHAT IS TREE?

A tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several data items. A tree structure depicts a parent-child relationship among the nodes of the tree. A tree is an acyclic simple, connected graph. A tree contains no loops and no cycles. There is not more than one edge between any pair of nodes. Trees are by nature recursive structure, with each node of a tree being itself the root of a similar, embedded sub tree.

54

The graph theoretic definition of tree is: it is a finite set of one or more data items (nodes) such that There is a special data items called root of the tree. And its remaining data items are partitioned into number of mutually exclusive (i.e. disjoint) subsets, each of which is itself a tree, and they are called sub-trees. TREE TERMINOLOGY There are number of terms associated with the trees which are listed as follows: ROOT: - It is specially designed data item in a tree. It is the first in the hierarchical arrangement of data items. From the above diagram ‘A’ is the root item. NODE: - Each data item in a tree is called a node. It is the basic structure in a tree. It specifies the data information and links (branches) to other data items. DEGREE OF A NODE: - It is the number of sub-trees of a node in a given tree. From the above example of tree, Degree of node A is 3 Degree of node C is 1 Degree of node B is 2 Degree of node H is 0 Degree of node I is 3 DEGREE OF A TREE: - It is the maximum degree of nodes in a given tree. In the above example the node A has degree 3 and another node I has also same degree. In all this value is the maximum. So, the degree of the above tree is 3. Terminal nodes: - A node with degree zero is known as terminal nodes. It is otherwise known as leaf of the tree. From the above example the tree having E, J, G, H, K L and M having the leaf nodes. NON-TERMINAL NODES: - Any node (except the root node) whose degree is not zero is called non-terminal nodes. Non-terminal nodes are the intermediate nodes in the traversing given tree from its root node to the terminal nodes (leaves). There are 5 non-terminal nodes form the above tree example.

A

B C D

E F G H I

F K L M

Level 0

Level 1

Level 2

Level 3

Root

(A tree structure)

55

SIBLINGS: - The children nodes of a given parent node are called siblings. They are also called bothers relationship in the above tree. In the above tree, E and F are the siblings of parent node of B K, L and M are the siblings of parent node I. LEVEL: - The entire tree is leveled in such a way that the root node is always at level 0. These immediate children are at level 1 and their immediate children are level 2 and so on up to the terminal nodes. In general, if node is at level n, then its children will be at level n + 1 in the 4 levels. EDGE: - It is a connecting line of two nodes. That is, the line drawn from one node to another node is called an edge. PATH: - It is a sequence of consecutive edges from the source node to the destination node. In the above tree, the path between A and J is given by the node pairs, (A, B), (B, F) and (F, J) DEPTH: - It is the maximum level of any node is given tree. In the above tree, the root node A has the maximum level. That is the number of levels one can descend the tree from its root to the terminal nodes (leaves). The term height is also used to denote the depth. FOREST: - It is a set of disjoint trees. In a given tree, if we remove its root node then it becomes a forest. In the above tree, there is forest with three trees. BINARY TREES

A binary tree is a finite set of data items, which is either empty or consists of a single item called the root and two disjoint binary trees called the left-sub tree and right-sub tree. A binary tree is a very important and the most commonly used non-linear data structure. In a binary tree the maximum degree of any node is at most two. That means, there may be a zero degree node (usually an empty tree) or a one-degree node and two degree node.

From the above binary tree, A is the root of the tree. The left sub tree consists of tree with root B and the right sub tree consists of the tree with root C. Further B has its left sub tree with root D and the right sub tree with root E.

Similarly, C has its left sub tree F and its right sub tree with root G. In the next level D have empty right sub tree and its left sub tree with root J. F has neither left sub tree nor right sub tree.

A

B C

D E F G

J K

L

A Binary Tree

56

PROPERTIES OF A BINARY TREE

It is finite (possibly empty) collection of elements.

A non-empty binary tree has a root element.

The remaining elements (if any) are partitioned into two binary trees i.e. left and right sub trees.

Each parent has at most two-child nodes i.e. it can have), 1 or 2 child nodes.

The maximum number of nodes in a binary tree of height ‘K’ is 2K-1, where K >= 1.

The minimum number of nodes in a binary tree of height ‘K’ is K. STRICTLY BINARY TREE If every non-terminal node in a binary tree consists of non-empty left sub tree and right sub tree, then such a tree is called strictly binary tree.

In the above binary tree all the non-terminal nodes such as B and E having non-empty left and right sub trees. DIFFERENCES BETWEEN A TREE AND A BINARY TREE

No nodes in a binary tree have a degree more than 2, where there is no limit on the degree of a node in a tree.

A binary tree may be empty; but a tree cannot be empty.

The sub-tree of a binary tree are ordered where as in case of tree are not ordered. Complete binary tree

A binary tree with n nodes and of depth d is a strictly binary tree all of whose terminal nodes are at level d. In a complex binary tree, there is exactly one node at level 0, two nodes at level 1, four nodes at level 2 and so on.

If there are m nodes at level 1 then a binary tree contains at most 2m nodes at level 1 + 1. A binary tree has exactly one node at root level and has at most 21 nodes at level 1. Taking into consideration of this property we can show, further that a complete binary tree of depth d contains exactly 21 nodes at each level. The vale 1 ranges from 1 to d.

A

B C

D E

F G

A Strictly Binary Tree

57

EXTENDED BINARY TREE

A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children. In such a case, the nodes with 2 children are called internal nodes and the nodes with 0 children are called external nodes.

Sometimes the nodes are distinguished in diagrams by using circle for internal nodes are squares are used for external nodes. The term extended binary tree comes from the following operations. Consider any binary tree T that may be converted into a 2-tree by replacing each empty sub tree by a new node as shown below. We can observe that the new tree is indeed, a 2 tree. New nodes are the external nodes of the extended binary tree.

REPRESENTATION OF BINARY TREE IN MEMORY Binary tree can be represented into two ways.

Array representation of binary tree.

Linked list representation of binary tree. Array representation of Binary tree An array can be used to store the nodes of a binary tree. The nodes stored in an array are accessible sequentially. In c programming language, arrays starts with index 0 to MAXSIZE – 1.

A

B C

D E F G

J K L

A Complete binary Tree with depth 4

K J L L L

(Binary Tree T) (Extended 2-Tree)

58

The root node is always at index 0. Then, in successive memory locations the left child and right child are stored. The maximum number of nodes is specified by MAXSIZE. The root node is always at index 0. Then, in successive memory locations the left child and right child are stored. Consider a binary tree with only three nodes as shown below, Here, A is the father of B and C. B is the left child of A and C is the right child of A. Let us extend the above tree by one more level s shown below, The father node and right and left node can be identified from arbitrary nodes. It is very simple to identify the father and children of a node. For any node n, 0 <= n <=(MAXSIZE – 1), then we have the following. Father (n): - The father of node having index n is at floor ((n-1)/2) if n is not equal to 0. If n = 0, then it is the root node and has no father. For example consider a node numbered 3 (i.e. D). Then the father of d is B whose index is 1. This can be obtained by the following formula. Floor ((3-1)/2) 2/2=1 (which is the index of the array). Lchild (n): - The left child of node numbered n is at (2n + 1). For example, in the above binary tree, lchild (c) = lchild (2) = 2 X 2 + 1 = 5 (i.e. the node with index 5 which is nothing but F)

A 0

B 1 C 2

Tree Representation

A

B

C

BT (0)

BT (1)

BT (2)

Array Representation of binary tree

A 0

B 1 C 2

Tree Representation

B

C

BT (0)

BT (1)

BT (2)

D E F G3 4 5 6 D

E

F

G

BT (3)

BT (4)

BT (5)

BT (6)

59

rchild (n): - The right child of node numbered n is index (2n + 2). For example, in the above binary tree, rchild(A) = rchild(0) = 2 X 0 + 2 = 2 (i.e. the node index 2 which is nothing but C). Siblings: - If the left child at index n is given then its right sibling (or brother) is at (n+1). And similarly, if the right child at index n is given, then its left sibling is at (n-1). For example, the right sibling is at (n-1) of node indexed 4 is at index 5 in an array representation. The array representation is more ideal for the complete binary tree. But, this is not suitable for other then complete binary tree as it results in unnecessary wastage of memory space. Linked list representation of Binary tree. Binary tree can be represented either using array representation or using a linked list representation. The basic component to be represented in a binary tree is a node. The node consists of three fields such as

Data

Left child

Right child The data field holds the value to be given. The left field is a link, which contains the address of its left node, and the right child contains the address of the right node. The logical representation of the node in C language is as follows: Struct node

char data; //character data type struct node * lchild; struct node * rchild; ; typedef struct node node; //Type defination for a node

Lchild Data Rchild

(A node structure of a binary tree)

60

Consider a binary tree, which can be represented in linked list representations.

A binary tree contains one root node and some non-terminal nodes (leaves). It is

clear from the observation of a binary tree that the non-terminal nodes have left child and right child nodes. But the terminal nodes have no left child and right child nodes. Their lchild and rchild pointer are set to NULL. Here the non-terminal nodes are called internal nodes and the terminal nodes are called external nodes. In a specific application user may maintain two sets of nodes for both internal nodes and external nodes. CREATION OF BINARY TREE Create_tree(intfo,node) Where, info-> information for which we have to create node. Node -> Structure type variable to pointer both left and right child. Step 1: [Check whether the tree is empty] If node = NULL Node = create a node. Left_child[node] = NULL Right_child[node] = NULL Step 2: [Test for the left child]

A

B C

Tree Representation

DE F

A

B C

D E F

G

(A binary tree in linked list representation)

61

If info[node] >= info Left_child[node] = call create_tree (info,left_child[node]) Else Right_child[node]=call create_tree(info,right_child[node]) Step 3: return (node) OPERATIONS ON BINARY TREE The basic operation to be performed on a binary tree is as follows: Create: - It creates an empty binary tree. MakeBT: - It creates a new binary tree having a single node with data field set to some value. EmptyBT: - It returns true of the binary tree is empty. Otherwise it returns false. Lchild: - It returns a pointer to the left child of the node. If the node has no left child it returns the null pointer. Rchild: - it returns a pointer to the right child of the node. If the node has no right child it returns the null pointer. Father: - it returns a pointer of the father of the node. Otherwise returns the null pointer. Sibling (Brother): - It returns a pointer to the brother of the node. Otherwise returns the null pointer. Data: - It returns the contents of the node. Besides these primitive operations, other operations that can be applied to the binary tree are; Tree traversals, Insertion of nodes, Deletion of nodes, Searching of nodes, Copying the binary trees etc. TRAVERSAL OF A BINARY TREE

Tree traversal is one of the most common operations performed on tree data structure. It is away in which each node in the tree is visited once in a systematic manner. There are many applications that essentially require traversal of binary trees. For example, a binary tree could be used to represent an arithmetic expression as shown below:

The full binary tree traversal would produce a linear order for the nodes in a binary tree, there are three popular ways of binary tree traversal. They are

+

* C

A B (Binary tree with arithmetic expression)

62

Pre-order traversal

In-order traversal

Post-order traversal Pre-order traversal The pre-order traversal of a non-empty binary tree is defined as follows:

1. Visit the root node (N). 2. Traverse the left sub tree in pre-order (L). 3. Traverse the right sub tree in pre-order (R).

That is in the pre-order traversal the root node is visited (or processed) before traversing its left and right subtrees. The pre-order notion is recursive in nature, so even within the left subtree and right subtree the above three steps are followed. The pre-order traversal of binary tree is shown below.

A B D E I C F G J Inorder Traversal The inorder traversal of a non-empty binary tree is defined as follows:

1. Traverse the left subtree in inorder (L). 2. Visit the root node (N). 3. Traverse the right subtree in inorder ( R).

That is, in an inorder traversal, the left subtree is traversed recursively in inorder before visiting the root node. After visiting the root node, the right subtree is taken up and it is traversed recursively againg in inorder. Postorder Traversal The postorder traversal of non-empty binary tree is defined as follows:

1. Traverse the left subtree in postorder (L) 2. Traverse the right subtree in postorder ( R). 3. Visit the root node.

A

B C

D E F G

I J

A binary tree (pre order traversal)

63

That is, in a postorder traversal the left and right subtree are recursively processed before visiting the root. The left subtree is taken up first and traversed in postorder. Then the right subtree is taken up and is traversed in post order. Finally, the data of the root is displayed.

AVL TREE (BINARY BALANCED TREE)

• Searching in a BST is efficient if the heights of both left and right sub-trees of any node are equal. A balanced binary tree (AVL tree) is a binary tree in which height of the two sub-trees of every node never differs by more than one.

• The name AVL tree is derived from the names of its inventors who are Adelson, Velskii, and Landies.

• The balance of a node in a binary tree is defined as the height of its left sub-tree minus the height of its right sub-tree.

• Each nodes balance factors is calculated by: BF = (left Sub-tree height) – (right sub-tree height).

• The efficiency of searching is ideal if the difference between the heights of left and right sub-trees of all the nodes in a BST has a balance factor of 1, -1 or 0.

• After insertion of a node in a binary tree, the resulting tree may or may not be balance.

• To represent a node of an AVL tree four fields are required. o For storing the data field. o For storing the address of the left child. o For storing the address of the right child. o An additional integer field to hold the balance factor.

Struct AVLNODE int data; struct AVLNODE *leftchild; struct AVLNODE *rightchild; int balancefactor; ;

• If a tree is not balance then rotate it by using various rotation methods to balance the height;

o LL rotation (Left sub-tree to left child) o RR rotation (Right sub-tree to right child) o LR rotation (Right sub-tree to Left child) o RL rotation (Left sub-tree to Right child)

LL Rotation: - When the new node is added to the left sub-tree of the child and then if any node is unbalanced, then do one LL rotation of the tree with the unbalanced node as root.

64

RR Rotation: - When the new node is added to the right sub-tree of right child and then if any node is unbalanced then do one RR rotation to the tree with the unbalanced node as root.

LR Rotation: - When the new node is added to the right sub-tree of left child and then if any node is unbalanced, then do one LR rotation to the tree with unbalanced node as root. RL Rotation: - When the new node is added to the left sub-tree of right child and then if any node is unbalanced, then do one RL rotation to the tree with the unbalanced node as root.

18

16

12

16

12 18

0

1

2 0

0 0

(Unbalanced Tree)

(Balanced Tree)

10

30

20

10 30

0

-1

-2 0

0 0

(Unbalanced Tree)

(Balanced Tree)

20

20

10

30

20

10 30

0

-1

2 0

0 0

(Unbalanced Tree)

(Balanced Tree)

25

75

50

50

25 75

0

1

-2 0

0 0

(Unbalanced Tree)

(Balanced Tree)

65

CHECK YOUR PROGRESS Define the term tree? Explain the use of tree in computer science. What is a Binary tree? What is the difference between tree and binary tree? What is an AVL tree? Explain about different types of operation of AVL tree.

66

U N I T – I V

67

& & & &

& & & &

""""* * * *

UNIT - 4

OBJECTIVES

After going through this unit, you should be able to:

• Know the basic concepts of searching;

• Know the process of performing the linear search & binary search with its

applications.

• Know about different types of sorting methods.

• It performance and its applications.

• Key terms: - Sequential, Binary, Heap, Bubble, Merge.

Searching is the process of looking for something: Finding one piece of data that has been stored within a whole group of data. It is often the most time consuming part of many computer programs. There are a variety of methods or algorithms, used to search for a data item, depending on how much data there is to look through, what kind of data it is, what type of structure that is stored and even where the data is stored – inside computer memory or on some external medium.

68

Searching is very common task in day-to-day life, where we are involved some or other time, in searching either some needful at home or office to market or even searching a word in a dictionary. All the above facts apply to our computer program also. Suppose we have a telephone directory stored in the memory in an array which contains name and telephone numbers. Now what happen if we have to find a number? The answer is searching that number in the array according to name. If the names were organized in some order, searching would have been fast. So, basically a search algorithm is an algorithm which accepts an argument ‘a’ and tries to find the corresponding data where the match of ‘a’ occurs in a file or in a table of data. Searching is an application which finds the location of a given element of an array. The search is said to be successful or unsuccessful on whether the element search is found or not. Two standard searching methods are used in computer science. They are as follows: Leaner search / sequential search. Binary search

LINEAR SEARCH / SEQUENTIAL SEARCH This is the simplest method of searching. In this method, the element to be found is sequentially searched in the list. This method can be applied to a sorted or un-sorted list. Searching in case of sorted list starts from 0th element and continues until the element is found or an element whose value is greater (assuming the list is sorted in ascending order) than the value being searched is reached. As against this, searching in case of un-sorted list starts from the 0th element and continues until the element is found or the end of the list is reached. Linear search is not the most efficient way to search for an item in a collection of items. However it is very simple for implementation. Moreover, if the array elements are arranged in random order, it is the only reasonable way to search. In addition, efficiency becomes important only in large arrays; if the array is small, there are not many elements to search and the amount of time it takes is not even noticed by the user. Hence, in many situations, linear search is a perfectly valid approach.

ALGORITHM (Linear Search of an Un-Sorted Array) Suppose a linear array DATA contains N elements, and suppose a specific ITEM of information is given. We want to find the location LOC of ITEM in the array DATA, or to send some message, such as LOC = -1, to indicate that ITEM does not appear in DATA. Here LB is the lower bound of an array, and UB is the upper bound of an array.

69

LINEAR (DATA, N, ITEM) Step 1: Set LOC = LB Step 2: Repeat while LOC <= UB If DATA [LOC] = ITEM then Return LOC. [Search found at LOC] Else Set LOC = LOC + 1 [Increment the counter] [End of loop] Step 3: return -1 Step 4: END

ANALYSIS OF LINEAR SEARCH Best case complexity: The KEY element is available at the first position i.e. at DATA [0]. T (n) = O (1).

Worst case complexity: The KEY element is available at the end position i.e. at DATA [N-1] or it is not available in the array. T (n) = O (n).

Average case complexity: Here, we assume that KEY does appear in DATA, and that is equally likely to occur at any position in the array. Accordingly, the number of comparisons can be any of the number 1, 2, 3 . . . n, and each number occur with probability P = 1 / n. Then, average case = Best case + worst case / 2. T (n) = O (n+1)/2.

BINARY SEARCH An un-sorted array is searched by linear search that scans the array elements only by one until the desired element is found. The reason for sorting an array is that we search the array “quickly”. Now, if the array is sorted, then we can employ binary search, which brilliantly halves the size of the search space each time it examines one array element. As the name suggests, binary means two, so it divides an array into two halves for searching. This approach is applicable only to an ordered element (i.e. in either ascending or in descending order). Binary search method is very fast and efficient. This method requires that the list of elements be in sorted order. In this method, to search an element we compare it with the element present at the center of the list.

If it matches then the search is successful. Otherwise, the list is divided into 2 halves. One from 0th to center element (1st half) and another from center element to the last element (2nd half).

70

As a result, all the elements in the 1st half are smaller than the center element, whereas, all the elements in 2nd half are greater than the centre element. The searching will now proceed in either of the 2 halves depending upon whether the element is greater or smaller than the center element. If the element is smaller than the center element then searching will be done in the 1st half otherwise in the 2nd half. Same process of comparing the required element with the centre element and if not found then dividing the elements into 2 halves is repeated for the 1st half or 2nd half. This procedure is repeated till the element is found, or the division of half parts gives one element. For example: Suppose, the array consists of 10 sorted numbers and 67 is the number that is to be searched. Then the binary search method when applied to this array work as follows.

67 is compared with the element present at the centre of the list (i.e. 11), since 67 is

greater than it, the searching is restricted only to the 2nd half of the array. Now 67 is compared with the center element of the 2nd half of the array (i.e. 29). Here

again 67 is greater than 29, so searching now proceed in the elements present between the 29 and the last element 99.

This process is repeated till 67 is found or no further division of sub-array is possible.

ALGORITHM (BINARY Search of a Sorted Array) Here, DATA is sorted arrays which contains N elements, and suppose a specific ITEM of information is given. We want to find the location LOC of ITEM in the array DATA, or to send some message, such as LOC = -1, to indicate that ITEM does not appear in DATA. Here LB is the lower bound of an array and UB is the upper bound of an array.

1 3 5 7 11 13 18 29 67 99

71

BINARY (DATA, N, ITEM) Step 1: Set lower = DATA [LB] and set upper = DATA [UB] and Set mid = (lower + upper) / 2. Step 2: Repeat step 3, step 4, and step 5 while lower <= upper Step 3: if DATA [mid] = ITEM then Return mid; [End if] Step 4: if ITEM < DATA [mid] then Set upper = mid -1 Else Set lower = mid + 1 Step 5: Set mid = (lower + upper) / 2 [End of step 2 loop] Step 6: return – 1 Step 7: END

ANALYSIS OF BINARY SEARCH Best case complexity The KEY element is available at the middle place i.e. at DATA [mid]. T (n) = O (1).

Worst case complexity The number of comparisons for a successful as well as for an unsuccessful search is approximately given by: N * (1/2) * (1/2) * …. (1/) = 1 N / (2K) = 1 2K = n k= log2n or O (log2n)

Sorting is a basic operation in computer science. Sorting refers to the operation of arranging data in some given sequence i.e., increasing order or decreasing order.

Sorting is categorized as Internal Sorting and External Sorting.

By internal sorting means we are arranging the numbers within the array only which is in computer primary memory, where the external sorting is the sorting of numbers from the external file by reading it from secondary memory.

SORTINGS

Let P be a list of m elements P1, P2, P3 ... Pn in memory. Sorting P means arranging the contents of P in either increasing or decreasing order i.e.

72

P1> p2> P3> P4>P5> Pn

There are m elements in the list; therefore there are m ways to arrange them. EXAMPLE Suppose an array value contain 10 elements as follows:

Values: 7, 9, 3, 6, 1, 2, 18, 5, 4, 7. After Sorting, VALUE must appears as VALUE: 1, 2, 3, 4, 5, 6, 7, 8, 9, 18

BUBBLE SORT

In bubble sort, each element is compared with its adjacent element. If the first element is larger than the second one then the position of the elements is interchanged, otherwise it is not changed.

Then next element is compared with its adjacent element and the same process is repeated for all the elements in the array.

During the next pass the same process is the largest leaving the largest element. During the pass, the second largest element occupies the second last position. During the next pass, the same process is repeated leaving the largest element. During this pass, the largest element occupies the n-l position.

The same process is repeated until no more elements are left for comparison. Finally the array is sorted one. ALGORITHM: BUBBLE SORT a [n] This algorithm sorts the Array a with n elements Step 1: Initialization

Set i = 0. Step 2: Repeat steps 3 to 5 until i < n Step 3: Set j = 0 Step 4: Repeat step 5 until j < n - i – 1 Step 5: if a [j] > a [ j + 1 ] then

Set temp = a[ j ] Set a[ j ] = a[j + 1 ] Set a[j + 1] = temp

end if 6. Exit ANALYSIS OF BUBBLE SORT

In the Bubble sort, the first pass require (n - 1) comparisons to fix the highest element to its location, the second pass require (n - 2),..., kth pairs requires (n -k) and the last pass requires only one comparison to be fixed at its proper position. Therefore the total comparisons are

f(n) = (n -1)+ (n -2)+(n -3) +.., + (n -k) +3+2+ 1 =n (n -1)/2 f (n) = O(n2)

73

SELECTION SORT

The selection sort technique is based upon the extension of the minimum/maximum technique. By means of a nest of for loops, a pass through the array is made to locate the minimum value.

Once this found, it is placed in the first position of the array (position 0). Another the remaining elements are made to find the next smallest element, which is placed in the second position (position 1), and so on. Once the next to last element is compared with the last element, all the elements have been sorted into ascending order.

More precisely: Let ‘a‘ be a linear array of n elements. Pass 1. Find the location loc of the smallest in the list of n elements. a [1], a[2],. . . , a[n], and then interchanged a[loc] and a[1]. Then: Pass 2. Find the location loc of the smallest in the sub-list of n -1 elements a[2], a[3], . . , a[n], and then interchange a[loc] and a[2]. Then: a[1], a[2] is stored, since a[1] <= a[2]. Pass 3. Find the location loc of the smallest in the sub-list of n - 2 elements a[3], a[4], . . ,a[n], and then interchanged a[loc] and a[3]. Then: a[l], a[2], . . , a[3] is stored, since a[2] <= a[3]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pass n - 1. Find the location loc of the smaller of the elements a[n - 1], a[n], and then

interchanged a[loc] and a[n - 1]. Then: a[l], a[2], . . , a[n] is stored, since a[n - 1] <= a[n].

ALGORITHM Procedure min (a, k, n, loc) An array ‘a’ is in memory. This procedure finds the location loc of the smallest element among a [ k ], a[ k + 1 ], . . , a[n] during k pass. min (a, k, n, loc) 1. Set min: = a [k] and loc: = k. [Initializes pointers.] 2. Repeat for j = k + 1, k + 2, . . , n:

It min > a [ j ], then: Set min: = a [ j ] and loc: = j. [End of loop.]

3. Return. ALGORITHM Selection sort (A [MAXSIZE], N) This algorithm can now be easily started 1. Repeat Steps 2 and 3 for k = 1, 2, . . , n - 1: 2. Call min(a, k, n, loc). 3. [Interchange a[ k ] and a[ loc ] ]

Set temp: = a[ k ], a[ k ] : = a[loc] and a[loc] : = temp. [End of Step 1 loop.]' .

4. Exit.

74

ANALYSIS OF SELECTION SORT The first pass required (n -1) comparisons to find the location loc of smallest element, the second pass required (n - 2) ... kth pass requires (n - k), and the last pass requires only one comparison. Total numbers of comparisons are

= ( n – 1 ) + ( n – 2 ) + ( n – 3 ) +. . . + ( n – k ) + 3 + 2 + 1 = n ( n – 1 ) / 2 = O (n2 ).

INSERTION SORT An insertion sort is one that sorts a set of values by inserting values into an existing

sorted file. Suppose an array ‘a’ with n elements a[1], a[2], . . , a[n] is in memory. The insertion

sort algorithm scans a from a[l] to a[n], inserting each element a[ k ] into its proper position in the previously sorted sub array a[I], a[2], . . , ark - 1]. That is :. Pass 1: a[l] by itself is trivially sorted. . Pass 2: a[2] is inserted either before or after a[1] so that: a[I], a[2] is sorted. a[3] 1s inserted into its proper place in a[I], a[2], that is, before a[1], between a[l] and a[2], or after a[2], so that: a[I], a[2], a[3] is sorted. a[4] is inserted into its proper place in a[I], a[2], a[3] so that: a[I], a[2], a[3], a[4] is sorted. a[n] is inserted into its proper place in a[I], a[2], . . , a[n - 1] so that: a[I]; a[2], . . , a[n] is sorted; ALGORITHM Let a be an array of n elements which we want to sort temp be a temporary variable to interchange the two values. K be the total number of passed and j be another control variable. Step 1: Set k = 1 Step 2: for k = 1 to (n – 1) Set temp = a [k]. Set k = k – 1 While temp M a [j] and (j >=0) perform the following steps. Set a [i+1] = a[j] [End of the loop] Assign the value of temp to a[j+1] [End of the loop structure] Step 3: END

75

QUICK SORT

It is one of the most popular- sorting techniques. Quick sort possesses a very good average case behavior among all the sorting techniques. This is developed by CA.R. Hoare:

The Quick sort algorithm works by partitioning the array to be sorted. And each partition is in turn sorted recursively. In partition, one of the array elements is chosen as a key value. This key value can be the first element of an array. That is, if a is an array then key=a [O]. And rest of the array elements are grouped into two partitions such that One partition contains elements smaller than the key value. Another partition contains elements larger than the key value.

Portion 1 Portion 2

n

Key Values > key Values < key (Keys and partition of element of an array)

76

U N I T – V

77

+ & '& + & '& + & '& + & '&

' ' ' '

&) &) &) &)

""""( $ , ( $ , ( $ , ( $ ,

UNIT – V

GRAPHS

This chapter investigates another non-linear data structure Graph. In this chapter we will discuss the representation of graphs, operations on them and algorithms such as Breadth first search and Depth first search, etc, certain application of graphs. BASIC CONCEPTS AND DEFINITIONS A graph G is defined as a set of two tuples that is G = (V, E), where V represents set of vertices of G and E represents the set of edges of G. there exists a mapping from the set of edges to a set of pair of elements of V.

From the above figures, it is clear that part (a) consists of 2 vertices each one is stand

alone; (b) contains an undirected graph having one edge and two vertices. Similarly (c)

78

consists of two vertices and one edge and it is a directed graph; (d) illustrates an undirected graph with 6 vertices and 9 edges. (e) is a directed graph with 5 vertices and 7 edges. In a graph G = (V,E), an edge that is directed from one vertex to another is called a directed edge, while an edge which has no specific direction is called an undirected and some are undirected in a graph, then the graph is called a mixed graph.

If a node in a tree is considered to be an entity and an edge between two nodes represent a relation between two entities then a tree can be conveniently represent a one to many relation from a given set of objects to the same set. However if the relationship is many to many then describing that relation using a tree would not be possible. These types of problems can be solved by graphs.

Consider a classic problem known as "Traveling sales person problem". A sales person has to tour a number of cities by road before coming back to the city where he started. The cities are connected by roads and the sales person has to minimize the distance to be covered by the tour. Note that some pairs of cities are directly connected.

Let the cities be represented by nodes and let an edge between two cities represent that these cities are connected. Moreover, suppose that each edge is labeled with the distance between the cities connected by the edge. Suppose there are five cities. The interconnection among these cities is shown in below Figure.

(Five cities and their interconnection by road.)

Such diagrams are called graphs. There are many other situations, which are

represented by graphs. There are many other domains of applications of graphs e.g. Electrical Engineering, Telephone Switching, Geographical Information System etc. where the problems are best represented by graphs.

GRAPH

A graph, G consists of two sets V and E, where V is a finite non empty set of vertices, and E is a set of pairs of vertices, these pairs are called edges.

V (G) and E (G) will represent the sets of vertices and edges of graph G. We will also write G = (V, E) to represent a graph.

If the number of vertices in a graph is infinite then the graph is said to be an infinite graph; otherwise the graph is called a finite graph. In this chapter finite graphs are only considered.

B

79

UNDIRECTED GRAPH

When the edges of a graph are unordered pairs then the graph is said to be an undirected graph. In an undirected graph the pair of vertices representing any edge is unordered. Thus the pairs (V1, V2) and (V2, V1) representing the same edge. The order in which the vertices Vi and Vj occur in the pair (Vi, Vj) is immaterial for describing the edge.

Graph with 5 vertices and 5 edges. V (G) = V1, V2, V3, V4, V5

E (G) = (v1, v2), (v1, v3), (v3, v4), (V2, V4), (V4, V5).

DEGREE OF A VERTEX

The degree of a vertex V in a graph G is equal to the number of edges in G, which are incident on V.

Deg (V1) = 2 Deg (V2) = 2 Deg (V3) = 2 Deg (V4) = 3 Deg (V5) = 1 Each edge is counted twice in counting the degrees of the vertices of G. The sum of the degrees of the vertices of a graph G is equal to twice the number of edges in G. (2+2+2+3+1 = 5 * 2).

DIRECTED GRAPH OR DIGRAPH The edges of a directed graph are drawn with an arrow from the tail to the head. In

directed graph each edge is represented by a directed pair (Vi, Vj). Vi is the tail and Vj is the head of the edge.

Therefore (Vj, Vi) and (Vi, Vj) represent two different edges.

V1 V2

V4 V3

V5

(Simple Undirected Graph)

(Directed Graph)

80

DEGREE OF A VERTEX

In a directed graph, we define the in-degree of a vertex V to be the number of edges

for which V is the head. The out-degree is defined as the number edges for which V is the

tail. The out-degree of a vertex V of G, written as outdeg(V), is the number of edges

beginning at V, and the in-degree of V, written as indeg(V), is the number of edges ending at V.

Outdeg(V1 )=3 indeg(V1)=1 Outdeg(V3)=O indeg(V3)=3

The sum of the out-degrees of the vertices of a digraph G equals the sum of the

in-degrees of the vertices, which equals the number of edges in G.

The sum of the out-degrees of the vertices of a digraph G equals the sum of the in-degrees of the vertices, which equals the number of edges in G.

(V1, V4) and (V4, V1) represent two different edges.

WEIGHTED GRAPH G is called a weighted graph if each edge e of G is assigned a non-negative number w(e)

called the weights. Weights of an edge are also called its cost. GRAPH TERMINOLOGY

V1 V2

V4 V3

V5

(Weighted Graph)

2

3

2

3 4

A D

C B

E

e1

e4

e5

e6 e7

(Undirected weighted graph)

81

PARALLEL EDGE Two edges are said to be parallel if they have the same pair of end vertices. In the fig4, e4 and e5 are parallel edges. SELFLOOP

A self-loop is an edge whose end vertices are identical (Le. same). In the fig4, the edge e6 is called a loop since its endpoints are the same vertex. MUL TI GRAPH

The graph which contains the parallel edges and self loops is called a multi-graph. ADJACENT VERTICES

A vertex Vi is adjacent to (or neighbor of) another vertex say, Vj, if there is an edge from Vii

to Vj. In the Fig4, the vertices adjacent to A are C and O. In the below figure the vertex

adjacent to A is C. the vertices adjacent to Care 8 and O. EDGES INCIENT ON VERTEX If there is an edge from Vi. to Vj, then we shall say that the edge 0h Vj) is incident on vertices Vj. In the Fig4, the edges incident on vertex 8 are 08(e3). and C8(e4). In the below figure the edge incident on vertex A is BA, and the edges incident on vertex B are DB and CB.

PATH A route in which no vertex appears more than once is called a path. Definition: A path from vertex Vp to vertex Vq in graph G is a sequence of vertices Vp, Vi1

Vi2. .. Vq such that (Vp, Vi1), (Vi2 V12), . . . . (Vin, Vq) are edges in E (G). The number of edges in a path is called the length of a path. A simple path is a path in which all vertices except possibly the first and last are distinct. A cycle is a path in which the first and last vertices are the same ACYCLIC GRAPH If a graph (or digraph) does not have any cycle then it is called acyclic graph. TRAVERSAL OF GRAPHS A number of graph problems involve traversal of graph. Traversal of graph means visiting each of its vertices exactly once. This accomplished by visiting the vertices in a systematic way. Two commonly used techniques of graph traversals are known as BFS and DFS.

A D

C B

(Undirected graph)

82

DEPTH-FIRST SEARCH (DFS) Depth first search of an undirected graph proceeds as follows. The start vertex V is

visited. Next an unvisited vertex W adjacent to V is selected and a depth first search from W is initiated.

When a vertex U is reached such that all its adjacent vertices have been visited, we back up to the last vertex visit, which, has an unvisited vertex W adjacent to it, and initiate a depth first search from W. The search terminates when no unvisited vertex can be reached from any of the visited ones. This procedure is best described recursively as follows: Algorithm DFS(V) Given an undirected graph G = (V, E) with n vertices and an array VISITED (n) initially set to zero, tile algorithm visits all vertices reachable from V. Step 1: VISITEDM =1 Step 2: for each vertex W adjacent to V do Step 3: if VISITED(W) = a then Step 4: call DFS(W)

end end

Step 5: EXIT Depth first searches are performed by dividing downward into a tree as quickly as

possible. Depth first search always generating a child node from the most recently expanded node, then generating that child's children and so on until a gO'11 is found or some cut off depth point d is reached. If a goal is not found when a leaf node is reached or at the cutoff point, the program backtracks the most recently expanded node and generates another of its children. This process continues until a goal is found or failure occurs.

Let us start with v1. v1 adjacent vertices are v2, v3. Let us pick on v2. v2 adjacent vertices are v1,v4,v5. v1 is already visited. Let us pick on v4. v4 adjacent vertices are v2 and v8. v2 is already visited. Let us visit v8. v8 adjacent vertices are v4, v5, v1, v6, v7. v4 and v1 are already visited. Let us visit v5. v5 adjacent vertices are v2,v8. Both are already visited. Therefore, we backtrack. V6, v7 unvisited in the list of v8. We may visit. Any, let us visit v6.

83

v6 adjacent are v8,v3. V8 is already visited. We visit v3. v3 adjacent vertices are v1, v7. We visit v7. . All the adjacent vertices of v7 are already visited. Therefore the sequence of traversal is v1, v2, v4, v8, v5, v6, v3, v7.

BREADTH-FIRST SEARCH (BFS)

Starting at vertex V and marking it as visited, breadth first search differs from depth first search in that all unvisited vertices adjacent to V, are visited next. Then unvisited vertices adjacent to these vertices are visited and so on.

ALGORITH BFS (V)

Given an undirected graph G = (V, E) with n vertices and an array VISITED (n) initially set to zero. A breadth first search of G is carried out beginning at vertex V. All vertices visited are marked as VISITED (i) = 1.

Step 1: VISITED(V) = 1 Step 2: Initialize Q to be empty //Q is a queue. Step 3: loop For all vertex W adjacent to V do If VISITED(W) = 0 then Call ADDQ(W,Q) VISITED(W)=1 End if [End of for loop] Step 4: if Q is empty then Return Step 5: Call DELETEQ(V, Q) [End of step 2 loop] Step 5: EXIT Breadth first search can be used to find the shortest distance between the starting

node and the remaining nodes of the graph. Breath first searches are performed by exploring all the nodes at a given depth before proceeding to the next level.

84

Let us start with v1. v1 adjacent vertices are v2, v3. We visit all the vertices one by one. We pick on one of these say v2. The unvisited adjacent vertices to v2 are v4, v5. We visit both. We go back to the remaining unvisited vertices of v1 and pick on one of those say v3. The unvisited adjacent vertices to v3 are v6 and v7. We visit both. We pick on one of these say v6. The unvisited adjacent vertex to v6 is v8. We visit that vertex. There are no more unvisited adjacent vertices to v8. The sequence of traversal is v1, v2, v3, v4, v5, v6, v7, v8

85

• Programming with C By Venugopal & Prasad (Tata Mcgraw Hill Publications)

• ANSI C By E. Balguruswami (Tata Mcgraw Hill Publications)

• Experts Data Structure with C By R.B. Patel (Khanna Book Publishing)

• Data Structure through C By J. Baluja (Danpat Ray publishing)

• Data structure By S. Lipschutz (Tata Mc-Graw Hill)

• Data Structure using C By Rajani Zindal (Tata Mcgraw Hill Publications)

• ANSI C programming Language –2nd Edition By Kernighan & Ritchie (Prentice-Hall of India Pvt. Ltd. 2004)

• C in Depth –2nd Revised Edition By Srivastava & Srivastava (BPB Publication)

• Data structures & Algorithms Using C – 3rd Edition By R.S. Salaria (Khanna Book Pub. Co. (P) Ltd. 2004)

: - [email protected]

!