0% found this document useful (0 votes)
4 views27 pages

DS E-Content - Module 1 Introduction

The document provides an overview of data structures using Java, detailing their classification, applications, and the importance of algorithms in data processing. It discusses efficiency, time-space trade-offs, asymptotic notations, and complexity analysis, emphasizing the significance of selecting appropriate data structures for effective data management. The content is structured into sections covering basic terminology, types of data structures, algorithm properties, and complexity analysis with examples such as insertion sort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

DS E-Content - Module 1 Introduction

The document provides an overview of data structures using Java, detailing their classification, applications, and the importance of algorithms in data processing. It discusses efficiency, time-space trade-offs, asymptotic notations, and complexity analysis, emphasizing the significance of selecting appropriate data structures for effective data management. The content is structured into sections covering basic terminology, types of data structures, algorithm properties, and complexity analysis with examples such as insertion sort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

School of Computer Science and Engineering

Data Structures using JAVA [R1UC303B]

Dr. A K Yadav

School of Computer Science and Engineering


Plat No 2, Sector 17A, Yamuna Expressway
Greater Noida, Uttar Pradesh - 203201

November 13, 2024

Dr. A K Yadav Data Structures using JAVA 1/27


School of Computer Science and Engineering

Contents

Introduction 3
Classification/Types of Data Structures 7
Applications of Data Structures 9
Algorithm 10
Efficiency of an algorithm 12
Time-space trade-off and complexity 13
Asymptotic notations 17
Complexity Analysis 21

Dr. A K Yadav Data Structures using JAVA 2/27


School of Computer Science and Engineering

Basic Terminology
What is Data Structure?
- A data structure is a particular way of organising data in a
computer so that it can be used effectively. The idea is to reduce
the space and time complexities of different tasks.
- The choice of a good data structure makes it possible to perform
a variety of critical operations effectively.
- An efficient data structure also uses minimum memory space and
execution time to process the structure.
- A data structure is not only used for organising the data. It is
also used for processing, retrieving, and storing data.
I Data: Data are simply values or sets of values.
I Data items: Data items refers to a single unit of values.

Dr. A K Yadav Data Structures using JAVA 3/27


School of Computer Science and Engineering

I Group items: Data items that are divided into sub-items are
called Group items. Ex: An Employee Name may be divided
into three subitems- first name, middle name, and last name.
I Elementary items: Data items that are not able to divide
into sub-items are called Elementary items. Ex: EnRollNo.
I Entity: An entity is something that has certain attributes or
properties which may be assigned values. The values may be
either numeric or non-numeric.
I Entities with similar attributes form an entity set.
I 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.
I The term information is sometimes used for data with given
attributes, in other words meaningful or processed data.

Dr. A K Yadav Data Structures using JAVA 4/27


School of Computer Science and Engineering

I Field is a single elementary unit of information representing


an attribute of an entity.
I Record is the collection of field values of a given entity.
I File is the collection of records of the entities in a given entity
set.
Need of Data Structure:
The structure of the data and the synthesis of the algorithm are
relative to each other. Data presentation must be easy to
understand to the developer, as well as the user, can make an
efficient implementation of the operation.
Data structures provide an easy way of organising, retrieving,
managing, and storing data. Here is a list of the needs for data
structure.
I Data structure modification is easy.
I It requires less time.
Dr. A K Yadav Data Structures using JAVA 5/27
School of Computer Science and Engineering

I Save storage memory space.


I Data representation is easy.
I Easy access to the large database

Dr. A K Yadav Data Structures using JAVA 6/27


School of Computer Science and Engineering

Classification/Types of Data Structures


1. Linear Data Structure
2. Non-Linear Data Structure
Linear Data Structure:
Linear data structures: Elements are accessed in a sequential order
but it is not compulsory to store all elements sequentially (say,
Linked Lists). Examples: Linked Lists, Stacks and Queues.
Non-Linear Data Structure:
Elements of this data structure are stored/accessed in a non-linear
order. Examples: Trees and graphs.

Dr. A K Yadav Data Structures using JAVA 7/27


School of Computer Science and Engineering

Dr. A K Yadav Data Structures using JAVA 8/27


School of Computer Science and Engineering

Applications of Data Structures


Data structures are used in various fields such as:
I Operating system
I Graphics
I Computer Design
I Blockchain
I Genetics
I Image Processing
I Simulation
I ...

Dr. A K Yadav Data Structures using JAVA 9/27


School of Computer Science and Engineering

Algorithm
I What is an algorithm?
- An algorithm is a set of rules for carrying out calculation
either by hand or on a machine.
- An algorithm is a sequence of computational steps that
transform the input into output.
- An algorithm is a sequence of operations performed on data
that have to be organized in data structures.
- A finite set of instructions that specify a sequence of
operations to be carried out in order to solve a specific
problem or class of problems.
- An algorithm is an abstraction of a program to be executed
on a physical machine.

Dr. A K Yadav Data Structures using JAVA 10/27


School of Computer Science and Engineering

- An algorithm is any well-defined computational procedure


that takes some value, or set of values, as input and produces
some value, or set of values, as output.
I Why do we study algorithms?
- To make solution more faster.
- To compare performance as a function of input size.
Two main property of algorithm is:
1. Correctness: Does the algorithm give solution to the problem
in a finite number of steps?
2. Efficiency: How much resources in terms of memory and
time, does it take to execute the algorithm.

Dr. A K Yadav Data Structures using JAVA 11/27


School of Computer Science and Engineering

Efficiency of an algorithm
I To go from city “A” to city “B”, there can be many ways of
accomplishing this: by flight, by bus, by train and also by
bicycle.
I Depending on the availability, convenience, and affordability
etc., we choose the one that suits us.
I Similarly, in computer science, multiple algorithms are
available for solving the same problem (for example, a sorting
problem has many algorithms, like insertion sort, selection
sort, quick sort and many more).
I Algorithm analysis helps us to determine which algorithm is
most efficient in terms of time and space consumed.
I The goal of the analysis of algorithms is to compare algorithms
(or solutions) mainly in terms of running time but also in
terms of other factors e.g., memory, developer effort, etc.
Dr. A K Yadav Data Structures using JAVA 12/27
School of Computer Science and Engineering

Time-space trade-off and complexity


A tradeoff is a situation where one thing increases and another
thing decreases. It is a way to solve a problem in:
I Either in less time and by using more space
I In very little space by spending a long amount of time.
The best Algorithm is that which helps to solve a problem that
requires less space in memory and also takes less time to generate
the output.
- But in general, it is not always possible to achieve both of these
conditions at the same time.
- The most common condition is an algorithm using a lookup table.
- This means that the answers to some questions for every possible
value can be written down.

Dr. A K Yadav Data Structures using JAVA 13/27


School of Computer Science and Engineering

- One way of solving this problem is to write down the entire


lookup table, which will let you find answers very quickly but will
use a lot of space.
- Another way is to calculate the answers without writing down
anything, which uses very little space, but might take a long time.
- Therefore, the more time-efficient algorithms you have, that
would be less space-efficient.
Types of Space-Time Trade-off:

Dr. A K Yadav Data Structures using JAVA 14/27


School of Computer Science and Engineering

I Compressed or Uncompressed data: A space-time trade-off


can be applied to the problem of data storage. If data stored
is uncompressed, it takes more space but less time. But if the
data is stored compressed, it takes less space but more time to
run the decompression algorithm. There are many instances
where it is possible to directly work with compressed data. In
that case of compressed bitmap indices, where it is faster to
work with compression than without compression.
I Re-Rendering or Stored images: In this case, storing only the
source and rendering it as an image would take more space
but less time i.e., storing an image in the cache is faster than
re-rendering but requires more space in memory.

Dr. A K Yadav Data Structures using JAVA 15/27


School of Computer Science and Engineering

I Smaller code or loop unrolling: Smaller code occupies less


space in memory but it requires high computation time that is
required for jumping back to the beginning of the loop at the
end of each iteration. Loop unrolling can optimize execution
speed at the cost of increased binary size. It occupies more
space in memory but requires less computation time.
I Lookup tables or Recalculation: In a lookup table, an
implementation can include the entire table which reduces
computing time but increases the amount of memory needed.
It can recalculate i.e., compute table entries as needed,
increasing computing time but reducing memory requirements.

Dr. A K Yadav Data Structures using JAVA 16/27


School of Computer Science and Engineering

Asymptotic notations
1. O - notation ”Big O” : Asymptotic upper bound,
O(g(n)) = {f (n) : ∃c, n0 > 0 such that 0 ≤ f (n) ≤
cg(n) for all n ≥ n0 }
f (n) ∈ O(g(n))
f (n) = O(g(n))

Dr. A K Yadav Data Structures using JAVA 17/27


School of Computer Science and Engineering

2. Ω - notation ”Big omega” : Asymptotic lower bound,


Ω(g(n)) = {f (n) : ∃c, n0 > 0 such that 0 ≤ cg(n) ≤
f (n) for all n ≥ n0 }
f (n) ∈ Ω(g(n))
f (n) = Ω(g(n))

Dr. A K Yadav Data Structures using JAVA 18/27


School of Computer Science and Engineering

3. Θ - notation : Asymptotic tight bound,


Θ(g(n)) = {f (n) : ∃c1 , c2 , n0 > 0 such that 0 ≤ c1 g(n) ≤
f (n) ≤ c2 g(n) for all n ≥ n0 }
f (n) ∈ Θ(g(n))
f (n) = Θ(g(n))

Dr. A K Yadav Data Structures using JAVA 19/27


School of Computer Science and Engineering

4. o - notation ”small o”: Asymptotic loose upper bound,


o(g(n)) = {f (n) : ∀c > 0, ∃n0 such that 0 ≤ f (n) <
cg(n) for all n ≥ n0 }
5. ω - notation ”small omega”: Asymptotic loose lower bound,
ω(g(n)) = {f (n) : ∀c > 0, ∃n0 such that 0 ≤ cg(n) <
f (n) for all n ≥ n0 }
Benefits of Asymptotic Notations:
- Simple representation of algorithm efficiency.
- Easy comparison of performance of algorithms.

Dr. A K Yadav Data Structures using JAVA 20/27


School of Computer Science and Engineering

Complexity analysis
Analyzing an algorithm means predicting the resources that the
algorithm requires. Resources may be memory, communication
bandwidth, computer hardware or CPU time. Our primary concern
is to measures the computational time required for the algorithm.
Running time:-The running time of an algorithm is the number of
primitive operations or steps executed on a particular input.
Why do we normally concentrate on finding only the worst-case
running time?
1. The worst-case running time of an algorithm gives us an
upper bound on the running time for any input. So it
guarantees that the algorithm will never slower than this. In
real applications, worst case normally occurs for example
searching a non existing data.

Dr. A K Yadav Data Structures using JAVA 21/27


School of Computer Science and Engineering

2. Best case is like an ideal case which guarantees that the


algorithm will never faster than stated. Based upon this we
can’t allocate the resources.
3. Average case normally perform as worst case because normally
we take average case as average of best and worst or best for
half size input and worst for other half size.

Dr. A K Yadav Data Structures using JAVA 22/27


School of Computer Science and Engineering

Complexity analysis: Insertion sort


Insertion-Sort(A,N) Cost Times
1. for j = 2 to N c1 n
2. key = A[j] c2 n-1
Insert A[j] in sorted A[1] to A[j − 1]
3. i = j − 1 c3 n-1
Pn
4. while i > 0 and A[i] > key c4 tj
Pj=2
n
5. A[i + 1] = A[i] c5 (t j −1)
Pj=2
n
6. i = i − 1 c6 j=2 (tj −1)
while-end
7. A[i + 1] = key c7 n-1
for-end

Dr. A K Yadav Data Structures using JAVA 23/27


School of Computer Science and Engineering
n
X n
X
T (n) = c1 n + c2 (n − 1) + c3 (n − 1) + c4 tj + c5 (tj − 1)
j=2 j=2
n
X
+c6 (tj − 1) + c7 (n − 1)
j=2

n
X n
X n
X
⇒ T (n) = an + b + c4 tj + c5 (tj − 1) + c6 (tj − 1)
j=2 j=2 j=2

Now consider different cases:

Dr. A K Yadav Data Structures using JAVA 24/27


School of Computer Science and Engineering

1. Best Case: The algorithm performs best if key ≤ A[i] for


every value of j in step 4.
Then it executes only once for each value of j and total of n-1
times.
Step 5 and 6 will not be execute at all.
This is the case when array is already sorted

T (n) = an + b = O(n)

Dr. A K Yadav Data Structures using JAVA 25/27


School of Computer Science and Engineering

2. Worst Case: The algorithm performs worst if key > A[i] for
each value of j and stops only when i < 1 in step 4.
Then it will execute always j times for each value of
j = 2, 3, . . . , n
so
n
X (n − 1)(2 + n)
j=
j=2
2

and step 5 and 6 will execute


n
X (n − 1)n
(j − 1) =
j=2
2

This is the case when array is already sorted in reverse order

T (n) = an2 + bn + c = O(n2 )


Dr. A K Yadav Data Structures using JAVA 26/27
School of Computer Science and Engineering

Thank you
Please send your feedback or any queries to
[email protected]

Dr. A K Yadav Data Structures using JAVA 27/27

You might also like