SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Problem solving aspects
Algorithms
PROGRAM VERIFICATION
• The application of mathematical proof techniques to establish that the results
obtained by the execution of a program with arbitrary inputs are in accord with
formally defined output specifications.
1. Computer model for program execution.
2. Input assertion specify any constraints that have been placed on the values of the input variables
used by the program.
3. Output assertion specify symbolically the results that the program is expected to produce for input
data that satisfies the input assertion.
4. Implications and symbolic execution.
5. Verification of straight-line program segments.
6. Verification of program segments with branches / loops / arrays.
7. Proof of termination.
2
THE EFFICIENCY OF ALGORITHMS
• Design algorithms that are economical in the use of CPU time and memory because of
high cost of computing resources.
• Suggestions that are useful in designing efficient algorithms.
1. Redundant computations.
2. Referencing array elements.
3. Inefficiency due to late termination.
4. Early detection of desired output conditions.
5. Trading storage for efficiency gains.
3
Analysis of algorithms
• Good algorithm possess the following qualities and capabilities
FUNDAMENTAL ALGORITHMS
EXCHANGING THE VALUES OF TWO VARIABLES
Problem Statement:
Given two variables a and b, interchange the values of the variables.
5
• Algorithm development
The problem of interchanging the data associated with two variables involves a
very fundamental mechanism that occurs in many sorting and data manipulation
algorithm.
Ex: a b
4 5
Here a has 4 and b has 5. our aim is to replace the variable of a with 4 and b with 5.
• So the target solution may look as
a b
5 4
• Here we can make use of the assignment operator a = b; b = a;
• where the b value is assigned to a. the value has been changed, and the changed
a value has been assigned to b.
a = 5, b = 5
• So we make use of the temporary variable.
t = a;
a = b;
b = t;
• Now,
t = 4, a = 5, b = 4
• Our target solution has been achieved.
Algorithm Description:
• 1. Save the original value of a in t.
• 2. Assign to a the original value of b.
• 3. Assign to b the original value of a that is stored in t.
APPLICATION
• This kind of swapping technique is applicable for all kinds of sorting algorithm.
Notes on design
1. The use of an intermediate temporary variable allows the exchange of two variables to proceed
correctly.
2. This example emphasized that at any stage in a computation a variable always assumes the
value dictated by the most recent assignment made to that variable
3. Working through the mechanism with a particular example can be a useful way of detecting
design faults
4. A more common application of exchange involves two array elements (eg. a[i] and a[j]). The
steps for such an exchange are:
t:= a[i];
a[i] := a[j];
a[j] := t
COUNTING
Problem Statement:
Given a set of n students examination marks (in the range 0 to 100), make a count of
number of students passed the examination. A pass is awarded for all marks of 50 and
above.
8
• Algorithm development
Count must be made of number of items in a set which possess some particular property
or which satisfy some particular condition or conditions. This class of problems is by
“examination marks”.
As a starting point for developing a computer algorithm for this problem we can consider
solving by hand.
Suppose that the set of marks are
55,42,77,63,29,57,89
Check is first mark >=50, so one student has passed so far. The second mark is then
examined and no change to the count. The process continues until all marks are
examined.
• In more detail:
Therefore, number of students passed = 5
Order in which
marks are
examined
Marks Counting details for passes
55 Previous count = 0 Current count = 1
42 Previous count = 1 Current count = 1
77 Previous count = 1 Current count = 2
63 Previous count = 2 Current count = 3
29 Previous count = 3 Current count = 3
57 Previous count = 3 Current count = 4
89 Previous count = 4 Current count = 5
Algorithm description:
1. Prompt and read the number of marks to be processed.
2. Initialize count to zero.
3. While there are still marks to be processed repeatedly do
a. Read next mark.
b. If it is a pass then add one to count.
4. Print total number of passed students.
Notes on design
1. Initially, and each time through the loop, the variable count represents the number of passes so far
encountered. On the termination (when i=n) count represents the total number of passes in the set.
Because I is incremented by 1 with each iteration, eventually the condition i<n will be violated and the
loop will terminate.
2. It was possible to use substitution to improve on the original solution to the problem. The simplest and
most efficient solution to a problem is usually the best all-round solution.
3. An end-of-line test is included to handle multiple lines of data.
Applications
All forms of counting
10
COUNTING
SUMMATION OF A SET OF N NUMBERS
Problem Statement:
Given a set of n numbers, design an algorithm that adds these numbers and returns the resultant sum.
Assume n is >=0.
11
Algorithm development
⁻Consider the addition of 421, 583 and 714
⁻In designing an algorithm to add a set of numbers a primary concern is the mechanism for the
addition process.
⁻Simplest way
s := a+b+c
⁻Make general enough that will successfully handle a wide variety of input conditions.
s = (a1+a2+a3+a4+…+an) which is equivalent to
s= 𝑖=1
𝑛
𝑎𝑖
Steps followed by a set of n iterative steps
1. Compute first sum (s=0) as special case
2. Build each of the n remaining sums from its predecessor by an iterative process
3. Write out the sum of n numbers
ALGORITHM:
1. Prompt and read total numbers to sum.
2. Initialize sum for zero numbers.
3. While less than n numbers have been summed repeatedly do
a. Read next mark.
b. Compute current sum by addin
4. Print total number of passed students.
Applications:
Average calculations, variance and least square calculations.
FACTORIAL COMPUTATION
Problem Statement:
Given a number n, compute n factorial (written as n!) where n>=0.
13
• Algorithm development
n! can be done as n!= 1x2x3x…..x(n-1)xn for n>=1
as 0! =1
We can generalize this by observing that n! can always be obtained from (n-1)! by simply
multiplying it by n (for n>=1)
n! = nx(n-1)! For n>=1
Using this:
1! = 1x0!
2!= 2x1!
3!= 3x2!
.
.
.
ALGORITHM:
1. Establish n, the factorial required where n>=0.
2. Set product p for 0! Also set product count to zero.
3. While less than n products have been calculated repeatedly do,
a. Increment product count.
b. Compute the ith product p by multiplying I by the most recent product.
4. Return the result n!.
Applications:
Probability, Statistical and mathematical computations.
14
SINE FUNCTION COMPUTATION
Problem Statement:
Design an algorithm to evaluate the function sin(x) as defined by the infinite series expansion
sin 𝑥 =
𝑥
1!
−
𝑥3
3!
+
𝑥5
5!
−
𝑥7
7!
15
Algorithm development
• The function we need to compute will involve the following steps:
fp := 1;
j := 0;
While j<i do
begin
j := j+1;
fp := fp*x/j
end
• This algorithm can be completed by implementing the addition and subtractions and making the appropriate termination.
• To generate consecutive terms of the sine series we can use
𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑖𝑡ℎ 𝑡𝑒𝑟𝑚 =
𝑥2
𝑖(𝑖 − 1)
𝑋 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑡𝑒𝑟𝑚
• To get alternate terms, sign := -sign
• Will generate alternate positive and negative terms
• The initial conditions are
i := i+2;
term := -term*x*x/(i*(i-1));
tsin = tsin + term
- How to terminate the algorithm
- Fix an acceptable error level and generate successive terms until the contribution of the current term is less than the acceptable error
ALGORITHM:
1. Set up initial conditions for the first term that cannot be computed iteratively.
2. While the absolute value of current term is greater than the acceptable error do
a. Identify the current ith term.
b. generate current term from its predecessor.
c. add current term with the appropriate sign to the accumulated sum for the sine function.
Applications:
Mathematical and Statistical computations.
17
REVERSING THE DIGITS OF AN INTEGER
Problem Statement:
Design an algorithm that accepts a positive integer and reverses the order of its digits.
18
Algorithm development
Digit reversal is a technique used to remove bias from a set of numbers. Eg. Input:
27953
Output: 35972
If we apply the following two steps
r := n mod 10
n := n div 10
The central steps in algorithm are:
1. While there are still digits in the number being reversed do
a) Extract the rightmost digit from the number being reversed and append the digit to the
right-hand end of the current reversed number representation;
b) Remove the rightmost digit from the number being reversed
ALGORITHM:
1. Establish n, the positive integer to be reversed.
2. Set the initial condition for the reversed integer dreverse.
3. While the integer being reversed is greater than zero do,
a. Use the remainder function to extract the rightmost digit of the number being
reversed.
b. Increase the previous reversed integer representation dreverse by a factor of 10
and add to it the most recently extracted digit to give the current dreverse value.
c. Use integer division by 10 to remove the rightmost digit from the number being
reversed.
dreverse := (previous value of dreverse)*10 + (most recently extracted rightmost digit)
Applications:
Hashing and information retrieval, database applications.
19
BASE CONVERSION
Problem Statement:
Convert a decimal integer to its corresponding octal representation.
20
Example:
The octal representation of decimal 275 is 423.
ALOGORITHM:
1. Establish the newbase and initialize the quotient q to the decimal number to be converted.
2. Set the new digit count ndigit to zero.
3. Repeatedly,
A. Compute the next most significant digit i.e octal from the current quotient q as the remainder
r after division by newbase.
B. Convert r to appropriate ascii value.
C. increment new digit count ndigit and store r in output array newrep.
D. Compute the next quotient q from its predecessor using integer division by newbase until the
quotient is zero.
Applications:
Interpretation of stored computer data and instructions.
21
CHARACTER TO NUMBER CONVERSION
Problem Statement:
Given the character representation of an integer convert it to its conventional decimal
format.
22
Example:
Character representation ‘125’ when converted to decimal format results in
125.
Algorithm:
1. Establish the character string for conversion to decimal and its length n.
2. Initialize decimal value to zero.
3. Set base0 value to the ascii or ordinal value of ‘0’.
4. While less than n characters have been examined do
a. Convert next character to corresponding decimal digit.
b. Shift current decimal value to the left one digit and add in digit for current
character.
5. Return decimal integer corresponding to input character representation.
Applications:
Business applications, tape processing.
23

Weitere ähnliche Inhalte

Ähnlich wie Lecture 7.pptx

DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdfishan743441
 
ALGO VS FLOW.pptx
ALGO VS FLOW.pptxALGO VS FLOW.pptx
ALGO VS FLOW.pptxNagendraK18
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0PMILebanonChapter
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptracha49
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptBhargaviDalal4
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptxssuser586772
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithmlilyMalar1
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptISHANAMRITSRIVASTAVA
 
Algorithm types performance steps working
Algorithm types performance steps workingAlgorithm types performance steps working
Algorithm types performance steps workingSaurabh846965
 

Ähnlich wie Lecture 7.pptx (20)

DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
 
ALGO VS FLOW.pptx
ALGO VS FLOW.pptxALGO VS FLOW.pptx
ALGO VS FLOW.pptx
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
 
C code examples
C code examplesC code examples
C code examples
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithm
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
chapter 1
chapter 1chapter 1
chapter 1
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Regression
RegressionRegression
Regression
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
Algorithm types performance steps working
Algorithm types performance steps workingAlgorithm types performance steps working
Algorithm types performance steps working
 

Kürzlich hochgeladen

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 

Kürzlich hochgeladen (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 

Lecture 7.pptx

  • 2. PROGRAM VERIFICATION • The application of mathematical proof techniques to establish that the results obtained by the execution of a program with arbitrary inputs are in accord with formally defined output specifications. 1. Computer model for program execution. 2. Input assertion specify any constraints that have been placed on the values of the input variables used by the program. 3. Output assertion specify symbolically the results that the program is expected to produce for input data that satisfies the input assertion. 4. Implications and symbolic execution. 5. Verification of straight-line program segments. 6. Verification of program segments with branches / loops / arrays. 7. Proof of termination. 2
  • 3. THE EFFICIENCY OF ALGORITHMS • Design algorithms that are economical in the use of CPU time and memory because of high cost of computing resources. • Suggestions that are useful in designing efficient algorithms. 1. Redundant computations. 2. Referencing array elements. 3. Inefficiency due to late termination. 4. Early detection of desired output conditions. 5. Trading storage for efficiency gains. 3
  • 4. Analysis of algorithms • Good algorithm possess the following qualities and capabilities
  • 5. FUNDAMENTAL ALGORITHMS EXCHANGING THE VALUES OF TWO VARIABLES Problem Statement: Given two variables a and b, interchange the values of the variables. 5 • Algorithm development The problem of interchanging the data associated with two variables involves a very fundamental mechanism that occurs in many sorting and data manipulation algorithm. Ex: a b 4 5 Here a has 4 and b has 5. our aim is to replace the variable of a with 4 and b with 5.
  • 6. • So the target solution may look as a b 5 4 • Here we can make use of the assignment operator a = b; b = a; • where the b value is assigned to a. the value has been changed, and the changed a value has been assigned to b. a = 5, b = 5 • So we make use of the temporary variable. t = a; a = b; b = t; • Now, t = 4, a = 5, b = 4 • Our target solution has been achieved.
  • 7. Algorithm Description: • 1. Save the original value of a in t. • 2. Assign to a the original value of b. • 3. Assign to b the original value of a that is stored in t. APPLICATION • This kind of swapping technique is applicable for all kinds of sorting algorithm. Notes on design 1. The use of an intermediate temporary variable allows the exchange of two variables to proceed correctly. 2. This example emphasized that at any stage in a computation a variable always assumes the value dictated by the most recent assignment made to that variable 3. Working through the mechanism with a particular example can be a useful way of detecting design faults 4. A more common application of exchange involves two array elements (eg. a[i] and a[j]). The steps for such an exchange are: t:= a[i]; a[i] := a[j]; a[j] := t
  • 8. COUNTING Problem Statement: Given a set of n students examination marks (in the range 0 to 100), make a count of number of students passed the examination. A pass is awarded for all marks of 50 and above. 8 • Algorithm development Count must be made of number of items in a set which possess some particular property or which satisfy some particular condition or conditions. This class of problems is by “examination marks”. As a starting point for developing a computer algorithm for this problem we can consider solving by hand. Suppose that the set of marks are 55,42,77,63,29,57,89 Check is first mark >=50, so one student has passed so far. The second mark is then examined and no change to the count. The process continues until all marks are examined.
  • 9. • In more detail: Therefore, number of students passed = 5 Order in which marks are examined Marks Counting details for passes 55 Previous count = 0 Current count = 1 42 Previous count = 1 Current count = 1 77 Previous count = 1 Current count = 2 63 Previous count = 2 Current count = 3 29 Previous count = 3 Current count = 3 57 Previous count = 3 Current count = 4 89 Previous count = 4 Current count = 5
  • 10. Algorithm description: 1. Prompt and read the number of marks to be processed. 2. Initialize count to zero. 3. While there are still marks to be processed repeatedly do a. Read next mark. b. If it is a pass then add one to count. 4. Print total number of passed students. Notes on design 1. Initially, and each time through the loop, the variable count represents the number of passes so far encountered. On the termination (when i=n) count represents the total number of passes in the set. Because I is incremented by 1 with each iteration, eventually the condition i<n will be violated and the loop will terminate. 2. It was possible to use substitution to improve on the original solution to the problem. The simplest and most efficient solution to a problem is usually the best all-round solution. 3. An end-of-line test is included to handle multiple lines of data. Applications All forms of counting 10 COUNTING
  • 11. SUMMATION OF A SET OF N NUMBERS Problem Statement: Given a set of n numbers, design an algorithm that adds these numbers and returns the resultant sum. Assume n is >=0. 11 Algorithm development ⁻Consider the addition of 421, 583 and 714 ⁻In designing an algorithm to add a set of numbers a primary concern is the mechanism for the addition process. ⁻Simplest way s := a+b+c ⁻Make general enough that will successfully handle a wide variety of input conditions. s = (a1+a2+a3+a4+…+an) which is equivalent to s= 𝑖=1 𝑛 𝑎𝑖
  • 12. Steps followed by a set of n iterative steps 1. Compute first sum (s=0) as special case 2. Build each of the n remaining sums from its predecessor by an iterative process 3. Write out the sum of n numbers ALGORITHM: 1. Prompt and read total numbers to sum. 2. Initialize sum for zero numbers. 3. While less than n numbers have been summed repeatedly do a. Read next mark. b. Compute current sum by addin 4. Print total number of passed students. Applications: Average calculations, variance and least square calculations.
  • 13. FACTORIAL COMPUTATION Problem Statement: Given a number n, compute n factorial (written as n!) where n>=0. 13 • Algorithm development n! can be done as n!= 1x2x3x…..x(n-1)xn for n>=1 as 0! =1 We can generalize this by observing that n! can always be obtained from (n-1)! by simply multiplying it by n (for n>=1) n! = nx(n-1)! For n>=1 Using this: 1! = 1x0! 2!= 2x1! 3!= 3x2! . . .
  • 14. ALGORITHM: 1. Establish n, the factorial required where n>=0. 2. Set product p for 0! Also set product count to zero. 3. While less than n products have been calculated repeatedly do, a. Increment product count. b. Compute the ith product p by multiplying I by the most recent product. 4. Return the result n!. Applications: Probability, Statistical and mathematical computations. 14
  • 15. SINE FUNCTION COMPUTATION Problem Statement: Design an algorithm to evaluate the function sin(x) as defined by the infinite series expansion sin 𝑥 = 𝑥 1! − 𝑥3 3! + 𝑥5 5! − 𝑥7 7! 15
  • 16. Algorithm development • The function we need to compute will involve the following steps: fp := 1; j := 0; While j<i do begin j := j+1; fp := fp*x/j end • This algorithm can be completed by implementing the addition and subtractions and making the appropriate termination. • To generate consecutive terms of the sine series we can use 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑖𝑡ℎ 𝑡𝑒𝑟𝑚 = 𝑥2 𝑖(𝑖 − 1) 𝑋 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑡𝑒𝑟𝑚 • To get alternate terms, sign := -sign • Will generate alternate positive and negative terms • The initial conditions are i := i+2; term := -term*x*x/(i*(i-1)); tsin = tsin + term - How to terminate the algorithm - Fix an acceptable error level and generate successive terms until the contribution of the current term is less than the acceptable error
  • 17. ALGORITHM: 1. Set up initial conditions for the first term that cannot be computed iteratively. 2. While the absolute value of current term is greater than the acceptable error do a. Identify the current ith term. b. generate current term from its predecessor. c. add current term with the appropriate sign to the accumulated sum for the sine function. Applications: Mathematical and Statistical computations. 17
  • 18. REVERSING THE DIGITS OF AN INTEGER Problem Statement: Design an algorithm that accepts a positive integer and reverses the order of its digits. 18 Algorithm development Digit reversal is a technique used to remove bias from a set of numbers. Eg. Input: 27953 Output: 35972 If we apply the following two steps r := n mod 10 n := n div 10 The central steps in algorithm are: 1. While there are still digits in the number being reversed do a) Extract the rightmost digit from the number being reversed and append the digit to the right-hand end of the current reversed number representation; b) Remove the rightmost digit from the number being reversed
  • 19. ALGORITHM: 1. Establish n, the positive integer to be reversed. 2. Set the initial condition for the reversed integer dreverse. 3. While the integer being reversed is greater than zero do, a. Use the remainder function to extract the rightmost digit of the number being reversed. b. Increase the previous reversed integer representation dreverse by a factor of 10 and add to it the most recently extracted digit to give the current dreverse value. c. Use integer division by 10 to remove the rightmost digit from the number being reversed. dreverse := (previous value of dreverse)*10 + (most recently extracted rightmost digit) Applications: Hashing and information retrieval, database applications. 19
  • 20. BASE CONVERSION Problem Statement: Convert a decimal integer to its corresponding octal representation. 20 Example: The octal representation of decimal 275 is 423.
  • 21. ALOGORITHM: 1. Establish the newbase and initialize the quotient q to the decimal number to be converted. 2. Set the new digit count ndigit to zero. 3. Repeatedly, A. Compute the next most significant digit i.e octal from the current quotient q as the remainder r after division by newbase. B. Convert r to appropriate ascii value. C. increment new digit count ndigit and store r in output array newrep. D. Compute the next quotient q from its predecessor using integer division by newbase until the quotient is zero. Applications: Interpretation of stored computer data and instructions. 21
  • 22. CHARACTER TO NUMBER CONVERSION Problem Statement: Given the character representation of an integer convert it to its conventional decimal format. 22 Example: Character representation ‘125’ when converted to decimal format results in 125.
  • 23. Algorithm: 1. Establish the character string for conversion to decimal and its length n. 2. Initialize decimal value to zero. 3. Set base0 value to the ascii or ordinal value of ‘0’. 4. While less than n characters have been examined do a. Convert next character to corresponding decimal digit. b. Shift current decimal value to the left one digit and add in digit for current character. 5. Return decimal integer corresponding to input character representation. Applications: Business applications, tape processing. 23