1. JSPM's Jayawantrao Sawant College
of Engineering,HADAPSAR,PUNE
Subject :Fundamentals of Data Structures
(SE COMP)
Presented by,
Department of computer engineering
1
2. Savitribai Phule Pune University Second Year of
Computer Engineering (2019 Course) 210242:
Fundamentals of Data Structures
• Teaching Scheme ,Lecture: 03 Hours/Week
• Credit Scheme : 03
• Examination Scheme and Marks:
Mid_Semester(TH): 30 Marks
End_Semester(TH): 70 Marks
• Prerequisite Courses :
110005: Programming and Problem Solving
• Companion Course :210247: Data Structures Laboratory
2
3. Course Objectives:
• The course is intended to provide the foundations of the practical
implementation and usage of Data Structures and Algorithms to ensure that the
learner evolves into a competent programmer capable of designing and
analyzing implementations of data structures and algorithms for different kinds
of problems.
• To understand the standard and abstract data representationmethods.
• To acquaint with the structural constraints and advantages in usage of the data.
• To understand various data structures, operations on it and the memory
requirements
• To understand various data searching and sorting methods.
• To understand various algorithmic strategies to approach the problemsolution.
3
4. Course Outcomes:
On completion of the course, learner will be able to–
• CO1: Design the algorithms to solve the programming problems, identify
appropriate algorithmic strategy for specific application, and analyze the time and
space complexity.
• CO2: Discriminate the usage of various structures,
Design/Program/Implement the appropriate data structures; use them in
implementations of abstract data types and Identity the appropriate data structure in
approaching the problem solution.
• CO3: Demonstrate use of sequential data structures- Array and Linked lists to store
and process data.
• CO4: Understand the computational efficiency of the principal algorithms for
searching and sorting and choose the most efficient one for the application.
• CO5: Compare and contrast different implementations of data structures
(dynamic and static).
• CO6: Understand, Implement and apply principles of data structures-stack and
queue to solve computational problems.
4
5. SUBJECT OVERVIEW
5
Unit I Introduction to Algorithm and Data
Structures
07 Hours
Unit II Linear Data Structure Using
Sequential
Organization
07 Hours
Unit III Searching and Sorting 07 Hours
Unit IV Linked List 07 Hours
Unit V Stack 07 Hours
Unit VI Queue 07 Hours
7. Unit I Introduction to Algorithm
and Data Structures (07 Hours)
• Introduction: From Problem to Program (Problem, Solution, Algorithm, Data
Structure and Program). Data Structures: Data, Information, Knowledge, and
Data structure, Abstract Data Types (ADT), Data Structure Classification
(Linear and Non-linear, Static and Dynamic, Persistent and Ephemeral data
structures).
• Algorithms: Problem Solving, Introduction to algorithm, Characteristics of
algorithm, Algorithm design tools: Pseudo-code and flowchart. Complexity of
algorithm: Space complexity, Time complexity, Asymptotic notation- Big-O,
Theta and Omega, finding complexity using step count method, Analysis of
programming constructs-Linear, Quadratic, Cubic, Logarithmic. Algorithmic
Strategies: Introduction to algorithm design strategies- Divide and Conquer,
and Greedy strategy.
7
8. Contents
• Introduction
• General Problem Solving Concepts
▫ Types of Problems
▫ Problem solving with computers
▫ Difficulties with problem solving
▫ Problem solvingAspect
• Problem Solving Concepts for
Computer
▫ Constants and variables
▫ Data types
▫ Functions
▫ Operators
▫ Expressions and equations
8
• Programming Concepts
▫ Communicating with computers
▫ Organizing the problem
▫ Using the tools
▫ Testing the solution
▫ Coding the program
9. Introduction
• People Make Decisions everyday to solve their problems
that affect their lives
• If bad decision is made time and resources are wasted
• Six steps for problem Solving
▫ Identify the problem
▫ Understand the problem
▫ Identify alternative ways to solve the problem
▫ Select the best way to solve the problem
▫ List instructions that enable you to solve the problem
using the selected solution
▫ Evaluate the solution
9
10. Example: Problem what to do this evening
1. Indentify the Problem: How do the Individuals wish to spend the evening?
2. Understand the problem: The knowledge base of the participants must be
considered. The only solutions that should be selected are ones that everyone
would know how to do. You probably would not select as a possible solution
playing a game of chess if the participants do not know how to play chess.
3. Identify Alternatives:
▫ Watch Television
▫ Invite friends over
▫ Play games
▫ Go to the movie
▫ Play miniature golf
▫ Go to a friends party
▫ Go to the amusement park
▫ List is complete only when you can think of no more alternatives
4. Select best way to solve the problem
▫ Cut out alternatives that are not acceptable
▫ Specify pros and Cons of each Remaining alternative
▫ Compare pros and cons to make final decision
5. Prepare a list of steps that will result in a fun evening.
6. Evaluate the solution. Are we having fun yet?
12. Types of Problems
• Problems can be solved with series ofactions
▫ Algorithmic Solution
• Problems do not always have straightforward solutions
▫ Heuristic solutions
• Problem solver can use six steps for both algorithmic andheuristic
solutions
• Evaluating correctness and Appropriateness of heuristic solutions
are far less certain.
• Most problems require a combination of the two kinds ofsolutions
12
13. Problem solving with computers
• Results means the outcome or the completed computer
assisted answer
• Program means the set of instructions that make up the
solution using programming language
• Computers are built to deal with algorithmic solutions
• Difficulty lies in Programming
• Solutions must be transformed into an algorithmic format
13
14. Difficulties with Problem Solving
• People have many problems with problem solving
• Afraid to make decisions
• People go through one or more steps incorrectly
• Problem solving process is not easy
• Problem solving on computer
▫ Difficult task of writing instructions
▫ Computer has specific system of communication
14
15. Problem Solving Aspect
• There is no universal method for problem solving
• Phases
▫ Problem Definition Phase
▫ Getting started on a problem
▫ The use of Specific Examples
▫ Similarities among problems
▫ Working backwards from the solution
▫ General Problem Solving Strategies
15
16. Problem Definition Phase
• What must be done?
• We must try to extract set of precisely defined tasks.
• E.g. Finding Square root, Greatest Common Divisor
Getting Started on a Problem
• Sometimes, even after problem definition people do not
know where to start?
• What can we do?
16
17. The use of Specific Examples
• Best approach when we are stuck is to make a start on a
problem is to pick a specific example
• Geometrical and graphical representing certain aspect of a
problem can be useful
Similarities among problems
• Bring as much as past experiences as possible
• New problem cannot be completely different
• Sometimes it blocks us from discovering a new thing
• In the first instance try to independently solve the problem
17
18. 18
Working Backwards from the solution
• In some cases we assume that we already have solution andthen
try to work backwards
• Whatever attempts we make that we make to get started ona
problem we should write it down
General Problem Solving Strategies
• Divide and Conquer
• Binary doubling
• Dynamic Programming
▫ Greedy method
▫ Branch and Bound
▫ Backtracking
20. Constants and Variables
• The Computer uses constants and variables to solve problems
• Constants is a value that never changes during processing of
all instructions
▫ Can be numeric, alphabetic or special Symbol
▫ Constant is given a value and then referred to by its
name
• In contrast, the value of a variable may change during
processing
• In many languages variables are called Identifiers
20
21. Rules for naming and Using Variables
1. Name a variable according to what it represent
2. Do not use spaces in variable name
3. Start variable with a letter
4. Do not use dash(-) in a program
5. After introduces variable name, exact variable name must
be used where data item is used
6. Be consistent while using UPPERCASE and lowercase
variables
7. Use naming conventions specified by your company
21
22. Data Types
• To process solutions computer must have data.
• Data are unorganized facts
• Computer Must be told the data types of each variable and
constant
22
DATA
Data
Processed
into
information
Report
Balance
Sheet
Calculates
the Balance
Checks
Deposits
Bk Charges
Fig. Processing Data: How a computer Balances a Checkbook
23. 23
Data Type Data Sets Examples
Numeric:
Integer
All whole numbers 3400
-34
Numeric:
Real
All real Numbers
(Whole + Decimal)
3256.344
12333.0
0.32323
Character :
Surrounded by
quotation mark
All letters, Numbers
and Special Symbols
‘a, ‘1’, ‘A’, ‘F’, ‘%’, ‘&’
‘$’
String:
Surrounded by
quotation mark
Combination of
more than one
character
“Rahul”
“343443”
“7070-3232-232”
Logical True, False True, False
24. Rules for Data Types
1. The data that define the value of a variable or a constant will
be of numeric, character, logical
2. Computer then associate variable with desired data type
3. Data types can not be mixed
4. Each of the data types uses what is called a data sets
5. Any numeric item that must be used in calculations resulting
in a numeric result must be of numeric type
24
25. Functions
• Functions are small set of instructions that perform specific
task and return values
• By using functions programmers can shorten the problem
solving time and improves readability
• Each language has set of functions within it
• Most languages allows programmers to write their own
functions
• Functions can be
▫ Mathematical functions
▫ String functions
▫ Conversion Functions
▫ Statistical functions
▫ Utility functions
25
26. Operators
• Operators are the data connectors within expressions
• They tell the computer how to process data
• Operators
▫ Mathematical + - / * % ^
▫ Relational < > <= >= != [<>]
▫ Logical && || !
• Operand and resultant are two concepts related to operator
• The data type of Operands and resultant depends on the
operator
26
27. 27
Mathematical
Operators
Computer
Symbol
Addition +
Subtraction -
Multiplication *
Division /
Integer Division
Modulo
Division
% or Mod
Relational
Operators
Computer
Symbol
Equal to = or ==
Less than <
Greater Than >
Less than or
equal to
<=
Greater than
and equal to
>=
Not equal to <> Or !=
Relational Operators
Logical
Operators
Computer
Symbol
Not !
And &&
Or ||
Mathematical Operators
Logical Operators
28. 28
Order of Operations Operand Data Type Resultant Data Type
1. Funtions
2. Power
3. %
4. *, /
5. + -
6. =, <> <, >, <=,>=
7. !
8. &&
9. ||
Numeric
Numeric
Numeric
Numeric
Numeric or String
Logical
Logical
Logical
Numeric
Numeric
Numeric
Numeric
Logical
Logical
Logical
Logical
29. Expressions and Equations
• Expressions and Equations make a part of instructions in the
solution
• An Expressions processes data, operands using operators
e.g. length * breadth
• An Equation stores the resultant of anexpression
e.g. area=length*breadth
• Setting up /Evaluating
▫ Numeric Expression
▫ Mathematical equations
▫ Relational Expression
▫ Logical expression
29
30. Examples
• X(3Y+4) – 4Y
X+6
• 5 * ( x + y ) – 4 * y / ( z + 6) where x=2, y=3, z=6
• A– 2 > B if A=6 and B =8
• F=NOT ( A < B )AND ( C OR D) where A=4, B=2 C=True,
D=False
30
32. Communicating with the computer
• We have to learn system of communication or Computer language
• System of communication requires instructions governed by
specific rules.
• Syntax are the rules governing the computer operating system
• An error is called a bug
• Bug must be found and corrected, a process is called Debugging
• Computers are exact machines but the people working with them
are not.
32
33. Organizing the problem
• Organizational tools will help us to solve problems on the
computer
• Tools
▫ Problem Analysis Chart
▫ Structure Chart or Interactivity Chart
▫ IPO chart
▫ Algorithms
▫ Flowcharts
33
34. Analyzing the Problem
• First programmer has to understand and analyze the
requirements of the problem. The Program Analysis
Chart(PAC) can be prepared.
• Good way to analyze problem is to separate it into four parts
1. The given data
2. The required Results
3. The processing that required in the problem
4. A list of solution alternatives
• PAC allows problem solver to eliminate the words and
separate the facts from the problem
34
35. 35
1. Given Data 2. Required Results
3. Processing Required 4. Solution Alternatives
Fig: Problem Analysis Chart
36. 36
1. Given Data
Hours
Pay rate
2. Required Results
Grosspay
3. Processing Required
Grosspay=Hours*Payrate
4. Solution Alternatives
1. Define the hours worked
and Payrate as constants
2. Define the hours worked
and payrate as input values
Fig: Problem Analysis Chart
37. Developing the
Interactivity(Structure) Chart
• Next task in organizing the solution is to divide the
processing into subtasks called modules
• Connect these modules to show interaction of processing
between modules
• Each module should contain the task to accomplish one
function
• There will be one module that controls the flow of most of the
other module[Control or Main module]
37
38. Control
Module 1
Module 3
Module 2
Module 6
Module 7
Module 3 Module 4 Module 5
Interactivity Chart
Payrollcontrol
Read Calc Print
Interactivity Chart for Payroll Problem
Until No Employee
41. Developing the IPO chart
• The input-processing-output chart extends and organizes the
information in the PAC
• IPO also shows that where in the solution processing takes
place
• Sections
1. Input
2. The processing
3. Module Reference
4. Output
41
42. 42
Input Processing Module
reference
Output
All data input
[From section 1 of
PAC]
All processing in
step
[From Section 3
and 4]
Module reference
from the
interactivity chart
All output
requirement
[from section1 and
2 from PAC]
Input Processing Module
reference
Output
Hours worked
Pay Rate
1. Enter Hours
Worked
2. Enter Pay Rate
3. Calculate Pay
4. Print Pay
5. End
Read
Read
Calc
Print
Payrollcontrol
Gross pay
The IPO Chart
The IPO Chart for the Payroll Problem
43. Writing the Algorithms
• Next steps in organizing the solution is to develop set of
instructions for the computer called algorithm
• Pseudo code is similar to algorithms
• Programmer writes a separate instructions for each module
in the structure chart
• Hardest part of problem solving
▫ The instructions cannot assume anything
▫ Cannot skip Steps
▫ Must be executable one step at a time
▫ Must be complete
43
44. Drawing Flowchart
• From the algorithms programmer develops the flowcharts
• Flowcharts are the graphical representation of the
algorithms
• Flowcharts can show errors in logic not readily visible in the
other charts
• A flowchart shows the flow of the processing from the
beginning to the end of a solution
44
46. Rules for drawing flowcharts
• You should write instructions inside the blocks
• If there is something you need to remember you can write a
note besides a block
• A flowchart always start at the top of the page and flows to the
bottom
• Use flowchart template to draw the symbols and flow lines
• Make the blocks big enough to write instructions so they can
be easily read
• Final copy of the flowchart may not be the first draft
46
47. Testing the Solutions
• When a solution is complete it is important to test it
• To test a solution programmers select test data, a set of
values
• A solution should never be assumed correct.
• If bug is detected, the solution has to be modified it and
then the testing process starts once again
47
48. Coding the Program
• Next step is writing the solution into a computer
language
• If programmer follows the proper steps in developing
solution there should be few logic errors in the program
48
49. Introduction to Algorithm
• The word algorism came from Persian mathematician “Abu
Abdullah Muhammad bin Musa al-Khwarizmi”
• Algorithm is a well defined computational method that take
some value as input and produce some value as output
• Each Algorithm must have
▫ Specification
▫ Pre conditions
▫ Body of the algorithm
▫ Post conditions
49
50. Example
• Algorithms are written in pseudo code
• Consider a simple algorithm for finding the factorial of n.
Specification: Computes n!.
Pre-condition: n >= 0
Post-condition: FACT = n!
Algorithm Factorial (n)
Step 1: FACT = 1
Step 2: for i = 1 to n do
Step 3: FACT = FACT * i
Step 4: print FACT
50
51. Algorithm
“Algorithm is any well defined
computational procedure that takes some
values or set of values as input and produces
some value or a set of values as output”
51
52. Characteristics of algorithm
• Input : algorithm is supplied with zero or moreexternal
quantities
• Output: Algorithm must produce one or moreresults/output
• Definiteness: Each step in an algorithm must be clearand
unambiguous
• Finiteness: Must have finite number ofsteps
• Effectiveness: Every instruction must be sufficientlybasic
52
53. Pseudo code
• Expression:
▫ Use standard mathematical symbols to describe
numeric and boolean expression
▫ Use for assignment
▫ Use = for equality relationship
• Method Declaration
▫ Algorithm name( param1,param2)
• Return : return value
53
55. Pseudo code
Algorithm arrayMax(A,n)
//Input: An array A storing n integers
//Output: The maximum element inA
currentMax A[0]
for i1 to n-1 do
If currentMax<A[i] then currentMaxA[i]
return currentMax
55
57. Pseudo code
Algorithm bubblesort(A,n)
//Input: An array A storing n integers
//Output: The sorted sequence in array A
for i:=0 to n-1 do
if(A[i]>A[i+1]) then
temp := A[i]
A[i]=A[i+1]
A[i+1]=temp
return
57
58. What is the good Algorithm?
• Efficient
▫ Running Time
▫ Space Used
• Efficiency as a function of input size
▫ The number of bits in an input number
▫ Number of data elements (numbers, points)
58
59. Analyzing an Algorithm
• How to find which is the best Algorithm?
• We can compare one algorithm with other and choose the
best.
• Performance is measured in terms of
▫ Time Complexity : Amount of time taken by
algorithm to perform the task
▫ Space Complexity : Amount of memory needed to
perform the task
59
60. Space Complexity
• Space complexity specifies amount of computer memory
required during the program execution as a function of
input size
• Can be done at two different times
▫ Compile Time
▫ Run Time
60
61. Space Complexity Example
• Program to compute the sum of list of numbers
• Write an algorithm
• For 10 numbers
• For 20 numbers
• For 50 Numbers
61
62. Time Complexity
• Time complexity : - Amount of computer time that it
needs to run program to completion
• Depends upon
▫ The machine we are executing on
▫ Its machine language instruction set
▫ The Time required by each machine instruction
▫ The translation, compiler will make from source to
machine language
62
63. Standard Time Complexity Functions
Functions Name
C Constant
log N Logarithmic
Log2 N Log-Squared
N Linear
N2 Quadratic
N3 Cubic
C N Exponential
An Algorithm can be characterized by a timing
function T (N)
63
64. Frequency count
• Denotes how many times particular statement is executed.
• Eg. Consider the code
• Void fun()
• {
▫ int a;
▫ a=10; … ……………….. 1
▫ Printf(“ %d”, a) …………………… 1
▫ }
▫ Frequency count is 2
•
64
65. • Eg. Consider the code
• Void fun()
• {
▫ int a;
▫ a=10; … ……………….. 1
for(i=0;i<n; i++) …………………. n+1
{ a=a+i ; …………………. N
}
▫ Printf(“ %d”, a) …………………… 1
▫ }
▫ Frequency count is 2n+3
65
66. Void fun(int a[] [], int b[] [])
{
int c[3][3];
for (i=0;i<m; i++)
{
for (j=0;j<n; j++)
{
………………………… m+1
…………………………. m(n+1)
c[i][j]= a[i][j] + b[i][j]; …………………. m.n
}}
Frequency count is 2m(1+n)+1
66
69. Time Complexity Example
• Program to compute the sum of list of numbers
• Write an algorithm
• For 10 numbers
• For 20 numbers
• For 50 Numbers
69
70. Space Complexity
70
Algorithm abc ( a, b, c)
{
return ( a + b + b * c + ( a + b – c ) / ( a + b ) + 4.0 )
}
Algorithm Sum ( a,n)
{
sum := 0.0;
for i:= 1 to n do
s := s + a[i];
return s;
}
Algorithm RSum ( a , n)
{
if(n<=0) then return 0.0;
else return Rsum(a,n-1)+ a[n];
}
71. Space Complexity
• The space needed by each of the algorithms is seem to be the
sum of the following components
▫ Fixed part that is independent of the characteristics of
inputs and outputs
Instruction Space, Simple Variables, Space for
constants, array size.
▫ Variable part that is dependent of the characteristics of
inputs and outputs
Reference variables, recursion stack space, all loops
S(P)- Space requirement , C- constant, Sp- space dependant
on instance characteristics(Variable part),
71
S ( P) = c + Sp (Instance Characteristics)
72. Time complexity
• 1. identify the basic operation of the algorithm
• 2. obtain the frequency count
• 3. consider the order of magnitude of FC & express it in terms of bigoh
notation.(O).
• Eg. If we are getting the Frequency count of any algorithm as “ 2n + 3”
• By neglecting the constant term & by considering the order of magnitude we
can express the FC in term of big oh notation as O(n).
72
73. 73
Algorithm Sum ( a,n)
{
sum := 0.0;
count:=count + 1; // it is initially zero
for i:= 1 to n do
{
count:=count + 1; // For for
s := s + a[i];
count:=count + 1; // for assignment
}
count:=count + 1; //For Last time of for
count:=count + 1; // For the return
return s;
} tn= 2n + 3
75. 75
Algorithm Add ( a, b, c, m, n)
{
for i : = 1 to m do
for j:= 1 to n do
c[ i, j] := a[ i, j] + b[ i, j]
}
76. 76
Algorithm Add ( a, b, c, m, n)
{
for i : = 1 to m do
{
count:=count + 1;
for j:= 1 to n do
{
count:=count + 1;
c[ i, j] := a[ i, j] + b[ i, j]
count:=count + 1;
}
count:=count + 1;
}
count:=count + 1;
} tn= 2mn+2m+1
77. 77
for (int i=0; i< n ; i++) for (int i=0; i< n ; i++)
{ for ( int j=0 ; j < n; j++)
cout << i; {
p = p + i; cout << i;
} p = p + i;
tn= 3n+1
} tn= 3n2+2n+1
for (i=1; i<= n ; i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(l=1;k<=n;j++)
{
c[i][j] = c[i][j] + a[i][k] * a[k][i];
}
}
}
tn= 2n3+3n2+2n+1
78. 78
Algorithm Fibonacci (n)
{
if(n<=1) then
write(n);
else
{
fnm2:= 0; fnm1:=1;
for i := 2 to n do
{
fn : = fnm1 + fnm2;
fnm2:=fnm1; fnm1:= fn;
}
write(fn);
}
} tn= ????
79. Best, Worst and Average Case
• Worst Case : - Function defined by maximum number of
steps taken on any instance of size n
• Best Case : - Function defined by minimum number of
steps taken on any instance of size n
• Average Case : - Function defined by average number of
steps taken on any instance of size n
79
80. Asymptotic Notations
• Ignoring machine and language dependent factors,
consider only order of magnitude of frequency count
• The notations are
▫ O(n)- Upper Bound on the time complexity value
▫ Ω(n)- Lower Bound of time an algorithm needs
▫ Θ(n)- when best and worst case requires same time,
Θ(n) is required
80
82. Asymptotic Analysis
• Goal: To simplify analysis of running time by getting rid of
“details”, which may be affected by specific implementation
and hardware
▫ Like “Rounding” 1000001=1000000
▫ 3n2=n2
• Capturing the essence : how the running time of an
algorithm increases with the size of input in the limit
82
83. Asymptotic Notation
• The “big oh” Notation
▫ Asymptotic upper bound
▫ f(n)= o(g(n)) if there exist constant c and n0, s.t
f(n)<=c *g(n) for n>=n0
▫ f(n) and g(n) are non negative
functions
• Used for worst case analysis
• 3n+2 is O(n) as 3n+2 <=4n for n>=2
83
84. 84
• 3n + 3 O(n) as 3n+3 <= 4n for all n>=3
• 100n+6 O(n) as 100n+6 <= 101n
• 10n2 + 4n + 2 is O(n2) as 10n2 + 4n + 2 <=11n2 for all n>= 5
• 1000n2 + 100n + 6 O(n2) as 1000n2 + 100n -6 <=1001n2 for all n>=100
85. Asymptotic Notation
• The Omega Notation
▫ Asymptotic lowerbound
▫ f(n)=Ω(g(n)) if there exists constant c & n0 s.t.
f(n)>=c*g(n) for n>=n0
• Used to describe best case
running time or lower bound
• 3n+2 = Ω(n) as 3n+2 >=3n for n>=1
85
86. Examples
• 3n + 3 = Ω(n) as 3n+3 >= 3n for all n>= 1
• 100n + 6 = Ω(n) as 100n + 6 >= 100n for all n>=1
• 10n2 + 4n + 2 = Ω ( n2) as 100n2 + 4n + 2 >=1
86
87. Asymptotic Notation
• The theta Notation [ Ө ]
▫ Function f(n) = Ө (g(n))
▫ If there exists positive constants c1,c2 and n0
c1*g(n)<= f(n) >=c2*g(n) for all n>= n0
• 3n + 2 = Ө (n) as 3n + 2 >=3n for all n>=2 and 3n +2<=4n
for all n>=2;
• Theta notation is more precise than both the big ohand
omega notations
87