Algorithms - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Algorithms

Description:

How to solve a problem using basic set of primitive instructions. ... Example: Fibonacci Numbers. 26. Fibonacci(n) If n=1 or n=2, Then Fibonacci(n)= 1 ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 43
Provided by: CDTL6
Category:

less

Transcript and Presenter's Notes

Title: Algorithms


1
Algorithms
2
Algorithms
  • How to explain to a dumb mechanical/computing
    device how to solve a problem?
  • How to solve a problem using basic set of
    primitive instructions.
  • Complexity of solving problems.

3
Goals
  • To develop framework for instructing computers to
    perform tasks.
  • To introduce notion of algorithm as means of
    specifying how to solve a problem.
  • To introduce and appreciate approaches for
    defining and solving very complex tasks in terms
    of simpler tasks.

4
  • Computer Science can be considered as the study
    of algorithms including
  • Their formal properties
  • Their hardware and software realisations
  • Their applications

5
Real Life Examples
  • Cook Recipe for preparing a dish
  • Paper Toy
  • Directions How to go to Nyugati railway station

6
Real Life Examples
  • Imprecise Instructions
  • Job can often be done even if the instructions
    are not followed precisely
  • Modifications may be done by the person following
    the instructions
  • Computers need to be told in precise manner what
    to do. Instructions cannot be vague or ambiguous.

7
Algorithm Definition
  • An algorithm for solving a problem is a finite
    sequence of unambiguous, executable
    steps/instructions, which if followed would
    ultimately terminate and give the solution of the
    problem.
  • Finite set of steps
  • Unambiguous
  • Executable
  • Terminate

8
Operations of Algorithms
  • sequence of steps/instructions
  • Iteration repeating task
  • Selection the continue of task depend on a
    condition

9
Main description of Alg.
  • Flow Charts
  • PseudoCode

10
PseudoCode
  • Somewhere between human language such as
    English and Computer languages.
  • Precise enough to describe what is meant
    without being tedious.

11
Flow Charts
  • Flow Charts Defined
  • A Flow charts is a pictorial representation
    showing all the step of a process

12
Creating a Flow Chart
  • First, familiarize the participants with the flow
    chart symbols.
  • Draw the process flow chart and fill it out in
    detail about each element.
  • Analyze the flow chart. Determine which steps
    add value and which dont in the process of
    simplifying the work.

13
Flow chart techniques
  • Basic symbols
  • Terminator
  • Process
  • Decision
  • Predefined process
  • On-page connector
  • Connector

14
Easy example
I have an idea
I raise my hand
Did my teacher call my name?
no
Wait
yes
Share my idea out loud
15
Example Adding two numbers
  • Input Two positive m digit decimal numbers
  • am, am-1, ., a1
  • bm, bm-1, ., b1
  • Output cm1, cm, ., c1

16
  • Step 1 Set the value of carry to 0
  • Step 2 Set the value of i to 1.
  • Step 3 Repeat steps 4 through 6 until the value
    of i is gt m.
  • Step 4 Add ai and bi to the current value of
    carry, to get xi.
  • Step 5 If xi lt 10, then let cixi and reset
    carry to 0.
  • Else (i.e. xi ? 10)
  • let cixi -10 and reset carry to 1.
  • Step 6 Increase the value of i by 1.
  • Step 7 Set cm1 to the value of carry.
  • Step 8 Print the final answer cm1, cm, ., c1
  • Step 9 Stop.

17
Iteration
  • For Instruction
  • For x ? a to b Do
  • ..
  • ..
  • Endfor
  • Execute the instructions .., first using xa,
    then using xa1, , and finally using xb.

18
Example Divisibility
  • A positive integer n is divisible by m if for
    some positive integer i?n, m ? i n

Is n divisible by m?
Yes
No
19
  • Divisible(m,n)
  • 1. For i?1 To n Do
  • If m?in, Then Output True and Stop
  • Else do nothing (continue with next
    i).
  • EndFor
  • 2. Output False and Stop
  • End

20
  • To communicate the algorithm to computer
  • need a way to represent the algorithm
  • Syntax --- representation
  • Semantics --- meaning
  • complex task --- Programming Languages
  • Need more English Like

21
Subprogram
  • Problems already shown to be doable, can be used
    as primitive (already known) instruction
  • Simplifies the process of writing
    algorithms/programs
  • Modularity, Abstraction, division of labour
  • Complex tasks can be divided and each part solved
    separately and combined later
  • Subroutines, procedures, modules, etc.

22
Example Prime Numbers
  • A number n is a prime iff it has no positive
    factors except for 1 and n.
  • In other words, n is not divisible by any
    positive number m such that 1 lt m lt n.

23
  • Prime(n)
  • 1. For m?2 To n-1 Do
  • If Divisible(m,n) Then Output False
  • and Stop
  • Else do nothing (continue with next
    m).
  • EndFor
  • 2. Output True and Stop
  • End

24
Recursion
  • As a means of solving bigger problems using
    smaller subproblems/subparts which may be of same
    kind.
  • Solve simplest cases directly
  • Solve bigger problems using smaller (simpler)
    subproblems.
  • Abstraction.

25
Example Fibonacci Numbers
  • F1 1
  • F2 1
  • F3 2
  • F4 3
  • ..
  • Fn Fn-2Fn-1

26
  • Fibonacci(n)
  • If n1 or n2, Then Fibonacci(n) 1
  • Else Fibonacci(n)Fibonacci(n-1)Fibonacci(n-2)
  • End

27
Example Tower of Hanoi
  • Three Pegs A, B and C
  • Peg A initially has n disks, all of different
    size, stacked one over another
  • Larger disks are below smaller disks
  • Problem is to move the n disks to Peg C
  • Can move only one disk at a time
  • Smaller disk should be above larger disk
  • Can use Peg B as intermediate

28
Example Tower of Hanoi
  • Example for 3 disks (disk 3 is largest, disk 1 is
    smallest)
  • (1) Move disk 1 from Peg A to Peg C.
  • (2) Move disk 2 from Peg A to Peg B.
  • (3) Move disk 1 from Peg C to Peg B.
  • (4) Move disk 3 from Peg A to Peg C.
  • (5) Move disk 1 from Peg B to Peg A.
  • (6) Move disk 2 from Peg B to Peg C.
  • (7) Move disk 1 from Peg A to Peg C.

29
  • Hanoi(n,A,B,C)
  • ( Move top n disks from A to C via B )
  • 1. If n1, Then move top disk on Peg A to Peg C.
  • 2. If ngt1, Then,
  • (a) Hanoi (n-1,A,C,B)
  • ( That is move top n-1 disks on Peg A to Peg
    B using Peg C as intermediate Peg. )
  • (b) Move the current top disk on Peg A to Peg C.
  • (c) Hanoi(n-1,B,A,C)
  • ( That is move top n-1 disks on Peg B to Peg C
    using
  • Peg A as intermediate Peg. )
  • End

30
Searching
  • 13, 28, 19, 74, 76, 14, 12, 18, 22, 55

31
Searching
  • List of numbers A1, A2, A3, ., An
  • A number x
  • Question Is x in the list?
  • Sequential Search(x, A1, A2, A3, ., An )
  • For i?1 To n Do
  • If xAi then output Yes and Stop.
  • Else do nothing (continue with next i).
  • EndFor
  • Output No and Stop
  • End

32
Binary Search
  • List is sorted.
  • That is A1? A2 ? A3 ? . ? An
  • Then we can do better.

33
Binary Search
  • 1, 4, 9, 11, 14, 43, 78

34
Binary Search
  • Input Sorted List A1? A2 ? A3 ? . ? An
  • A number x.
  • Question Is x in the list.
  • 1. First 1
  • Last n
  • 2. While First ? Last
  • mid ? (firstlast)/2?
  • If xAmid Then output Yes and Stop
  • Else If xlt Amid Then Lastmid-1
  • Else If x gt AmidThen Firstmid1
  • EndWhile
  • 3. Output False and Stop
  • End

35
Time Complexity
  • Sequential Search
  • Worst Case n comparisons
  • Best Case 1 comparison
  • Average Case n/2 comparisons
  • Binary Search
  • Worst Case approximately log2 n comparisons
  • Best Case 1 comparisons
  • Average Case approximately log2 n-1
    comparisons

36
Characteristics of Algorithm
  • Correctness
  • Complexity --- time, space (memory), any other
    measure
  • Ease of understanding
  • Ease of coding
  • Ease of maintainence

37
Logo?
  • Full-featured computer programming language
    derived from LISP
  • A language for learning
  • A tool to teach the process of learning and
    thinking
  • An environment where students assume the role of
    teacher

38
Sequences
  • forward 50 right 90 forward 50right
    90forward 50right 90forward 50right 90

39
Iteration
  • repeat 4 forward 50 right 90

40
Procedures
  • to squarerepeat 4 forward 50 right 90end

41
Procedures
  • You could draw a flag
  • forward 60squareback 60

42
Further Iteration
  • You could make a circle of squaresrepeat
    12square right 30

43
Procedures
  • to house square forward 50 right
    90 triangleend
  • to trianglerepeat 3 forward 50 left 120end

44
Passing Values into Procedures
  • to square sizerepeat 4 forward size right
    90end
  • square 10
  • square 20 etc

45
Recursion
  • to design clearscreen right 30 polyspi 5
    120end
  • to polyspi size angleif size gt 205
    stop forward size right angle polyspi
    size5 angle.12end
Write a Comment
User Comments (0)
About PowerShow.com