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

UNIT-1 Introduction to Data Structure

Uploaded by

vrajpatel.2060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UNIT-1 Introduction to Data Structure

Uploaded by

vrajpatel.2060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

Unit-1

Introduction to Data Structure

Prof. Nilesh Parmar


What is Data?
• Data is a collection of facts such as numbers, words,
measurements, observations or just descriptions of
things.
• There are two different types of data Numeric data and
Character data
• When a programmer collects such type of data for
processing, he would require to store them in
computer’s main memory.
• The process of storing data items in computer’s main
memory is called representation
• Data to be processed must be organized in a particular
fashion, these organization leads to structuring of data,
and hence the mission to study the Data Structures.
Data types of C language
• Primary data types: These are fundamental data types in
C namely integer(int), floating point(float),
character(char), Boolean (bool) and void.
• Derived data types (user defined data types): Derived
data types are nothing but primary datatypes which are
little twisted or grouped together
like array, structure, union and pointer.
• Data type determines the type of data a variable will hold.
• If a variable x is declared as int. it means x can hold only
integer values
• Every variable which is used in the program must be
declared as what data-type it is.
Integer type
• Integers are used to store whole numbers.
• Size and range of Integer type on 16-bit machine:
Floating point type
• Floating types are used to store real numbers.
• Size and range of Integer type on 16-bit
machine
Character type
• Character types are used to store characters
value.
• Size and range of Integer type on 16-bit
machine
void type
• void type means no value
• his is usually used to specify the type of
functions which returns nothing
Structures in C
• A structure is a user-defined data type available in C that
allows to combining data items of different kinds
• Structures are used to represent a record
• Defining a structure: To define a structure, you must
use the struct statement. The struct statement defines a
new data type, with more than one member
//Syntax // declaring structure
struct [structure name] struct struct_example
{ {
member definition; int integer;
member definition; ... float decimal;
member definition; char name[20];
}; };
Union
• A union is a special data type available in C that allows
storing different data types in the same memory
location
• You can define a union with many members, but only
one member can contain a value at any given time
• Unions provide an efficient way of using the same
memory location for multiple purposes
• Defining a Union: To define a union, you must use
the union statement in the same way as you did while
defining a structure
• The union statement defines a new data type with more
than one member for your program
CONT..
union [union name] // declaraing union

{ union union_example
{
member definition; int integer;
float decimal;
member definition; ... char name[20];
member definition; };

};
Structure v/s Union

struct struct_example
union union_example
{
{
int integer; 2 bytes int integer; 2 bytes
Total: Max (integer,
float decimal; 4 bytes 26 bytes
float decimal; 4 bytes decimal, name[20])
char name[20]; 20 bytes char name[20]; 20 bytes = 20 bytes
};
};

In above example, structure will occupy 26 bytes where as union will occupy 20 bytes.
DIFFERENCE BETWEEN STRUCTURE AND
UNION
Pointer
• Pointer stores the address of another variable. It refers to a
memory location.

Features of Pointer
• Pointers save memory space
• Execution time of using pointer is faster
• Pointers are used with data structure
• Pointers are used in file handling
Pointer Declaration
• The general form is
data_type *variableName;
• Where * is Indirection operator / Dereference operator
• Examples
int *iptr; //iptr is a pointer to an integer variable
float *fptr; //fptr is a pointer to a float variable
char *cptr; //cptr is a pointer to a character
const int *ptric; //ptric is a pointer to an integer constant
Assigning Values to pointer using ‘&’
operator:
• Declare a pointer
• Assign the address of a variable to a 1000
pointer 1001
10 a

• Example 1002

int *ptr, a=10; 1003

ptr = &a; 1005


1000 ptr
• a = 10 1006 *ptr
1007
• ptr = address of a (1000)
1008
• *ptr = 10 (value stored in 1000)
Accessing the address of a variable
#include <stdio.h>
int main ()
{
int num = 100; //normal variable
int *ptr; //pointer variable
ptr = &num; //pointer initialization
printf("value of num = %d\n", *ptr); //printing the value
return 0;
}
Output
value of num =
100
Pointer – Example-1
#include <stdio.h>
void main()
{ Output:
a = 123 is stored at address 1002
int a, *iptr;
b = 12.34 stored at address 1005
float b, *fptr; Character a stored at address 1004
char ch, *cptr;
a=123;
b=12.34f;
ch='a';
iptr=&a;
fptr=&b;
cptr=&ch;
printf("a = %d is stored at address %u\n", *iptr, iptr);
printf("b = %f is stored at address %u\n", *fptr, fptr);
printf("Character %c is stored at address %u\n", *cptr, cptr);
Pointer Example
Pointer – Example-2
#include <stdio.h>
#include <conio.h>
void main()
{
int a[8]={1,5,3,7,9,2,6,4},b=22;
int *p,*t,i;
clrscr();
t=&b;
p=a;
printf("%d %u %u %d %u",*t, t, &t, *p, p);
for( i=1;i<11;i++)
printf("%d",*(p+i));
getch();
}
Data Structures
• Structure means particular way of data organization.

• Data structure (definition): is a way to store and organize data in


computer memory so that it can be used efficiently.

• In other words, a data structure is a way of organizing all data items


that considers not only the elements stored but also their relationship
to each other.
Why to learn Data Structures?

• Data Search − Consider an inventory of 1 million(106) items of a


store. If the application is to search an item, it has to search an item in
1 million(106) items every time slowing down the search. As data
grows, search will become slower.
• Processor speed − Processor speed although being very high, falls
limited if the data grows to billion records.
• Multiple requests − As thousands of users can search data
simultaneously on a web server, even the fast server fails while
searching the data.
Some well known problems in data structure
• The following computer problems can be solved using Data Structures
• Fibonacci number series
• Knapsack problem
• Tower of Hanoi
• All pair shortest path by Floyd-Warshall
• Shortest path by Dijkstra
• Project scheduling
Application of Data Structures:
• Database Management System.
• Compiler Design.
• Network Analysis.
• Numerical Analysis.
• Artificial Intelligence.
• Simulation.
• Operating System.
• Graphics.
Classification of Data Structures
Primitive data structures
• Primitive data structures are basic structures and are directly
operated upon by machine instructions.
• It has different representations on different computers.
• Integer: allows all values without fraction part.
• Float: used for storing fractional numbers.
• Character: used for character values.
• Boolean: It contains value True or False.
Non-Primitive data structures
• These are derived from primitive data structures.
• The non-primitive data structures emphasize on structuring of a group
of homogeneous or heterogeneous data items.
• A Non-primitive data type is further divided into Linear and Non-
Linear data structure
Linear / Non-Linear data structure
• Linear data structures
• If one element is connected with its previous and next adjacent elements only, it
called as linear data structures.
• Only single level is involved in linear data structure.
• Examples of Linear Data Structure are Array, Linked list, Stack and Queue.
• Nonlinear data structures
• If one element is connected with multiple elements, it is called as non linear data
structures. It involves multiple levels.
• Examples of Non-linear Data Structure are Tree and Graph

Stack Queue Tree


Graph
Linear data structures
• Stack: Stack is ordered linear data structure which is modified form of
an array. a data structure in which insertion and deletion operations are
performed at one end only. Stack is also called as Last in First out
(LIFO) data structure.

• Queue: The data structure which permits the insertion at one end and
Deletion at another end, known as Queue. Queue is also called as First
in First out (FIFO) data structure.
Linear Vs Non Linear Data Structures
Linear Data Structure Non-Linear Data Structure

Every item is related to its Every item is attached with many


previous and next item. other items.

Data is arranged in linear Data is not arranged in sequence.


sequence.
Data items can be traversed in a Data cannot be traversed in a
single run. single run.
Eg. Array, Stacks, linked list, Eg. tree, graph.
queue.
Implementation is easy. Implementation is difficult.
Possible operations on Data Structure
• Create
Create operation results in reserving memory for program elements.
• Destroy
Destroy operation destroys memory space allocated for specified data structure
• Selection
Selection operation deals with accessing a particular data within a data structure
• Traversing
Traversal is a process of visiting each and every node of a list in systematic manner
• Updating
It updates or modifies the data in the data structure.
• Searching
To search for a particular value in the data structure for the given key value.
Cntd…
• Sorting
To arrange the values in the data structure in a particular order.
• Inserting
To add a new value to the data structure
• Deleting
To remove a value from the data structure
• Splitting
Splitting is a process of partitioning single list to multiple list.
• Merging
To join two same type of data structure values
Introduction to algorithm
• The word algorithm has been derived from the Persian author's name, Abu Ja 'far
Mohammed ibn Musa al Khowarizmi (c. 825 A.D.), who has written a textbook
on Mathematics.

• The word is taken based on providing a special significance in computer science.

• The algorithm is understood as a method that can be utilized by the computer as


when required to provide solutions to a particular problem.
Algorithm
• 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.

• Definition: An algorithm is a sequence of computational steps to perform certain


task.

• Example: recipe to make tea, algorithm to find maximum common divisor of two
integers, algorithm to find factorial of a number.

• An algorithm is correct, if, for each input instance, it gets the correct output and
gets terminated.
Contd…
• An algorithm is the best way to represent the solution of a particular problem in a
very simple and efficient way.

• If we have an algorithm for a specific problem, then we can implement it in any


programming language, i.e. the algorithm is independent from any programming
language.

• An algorithm can be described by incorporating a natural language such as English,


Computer language, or a hardware language.
Properties of an algorithm
• Input: It should externally supply zero or more quantities.
• Output: It results in at least one quantity.
• Definiteness: Each instruction should be clear and ambiguous.
• Finiteness: An algorithm should terminate after executing a finite number of steps.
• Effectiveness: Every instruction should be fundamental to be carried out, in
principle, by a person using only pen and paper.
Contd…
• Feasible: It must be feasible enough to produce each instruction.
• Flexibility: It must be flexible enough to carry out desired changes with no efforts.
• Efficient: The term efficiency is measured in terms of time and space required by
an algorithm to implement. Thus, an algorithm must ensure that it takes little time
and less memory space meeting the acceptable limit of development time.
• Independent: An algorithm must be language independent, which means that it
should mainly focus on the input and the procedure required to derive the output
instead of depending upon the language
Advantages of an Algorithm
• Effective Communication: Since it is written in a natural language like English, it
becomes easy to understand the step-by-step delineation of a solution to any
particular problem.
• Easy Debugging: A well-designed algorithm facilitates easy debugging to detect the
logical errors that occurred inside the program.
• Easy and Efficient Coding: An algorithm is nothing but a blueprint of a program
that helps develop a program.
• Independent of Programming Language: Since it is a language-independent, it can
be easily coded by incorporating any high-level language.
Disadvantages of an Algorithm
• Developing algorithms for complex problems would be time-consuming and
difficult to understand.

• It is a challenging task to understand complex logic through algorithms.


Pseudocode
• Pseudocode refers to an informal high-level description of the operating principle
of a computer program or other algorithm.
• It uses structural conventions of a standard programming language intended for
human reading rather than the machine reading.
• It contains statements which looks like code.
Advantages of Pseudocode
• Since it is similar to a programming language, it can be quickly transformed into
the actual programming language.
• The layman can easily understand it.
• Its implementation is very beneficial for structured, designed elements.
• Some errors can be easily detected through pseudocode before transforming it into
a code.
Disadvantages of Pseudocode
• Since it does not incorporate any standardized style or format, it can vary from one
company to another.
• It does not depict the design.
Algorithm v/s Pseudocode
• Algorithm is a series of instructions to solve a problem.
• It not only helps in simplifying the problem but also to have a better understanding
of it.
• However, Pseudocode is a way of writing an algorithm.
• Programmers can use informal, simple language to write pseudocode without
following any strict syntax.
• It encompasses semi-mathematical or program-like statements.
• Let’s understand that difference by an example: suppose there are 60 students in the
class. How will you calculate the number of absentees in the class?
• Algorithmic Approach:
1. Initialize a variable called as Count to zero, absent to zero, total to 60
2. FOR EACH Student PRESENT DO the following:
Increase the Count by One
3. Then Subtract Count from total and store the result in absent
4. Display the number of absent students

• Pseudocode Approach:
1. Count <- 0, absent <- 0, total <- 60
2. REPEAT till all students counted
Count <- Count + 1
3. absent <- total - Count
4. Print "Number absent is:" , absent
Performance analysis of an algorithm
• Which is the best algorithm to solve any problem? How do we judge? How do we
identify?
• The analysis is a process of estimating the efficiency of an algorithm as well as
calculating/predicting the resources that the algorithm requires. .
• There are two fundamental parameters based on which we can analysis the
algorithm:
• Space Complexity: The space complexity can be understood as the amount of
space required by an algorithm to run to completion.
• Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.
Why analysis is required?
• To know in advance about resources (time & memory) requirement.
• To compare 2 or more algorithms designed to solve a particular
problem and select the algorithm with minimum resource (time &
memory) requirement
• To optimize resource requirement (time-space) of an algorithm
Analysis of Algorithm
Best case Average case Worst case
Resource usage is Resource usage is Resource usage is
minimum average maximum
Algorithm’s behavior Algorithm’s behavior Algorithm’s behavior
under optimal under random under the worst
condition condition condition
Minimum number of Average number of Maximum number of
steps or operations steps or operations steps or operations
Lower bound on Average bound on Upper bound on
running time running time running time
Generally do not Average and worst-case performances are
occur in real the most used in algorithm analysis.
Number Sorting
• Suppose you are sorting numbers in Ascending order.
• The initial arrangement of given numbers can be in any
of the following three orders.
1. Numbers are already in required order, i.e., Ascending order
No change is required – Best Case
2. Numbers are randomly arranged initially.
Some numbers will change their position – Average Case
3. Numbers are initially arranged in Descending or Decreasing
order.
All numbers will change their position – Worst Case

5 9 12 23 32 41

9 5 12 32 23 41 5 9 12 23 32 41

41 32 23 12 9 5
Best, Average, & Worst Case
Best Case Average Case Worst Case
Problem

Element in any Element at last


Linear Search Element at the of the middle position or not
first position
positions present

The first book Any book in- The last book


Book Finder between

Already sorted Randomly Sorted in


Sorting arranged reverse order
Asymptotic notations: Growth of functions
• The growth of time and space complexity with increasing input size N is a suitable
measure for the comparison of algorithms.
• The growth rate of functions is usually described using the Asymptotic notations
 Three most widely used asymptotic notations are:
• Big-O (Big oh) notation
• Big-Ω (Big omega) notation
• Θ (Big theta) notation
 Other asymptotic notations are:
• ο (small/little oh) notation
• ω (small/little omega) notation
Big – O notation
• It is denoted by 'O'.
• It is used to find the upper bound time/space of an algorithm , that means the
maximum time/space taken by the algorithm.
Definition: Let f(n),g(n) are two non-negative functions. If f(n) ≤ c*g(n) for two
positive constants c and n0 such that c>0 and for all n ≥ n0 then we say that
f(n)=O(g(n))

• The idea behind the big-O notation is to establish an upper bound (upper limit)
for the growth of a function f(n) for large n.
• We accept the constant C in the requirement
f(n) ≤ C*g(n) whenever n ≥ n0,
because C does not grow with x
• We are only interested in large n, so it is OK if f(n) > C*g(n) for n < n0.
Contd…
• Graphically, it can be represented as follows:
Big – O notation Examples
• Prove that f(n) = 3n + 5 is O(n).
f(n) = 3n + 5
≤ 3n + n , where n ≥ 5
≤ 4n
≤ C g(n) , where C=4 & n0=5
• Hence, f(n) = 3n + 5 is O(n) is proved.
• Similarly, Prove for the followings:
• f(n) = 27n2 + 16n is O(n2).
• f(n) = 2n3 + n+ + 2n is O(n3).
• f(n) = 4n3 + 2n + 3 is O(n3).
• f(n) = 2n + 6n2 + 3n is O(2n).
Big – O notation incorrect bound
• Prove that f(n) = 7n + 5 ≠ O(1).
• Proof by contradiction:
• Assume that 7n + 5 = O(1). So, we must have
• 7n + 5 ≤ C.1 for n ≥ n0.
• above statement is not true for large values of n
• Hence, our assumption is false.
• Hence, f(n) = 3n + 5 is O(n) is proved.
• Similarly, Prove for the followings:
• f(n) = 10n2 + 7 ≠ O(n).
• f(n) = 27n2 + 16n + 25 ≠ O(n).
• f(n) = 3n3 + 4n ≠ O(n2).
Big – O notation Loose bounds
• Question: If f(x) is O(x2), is it also O(x3)?

• Yes. x3 grows faster than x2, so x3 grows also faster than f(x).

• Therefore, we always have to find the smallest (closest) standard function g(x) for
which f(x) is O(g(x)).

• Ex: f(n) = 2n + 3 = O(n)


Big – Ω notation
• It is denoted by ' Ω'.
• It is used to find the lower bound time/space of an algorithm, that means the
minimum time/space taken by an algorithm.
• Definition : Let f(n),g(n) are two non-negative functions. If there exists two
positive constants c,n0 such that c>0 and for all n ≥ n0 if f(n) ≥ c*g(n) then we say
that
f(n)=Ω(g(n))
• The idea behind the big- Ω notation is to establish an lower bound (lower limit)
for the growth of a function f(n) for large n.
• It indicates the best case.
Contd…
• Graphically, it can be represented as follows:
Big – Ω notation Examples
• Prove that f(n) = 3n + 5 is Ω(n).
f(n) = 3n + 5
≥ 3n , where n ≥ 1
≥ C g(n) , where C=3 & n0=1

• Hence, f(n) = 3n + 5 is Ω(n) is proved.


• Similarly, Prove for the followings:
• f(n) = 27n2 + 16n is Ω(n2).
• f(n) = 2n3 + n2 + 2n is Ω(n3).
• f(n) = 4n3 + 2n + 3 is Ω(n3).
• f(n) = 2n + 6n2 + 3n is Ω(2n).
Big – Ω notation Examples-Incorrect bound
• Prove that f(n) = 7n + 5 ≠ Ω(n2).
Proof by contradiction: Assume that 7n + 5 = Ω(n2). So, we must have
7n + 5 ≥ C*n2 for n ≥ n0.
C*n2 / (7n + 5) ≤ 1.
if we take, C=1 and n ≥ 8 (n0=8) then above statement is not true.
• Hence, our assumption is false.
• Hence, f(n) = 7n + 5 is Ω(n) is proved.
• Similarly, Prove for the followings:
• f(n) = 10n2 + 7 ≠ Ω(n3).
• f(n) = 27n2 + 16n + 25 ≠ Ω(n3).
• f(n) = 3n3 + 4n ≠ Ω(2n).
Big – Ω notation Examples- Loose bounds
• Question: If f(x) is  (x3), is it also (x2)?

• Yes.

• Therefore, we always have to find the Largest simple function g(x) for which f(x)
is (g(x)).

• Ex: f(n) = 2n + 3 = (n)


Big – ϴ notation
• Definition : Let f(n),g(n) are two non-negative functions. If there exists positive
constants c1,c2 and n0 such that c1>0, c2>0 and for all n>=n0. if c1*g(n)<=f(n)<=c2*g(n)
then we say that
f(n)=Θ(g(n))

• It is denoted by the symbol called as (Θ).


• It is used to find the tight bound of time/space complexity of an algorithm.
Contd…
• Graphically, it can be represented as follows:
Big – ϴ Examples
Prove that f(n) = 3n + 5 is ϴ(n).

We can say that 3n ≤ 3n + 5


C1 n ≤ f(n), where C1 = 2

similarly, f(n) = 3n + 5
≤ 2n + n , where n ≥ 5
≤ 4n
≤ C2 g(n) , where C2=4

Hence, f(n) = 3n + 5 is ϴ(n) is proved.


Similarly, Prove for the followings:
f(n) = 27n2 + 16n is ϴ(n2).
f(n) = 2n3 + n2 + 2n is ϴ(n3).
f(n) = 4n3 + 2n + 3 is ϴ(n3).
f(n) = 2n + 6n2 + 3n is ϴ(2n).
Big – ϴ
• For any two functions g(n) and f(n), f(n) = (g(n)) if and only if
f(n) = O(g(n)) and f(n) = (g(n)).
i.e., (g(n)) = O(g(n)) Ç W(g(n))
Little – o notation
• Definition : Let f(n) and g(n) be functions that map positive integers to positive real
numbers. If for any real constant c > 0, there exists an integer constant n0 ≥ 1 such that
0 ≤ f(n) < c*g(n), then We say that
f(n) is ο(g(n)) OR f(n) ∈ ο(g(n))

• Mathematical definition: Let f(n),g(n) are two non-negative functions. If


[f(n) / g(n)]=0 then we say that f(n)=o(g(n)).

• Big-Ο is used as a tight upper-bound for the growth of an algorithm, where as little o
means loose upper-bound.
Contd…
• Example : consider f(n)=2n+3, g(n)=n2

• = = == = =0

• So, f(n) = o(n2) i.e. f(n) ∈ o(n2)

• Solve: If f(n) = n2 and g(n) = n3 then check whether f(n) = o(g(n)) or not.
Little – ω notation
• Definition : Let f(n) and g(n) be functions that map positive integers to positive real
numbers. If for any real constant c > 0, there exists an integer constant n 0 ≥ 1 such that
f(n) > c * g(n) ≥ 0 for every integer n ≥ n0, then we say that
f(n) is ω(g(n)) OR f(n) ∈ ω(g(n))

• Mathematical definition: Let f(n) and g(n) are two non-negative functions. If
[f(n) / g(n)]= then we say that f(n)= ω (g(n)).

• Big- Ω is used as a tight lower-bound for the growth of an algorithm, where as little ω
means loose lower-bound.
Contd…
• Example : consider f(n)= 4n2+6, g(n)= n

• = = = ==

• So, f(n) = ω(n) i.e. f(n) ∈ ω(n)

• Solve: If f(n) = 2n-3 and g(n) =1 then check whether f(n) = ω(g(n)) or not.
Common time complexities
BETTER
O(1) Constant time
O(log n) Logarithmic time
O(n) Linear time
O(n log n) Logarithmic linear time
O(n2) Quadratic time
O(n3) Cubic time
O(2n) Exponential time
WORSE
O(n!) Factorial time
O(nn)

You might also like