Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Data Structure

What is a Data Structure


• A data structure is a particular way of organizing data in a computer so
that it can be used effectively.
• It is a Mathematical or Logical model of a particular organization of
data items
Data structure study covers the following points
1) Amount of Memory required to store
2) Amount of Time required to Process
3) Representation of Data in Memory
4) Operations performed on that Data
Classification of Data Structure
Algorithms
• An algorithm is an exact specification of how to solve a computational
problem

• An algorithm must specify every step completely, so a computer can


implement it without any further “understanding”
Algorithms
• An algorithm must work for all possible inputs of the problem.

• Algorithms must be:


• Correct: For each input produce an appropriate output
• Efficient: run as quickly as possible, and use as little memory as possible –
more about this later
• There can be many different algorithms for each computational
problem.
Time and space

 To analyze an algorithm means:


 developing a formula for predicting how fast an
algorithm is, based on the size of the input (time
complexity), and/or
 developing a formula for predicting how much memory
an algorithm requires, based on the size of the input
(space complexity)
 Usually time is our biggest concern
 Most algorithms require a fixed amount of space

8
What does “size of the input” mean?
 If we are searching an array, the “size” of the input could be the size of the
array
 If we are merging two arrays, the “size” could be the sum of the two array
sizes
 If we are computing the nth Fibonacci number, or the nth factorial, the
“size” is n
 We choose the “size” to be the parameter that most influences the actual
time/space required
 It is usually obvious what this parameter is
 Sometimes we need two or more parameters

9
Characteristic operations
 In computing time complexity, one good approach
is to count characteristic operations
 What a “characteristic operation” is depends on the
particular problem
 If searching, it might be comparing two values
 If sorting an array, it might be:
 comparing two values
 swapping the contents of two array locations
 both of the above
 Sometimes we just look at how many times the
innermost loop is executed

10
Average, best, and worst cases of algorithm
 Usually we would like to find the average time to perform an algorithm
 However,
 Sometimes the “average” isn’t well defined
 Example: Sorting an “average” array
 Time typically depends on how out of order the array is

How out of order is the “average” unsorted array?


 Sometimes finding the average is too difficult


 Often we have to be satisfied with finding the worst (longest) time required
 Sometimes this is even what we want (say, for time-critical operations)
 The best (fastest) case is seldom of interest

11
Constant time is (usually)
better than linear time
 Suppose we have two algorithms to solve a task:
 Algorithm A takes 5000 time units
 Algorithm B takes 100*n time units
 Which is better?
 Clearly, algorithm B is better if our problem size is small,
that is, if n < 50
 Algorithm A is better for larger problems, with n > 50
 So B is better on small problems that are quick anyway
 But A is better for large problems, where it matters more
 We usually care most about very large problems
 But not always!

12
Common time complexities
BETTER  O(1) constant time
 O(log n) log time
 O(n) linear time
 O(n log n) log linear time
 O(n2) quadratic time
 O(n3) cubic time
 O(2n) exponential time
WORSE

13
Example

14
Best, worst and average case analysis
• Best case is the function which performs the minimum number of
steps on input data of n elements.

• Worst case is the function which performs the maximum number of


steps on input data of size n.

• Average case is the function which performs an average number of


steps on input data of n elements.
ADT - General Concept

 The user of an ADT needs only to know that


a set of operations are available for the data
type, but does not need to know how they
are applied
 Several simple ADTs, such as integer, real,
character, pointer and so on, have been
implemented and are available for use in
most languages
ADT in Simple Words

 Definition:
• Is a set of operation
• Mathematical abstraction
• No implementation detail
 Example:
• Lists, sets, graphs, stacks are examples of
ADT along with their operations

17
Why ADT?
 Modularity
• divide program into small functions
• easy to debug and maintain
• easy to modify
• group work
 Reuse
• do some operations only once
 Easy to change the implementation
• transparent to the program

18
Implementing an ADT
 To implement an ADT, you need to choose:
• A data representation
 must be able to represent all necessary values of the
ADT
 should be private
• An algorithm for each of the necessary operation:
 must be consistent with the chosen representation
 all auxiliary (helper) operations that are not in the
contract should be private
 Remember: Once other people are using it
• It’s easy to add functionality

19
The List ADT
 The List is an
• Ordered sequence of data items called
elements
• A1, A2, A3, …,AN is a list of size N
• size of an empty list is 0
• Ai+1 succeeds Ai
• Ai-1 preceeds Ai
• Position of Ai is i
• First element is A1 called “head”
• Last element is AN called “tail”

20
List – An Example

 The elements of a list are 34, 12, 52, 16, 12


• Find (52) -> 3
• Insert (20, 4) -> 34, 12, 52, 20, 16, 12
• Delete (52) -> 34, 12, 20, 16, 12
• FindKth (3) -> 20

21
List - Implementation

 Lists can be implemented using:


• Arrays
• Linked List

22
ADT in C
 The C language only has support for structs with data
members (i.e. member variables). While this is sufficient to
represent the data of an ADT, the functions that operate on
the ADT must be defined separately from the struct. The
following is the data definition of an ADT to represent
triangles:

Tuesday, February 8, 2022 23


ADT in C
 // A triangle ADT.
 struct Triangle {
 double a;
 double b;
 double c;
 };

 int main() {
 Triangle t1 = { 3, 4, 5 };
 Triangle t2 = { 2, 2, 2 };
 }

Tuesday, February 8, 2022 24


ADT in C
 The Triangle struct contains three member variables, one for each side of the triangle, each
represented by a double. The example in main() creates and initializes two Triangle structs,
resulting in the memory layout in Figure below

Tuesday, February 8, 2022 25

You might also like