KEMBAR78
Data Structures_Introduction to algorithms.pptx
Course Contents
UNIT-I Introduction to Algorithm and Analysis of
Algorithms
6 Hours
Concept of Problem Solving, Introduction to Algorithms,
Characteristics of Algorithms, Introduction to Data Structure, Data
Structure Classification (Linear and Non-linear, Static and Dynamic,
Persistent and Ephemeral data structures), Time complexity and Space
complexity, Asymptotic Notation - The Big-O, Omega and Theta
notation, Algorithmic upper bounds, lower bounds, Best, Worst and
Average case analysis of an Algorithm, Abstract Data Types (ADT).
• Programming is a process of problem
solving
• Problem solving techniques
– Analyze the problem
– Outline the problem requirements
– Design steps (algorithm) to solve the problem
• Algorithm:
– Step-by-step problem-solving process
– Solution achieved in finite amount of time
Problem Solving
• Step 1 - Analyze the problem
– Outline the problem and its requirements
– Design steps (algorithm) to solve the
problem
• Step 2 - Implement the algorithm
– Implement the algorithm in code
– Verify that the algorithm works
• Step 3 - Maintenance
– Use and modify the program if the
problem domain changes
Problem Solving Process
• Thoroughly understand the problem
• Understand problem requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output?
• If the problem is complex, divide it into
subproblems
– Analyze each subproblem as above
Analyze the Problem
• The idea behind the computer program
• Stays the same independent of
– Which kind of hardware it is running on
– Which programming language it is written in
• Solves a well-specified problem in a
general way
• Is specified by
– Describing the set of instances (input) it must
work on
– Describing the desired properties of the
output
What is an algorithm?
• Before a computer can perform a task, it
must have an algorithm that tells it what to
do.
• Informally: “An algorithm is a set of steps
that define how a task is performed.”
• Formally: “An algorithm is an ordered set of
unambiguous executable steps, defining a
terminating process.”
– Ordered set of steps: structure!
– Executable steps: doable!
– Unambiguous steps: follow the directions!
– Terminating: must have an end!
What is an algorithm? (Cont’d)
Basic Concepts
• Overview: System Life Cycle
• Algorithm Specification
• Data Abstraction
• Performance Analysis
• Performance Measurement
1.1 Overview: system life cycle (1/2)
• Good programmers regard large-scale
computer programs as systems that
contain many complex interacting parts.
• As systems, these programs undergo a
development process called the system
life cycle.
1.1 Overview (2/2)
• We consider this cycle as consisting of
five phases.
– Requirements
– Analysis: bottom-up vs. top-down
– Design: data objects and operations
– Refinement and Coding
– Verification
• Program Proving
• Testing
• Debugging
1.2 Algorithm Specification (1/10)
• 1.2.1 Introduction
– An algorithm is a finite set of instructions that
accomplishes a particular task.
– Criteria
• input: zero or more quantities that are externally supplied
• output: at least one quantity is produced
• definiteness: clear and unambiguous
• finiteness: terminate after a finite number of steps
• effectiveness: instruction is basic enough to be carried out
– A program does not have to satisfy the finiteness criteria.
1.2 Algorithm Specification (2/10)
• Representation
– A natural language, like English or Chinese.
– A graphic, like flowcharts.
– A computer language, like C.
• Algorithms + Data structures =
Programs [Niklus Wirth]
• Sequential search vs. Binary search
• Example 1.1 [Selection sort]:
– From those integers that are currently unsorted, find the
smallest and place it next in the sorted list.
i [0] [1] [2] [3] [4]
- 30 10 50 40 20
0 10 30 50 40 20
1 10 20 40 50 30
2 10 20 30 40 50
3 10 20 30 40 50
1.2 Algorithm Specification (3/10)
• Correct
– always returns the desired output for all legal
instances of the problem.
• Unambiguous
• Precise
• Efficient
– Can be measured in terms of
• Time
• Space
– Time tends to be more important
Important Properties of Algorithms
• A single algorithm can be represented in
many ways:
– Formulas: F = (9/5)C + 32
– Words: Multiply the Celsius by 9/5 and add
32.
– Flow Charts.
– Pseudo-code.
• In each case, the algorithm stays the
same; the implementation differs!
Representation of Algorithms
– A program is a representation of an algorithm
designed for computer applications.
– Process: Activity of executing a program, or
execute the algorithm represented by the
program
–  Process: Activity of executing an algorithm.
Representation of Algorithms (Cont’d)
Flow Chart Symbols
Start and End
Input / output
Selection
Calculation
Data
Flow
• A Flowchart is
– A flowchart is the graphical or pictorial representation of an algorithm with the
help of different symbols, shapes, and arrows to demonstrate a process or a
program. With algorithms, we can easily understand a program. The main
purpose of using a flowchart is to analyze different methods. Several standard
symbols are applied in a flowchart:
• English description
• Pseudo-code
• High-level
programming
language
Expressing Algorithms
More
precise
More easily
expressed
• Pseudocode is like a programming
language but its rules are less stringent.
• Written as a combination of English and
programming constructs
– Based on selection (if, switch) and iteration
(while, repeat) constructs in high-level
programming languages
• Design using these high level primitives
– Independent of actual programming language
Pseudocode
Pseudocode (Cont’d)
Example: The sequential search algorithm in
pseudocode
How to write a Pseudo-code?
• Use appropriate naming conventions. The human tendency follows the approach to
follow what we see. If a programmer goes through a pseudo code, his approach will
be the same as per it, so the naming must be simple and distinct.
• Use appropriate sentence casings, such as CamelCase for methods, upper case for
constants and lower case for variables.
• Elaborate everything which is going to happen in the actual code. Don’t make the
pseudo code abstract.
• Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the way
we use it in programming.
• Check whether all the sections of a pseudo code is complete, finite and clear to
understand and comprehend.
• Don’t write the pseudo code in a complete programmatic manner. It is necessary to
be simple to understand even for a layman or client, hence don’t incorporate too
many technical terms.
Functions in Pseudocode
The syntax of a function in pseudocode is as follows:
FUNCTION function_name (parameters)
statements
END FUNCTION
FUNCTION calculate_sum (a, b)
sum = a + b
RETURN sum
END FUNCTION
INPUT a, b
sum = calculate_sum(a, b)
OUTPUT sum
• The Two Steps of Program Development:
– 1. Discover the algorithm.
– 2. Represent the algorithm as a program.
• Step 2 is the easy step!
• Step 1 can be very difficult!
• To discover an algorithm is to solve the
problem!
Algorithm Discovery
• Problem solving techniques are not unique
to Computer Science.
• The CS field has joined with other fields to
try to solve problems better.
• Ideally, there should be an algorithm to
find/develop algorithms.
• However, this is not the case as some
problems do not have algorithmic
solutions.
• Problem solving remains an art!
Problem Solving: A creative process
Categories of Algorithm:
• Based on the different types of steps in an
Algorithm, it can be divided into three
• categories, namely
•  Sequence
•  Selection and
•  Iteration
Sequence type of algorithm
• The steps described in an algorithm are performed
successively one by one without skipping any step.
• The sequence of steps defined in an algorithm should be
simple and easy to understand.
• Each instruction of such an algorithm is executed,
because no selection procedure or conditional branching
exists in a sequence algorithm.
Example:
// adding two numbers
Step 1: start
Step 2: read a,b
Step 3: Sum=a+b
Step 4: write Sum
Step 5: stop
Selection type of algorithm
• The sequence type of algorithms are not sufficient to solve the
problems, which involves decision and conditions. In order to solve
the problem which involve decision making or option selection, we
go for Selection type of algorithm.
• The general format of Selection type of statement is as shown
below:
if(condition)
Statement-1;
else
Statement-2;
Iteration
• Iteration type algorithms are used in solving the
problems which involves repetition of statement. In this
type of algorithms, a particular number of statements are
repeated ‘n’ no. of times.
Example1:
Step 1 : start
Step 2 : read n
Step 3 : repeat step 4 until n>0
Step 4 : (a) r=n mod 10
(b) s=s+r
(c) n=n/10
Step 5 : write s
Step 6 : stop
Use Flowcharts to Represent Algorithms
Example 1: Print 1 to 20:
Algorithm:
Step 1: Initialize X as 0,
Step 2: Increment X by 1,
Step 3: Print X,
Step 4: If X is less than 20 then go back
to step 2.
Flowchart:
• Example 2: Convert Temperature from
Fahrenheit ( ) to Celsius ( )
℉ ℃
Algorithm:
Step 1: Read temperature in Fahrenheit,
Step 2: Calculate temperature with formula
C=5/9*(F-32),
Step 3: Print C.
• Flowchart
Recursion
Introduction
• A programming technique in which a
function calls itself.
• One of the most effective techniques in
programming.
Triangular Numbers
• Consider the numbers 1, 3, 6, 10, 15….
• What is so peculiar about them?
• The nth
term in the series is obtained by
adding n to the previous number.
• Recursion can be used to find the nth
term.
Finding nth Term
Using Loop
int triangle(int n)
{
int total = 0;
while (n > 0)
{
total = total + n;
--n;
}
return total;
}
Using Recursion
int triangle(int n)
{
if(n == 1)
{
return 1;
}
else
return(n + triangle(n-1));
}
Our observation (between loop vs.
recursion)
• If a loop is used, the method cycles
around the loop n times, adding n to the
total the first time, n-1 the second time and
so on, down to 1, quitting the loop when n
becomes 0.
• If recursion is used, then a base case is
used that determines when the recursion
ends.
Problems defined recursively
• There are many problems whose
solution can be defined recursively
Coding the factorial function
• Recursive implementation
int Factorial(int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1);
}
Characteristics of Recursive Methods
• The recursive method calls itself to solve a smaller
problem.
• The base case is the smallest problem that the routine
solves and the value is returned to the calling method.
(Terminal condition)
• Calling a method involves certain overhead in
transferring the control to the beginning of the method
and in storing the information of the return point.
• Memory is used to store all the intermediate arguments
and return values on the internal stack.
• The most important advantage is that it simplifies the
problem conceptually.
Linear vs Non Linear Data Structures
Linear Data Strcutures Non linear Data
Structures
Basic In this structure, the elements are
arranged sequentially or linearly and
attached to one another.
In this structure, the
elements are arranged
hierarchically or non-linear
manner.
Types Arrays, linked list, stack, queue are
the types of a linear data structure.
Trees and graphs are the
types of a non-linear data
structure.
implementation Due to the linear organization, they
are easy to implement.
Due to the non-linear
organization, they are
difficult to implement.
Traversal As linear data structure is a single
level, so it requires a single run to
traverse each data item.
The data items in a non-
linear data structure
cannot be accessed in a
single run. It requires
multiple runs to be
traversed.
Linear vs Non Linear Data Structures
Linear Data Strcutures Non linear Data Structures
Arrangement Each data item is attached to the
previous and next items.
Each item is attached to many
other items.
Levels This data structure does not
contain any hierarchy, and all the
data elements are organized in a
single level.
In this, the data elements are
arranged in multiple levels.
Memory
utilization
In this, the memory utilization is
not efficient.
In this, memory is utilized in a
very efficient manner.
Time
complexity
The time complexity of linear
data structure increases with the
increase in the input size.
The time complexity of non-linear
data structure often remains
same with the increase in the
input size.
Applications Linear data structures are mainly
used for developing the software.
Non-linear data structures are
used in image processing and
Artificial Intelligence.
Ephemeral: A modification destroys the version which we
modify.
Persistent: Modifications are nondestructive. Each
modification creates a new version. All version coexist.
We have a big data structure that represent all versions
Partially persistent: Can access any version, but can modify only the most
recent one.
V1
V2
V3
V4
V5
fully persistent: Can access and modify any version any number of times .
V1
V2
V3
V4
V5
V5
confluently persistent: fully persistent and there is an operation that
combines two or more versions to one new version.
V1
V2
V3
V4
V5
V5
Purely functional: You are not allowed to change a field in a node after it is
initialized.
This is everything you can do in a pure functional programming language
Analysis Of Algorithm
• Space Complexity: The space complexity
of a program is the amount of memory it
needs to run to completion.
• Time Complexity: The time complexity of a
program is the amount of computer time it
needs to run to completion.
C. –C. Yao
Performance Analysis
C. –C. Yao
Time Complexity in C++
Space Complexity
S(P)=C+SP(I)
Fixed Space Requirements (C)
Independent of the characteristics of the inputs
and outputs
instruction space
space for simple variables, fixed-size structured
variable, constants
Variable Space Requirements (SP(I))
depend on the instance characteristic I
number, size, values of inputs and outputs associated
with I
recursive stack space, formal parameters, local
variables, return address
CHAPTER 1
– Example 1.8: Program 1.11 is a recursive
function for addition. Figure 1.1 shows the
number of bytes required for one recursive call.
1.4 Performance analysis (4/17)
Ssum(I)=Ssum(n)=6n
• 1.4.2 Time Complexity:
T(P)=C+TP(I)
– The time, T(P), taken by a program, P, is the
sum of its compile time C and its run (or
execution) time, TP(I)
– Fixed time requirements
• Compile time (C), independent of instance
characteristics
– Variable time requirements
• Run (execution) time TP
• TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
1.4 Performance analysis (5/17)
• A program step is a syntactically or
semantically meaningful program segment
whose execution time is independent of the
instance characteristics.
– Example
(Regard as the same unit machine independent)
• abc = a + b + b * c + (a + b - c) / (a + b) + 4.0
• abc = a + b + c
• Methods to compute the step count
– Introduce variable count into programs
– Tabular method
• Determine the total number of steps contributed by
each statement step per execution  frequency
• add up the contribution of all statements
1.4 Performance analysis (6/17)
1.4 Performance analysis (7/17)
2n + 3 steps
• Tabular Method
• *Figure 1.2: Step count table for Program 1.10 (p.26)
1.4 Performance analysis (8/17)
Statement s/e Frequency Total steps
float sum(float list[ ], int n)
{
float tempsum = 0;
int i;
for(i=0; i <n; i++)
tempsum += list[i];
return tempsum;
}
0 0 0
0 0 0
1 1 1
0 0 0
1 n+1 n+1
1 n n
1 1 1
0 0 0
Total 2n+3
steps/execution
Iterative function to sum a list of numbers
1.4 Performance analysis (9/17)
2n+2 steps
• *Figure 1.3: Step count table for recursive summing function
(p.27)
1.4 Performance analysis (10/17)
Statement s/e Frequency Total steps
float rsum(float list[ ], int n)
{
if (n)
return rsum(list, n-1)+list[n-1];
return list[0];
}
0 0 0
0 0 0
1 n+1 n+1
1 n n
1 1 1
0 0 0
Total 2n+2
• THREE types of Asymptotic Notations and
those are as follows...
• Big - Oh (O)
• Big - Omega (Ω)
• Big - Theta (Θ)
Asymptotic Notation
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n)
<= C x g(n) for all values of C > 0 and n0
>= 1
f(n) <= C g(n)
3n + 2 <= C n
⇒
Above condition is always TRUE for all values of C = 4 and n
>= 2.
By using Big - Oh notation we can represent the time
complexity as follows...
3n + 2 = O(n)
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n)
>= C g(n) for all values of C > 0 and n0
>= 1
f(n) >= C g(n)
3n + 2 >= C n
⇒
Above condition is always TRUE for all values of C = 1 and n
>= 1.
By using Big - Omega notation we can represent the time
complexity as follows...
3n + 2 = Ω(n)
<=
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy C1
g(n) <=
f(n) <= C2
g(n) for all values of C1
, C2
> 0 and n0
>= 1
C1
g(n) <= f(n) <= C
⇒ 2
g(n)
C1
n <= 3n + 2 <= C2
n
Above condition is always TRUE for all values of C1
= 1, C2
= 4 and n
>= 1.
By using Big - Theta notation we can represent the time compexity as
follows...
3n + 2 = Θ(n)
Programming Style
• Clarity and simplicity of Expression
• Naming
• Control Constructs
• Information hiding
• Nesting
• User-defined types
• Module size
• Module Interface
• Side-effects
Refinement of Coding
• Stepwise refinement of Coding: design a
problem solution by
– stating the solution at a high level
– refining steps of the solution into simpler steps
– repeating step 2, until steps are simple enough to
execute
• Decompose based on function of each step
• Makes heavy use of pseudocode
• Example:
• Problem: Print the count and average of a sequence of positive integers
• Initial Solution:
– Initialize data
– Get data
– Calculate results
– Output results
• Refinement:
– Initialize data
– get data
• 2.1 loop
• 2.2 get integer x
• 2.3 exit when x < 1
• 2.4 process x
– Calculate results
• 3.1 avg = sum / count
– Output results
• Further refinement (of step 2.4):
– 2.4.1 increase count by 1
– 2.4.2 increase sum by x
• Process of refinement is complete when all steps can be easily programmed
Testing
• Software testing can be divided into two steps:
1. Verification: it refers to the set of tasks that ensure
that software correctly implements a specific function.
• 2. Validation: it refers to a different set of tasks that
ensure that the software that has been built is traceable
to customer requirements.
• Verification: “Are we building the product right?”
Validation: “Are we building the right product?”
Types of software testing
1. Manual Testing: Manual testing includes testing a software
manually, i.e., without using any automated tool or any
script. In this type, the tester takes over the role of an end-
user and tests the software to identify any unexpected
behaviour or bug. Testers use test plans, test cases, or test
scenarios to test a software to ensure the completeness of
testing.
2. Automation Testing: Automation testing, which is also
known as Test Automation, is when the tester writes scripts
and uses another software to test the product. This process
involves automation of a manual process. Automation
Testing is used to re-run the test scenarios that were
performed manually, quickly, and repeatedly.
Data Abstraction
• The concept of representing important
details and hiding away the
implementation details is called data
abstraction.
• An abstract data type or ADT is a
collection of data and a set of
operations on the data that does not
specify how the data is stored or how
the operations accomplish their functions.
• Let us see some operations of Stack ADT −
• isFull(), This is used to check whether stack is full or
not
• isEmpty(), This is used to check whether stack is
empty or not
• push(x), This is used to push x into the stack
• pop(), This is used to delete one element from top of
the stack
• peek(), This is used to get the top most element of
the stack
• size(), this function is used to get number of
elements present into the stack
Difference Between Static and Dynamic Memory Allocation in C
Sr.
no.
Static Memory Allocation Dynamic Memory Allocation
1 In the static memory allocation,
variables get allocated permanently,
till the program executes or function
call finishes.
In the Dynamic memory allocation, the
memory is controlled by the
programmer. It gets allocated whenever
a malloc() is executed gets deallocated
wherever the free() is executed.
2 Static Memory Allocation is done
before program execution.
Dynamic Memory Allocation is done
during program execution.
3 It uses stack for managing the static
allocation of memory
It uses heap (not heap data structure)
of memory for managing the dynamic
allocation of memory
4 It is less efficient It is more efficient
5 In Static Memory Allocation, there is
no memory re-usability
In Dynamic Memory Allocation, there is
memory re-usability and memory can
be freed when not required
6 In static memory allocation, once
the memory is allocated, the
memory size can not change.
In dynamic memory allocation, when
memory is allocated the memory size
can be changed.
Difference Between Static and Dynamic Memory Allocation in C
Sr.
no.
Static Memory Allocation Dynamic Memory Allocation
7 In this memory allocation scheme,
we cannot reuse the unused
memory.
This allows reusing the memory. The
user can allocate more memory when
required. Also, the user can release the
memory when the user needs it.
8 In this memory allocation scheme,
execution is faster than dynamic
memory allocation.
In this memory allocation scheme,
execution is slower than static memory
allocation.
9 In this memory is allocated at
compile time.
In this memory is allocated at run time.
10 In this allocated memory remains
from start to end of the program.
In this allocated memory can be
released at any time during the program.
11 Example: This static memory
allocation is generally used for
array.
Example: This dynamic memory
allocation is generally used for linked
list.

Data Structures_Introduction to algorithms.pptx

  • 1.
    Course Contents UNIT-I Introductionto Algorithm and Analysis of Algorithms 6 Hours Concept of Problem Solving, Introduction to Algorithms, Characteristics of Algorithms, Introduction to Data Structure, Data Structure Classification (Linear and Non-linear, Static and Dynamic, Persistent and Ephemeral data structures), Time complexity and Space complexity, Asymptotic Notation - The Big-O, Omega and Theta notation, Algorithmic upper bounds, lower bounds, Best, Worst and Average case analysis of an Algorithm, Abstract Data Types (ADT).
  • 2.
    • Programming isa process of problem solving • Problem solving techniques – Analyze the problem – Outline the problem requirements – Design steps (algorithm) to solve the problem • Algorithm: – Step-by-step problem-solving process – Solution achieved in finite amount of time Problem Solving
  • 4.
    • Step 1- Analyze the problem – Outline the problem and its requirements – Design steps (algorithm) to solve the problem • Step 2 - Implement the algorithm – Implement the algorithm in code – Verify that the algorithm works • Step 3 - Maintenance – Use and modify the program if the problem domain changes Problem Solving Process
  • 5.
    • Thoroughly understandthe problem • Understand problem requirements – Does program require user interaction? – Does program manipulate data? – What is the output? • If the problem is complex, divide it into subproblems – Analyze each subproblem as above Analyze the Problem
  • 6.
    • The ideabehind the computer program • Stays the same independent of – Which kind of hardware it is running on – Which programming language it is written in • Solves a well-specified problem in a general way • Is specified by – Describing the set of instances (input) it must work on – Describing the desired properties of the output What is an algorithm?
  • 7.
    • Before acomputer can perform a task, it must have an algorithm that tells it what to do. • Informally: “An algorithm is a set of steps that define how a task is performed.” • Formally: “An algorithm is an ordered set of unambiguous executable steps, defining a terminating process.” – Ordered set of steps: structure! – Executable steps: doable! – Unambiguous steps: follow the directions! – Terminating: must have an end! What is an algorithm? (Cont’d)
  • 8.
    Basic Concepts • Overview:System Life Cycle • Algorithm Specification • Data Abstraction • Performance Analysis • Performance Measurement
  • 9.
    1.1 Overview: systemlife cycle (1/2) • Good programmers regard large-scale computer programs as systems that contain many complex interacting parts. • As systems, these programs undergo a development process called the system life cycle.
  • 10.
    1.1 Overview (2/2) •We consider this cycle as consisting of five phases. – Requirements – Analysis: bottom-up vs. top-down – Design: data objects and operations – Refinement and Coding – Verification • Program Proving • Testing • Debugging
  • 11.
    1.2 Algorithm Specification(1/10) • 1.2.1 Introduction – An algorithm is a finite set of instructions that accomplishes a particular task. – Criteria • input: zero or more quantities that are externally supplied • output: at least one quantity is produced • definiteness: clear and unambiguous • finiteness: terminate after a finite number of steps • effectiveness: instruction is basic enough to be carried out – A program does not have to satisfy the finiteness criteria.
  • 12.
    1.2 Algorithm Specification(2/10) • Representation – A natural language, like English or Chinese. – A graphic, like flowcharts. – A computer language, like C. • Algorithms + Data structures = Programs [Niklus Wirth] • Sequential search vs. Binary search
  • 13.
    • Example 1.1[Selection sort]: – From those integers that are currently unsorted, find the smallest and place it next in the sorted list. i [0] [1] [2] [3] [4] - 30 10 50 40 20 0 10 30 50 40 20 1 10 20 40 50 30 2 10 20 30 40 50 3 10 20 30 40 50 1.2 Algorithm Specification (3/10)
  • 14.
    • Correct – alwaysreturns the desired output for all legal instances of the problem. • Unambiguous • Precise • Efficient – Can be measured in terms of • Time • Space – Time tends to be more important Important Properties of Algorithms
  • 15.
    • A singlealgorithm can be represented in many ways: – Formulas: F = (9/5)C + 32 – Words: Multiply the Celsius by 9/5 and add 32. – Flow Charts. – Pseudo-code. • In each case, the algorithm stays the same; the implementation differs! Representation of Algorithms
  • 16.
    – A programis a representation of an algorithm designed for computer applications. – Process: Activity of executing a program, or execute the algorithm represented by the program –  Process: Activity of executing an algorithm. Representation of Algorithms (Cont’d)
  • 17.
    Flow Chart Symbols Startand End Input / output Selection Calculation Data Flow • A Flowchart is – A flowchart is the graphical or pictorial representation of an algorithm with the help of different symbols, shapes, and arrows to demonstrate a process or a program. With algorithms, we can easily understand a program. The main purpose of using a flowchart is to analyze different methods. Several standard symbols are applied in a flowchart:
  • 18.
    • English description •Pseudo-code • High-level programming language Expressing Algorithms More precise More easily expressed
  • 19.
    • Pseudocode islike a programming language but its rules are less stringent. • Written as a combination of English and programming constructs – Based on selection (if, switch) and iteration (while, repeat) constructs in high-level programming languages • Design using these high level primitives – Independent of actual programming language Pseudocode
  • 20.
    Pseudocode (Cont’d) Example: Thesequential search algorithm in pseudocode
  • 21.
    How to writea Pseudo-code? • Use appropriate naming conventions. The human tendency follows the approach to follow what we see. If a programmer goes through a pseudo code, his approach will be the same as per it, so the naming must be simple and distinct. • Use appropriate sentence casings, such as CamelCase for methods, upper case for constants and lower case for variables. • Elaborate everything which is going to happen in the actual code. Don’t make the pseudo code abstract. • Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the way we use it in programming. • Check whether all the sections of a pseudo code is complete, finite and clear to understand and comprehend. • Don’t write the pseudo code in a complete programmatic manner. It is necessary to be simple to understand even for a layman or client, hence don’t incorporate too many technical terms.
  • 22.
    Functions in Pseudocode Thesyntax of a function in pseudocode is as follows: FUNCTION function_name (parameters) statements END FUNCTION FUNCTION calculate_sum (a, b) sum = a + b RETURN sum END FUNCTION INPUT a, b sum = calculate_sum(a, b) OUTPUT sum
  • 23.
    • The TwoSteps of Program Development: – 1. Discover the algorithm. – 2. Represent the algorithm as a program. • Step 2 is the easy step! • Step 1 can be very difficult! • To discover an algorithm is to solve the problem! Algorithm Discovery
  • 24.
    • Problem solvingtechniques are not unique to Computer Science. • The CS field has joined with other fields to try to solve problems better. • Ideally, there should be an algorithm to find/develop algorithms. • However, this is not the case as some problems do not have algorithmic solutions. • Problem solving remains an art! Problem Solving: A creative process
  • 28.
    Categories of Algorithm: •Based on the different types of steps in an Algorithm, it can be divided into three • categories, namely •  Sequence •  Selection and •  Iteration
  • 29.
    Sequence type ofalgorithm • The steps described in an algorithm are performed successively one by one without skipping any step. • The sequence of steps defined in an algorithm should be simple and easy to understand. • Each instruction of such an algorithm is executed, because no selection procedure or conditional branching exists in a sequence algorithm. Example: // adding two numbers Step 1: start Step 2: read a,b Step 3: Sum=a+b Step 4: write Sum Step 5: stop
  • 30.
    Selection type ofalgorithm • The sequence type of algorithms are not sufficient to solve the problems, which involves decision and conditions. In order to solve the problem which involve decision making or option selection, we go for Selection type of algorithm. • The general format of Selection type of statement is as shown below: if(condition) Statement-1; else Statement-2;
  • 31.
    Iteration • Iteration typealgorithms are used in solving the problems which involves repetition of statement. In this type of algorithms, a particular number of statements are repeated ‘n’ no. of times. Example1: Step 1 : start Step 2 : read n Step 3 : repeat step 4 until n>0 Step 4 : (a) r=n mod 10 (b) s=s+r (c) n=n/10 Step 5 : write s Step 6 : stop
  • 32.
    Use Flowcharts toRepresent Algorithms Example 1: Print 1 to 20: Algorithm: Step 1: Initialize X as 0, Step 2: Increment X by 1, Step 3: Print X, Step 4: If X is less than 20 then go back to step 2. Flowchart:
  • 33.
    • Example 2:Convert Temperature from Fahrenheit ( ) to Celsius ( ) ℉ ℃
  • 34.
    Algorithm: Step 1: Readtemperature in Fahrenheit, Step 2: Calculate temperature with formula C=5/9*(F-32), Step 3: Print C.
  • 35.
  • 36.
  • 37.
    Introduction • A programmingtechnique in which a function calls itself. • One of the most effective techniques in programming.
  • 38.
    Triangular Numbers • Considerthe numbers 1, 3, 6, 10, 15…. • What is so peculiar about them? • The nth term in the series is obtained by adding n to the previous number. • Recursion can be used to find the nth term.
  • 39.
    Finding nth Term UsingLoop int triangle(int n) { int total = 0; while (n > 0) { total = total + n; --n; } return total; } Using Recursion int triangle(int n) { if(n == 1) { return 1; } else return(n + triangle(n-1)); }
  • 40.
    Our observation (betweenloop vs. recursion) • If a loop is used, the method cycles around the loop n times, adding n to the total the first time, n-1 the second time and so on, down to 1, quitting the loop when n becomes 0. • If recursion is used, then a base case is used that determines when the recursion ends.
  • 41.
    Problems defined recursively •There are many problems whose solution can be defined recursively
  • 42.
    Coding the factorialfunction • Recursive implementation int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }
  • 44.
    Characteristics of RecursiveMethods • The recursive method calls itself to solve a smaller problem. • The base case is the smallest problem that the routine solves and the value is returned to the calling method. (Terminal condition) • Calling a method involves certain overhead in transferring the control to the beginning of the method and in storing the information of the return point. • Memory is used to store all the intermediate arguments and return values on the internal stack. • The most important advantage is that it simplifies the problem conceptually.
  • 52.
    Linear vs NonLinear Data Structures Linear Data Strcutures Non linear Data Structures Basic In this structure, the elements are arranged sequentially or linearly and attached to one another. In this structure, the elements are arranged hierarchically or non-linear manner. Types Arrays, linked list, stack, queue are the types of a linear data structure. Trees and graphs are the types of a non-linear data structure. implementation Due to the linear organization, they are easy to implement. Due to the non-linear organization, they are difficult to implement. Traversal As linear data structure is a single level, so it requires a single run to traverse each data item. The data items in a non- linear data structure cannot be accessed in a single run. It requires multiple runs to be traversed.
  • 53.
    Linear vs NonLinear Data Structures Linear Data Strcutures Non linear Data Structures Arrangement Each data item is attached to the previous and next items. Each item is attached to many other items. Levels This data structure does not contain any hierarchy, and all the data elements are organized in a single level. In this, the data elements are arranged in multiple levels. Memory utilization In this, the memory utilization is not efficient. In this, memory is utilized in a very efficient manner. Time complexity The time complexity of linear data structure increases with the increase in the input size. The time complexity of non-linear data structure often remains same with the increase in the input size. Applications Linear data structures are mainly used for developing the software. Non-linear data structures are used in image processing and Artificial Intelligence.
  • 57.
    Ephemeral: A modificationdestroys the version which we modify. Persistent: Modifications are nondestructive. Each modification creates a new version. All version coexist. We have a big data structure that represent all versions
  • 58.
    Partially persistent: Canaccess any version, but can modify only the most recent one. V1 V2 V3 V4 V5
  • 59.
    fully persistent: Canaccess and modify any version any number of times . V1 V2 V3 V4 V5 V5
  • 60.
    confluently persistent: fullypersistent and there is an operation that combines two or more versions to one new version. V1 V2 V3 V4 V5 V5
  • 61.
    Purely functional: Youare not allowed to change a field in a node after it is initialized. This is everything you can do in a pure functional programming language
  • 62.
  • 63.
    • Space Complexity:The space complexity of a program is the amount of memory it needs to run to completion. • Time Complexity: The time complexity of a program is the amount of computer time it needs to run to completion. C. –C. Yao Performance Analysis
  • 64.
    C. –C. Yao TimeComplexity in C++
  • 65.
    Space Complexity S(P)=C+SP(I) Fixed SpaceRequirements (C) Independent of the characteristics of the inputs and outputs instruction space space for simple variables, fixed-size structured variable, constants Variable Space Requirements (SP(I)) depend on the instance characteristic I number, size, values of inputs and outputs associated with I recursive stack space, formal parameters, local variables, return address CHAPTER 1
  • 66.
    – Example 1.8:Program 1.11 is a recursive function for addition. Figure 1.1 shows the number of bytes required for one recursive call. 1.4 Performance analysis (4/17) Ssum(I)=Ssum(n)=6n
  • 67.
    • 1.4.2 TimeComplexity: T(P)=C+TP(I) – The time, T(P), taken by a program, P, is the sum of its compile time C and its run (or execution) time, TP(I) – Fixed time requirements • Compile time (C), independent of instance characteristics – Variable time requirements • Run (execution) time TP • TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n) 1.4 Performance analysis (5/17)
  • 68.
    • A programstep is a syntactically or semantically meaningful program segment whose execution time is independent of the instance characteristics. – Example (Regard as the same unit machine independent) • abc = a + b + b * c + (a + b - c) / (a + b) + 4.0 • abc = a + b + c • Methods to compute the step count – Introduce variable count into programs – Tabular method • Determine the total number of steps contributed by each statement step per execution  frequency • add up the contribution of all statements 1.4 Performance analysis (6/17)
  • 69.
    1.4 Performance analysis(7/17) 2n + 3 steps
  • 70.
    • Tabular Method •*Figure 1.2: Step count table for Program 1.10 (p.26) 1.4 Performance analysis (8/17) Statement s/e Frequency Total steps float sum(float list[ ], int n) { float tempsum = 0; int i; for(i=0; i <n; i++) tempsum += list[i]; return tempsum; } 0 0 0 0 0 0 1 1 1 0 0 0 1 n+1 n+1 1 n n 1 1 1 0 0 0 Total 2n+3 steps/execution Iterative function to sum a list of numbers
  • 71.
    1.4 Performance analysis(9/17) 2n+2 steps
  • 72.
    • *Figure 1.3:Step count table for recursive summing function (p.27) 1.4 Performance analysis (10/17) Statement s/e Frequency Total steps float rsum(float list[ ], int n) { if (n) return rsum(list, n-1)+list[n-1]; return list[0]; } 0 0 0 0 0 0 1 n+1 n+1 1 n n 1 1 1 0 0 0 Total 2n+2
  • 73.
    • THREE typesof Asymptotic Notations and those are as follows... • Big - Oh (O) • Big - Omega (Ω) • Big - Theta (Θ) Asymptotic Notation
  • 75.
    Example Consider the followingf(n) and g(n)... f(n) = 3n + 2 g(n) = n If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C x g(n) for all values of C > 0 and n0 >= 1 f(n) <= C g(n) 3n + 2 <= C n ⇒ Above condition is always TRUE for all values of C = 4 and n >= 2. By using Big - Oh notation we can represent the time complexity as follows... 3n + 2 = O(n)
  • 77.
    Example Consider the followingf(n) and g(n)... f(n) = 3n + 2 g(n) = n If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C g(n) for all values of C > 0 and n0 >= 1 f(n) >= C g(n) 3n + 2 >= C n ⇒ Above condition is always TRUE for all values of C = 1 and n >= 1. By using Big - Omega notation we can represent the time complexity as follows... 3n + 2 = Ω(n)
  • 78.
  • 79.
    Example Consider the followingf(n) and g(n)... f(n) = 3n + 2 g(n) = n If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n) <= C2 g(n) for all values of C1 , C2 > 0 and n0 >= 1 C1 g(n) <= f(n) <= C ⇒ 2 g(n) C1 n <= 3n + 2 <= C2 n Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 1. By using Big - Theta notation we can represent the time compexity as follows... 3n + 2 = Θ(n)
  • 80.
    Programming Style • Clarityand simplicity of Expression • Naming • Control Constructs • Information hiding • Nesting • User-defined types • Module size • Module Interface • Side-effects
  • 81.
    Refinement of Coding •Stepwise refinement of Coding: design a problem solution by – stating the solution at a high level – refining steps of the solution into simpler steps – repeating step 2, until steps are simple enough to execute • Decompose based on function of each step • Makes heavy use of pseudocode
  • 82.
    • Example: • Problem:Print the count and average of a sequence of positive integers • Initial Solution: – Initialize data – Get data – Calculate results – Output results • Refinement: – Initialize data – get data • 2.1 loop • 2.2 get integer x • 2.3 exit when x < 1 • 2.4 process x – Calculate results • 3.1 avg = sum / count – Output results • Further refinement (of step 2.4): – 2.4.1 increase count by 1 – 2.4.2 increase sum by x • Process of refinement is complete when all steps can be easily programmed
  • 83.
    Testing • Software testingcan be divided into two steps: 1. Verification: it refers to the set of tasks that ensure that software correctly implements a specific function. • 2. Validation: it refers to a different set of tasks that ensure that the software that has been built is traceable to customer requirements. • Verification: “Are we building the product right?” Validation: “Are we building the right product?”
  • 84.
    Types of softwaretesting 1. Manual Testing: Manual testing includes testing a software manually, i.e., without using any automated tool or any script. In this type, the tester takes over the role of an end- user and tests the software to identify any unexpected behaviour or bug. Testers use test plans, test cases, or test scenarios to test a software to ensure the completeness of testing. 2. Automation Testing: Automation testing, which is also known as Test Automation, is when the tester writes scripts and uses another software to test the product. This process involves automation of a manual process. Automation Testing is used to re-run the test scenarios that were performed manually, quickly, and repeatedly.
  • 85.
    Data Abstraction • Theconcept of representing important details and hiding away the implementation details is called data abstraction. • An abstract data type or ADT is a collection of data and a set of operations on the data that does not specify how the data is stored or how the operations accomplish their functions.
  • 88.
    • Let ussee some operations of Stack ADT − • isFull(), This is used to check whether stack is full or not • isEmpty(), This is used to check whether stack is empty or not • push(x), This is used to push x into the stack • pop(), This is used to delete one element from top of the stack • peek(), This is used to get the top most element of the stack • size(), this function is used to get number of elements present into the stack
  • 90.
    Difference Between Staticand Dynamic Memory Allocation in C Sr. no. Static Memory Allocation Dynamic Memory Allocation 1 In the static memory allocation, variables get allocated permanently, till the program executes or function call finishes. In the Dynamic memory allocation, the memory is controlled by the programmer. It gets allocated whenever a malloc() is executed gets deallocated wherever the free() is executed. 2 Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution. 3 It uses stack for managing the static allocation of memory It uses heap (not heap data structure) of memory for managing the dynamic allocation of memory 4 It is less efficient It is more efficient 5 In Static Memory Allocation, there is no memory re-usability In Dynamic Memory Allocation, there is memory re-usability and memory can be freed when not required 6 In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed.
  • 91.
    Difference Between Staticand Dynamic Memory Allocation in C Sr. no. Static Memory Allocation Dynamic Memory Allocation 7 In this memory allocation scheme, we cannot reuse the unused memory. This allows reusing the memory. The user can allocate more memory when required. Also, the user can release the memory when the user needs it. 8 In this memory allocation scheme, execution is faster than dynamic memory allocation. In this memory allocation scheme, execution is slower than static memory allocation. 9 In this memory is allocated at compile time. In this memory is allocated at run time. 10 In this allocated memory remains from start to end of the program. In this allocated memory can be released at any time during the program. 11 Example: This static memory allocation is generally used for array. Example: This dynamic memory allocation is generally used for linked list.