chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · web viewthere are...

26

Click here to load reader

Upload: hoangkhue

Post on 10-Mar-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

UNIT III – PROBLEM SOLVING TECHNIQUES

Programming life cycle phases – problem solving – implementation –

maintenance – pseudocode representation – flowcharts – algorithms –

algorithmic efficiency – complexity of algorithms.

Programming Life Cycle PhasesBefore starting the process of writing a program (coding), the programmer

has to determine the problem that needs to be solved. There are different

approaches to problem solving. Most require breaking the problem into a

series of smaller steps, independent of the programming language. One

common technique is to use the program development cycle, with the

number of steps that may vary according to the person who has formalized

the development. Often the process runs in a loop, for example, as the

current process is completed, new demands appear and the development

process commences again. Development cycle of a program includes the

following phases:

1. Analyze /Define the problem:

The problem is analyzed precisely and completely. Based on

understanding, the developer knows about the scope within

which the problem needs to be analyzed.

2. Task analysis:

After analyzing the problem, the developer needs to develop

various solutions to solve the given problem. From these solutions, the

1

Page 2: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

optimum solution (by experimenting with all the solutions) is chosen,

which can solve the problem comfortably and economically.

3. Designing phase:

After selecting the appropriate solution, algorithm is

developed to depict the basic logic of the selected solution. An

algorithm depicts the solution in logical steps (sequence of

instructions). Further, algorithm is represented by flowcharts and

pseudo codes. These tools make program logic clear and they

eventually help in coding. This phase is also known as design phase.

4. Testing the algorithm for accuracy:

Before converting the algorithms into actual code, it should

be checked for accuracy. The main purpose of checking algorithm is to

identify major logical errors at an early stage, because logical errors

are often difficult to detect and correct at later stages. The testing also

ensures that the algorithm is a ‘true ‘one and it should work for both

normal as well as unusual data.

5. Coding:

After meeting all the design considerations, the actual coding

of the program takes place in the chosen programming language.

Depending upon application domain and available resources, a

program can be written by using computer languages of different levels

such as machine, assembly or high-level languages.

6. Test and Debug the program:

It is common for the initial program code to contain errors. A

program compiler and programmer-designed test data machine

tests the code for syntax errors. The results obtained are

2

Page 3: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

compared with results calculated manually from this test data.

Depending upon the complexity of the program, several rounds

of testing may be required.

7. Documentation and Implementation:

Once the program is free from all the errors, it is the duty of the

program developers to ensure that the program is supported by suitable

documentation. After documentation, the program is installed on the

end user’s machine.

Problem SolvingProgram is a sequence of instructions written in a programming language.

There are various programming languages each having its own advantages

for program development.

Generally every program takes an input, manipulates it & produces an

output.

Input Output

For better designing of a program, systematic planning must be done.

Planning makes the program more efficient & more effective. A

programmer should use planning tools before coding a program. By doing

so, all instructions are properly interrelated in the program code & the

logical errors are minimized.

3

Program Code

Page 4: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

There are various planning tools for mapping the program logic such

as Algorithms, Flowcharts, Pseudo code etc.

Algorithm- Finite sequence of explicit instructions that, when provided

with a set of input values produces an output and then

terminates.

- Properties:

o There must be no ambiguity in any

instruction

o There should be not any uncertainty about

which instruction is to be executed next.

o The description of the algorithm must be

finite.

o The execution of the algorithm should

conclude after a finite number of steps.

o The algorithm should be general enough

to deal with any contingency.

Example algorithm to find largest of 3 numbers.

Step 1: Start

Step 2: Read 3 numbers A, B, C.4

Page 5: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Step 3: Find the larger number between A and B and store it in

MAX_AB. Step 4: Find the larger number between MAX_AB and

C and store it in MAX. Step 5: Display MAX.

Step 5: End.

Flowchart- Pictorial representation of an algorithm in which the

steps are drawn in the form of different shapes of boxes

and logical flow is indicated by interconnecting arrows.

- Boxes represent the operation and arrows indicate the

sequence in which the operations are carried out.

- Purpose of the flowchart is to help the

programmer in understanding the logic of the

program.

- drawn according to the defined rules and using standard

flowchart symbols prescribed by ANSI.

Flowchart Symbols

5

Page 6: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

ADVANTAGES OF USING FLOWCHARTS

The benefits of flowcharts are as follows:

1. Communication: Flowcharts are better way of communicating the logic of a

system to all concerned.

2. Effective analysis: With the help of flowchart, problem can be analysed in

more effective way.

3. Proper documentation: Program flowcharts serve as a good

program documentation, which is needed for various purposes.

4. Efficient Coding: The flowcharts act as a guide or blueprint during the

systems analysis and program development phase.

5. Proper Debugging: The flowchart helps in debugging process.

6. Efficient Program Maintenance: The maintenance of operating program

becomes easy with the help of flowchart. It helps the programmer to put

efforts more efficiently on that part.

6

Page 7: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

LIMITATIONS OF USING FLOWCHARTS

1. Complex logic: Sometimes, the program logic is quite complicated. In that

case, flowchart becomes complex and clumsy.

2. Alterations and Modifications: If alterations are required the flowchart

may require re-drawing completely.

3. Reproduction: As the flowchart symbols cannot be typed,

reproduction of flowchart becomes a problem.

4. The essentials of what is done can easily be lost in the technical details of

how it is done.

Pseudo codePseudo code is a logic development tool that uses English-like statements or

clauses to present the logical steps necessary to solve a problem. “Pseudo”

technically means “false”, so pseudo code, taken literally, means “false code”. As

used in program development, pseudo code is made up of statements written to

depict the steps, in the correct sequence, required to solve a specific problem.

These statements can then be directly translated into computer programming

language instructions. Pseudo code is “false code” only in the sense that it is not

the programming language statements or “code” that is used to direct the action of

the computer.

Pseudo code isn't a language; it's more of a thought process. The focus isn't on

writing code in a particular programming language. It's more like drawing a map

through a problem. Once you have a clear idea of what to do, it's easy to write the

code to fill in the blanks, and actually do the work. There are no special symbols

required with pseudo code.

7

Page 8: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Some of the advantages of pseudo code are:

It is easy to use and create with a text editor because it uses English-like

statements.

No special symbols are used to represent specific functions.

Very little specific syntax is required.

Statements can generally be translated on a one-for-one basis to any high-

level language.

Pseudo coding the problem logic first drastically reduces time spent in

coding, testing, and debugging systems.

Pseudo code is composed of words, clauses and sentences.

The following rules should be followed:

No actual programming code should appear in the pseudocode. You are

writing down the logical steps at this stage, not coding the program.

All statements should be presented in enough detail so the reader can clearly

understand the activity or action being described in each statement.

The names of “variables” being used to solve the problem should be totally

descriptive of what they represent, not shortened versions you use in your

source code.

Each activity or action being depicted should be presented in a single line.

Appropriate indentation should be used if the activity or action being

described cannot fit on one line.

8

Page 9: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Indentation should be used where appropriate to show the logical grouping

of related activities or actions.

Most of your pseudocode should not be done with capital letters, but certain

key structures should be capitalized. They are:

o IF, THEN, ELSE and ENDIF in decision structures.

o WHILE DO, in pre-test loop structures.

o DO WHILE in post-test loop structures.

o CASE in a case structure.

Pseudocode uses some keywords to denote programming processes. Some of them

are

Input: READ, OBTAIN,GET and PROMPT

Output: PRINT, DISPLAY and SHOW

Compute: Compute, CALCULATE, DETERMINE

Initialise: SET and INITIALISE

Add One: INCREMENT

Subtract One: DECREMENT

Fundamentals of analysis of Algorithm efficiencyInvestigation of an algorithm’s efficiency with respect to two resources:

running time & memory space. Analysis Frame work:

A general framework for analyzing the efficiency of algorithms.

Two kinds of efficiency

o Time efficiency

o Space efficiency.

Time Efficiency:9

Page 10: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Indicates how fast an algorithm runs.

Space Efficiency:

Deals with the extra space the algorithm requires.

Now a days due to the availability of cheap memory, how much memory

the algorithm require is not much important, but there is difference

between fast main memory, the secondary memory and the cache.

But importance of time has not diminished.

Measuring an Input’s Size

o Almost all algorithms run longer on larger inputs.

o Investigating an algorithm’s efficiency as a functions of some

parameter ‘n’ indicating the algorithm’s input size is necessary.

o Selection of input size parameter

1. Sorting , Searching: Size of list

2. Polynomial evolution: degree of polynomial

3. Matrix Multiplication: (1)Order of matrix & (2)Total number of

elements10

Page 11: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

4. Spell- Checking algorithm

Character-by-character checking method- number of characters

Processing words- word count

5. Checking prim e nature of ’n’ – lower bound (log2 n)+1 (no.

of list to represent the number)

6. Finding factorial of n – lower bound (log2 n)+1

Units for measuring Running time:

Use standard unit of time measurement- sec/ms/micro sec, etc..,

Draw Backs:

1. Dependence on quality of program

2. Compiler used

3. Difficulty of clocking the actual run-time of the program.

A metric that does not depend on these extraneous factors

11

Page 12: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Count number of times each of the algorithm’s operations is executed.

Identify basic operation & count how many times it is executed.

Basic operation:

Most time-consuming operation in the algorithm’s inner most loop

Eg:

Sorting algorithm – comparison

Estimating Runtime ,T(n):

Cop= time of execution of an algorithm’s basic operation on

a particular computer. C (n) – Number of times this operation

needs to be executed for this algorithm.

T (n) = Cop C (n)

Efficiency analysis framework ignores multiplicative constant

and low-order terms and concentrates on order of growth.

Orders of Growth:

12

Page 13: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Function growing slowest is logarithmic function fastest is exponential

functions. (2^n) and factorial function (n!).

Algorithm that require an exponential number of operation (2^n or n!)

are practical for solving only problem of very small size.

Inc

rease in

‘n’

Suppos

e ‘n’

doubles

log (2n)=log2 2+ log2 n= log

2n+1 – increase by1 n2n –two

field

n^2 (2n)^2

=4n^2 – four field

n^3 (2n)63

=8n^3 – eight field

2^n 2^2n =(2^n)^2 – squared

n! (2n)! much more than square

Worst-case, Best-case & Average-case

o The running time of an algorithm sometimes depends not only on

13

Page 14: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

the size of the input but also on the specifies of a particular input.

o Eg:

Sequential Search – not only on ‘n’ the size of array but also on what

value is being searched.

Worst-case efficiency

Worst-case efficiency of an algorithm is the efficiency for worst-case input

of size ‘n’ for while the algorithm runs the longest among all possible

inputs of that size.

Sequential search – Search for last element or element not in array

Cworst (n) = n

Best-case efficiency

Best-case efficiency of an algorithm is the efficiency for the

best-case input of size n. Cbest (n)

For sequential- Best case: Searching for 1st element

Cbest (n) = 1.

Average-case efficiency

Average-case efficiency of an algorithm provides about algorithm’s is behaviour

on a typical and random input

Cavg (n)

Assumption have to be made to find Cavg (n).

14

Page 15: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Sequential Search

Assumption 1: The probability of the successful search is equal to p(0<=p<=1)

Assumption 2: The probability of the 1st match occurring a ith position is same for

every ‘i’

Cavg (n) = [1*(p/n) + 2*(p/n) + …+ i*(p/n) + …. + n * (p/n)] + n(1-p)

= p/n[1+2+3+…+n]+ n(1-p)

= p* n+1)/2 + n (1-p) Cavg (n) = p(n+1)/2 + n(1-p)

If p=1 (Successful Search)

Cavg (n) = (n+1)/2( Half the number of keys) If p=0 (Unsuccessful Search)

Cavg (n) = n

Amortized Effici ency:

It applies not to a single run of an algorithm, but rather to a

sequence of operations performed on the same DS.

Sometimes a single operation may be expensive, but the

total time for an entire sequence of such operations (n) is

always significantly better than 1 operation x n.

Asymptotic Notations:

Notation used to give meaningful statements regarding complexity of

algorithms.

Popular notation

O (Big oh)

Ω (Big omega)15

Page 16: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

Ө (theta)

O-notation:

A function t (n) is said to be in O(g(n)) denoted by t (n) € O(g(n)), if

there exists some positive constant ‘c’ and some nonnegative integer no such

that

T (n)<= c g(n) for all n>= no

T (n) € O (g(n))

O-notation provides an upper bound.

Ω -notation

A function t (n) is said to be in Ω (g(n)), denoted by T (n) € Ω (g(n)) if

there exists some positive constant ‘c’ and some nonnegative integer no

such that

T (n)>= c g(n), for all n>= no

16

Page 17: chettinadtech.ac.inchettinadtech.ac.in/storage/11-11-08/11-11-08-09-36-2… · Web viewThere are different approaches to problem solving. Most require breaking the problem into a

T (n) € Ω (g(n))

Ω-notation provides a lower bound.

Ө-notation:

A function t (n) is said to be in Ө (g(n)) denoted T (n) € Ө (g(n)), if there

exists positive constants c1 & c2 and some nonnegative integer no S.T

C2 g(n)<= t (n) <= C1 g(n) for all n>=no

Best Efficiency Classes:

Class Name Comments1 Constant Very few reasonable egs.log n Logarithmic Cutting a problems’ size

by a constant factorn Linear Scan a list of size nn* log n n-log-n Many divide & conquer n^2 Quadratic Two embedded loopsn^3 Cubic Three embedded loopsn^4 exponential All subsets of n-elementn! Factorial Permutations

17