Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Data Structures Introduction1

This document provides an overview of data structures, describing both linear data structures like arrays, stacks, and queues as well as non-linear structures like trees and graphs. It discusses the key concepts of data abstraction, primitive and non-primitive data types, and how data structures are used to organize data in computer memory to allow for efficient retrieval and use. The document also outlines some common data structures and provides examples to illustrate their usage and implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Data Structures Introduction1

This document provides an overview of data structures, describing both linear data structures like arrays, stacks, and queues as well as non-linear structures like trees and graphs. It discusses the key concepts of data abstraction, primitive and non-primitive data types, and how data structures are used to organize data in computer memory to allow for efficient retrieval and use. The document also outlines some common data structures and provides examples to illustrate their usage and implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Structures

Prerequisite: C programming and Basic of Mathematics


L-T-P: 2-0-2, Credits: 3
Type: Engineering Essential/ Core Essential

Dr Deepak Gupta
Assistant Professor, SMIEEE
CSED, MNNIT Allahabad, Prayagraj
Email: deepakg@mnnit.ac.in
Course Description

This course introduces the student’s fundamentals of data structures and takes
them forward to software design along with the course on Algorithms. It details
how the choice of data structures impacts the performance of programs for given
software application. This is a precursor to DBMS and Operating Systems. A lab
course is associated with it to strengthen the concepts.
Syllabus
UNIT 1: UNIT-1: Introduction: Basic Terminology, Elementary Data Organization,
Algorithm, Efficiency of an Algorithm, Time and Space Complexity, Asymptotic
notations: Theta, Big-O, and Omega, Time-Space trade-off. Abstract Data Types (ADT)
UNIT II: 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
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: Abstract Data Type, Operations on Queue: Create, Add, Delete, Full and
Empty, Circular queues, Array and linked implementation of queues in C, Deque and
Priority Queue.
Syllabus

UNIT III: Basic terminology, k-ary trees, 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: In order, Preorder and Post order, Binary Search Trees, Threaded Binary trees,
Traversing Threaded Binary trees, Forest, Huffman algorithm, Heap, B/B+ Tree, AVL tree
UNIT IV: Sequential search, Binary Search, Comparison and Analysis Internal Sorting:
Bubble Sort, Selection Sort, Insertion Sort, Two Way Merge Sort, Heap Sort, Quick Sort,
Hashing
Unit V: Terminology, Sequential and linked Representations of Graphs: Adjacency Matrices,
Adjacency List, Adjacency Multi list, Graph Traversal: Depth First Search and Breadth First
Search, Connected Component, Spanning Trees, Minimum Cost Spanning Trees: Prims and
Kruskal algorithm. Shortest Path algorithm: Dijikstra Algorithm
Books

Text Books:

1. Aaron M. Tenenbaum, YedidyahLangsam and Moshe J. Augenstein “Data


Structures Using C and C++”, PHI

Reference Books:
1. Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia
Publication
2. Donald Knuth, “The Art of Computer Programming”, vol. 1 and vol. 3.
3. Jean Paul Trembley and Paul G. Sorenson, “An Introduction to Data
Structures with applications”, McGraw Hill
4. R. Kruse et al, “Data Structures and Program Design in C”, Pearson
Education
5. Lipschutz, “Data Structures” Schaum’s Outline Series, TM
Time Table
Introduction
Data: Data is the plural of “datum” (which is rarely used) and may be thought
of as representing numbers, words, facts, figures, images, etc.

Raw Data: A collection of such data which needs further processing by


computers or human being is referred to as Raw Data.
For example, a collection of data related to the marks of students. It may
processed to get the average marks of the class.

Information: If data is arranged in some systematic way then it gets a


structure and becomes meaningful. This meaningful or processed data is
called information.
Knowledge: Knowledge is useful information that supports decision-making.
Data
Abstraction
Information
Abstraction
Knowledge
Fig: Process of Abstraction
Abstraction: The process of providing only the essentials and hiding the
details is known as Abstraction.

Data Type: A data type is a collection of objects and a set of operations that
act on those objects.
For example in C, int data type can take values in a range and operations that
can be performed are addition, subtraction, multiplication, division, bitwise
operations etc.
Data type or Primitive Data types are the predefined data types that are
supported in the programming language. The size depends upon the type of
data type. Primitive Data types can hold only a single value in one specific
location.

Examples of data types are integer, character, float, Boolean, etc.


Data Structures: Data Structure is a particular way of storing and organizing
data in the memory of the computer so that these data can easily be retrieved
and efficiently utilized in the future when required. It is the physical
implementation of Abstract data types (ADTs).

Figure 2: Classifications of Data Structures


Primitive Data Structure/ Types
Primitive data structures are built into a programming language and are
generally considered the most basic data types. They are called primitive
because they are not composed of any other data types.
Examples of primitive data structures include integers, float, characters,
and boolean values.
Non-Primitive Data Structures
These are more complex data types that are composed of primitive data
types or other non-primitive data types. They are also referred to as
composite data types or reference data types.
Examples of non-primitive data structures include arrays, stacks,
queues, and trees.

Based on the structure and arrangement of data, we can divide non-


primitive data structures into two sub-categories:
1. Linear Data Structures

2. Non-Linear Data Structures


Linear Data Structures
A data structure that preserves a linear connection among its data elements is
known as a Linear Data Structure. The arrangement of the data is done
linearly, where each element consists of the successors and predecessors
except the first and the last data elements.
However, it is not necessarily true in the case of memory, as the
arrangement may not be sequential.

Based on memory allocation, the Linear Data Structures are further


classified into two types:

Linear Data Structures

Static Data Structures Dynamic Data Structures


Static Data Structures:
The data structures having a fixed size are known as Static Data Structures.
The memory for these data structures is allocated at the compiler time, and
their size cannot be changed by the user after being compiled; however, the
data stored in them can be altered.
Example: The Array has a fixed size, and its data can be modified later.

An Array is a data structure used to collect multiple data elements of the


same data type into one variable.
• Arrays are declared using the following syntax:
type name[size];

int marks[10];

marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Dynamic Data Structures:
The data structures having a dynamic size are known as Dynamic Data
Structures. The memory of these data structures is allocated at the run time,
and their size varies during the run time of the code. Moreover, the user
can change the size as well as the data elements stored in these data
structures at the run time of the code.

Example: Linked Lists, Stacks, and Queues are common examples of


dynamic data structures.

Linked Lists
A Linked List is another example of a linear data structure used to store a
collection of data elements dynamically. Data elements in a linked list are
represented by the nodes, connected using links or pointers.
Stack
A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out)
principle that allows operations like insertion and deletion from one end of
the Stack, i.e., Top.
Real-life examples of Stacks are piles of books,
a deck of cards, piles of money, and many
more.

Queue
It follows the First-In-First-Out (FIFO) principle. Elements are inserted at the
back of the queue and removed from the front.
Non-Linear Data Structures
Non-Linear Data Structures are data structures where the data elements are
not arranged in sequential order. Here, the insertion and removal of data are
not feasible in a linear manner. There exists a hierarchical relationship
between the individual data items.

Example: Tree and Graph are common examples of Non-linear data


structures.
Tree:
A Tree is a non-linear data structure and a hierarchy containing a collection
of nodes such that each node of the tree stores a value and a list of references
to other nodes (the "children").
Graph:
A Graph is another example of a Non-Linear Data Structure comprising a
finite number of nodes or vertices and the edges connecting them.
The Graph data structure, G is considered a mathematical structure
comprised of a set of vertices, V, and a set of edges, E as shown below:

G = (V, E)
Algorithm: An algorithm is a finite set of instructions that, if followed,
accomplishes a particular task.
In addition, all algorithms must satisfy the following criteria:
1. Input: There are zero or more quantities that are externally supplied.

2. Output: At least one quantity is produced.


3. Definiteness: Each instruction is clear and unambiguous.

4. Finiteness: The algorithm terminates after a finite number of steps.


5. Effectiveness: Every instruction must be basic enough to be carried out.

Program: A program is a language specific implementation of the algorithm.


It does not satisfy the fourth condition.
Analysis of Algorithms: Analysis of algorithms is required to dictate the
correctness and measure the quantitative efficiency of an algorithm. There may
be many algorithms for solving any problem and we would like to use the most
efficient one.
Analysis of algorithms is required to compare these algorithms and recognize
the best one based on the following criteria:
 Time Complexity
 Space Complexity

 Space Complexity: The space complexity of an algorithm is the amount of


computer memory required during the program execution, as a function of
the input size.

 Time Complexity: The time complexity of an algorithm is basically the


running time of the program as a function of the input size.
The running time of the algorithm is the sum of the running times of each
statement.
The space needed by a program depends on:

 The fixed part includes space needed for storing instructions, constants,
variables, and structured variables.
float abc(float a, float b, float c)
{ Sabc(I) = 0.
return a+b+b*c+(a+b-c)/(a+b)+4
}

 Variable part includes space needed for the recursion stack, and for
structured variables that are allocated space dynamically during the run-
time of the program.
Type Name Number of bytes
Ex: Recursive function for summing a list
Parameter: array pointer list[] 4
of numbers
float rsum(float list[], int n) Parameter: integer n 4
{
return address 4
if (n) return rsum(list, n-1)+list(n-1);
return 0; Total per recursive call 12
}
Srsum(Max_size) = 12*Max_size.
Ex: Iterative function for summing a list of numbers
float sum(float list[], int n)
{
float tempsum = 0;
int i; Sabc(I) = 0.
for(i=0; i<n; i++)
tempsum += list[i];
return tempsum
} Time Complexity

Statement Steps/execution (s/e) Frequencies Total Steps


float sum(float list[], int n) 0 0 0
{ 0 0 0
float tempsum = 0; 1 1 1
int i; 0 0 0
for(i=0; i<n; i++) 1 n+1 n+1
tempsum += list[i]; 1 n n
return tempsum 1 1 1
} 0 0 0
Total 2n+3
Ex: Recursive function for summing a list of numbers
float rsum(float list[], int n)
{
if (n)
return rsum(list, n-1)+list(n-1);
return 0;
}
Statement Steps/execution Frequencies Total Steps
(s/e)
float rsum(float list[], int n) 0 0 0
{ 0 0 0
if (n) 1 n+1 n+1
return rsum(list, n-1) + list(n-1); 1 n n
return list[0]; 1 1 1
} 0 0 0
Total 2n+2
Time Complexity
Ex: Matrix Addition
void add (int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE], int rows, int cols)
{
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
Statement Steps/execution Frequencies Total Steps
(s/e)
void add (int a[ ][MAX_SIZE]… 0 0 0
{ 0 0 0
int i, j; 0 0 0
for (i=0; i<rows; i++) 1 rows+1 rows+1
for (j=0; j<cols; j++) 1 rows . (cols+1) rows . cols
+ rows
c[i][j] = a[i][j] + b[i][j]; 1 rows . cols rows . cols
} 0 0 0
Total 2rows . Cols + 2rows +1
Categories of Algorithms

• Constant time algorithms have running time complexity given as O(1)


• Linear time algorithms have running time complexity given as O(n)
• Logarithmic time algorithms have running time complexity given as
O(log n)
• Polynomial time algorithms have running time complexity given as
O(nk) where k>1
• Exponential time algorithms have running time complexity given as
O(2n)

n O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n)

1 1 1 1 1 1 1 2

2 1 1 2 2 4 8 4

4 1 2 4 8 16 64 8

8 1 3 8 24 64 512 256

16 1 4 16 64 256 4,096 65536

You might also like