2
Most read
10
Most read
13
Most read
Introduction to Data Structure
and Algorithms
8/11/20181
Data Structures
8/11/20182
It is set of procedures to define, store, access and
manipulate data.
or
The organized collection of data is called data
structure.
or
The logical or mathematical model of a particular
organization of data is called data structure.
Types of Data Structure
 Physical Data Structures
The data structures that have actually or physically
same space in memory are called physical data
structures
o For example, one dimensional array
 Logical Data Structures
The data structures which are treated, defined and
stored in their logical picture
o For example, Boolean data type
8/11/20183
8/11/20184
Types of Data Structures
 Primitive(Elementary Data Types)
The data types which are processed or manipulated
as a single whole(i.e., alone). They are also called built-in
data types.
o Example, integer, real, character etc.
 Non-Primitive(Structure Data Types)
The items which are collection of other data
structures are called non-primitive items. If more than
one values are combined in the single name is called non-
primitive data structures
o Example, Arrays, Stack, Queues etc
8/11/20185
Types of Non-Primitive Data Structures
 Linear Data Structures
The DS in which there is concept of linearity b/w
the components is called linear data structure. The
components exists in a certain sequence one after
another. They have some successor or predecessor
o Example, Arrays, Stack, Queues etc.
 Non Linear Data Structures
The data structure in which the order of data
structure does not matter and is not fixed.
o Example Tree, Graph
8/11/20186
Algorithms
An algorithm is a well-defined list of steps for solving
a particular problem.
The efficiency of an algorithm depends on space and
time.
Characteristics of Algorithms
The main characteristics of algorithms are as follows:
 Algorithms must have a unique name
 Algorithms should have explicitly defined set of inputs
and outputs
 Algorithms are well-ordered with unambiguous
operations
 Algorithms halt in a finite amount of time. Algorithms
should not run for infinity, i.e., an algorithm must end at
some point
8/11/20187
Algorithm Efficiency
 algorithmic efficiency is a property of an algorithm which
relates to the number of computational resources used by
the algorithm.
 For maximum efficiency we wish to minimize resource
usage. However, the various resources (e.g. time, space)
cannot be compared directly, so which of two algorithms is
considered to be more efficient often depends on which
measure of efficiency is considered the most important, e.g.
the requirement for high speed, minimum memory usage
8/11/20188
9
• Let’s look at the following algorithm for initializing the values in
an array:
final int N = 500;
int [] counts = new int[N];
for (int i=0; i<counts.length; i++)
counts[i] = 0;
• The length of time the algorithm takes to execute depends on the
value of N.
• In that algorithm, we have one loop that processes all of the
elements in the array.
• Intuitively:
– If N was half of its value, we would expect the algorithm to
take half the time
– If N was twice its value, we would expect the algorithm to
take twice the time
• That is true and we say that the algorithm efficiency relative to N
is linear.
8/11/2018
10
• Let’s look at another algorithm for initializing the values in a
different array:
final int N = 500;
int [] [] counts = new int[N][N];
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
counts[i][j] = 0;
• The length of time the algorithm takes to execute still depends on the
value of N.
• However, in the second algorithm, we have two nested loops to
process the elements in the two dimensional array
• Intuitively:
– If N is half its value, we would expect the algorithm to take one
quarter the time.
– If N is twice its value, we would expect the algorithm to take
quadruple the time.
• That is true and we say that the algorithm efficiency relative to N is
quadratic.
8/11/2018
Time Complexity
 It’s a function describing the amount of time required to
run an algorithm in terms of the size of the input. "Time"
can mean the number of memory accesses performed, the
number of comparisons between integers, the number of
times some inner loop is executed, or some other natural
unit related to the amount of real time the algorithm will
take.
 Time complexity of an algorithm signifies the total time
required by the program to run till its completion.
 Worst-case − The maximum number of steps taken on any
instance of size a.
 Best-case − The minimum number of steps taken on any
instance of size a.
 Average case − An average number of steps taken on any
instance of size a.
8/11/201811
Space Complexity
 Space complexity is a function describing the amount of
memory (space) an algorithm takes in terms of the
amount of input to the algorithm.
 We often speak of extra memory needed, not counting the
memory needed to store the input itself. Again, we use
natural (but fixed-length) units to measure this.
 We can use bytes, but it's easier to use, say, the number of
integers used, the number of fixed-sized structures, etc.
 In the end, the function we come up with will be
independent of the actual number of bytes needed to
represent the unit.
 Space complexity is sometimes ignored because the space
used is minimal and/or obvious, however sometimes it
becomes as important issue as time complexity
8/11/201812
Asymptotic Notation
 Execution time of an algorithm depends on the instruction set,
processor speed, disk I/O speed, etc. Hence, we estimate the
efficiency of an algorithm asymptotically.
 Time function of an algorithm is represented by T(n), where n
is the input size.
 Different types of asymptotic notations are used to represent
the complexity of an algorithm. Following asymptotic
notations are used to calculate the running time complexity of
an algorithm.
 O − Big Oh
 Ω − Big omega
 θ − Big theta
 o − Little Oh
 ω − Little omega
Examples:
• 3 * n2 + n/2 + 12 ∈ O(n2)
• 4*n*log2(3*n+1) + 2*n-1 ∈ O(n * log n)
8/11/201813

Introduction to data structure and algorithms

  • 1.
    Introduction to DataStructure and Algorithms 8/11/20181
  • 2.
    Data Structures 8/11/20182 It isset of procedures to define, store, access and manipulate data. or The organized collection of data is called data structure. or The logical or mathematical model of a particular organization of data is called data structure.
  • 3.
    Types of DataStructure  Physical Data Structures The data structures that have actually or physically same space in memory are called physical data structures o For example, one dimensional array  Logical Data Structures The data structures which are treated, defined and stored in their logical picture o For example, Boolean data type 8/11/20183
  • 4.
  • 5.
    Types of DataStructures  Primitive(Elementary Data Types) The data types which are processed or manipulated as a single whole(i.e., alone). They are also called built-in data types. o Example, integer, real, character etc.  Non-Primitive(Structure Data Types) The items which are collection of other data structures are called non-primitive items. If more than one values are combined in the single name is called non- primitive data structures o Example, Arrays, Stack, Queues etc 8/11/20185
  • 6.
    Types of Non-PrimitiveData Structures  Linear Data Structures The DS in which there is concept of linearity b/w the components is called linear data structure. The components exists in a certain sequence one after another. They have some successor or predecessor o Example, Arrays, Stack, Queues etc.  Non Linear Data Structures The data structure in which the order of data structure does not matter and is not fixed. o Example Tree, Graph 8/11/20186
  • 7.
    Algorithms An algorithm isa well-defined list of steps for solving a particular problem. The efficiency of an algorithm depends on space and time. Characteristics of Algorithms The main characteristics of algorithms are as follows:  Algorithms must have a unique name  Algorithms should have explicitly defined set of inputs and outputs  Algorithms are well-ordered with unambiguous operations  Algorithms halt in a finite amount of time. Algorithms should not run for infinity, i.e., an algorithm must end at some point 8/11/20187
  • 8.
    Algorithm Efficiency  algorithmicefficiency is a property of an algorithm which relates to the number of computational resources used by the algorithm.  For maximum efficiency we wish to minimize resource usage. However, the various resources (e.g. time, space) cannot be compared directly, so which of two algorithms is considered to be more efficient often depends on which measure of efficiency is considered the most important, e.g. the requirement for high speed, minimum memory usage 8/11/20188
  • 9.
    9 • Let’s lookat the following algorithm for initializing the values in an array: final int N = 500; int [] counts = new int[N]; for (int i=0; i<counts.length; i++) counts[i] = 0; • The length of time the algorithm takes to execute depends on the value of N. • In that algorithm, we have one loop that processes all of the elements in the array. • Intuitively: – If N was half of its value, we would expect the algorithm to take half the time – If N was twice its value, we would expect the algorithm to take twice the time • That is true and we say that the algorithm efficiency relative to N is linear. 8/11/2018
  • 10.
    10 • Let’s lookat another algorithm for initializing the values in a different array: final int N = 500; int [] [] counts = new int[N][N]; for (int i=0; i<N; i++) for (int j=0; j<N; j++) counts[i][j] = 0; • The length of time the algorithm takes to execute still depends on the value of N. • However, in the second algorithm, we have two nested loops to process the elements in the two dimensional array • Intuitively: – If N is half its value, we would expect the algorithm to take one quarter the time. – If N is twice its value, we would expect the algorithm to take quadruple the time. • That is true and we say that the algorithm efficiency relative to N is quadratic. 8/11/2018
  • 11.
    Time Complexity  It’sa function describing the amount of time required to run an algorithm in terms of the size of the input. "Time" can mean the number of memory accesses performed, the number of comparisons between integers, the number of times some inner loop is executed, or some other natural unit related to the amount of real time the algorithm will take.  Time complexity of an algorithm signifies the total time required by the program to run till its completion.  Worst-case − The maximum number of steps taken on any instance of size a.  Best-case − The minimum number of steps taken on any instance of size a.  Average case − An average number of steps taken on any instance of size a. 8/11/201811
  • 12.
    Space Complexity  Spacecomplexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm.  We often speak of extra memory needed, not counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units to measure this.  We can use bytes, but it's easier to use, say, the number of integers used, the number of fixed-sized structures, etc.  In the end, the function we come up with will be independent of the actual number of bytes needed to represent the unit.  Space complexity is sometimes ignored because the space used is minimal and/or obvious, however sometimes it becomes as important issue as time complexity 8/11/201812
  • 13.
    Asymptotic Notation  Executiontime of an algorithm depends on the instruction set, processor speed, disk I/O speed, etc. Hence, we estimate the efficiency of an algorithm asymptotically.  Time function of an algorithm is represented by T(n), where n is the input size.  Different types of asymptotic notations are used to represent the complexity of an algorithm. Following asymptotic notations are used to calculate the running time complexity of an algorithm.  O − Big Oh  Ω − Big omega  θ − Big theta  o − Little Oh  ω − Little omega Examples: • 3 * n2 + n/2 + 12 ∈ O(n2) • 4*n*log2(3*n+1) + 2*n-1 ∈ O(n * log n) 8/11/201813