0% found this document useful (0 votes)
12 views

Data Structure Unit 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Structure Unit 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

DATA STRUCTURES –

CS2001
UNIT 1 - INTRODUCTION
CONTENT:

• Definition of Data Structure


• Definition of Algorithm
• Abstract Data Type
• Algorithm - Time and Space Complexity
• Asymptotic notations
• Structures and Arrays – C programming
Why Structure the data?
WHAT IS DATA STRUCTURE?

• Storing and organizing data in computer memory in


a manner that it can be used efficiently when
required.

• Examples: files, arrays, stacks, queues, list, etc.

• Broad categories:
• Linear DS: elements are accessed in sequential manner not
necessarily stored in sequential manner too. E.g. Arrays, lists.
• Non Linear DS: stored/accessed in non linear manner. E.g.
Graph and trees.
DATA STRUCTURE TYPES

Any data structure provides two facilities:


• Hold the value or data
• Lets us do stuff/operations on these values or data

• Another Categorization:
• Primitive data types:
(integer, float, character)
• Derived data types: uses primitive types
(Array, pointers, structure, union)
ABSTRACT DATA TYPE (ADT)

• System Vs User defined Data type


• Implementation is provided by system (type +
functions)
• Implementation is provided by user (type +
functions)

• User defined data types are usually defined, along


with there functionality, by users. Such data types
are called ADT’s because they hide the details of
how their functions work. E.g. Link list, queues, trees,
graphs, hash tables, etc.
ADT EXAMPLE

• Stack is a user defined data type


in which we can add and
remove data using functions:
• Push() // add element
• Pop() //remove element

• Queue data structure generally


uses functions enqueue() and
dequeue() to add and remove
items.

Users do not worry about how these


functions work as these functions
are coded by programmers.
WHAT IS AN ALGORITHM?

• A series of finite number of steps to solve a problem.

• Generally, a pseudo-code in computer programming,


independent of programming language.

• Characteristics of an Algorithm:
• May take zero or more number of inputs
• The sequence of steps is well defined and each step produces
a definite effect
• Contains finite number of steps
• Must have a termination point
• May produce one or more outputs
EXAMPLE

• Write an algorithm to calculate sum of two number

• Write an algorithm to find area of a triangle

• Write an algorithm to find whether a triangle is right


angle triangle or not.

• Write an algorithm to print “Hello World” on display


screen.

• Write an algorithm to prepare tea


WHAT AN ‘ALGO’ LOOKS LIKE?

• 1) Get the frying pan.

• 2) Get the oil.


• a. Do we have oil?
• i. If yes, put it in the pan.
• ii. If no, do we want to buy oil?
• 1. If yes, then go out and buy.
• 2. If no, we can terminate.

• 3) Turn on the stove, etc...


SOME OTHER ALGORITHMS TO TRY

• Find the number of paths to reach chandigarh from


Delhi. The path may include national highway only.

• Find the number of paths to reach chandigarh from


Delhi. The path may include national highway and state
highway only.

• Find the number of paths to reach chandigarh from


Delhi. The path may include national highway, state
highway and city roads only.

• In how many ways you can distribute the workload of


your section among all the faculties, for this semester.
PERFORMANCE OF AN ALGORITHM

Time and space complexity

• There may be multiple ways of solving a problem,


but, which one is the best way?
OR
There can be other reasons due to which we may
want to analyze performance of an algorithm.
CONTINUED…

• Running Time Analysis


It is the growth of time taken by algorithm as a function of
input size. (Processing time based on size of problem)

• Ways to compare performance


• Execution times
Not best way because of differences that may exist in
underlying hardware.
• Number of statements
May differ with different programing languages and styles
• Apriori analysis
Best method as it only considers the size of input set
PERFORMANCE OF ARRAY PROG
(QUADRATIC FUNCTION)
INPUT VS EXPONENTIAL FUNCTION
TIME COMPLEXITIES OF FUNCTIONS
TYPE OF INPUTS AND ANALYSIS

• Worst case
The input set for which an algorithm takes maximum
time and effort to execute

• Best case
The input sets for which an algorithm takes minimum
time and effort to execute

• Average case
The input sets for which an algorithm takes average
time and effort to execute
INPUT VS ALGO WORKLOAD

void insertionSort(int
array_tobe_sorted[], int array_size)
{
int i, value, j;

for (i = 1; i < n; i++) {


value = array_tobe_sorted[i];
j = i - 1;

/* Swap the current element with


already sorted */
while (j >= 0 && array_tobe_sorted[j] >
value) {
array_tobe_sorted[j + 1] = arr[j];
j = j - 1;
}

array_tobe_sorted[j + 1] = value;
}
}
ASYMPTOTIC NOTATIONS

• Performance analysis for large set of input (towards


infinity)

• Big – „O‟
• Theta ()
• Omega ()
• Little – „o‟
• Little omega – ()
BIG – ‘O’

• Pronounced as big-oh („O‟)

• If we want to compare two functions say f(n) and


g(n) where „n‟ is the size of input set

then, f(n)=O(g(n)),
read as, f(n) is equal to big-O g(n)

meaning, g(n) is the upper bound for function f(n) in terms of


time complexity, i.e., f(n) always takes lesser time than g(n) for
n size of input.
CONTINUED…

• Suppose f(n)= n2 and g(n)= 2n ,


represents the time complexities of two algorithms. Then, we can state
intuitively, that for large number of n, g(n) will always take more time
than f(n).

n= 1 2 3 4 5 6 7 8 9 10
n2 1 4 9 16 25 36 49 64 81 100
2n 2 4 8 16 32 64 128 256 512 1024

• Hence, f(n)=O(g(n))
i.e. g(n) is upper bound of function f(n) or f(n)≤ g(n),
asymptotically.
CONTINUED…

Similarly,

•  notation: asymptotic “greater than”:

f(n)=  (g(n)) implies: f(n) ≥ g(n)

meaning, g(n) is lower bound to f(n)

•  notation: asymptotic “equality”:

f(n)=  (g(n)) implies: f(n) = g(n) , asymptotically

meaning, g(n) and f(n) has same order of growth for n input size
CONTINUED…

• n0 is the value of
„n‟ after which the
behavior of the
complexity of the
two functions does
not change for
each other, if other
factors remains
constant. n0
DEFINITION – BIG ‘O’
DEFINITION – OMEGA ‘’
DEFINITION – THETA ‘ ’
ARRAYS

• Continuous storage of homogenous elements

Arrays

Two- Multi-
Linear
Dimensional Dimensional
CONTINUED…

• 1-D Arrays
• Declaring the arrays
• Storing values in an array
• Printing the values of an array

• Calculation address of an array location


• Address of data element,
A[i] = Base_add (A) + size_of_type * (index_number– lowest_index)

• Length of array: (upper_index – lower_index) + 1


ARRAY OPERATIONS

• Array Traversal
• Insertion of element
• Searching of element
• Deleting an element
• Sorting of an array
ARRAY AND POINTERS

• int* ptr = &arr[0]


Ptr pointer stores the address of element 0 of array

• int* ptr = &arr[2]


Ptr pointer stores the address of element 2 of array

• ptr++ can be used to traverese the arrar


int *ptr = &arr[2];
ptr++;
printf("\n The value of the second element of the array is %d", *ptr);

This will print the value of element stored at index 3 of the given array

• We can also have array of pointers: int* ptr[5];


PASSING AN ARRAY AS FUNCTION

• Passing one element

• Passing complete array


2-D ARRAYS

• Initializing 2-D arrays


int marks[3][5];
Int arr[2][4];
type array_name[row][col];

• Storing values
int arr[0][0]=5;
int arr[0][1]=7;
Int arr[2][4]={{5,7,9,0},{2,4,6,8}}
CONTINUED…

The data of 2-d array is still stored in one continuous


block of memory in linear order.

Two possibilities are:


• Row major order

• Column major order


ADDRESS CALCULATION

• For row major order


Address(A[I][J]) = Base_Address + w{M ( I – S) + (J – S)}

• For column major order


Address(A[I][J]) = Base_Address + w{N ( J – S) + (I – S)}

W- size of type of array


M- number of columns
N- number of rows
S- Starting index
EXAMPLE

• Consider a, 20 by 5, two-dimensional array „marks‟


which has its base address = 1000 and the size of an
element = 2. Now compute the address of the
element, marks[18][ 4] assuming that the elements
are stored in row major order.

Address(A[I][J]) = Base_Address + w{M (I – S) + (J – S)}


Address(marks[18][4]) = 1000 + 2 {5(18 – 0) + (4 – 0)}
= 1000 + 2 {5(18) + 4}
= 1000 + 2 (90+4)
= 1000 + 188 = 1188
OPERATIONS

• Searching and accessing the elements in 2-D array


• Sum c[i][j]=a[i][j]+b[i][j]
• Difference
• Transpose and product

• Passing the 2-D array


STRUCTURES

• Collection of homogenous objects with different type of


attributes
• Defining a structure:
struct struct–name
{
data_type var–name;
data_type var–name;
};

struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
DOT OPERATOR

• Initializing the structure elements


struct student stu1, stu2, stu3;

• Assigning values to data members


Stu1.r_no=1;
Stu1.name=“John”;
Stu1.course=“CSE”;
Stu1.fee=25000.75;

• Accessing the members of a structure


printf(“%d”, stu1.r_no );
int some_x=stu1.r_no;
POINTER DECLARATION

• To create the nodes/elements of a structure at


runtime
struct student *stu;
stu=malloc(sizeof(struct student));

• arrow operator (->)


• assigning values using arrow operator
stu->r_no=1;
Stu->name=“Alexa”;
• accessing the values
printf(“%d”,stu->r_no);
OTHER FUNCTIONS

• Passing structure members as argument functions

• Passing the structure node as function argument

• Nested structures
ASSIGNMENT

• Define data structure. Discuss various categories and


types of data structures.
• What is an algorithm? Discuss characteristic properties of
an algorithm.
• How can we measure the performance of an
algorithms? Discuss various asymptotic notations with
example.
• What is an 1-D and 2-D array? What are the various
functions related to arrays data structure?
• Discuss the difference between the use of dot and arrow
operator in structures.
• What is an ADT? What is the purpose of having an ADT in
data structures?
END

You might also like