Data Structure Notes, Lecture notes of Data Structures and Algorithms

Data Structure Notes for Computer Science

Typology: Lecture notes

2017/2018

Uploaded on 02/20/2018

shobhitshukla89
shobhitshukla89 🇮🇳

4.5

(2)

1 document

1 / 170

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
NOTES
SUBJECT: DATA STRUCTURES USING C
SUBJECT CODE: NCS-301
BRANCH: CSE
SEM: 3rd
SESSION: 2014-15
Evaluation Scheme
Subject
Code
Name of
Subject
Periods
Evaluation Scheme
Subject
Total
Credit
L
T
CT
TA
TOTAL
ESE
NCS
-
301
Data
Structures
Using C
3
1
0
30
20
50
100
150
4
Asst. Prof. Swimpy Pahuja & Priyanka Gupta
CSE Department, AKGEC Ghaziabad
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Data Structure Notes and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

NOTES

SUBJECT: DATA STRUCTURES USING C

SUBJECT CODE: NCS-

BRANCH: CSE

SEM: 3

rd

SESSION: 2014-

Evaluation Scheme

Subject Code

Name of Subject

Periods Evaluation Scheme Subject Total

Credit L T P CT TA TOTAL ESE NCS- 301 Data Structures Using C

3 1 0 30 20 50 100 150 4

Asst. Prof. Swimpy Pahuja & Priyanka Gupta CSE Department, AKGEC Ghaziabad

CONTENTS

UNIT-1 INTRODUCTION

1.1 BASIC TERMINOLOGY: ELEMENTARY DATA ORGANIZATION

1.1.1 Data and Data Item 1.1.2 Data Type 1.1.3 Variable 1.1.4 Record 1.1.5 Program 1.1.6 Entity 1.1.7 Entity Set 1.1.8 Field 1.1.9 File 1.1.10 Key 1.2 ALGORITHM 1.3 EFFICIENCY OF AN ALGORITHM 1.4 TIME AND SPACE COMPLEXITY 1.5 ASYMPTOTIC NOTATIONS 1.5.1 Asymptotic 1.5.2 Asymptotic Notations 1.5.2.1 Big-Oh Notation (O) 1.5.2.2 Big-Omega Notation (Ω) 1.5.2.3 Big-Theta Notation (Θ) 1.5.3 Time Space Trade-off 1.6 ABSTRACT DATA TYPE 1.7 DATA STRUCTURE 1.7.1 Need of data structure 1.7.2 Selecting a data structure 1.7.3 Type of data structure 1.7.3.1 Static data structure 1.7.3.2 Dynamic data structure 1.7.3.3 Linear Data Structure 1.7.3.4 Non-linear Data Structure 1.8 A BRIEF DESCRIPTION OF DATA STRUCTURES 1.8.1 Array 1.8.2 Linked List 1.8.3 Tree 1.8.4 Graph 1.8.5 Queue 1.8.6 Stack 1.9 DATA STRUCTURES OPERATIONS 1.10 ARRAYS: DEFINITION 1.10.1 Representation of One-Dimensional Array

3.8 THREADED BINARY TREE
3.9 HUFFMAN CODE

UNIT-4 GRAPHS

4.1 INTRODUCTION
4.2 TERMINOLOGY
4.3 GRAPH REPRESENTATIONS

4.3.1 Sequential representation of graphs 4.3.2 Linked List representation of graphs 4.4 GRAPH TRAVERSAL 4.5 CONNECTED COMPONENT 4.6 SPANNING TREE 4.6.1 Kruskal’s Algorithm 4.6.2Prim’s Algorithm 4.7 TRANSITIVE CLOSURE AND SHORTEST PATH ALGORITHM 4.6.1 Dijikstra’s Algorithm 4.6.2Warshall’s Algorithm 4.8 INTRODUCTION TO ACTIVITY NETWORKS

UNIT-5 SEARCHING

5.1 SEARCHING

5.1.1 Linear Search or Sequential Search 5.1.2 Binary Search 5.2 INTRODUCTION TO SORTING 5.3 TYPES OF SORTING 5.3.1 Insertion sort 5.3.2 Selection Sort 5.3.3 Bubble Sort 5.3.4 Quick Sort 5.3.5 Merge Sort 5.3.6 Heap Sort 5.3.7 Radix Sort 5.4 PRACTICAL CONSIDERATION FOR INTERNAL SORTING 5.5 SEARCH TREES 5.5.1 Binary Search Trees 5.5.2 AVL Trees 5.5.3 M-WAY Search Trees 5.5.4 B Trees 5.5.5 B+ Trees 5.6 HASHING 5.6.1 Hash Function 5.6.2 Collision Resolution Techniques 5.7 STORAGE MANGMENT 5.7.1 Garbage Collection 5.7.2Compaction

AJAY KUMAR GARG ENGINEERING COLLEGE GHAZIABAD

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

COURSE: B.Tech. SEMESTER: III

SUBJECT CODE: N CS-301 L: 3 T: 1 P: 0

NCS-301: DATA STRUCTURES USING C

Prerequisite: Students should be familiar with procedural language like C and concepts of mathematics

Objective: To make students understand specification, representation, and implementation of data types and data structures, basic techniques of algorithm analysis, recursive methods, applications of Data Structures.

Unit - I Introduction: Basic Terminology, Elementary Data Organization, Algorithm, Efficiency of an Algorithm, Time and Space Complexity, Asymptotic notations: Big-Oh, Time-Space trade-off. Abstract Data Types (ADT) Arrays: Definition, Single and Multidimensional Arrays, Representation of Arrays: Row Major Order, and Column Major Order, Application of arrays, Sparse Matrices and their representations. Linked lists: Array Implementation and Dynamic Implementation of Singly Linked Lists, Doubly Linked List, Circularly Linked List, Operations on a Linked List. Insertion, Deletion, Traversal, Polynomial Representation and Addition, Generalized Linked List.

Unit – II Stacks: Abstract Data Type, Primitive Stack operations: Push & Pop, Array and Linked Implementation of Stack in C, Application of stack: Prefix and Postfix Expressions, Evaluation of postfix expression, Recursion, Tower of Hanoi Problem, Simulating Recursion, Principles of recursion, Tail recursion, Removal of recursion Queues, Operations on Queue: Create, Add, Delete, Full and Empty, Circular queues, Array and linked implementation of queues in C, Dequeue and Priority Queue.

Unit – III Trees: Basic terminology, Binary Trees, Binary Tree Representation: Array Representation and Dynamic Representation, Complete Binary Tree, Algebraic Expressions, Extended Binary Trees, Array and Linked Representation of Binary trees, Tree Traversal algorithms: Inorder, Preorder and Postorder, Threaded Binary trees, Traversing Threaded Binary trees, Huffman algorithm.

UNIT-

1.1 BASIC TERMINOLOGY: ELEMENTARY DATA ORGANIZATION

1.1.1 Data and Data Item Data are simply collection of facts and figures. Data are values or set of values. A data item refers to a single unit of values. Data items that are divided into sub items are group items; those that are not are called elementary items. For example, a student’s name may be divided into three sub items – [first name, middle name and last name] but the ID of a student would normally be treated as a single item.

In the above example ( ID, Age, Gender, First, Middle, Last, Street, Area ) are elementary data items, whereas (Name, Address ) are group data items.

1.1.2 Data Type Data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; and the way values of that type can be stored. It is of two types: Primitive and non-primitive data type. Primitive data type is the basic data type that is provided by the programming language with built-in support. This data type is native to the language and is supported by machine directly while non-primitive data type is derived from primitive data type. For example- array, structure etc.

1.1.3 Variable It is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents. A variable name in computer source code is usually associated with a data storage location and thus also its contents and these may change during the course of program execution.

1.1.4 Record Collection of related data items is known as record. The elements of records are usually called fields or members. Records are distinguished from arrays by the fact that their number of fields is typically fixed, each field has a name, and that each field may have a different type.

1.1.5 Program A sequence of instructions that a computer can interpret and execute is termed as program.

1.1.6 Entity An entity is something that has certain attributes or properties which may be assigned some values. The values themselves may be either numeric or non-numeric. Example:

1.1.7 Entity Set An entity set is a group of or set of similar entities. For example, employees of an organization, students of a class etc. Each attribute of an entity set has a range of values, the set of all possible values that could be assigned to the particular attribute. The term “information” is sometimes used for data with given attributes, of, in other words meaningful or processed data.

1.1.8 Field A field is a single elementary unit of information representing an attribute of an entity, a record is the collection of field values of a given entity and a file is the collection of records of the entities in a given entity set.

1.1.9 File File is a collection of records of the entities in a given entity set. For example, file containing records of students of a particular class.

1.1.10 Key A key is one or more field(s) in a record that take(s) unique values and can be used to distinguish one record from the others.

1.2 ALGORITHM

A well-defined computational procedure that takes some value, or a set of values, as input and produces some value, or a set of values, as output. It can also be defined as sequence of computational steps that transform the input into the output.

An algorithm can be expressed in three ways:- (i) in any natural language such as English, called pseudo code. (ii) in a programming language or (iii) in the form of a flowchart.

1.3 EFFICIENCY OF AN ALGORITHM

In computer science, algorithmic efficiency are the properties of an algorithm which relate to the

amount of resources used by the algorithm. An algorithm must be analyzed to determine its

numbers to the set of real numbers.

We say that f and g are asymptotic and write f(x) ≈ g(x) if

f(x) / g(x) = c (constant)

1.5.2 Asymptotic Notations

1.7.2.1 Big-Oh Notation (O)

It provides possibly asymptotically tight upper bound for f(n) and it does not give best case complexity but can give worst case complexity.

Let f be a nonnegative function. Then we define the three most common asymptotic bounds as

follows.

We say that f(n) is Big-O of g(n), written as f(n) = O(g(n)), iff there are positive constants c and

n0 such that

0 ≤ f(n) ≤ c g(n) for all n ≥ n

If f(n) = O(g(n)), we say that g(n) is an upper bound on f(n).

Example - n^2 + 50n = O(n^2 )

0 ≤ h(n) ≤ c g(n)

0 ≤ n^2 + 50n ≤ c n^2

0/n^2 ≤ n^2 /n^2 + 50n/n^2 ≤ c n^2 /n^2 Divide by n^2

0 ≤ 1 + 50/n ≤ c Note that 50/n → 0 as n → ∞

Pick n = 50

0 ≤ 1 + 50/50 = 2 ≤ c = 2 With c=

0 ≤ 1 + 50/n 0 ≤ 2 Find n 0

-1 ≤ 50/n 0 ≤ 1

-20n 0 ≤ 50 ≤ n 0 = 50 n 0 =

0 ≤ n^2 + 50n ≤ 2n^2 ∀ n ≥ n 0 =50, c=

1.7.2.2 Big-Omega Notation (Ω)

It provides possibly asymptotically tight lower bound for f(n) and it does not give worst case

complexity but can give best case complexity

f(n) is said to be Big-Omega of g(n), written as f(n) = Ω(g(n)), iff there are positive constants c

and n0 such that

0 ≤ c g(n) ≤ f(n) for all n ≥ n

If f(n) = Ω(g(n)), we say that g(n) is a lower bound on f(n).

Example - n^3 = Ω(n^2 ) with c=1 and n 0 =

0 ≤ c g(n) ≤ h(n)

0 ≤ 1*1^2 = 1 ≤ 1 = 1^3

0 ≤ c g(n) ≤ h(n)

O Determine c 2 = ½

½-2/n ≤ c 2 = ½ ½-2/n = ½ maximum of ½-2/n

Ω Determine c 1 = 1/

0 < c 1 ≤ ½-2/n 0 < c 1 minimum when n=

0 < c 1 ≤ ½-2/

0 < c 1 ≤ 5/10-4/10 = 1/

n 0 Determine n 0 = 5

c 1 ≤ ½-2/n 0 ≤ c 2

1/10 ≤ ½-2/n 0 ≤ ½

1/10-½ ≤ -2/n 0 ≤ 0 Subtract ½

-4/10 ≤ -2/n 0 ≤ 0

-4/10 n 0 ≤ -2 ≤ 0 Multiply by n 0

-n 0 ≤ -2*10/4 ≤ 0 Multiply by 10/

n 0 ≥ 2*10/4 ≥ 0 Multiply by -

n 0 ≥ 5 ≥ 0

n 0 ≥ 5 n 0 = 5 satisfies

Θ 0 < c 1 n^2 ≤ n^2 /2-2n ≤ c 2 n^2 ∀n ≥ n 0 with c 1 =1/10, c 2 =½ and n 0 =

1.5.3 Time Space Trade-off

The best algorithm to solve a given problem is one that requires less memory space and less time to run to completion. But in practice, it is not always possible to obtain both of these objectives. One algorithm may require less memory space but may take more time to complete its execution. On the other hand, the other algorithm may require more memory space but may take less time to run to completion. Thus, we have to sacrifice one at the cost of other. In other words, there is Space-Time trade-off between algorithms.

If we need an algorithm that requires less memory space, then we choose the first algorithm at the cost of more execution time. On the other hand if we need an algorithm that requires less time for execution, then we choose the second algorithm at the cost of more memory space.

1.6 ABSTRACT DATA TYPE

It can be defined as a collection of data items together with the operations on the data. The word

“abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented. It involves what can be done with the data, not how

has to be done. For ex, in the below figure the user would be involved in checking that what can

be done with the data collected not how it has to be done.

An implementation of ADT consists of storage structures to store the data items and algorithms

for basic operation. All the data structures i.e. array, linked list, stack, queue etc are examples of

ADT.

1.7 DATA STRUCTURE

In computer science, a data structure is a particular way of storing and organizing data in a computer’s memory so that it can be used efficiently. Data may be organized in many different ways; the logical or mathematical model of a particular organization of data is called a data structure. The choice of a particular data model depends on the two considerations first; it must be rich enough in structure to mirror the actual relationships of the data in the real world. On the other hand, the structure should be simple enough that one can effectively process the data whenever necessary.

1.7.1 Need of data structure ∑ It gives different level of organization data. ∑ It tells how data can be stored and accessed in its elementary level. ∑ Provide operation on group of data, such as adding an item, looking up highest priority item. ∑ Provide a means to manage huge amount of data efficiently.

b) The other way is to have the linear relationship between the elements represented by means of pointers or links. These linear structures are called linked lists. The common examples of linear data structure are arrays, queues, stacks and linked lists.

1.7.3.4 Non-linear Data Structure This structure is mainly used to represent data containing a hierarchical relationship between elements. E.g. graphs, family trees and table of contents.

1.9 A BRIEF DESCRIPTION OF DATA STRUCTURES

1.8.1 Array The simplest type of data structure is a linear (or one dimensional) array. A list of a finite number n of similar data referenced respectively by a set of n consecutive numbers, usually 1, 2, 3....... n. if we choose the name A for the array, then the elements of A are denoted by subscript notation A 1, A 2, A 3.... A n or by the parenthesis notation A (1), A (2), A (3)...... A (n) or by the bracket notation A [1], A [2], A [3]...... A [n] Example: A linear array A[8] consisting of numbers is pictured in following figure.

1.8.2 Linked List A linked list or one way list is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. Each node is divided into two parts: ∑ The first part contains the information of the element/node ∑ The second part contains the address of the next node (link /next pointer field) in the list. There is a special pointer Start/List contains the address of first node in the list. If this special pointer contains null, means that List is empty. Example:

1.8.3 Tree Data frequently contain a hierarchical relationship between various elements. The data structure which reflects this relationship is called a rooted tree graph or, simply, a tree.

1.8.4 Graph Data sometimes contains a relationship between pairs of elements which is not necessarily hierarchical in nature, e.g. an airline flights only between the cities connected by lines. This data structure is called Graph.

1.8.5 Queue A queue, also called FIFO system, is a linear list in which deletions can take place only at one end of the list, the Font of the list and insertion can take place only at the other end Rear.

1.8.6 Stack It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out).

1.9 DATA STRUCTURES OPERATIONS The data appearing in our data structures are processed by means of certain operations. In fact, the particular data structure that one chooses for a given situation depends largely in the frequency with which specific operations are performed. The following four operations play a major role in this text:

Traversing: accessing each record/node exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called “visiting” the record.) ∑ Searching: Finding the location of the desired node with a given key value, or finding the locations of all such nodes which satisfy one or more conditions. ∑ Inserting: Adding a new node/record to the structure. ∑ Deleting: Removing a node/record from the structure.

1.10 ARRAYS: DEFINITION

So the address of forth element is 503 because the first element in 500.

When the program indicate or dealing with element of array in any instruction like (write (X [I]), read (X [I] ) ), the compiler depend on going relation to bounding the requirement address.

1.10.2 Two-Dimensional Arrays

The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y you would write something as follows:

type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier. A two- dimensional array can be think as a table which will have x number of rows and y number of columns. A 2-dimensional array a , which contains three rows and four columns can be shown as below:

Thus, every element in array a is identified by an element name of the form a[ i ][ j ] , where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.

1.10.2.1 Representation of two dimensional arrays in memory A two dimensional ‘m x n’ Array A is the collection of m X n elements. Programming language stores the two dimensional array in one dimensional memory in either of two ways- ∑ Row Major Order: First row of the array occupies the first set of memory locations reserved for the array; Second row occupies the next set, and so forth.

To determine element address A[i,j]:

Location ( A[ i,j ] ) =Base Address + ( N x ( I - 1 ) ) + ( j - 1 )

For example:

Given an array [1…5,1…7] of integers. Calculate address of element T[4,6], where BA=900.

Sol) I = 4 , J = 6

M= 5 , N= 7

Location (T [4,6]) = BA + (7 x (4-1)) + (6-1)

= 900+ (7 x 3) +

= 900+ 21+

= 926

∑ Column Major Order : Order elements of first column stored linearly and then comes elements of next column.

To determine element address A[i,j]:

Location ( A[ i,j ] ) =Base Address + ( M x ( j - 1 ) ) + ( i - 1 )

For example:

Given an array [1…6,1…8] of integers. Calculate address element T[5,7], where BA=

Sol) I = 5 , J = 7

M= 6 , N= 8

Location (T [4,6]) = BA + (6 x (7-1)) + (5-1)

= 300+ (6 x 6) +