Algorithmics - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Algorithmics

Description:

: return return 3+8 3+8 Pseudo Code for Euclid Alg. Algorithm Euclid_GCD ... – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 41
Provided by: edut1550
Category:

less

Transcript and Presenter's Notes

Title: Algorithmics


1
Algorithmics
  • The discipline of algorithms

2
Abu Jafar Mohammed ibn Musa al Khowarizmi
  • Algorithm comes from the name of a Persian, Abu
    Jafar Mohammed ibn Musa al Khowarizmi (c. 825 A.
    D.).

3
al-Khwarizmi
  • ???(algorithm)?????al-Khwarizmi,????????780?????
    ??( Iraq )???( Baghdad )????????????????(Abu
    Jafar Muhammad ibn Musa al-Khwarizmi)????????,????
    ?????????????????????(????????????????????),??????
    ??ilm al jabr wal-muqabala???,?????????????????
    ,?????????????12?????????,??Algebra et
    Almucabala, (Algebra?????????al
    jabr),??????(Algebra)??????

4
What is an algorithm (1/2)
  • Any special method of solving a certain kind of
    problem -- Websters
  • A precise method useable by a computer for the
    solution of a problem. -- Viewed by Computer
    Engineers
  • In mathematics and computing, an algorithm is a
    procedure (a finite set of well-defined
    instructions) for accomplishing some task which,
    given an initial state, will terminate in a
    defined end-state. -- WiKipedia

5
What is an algorithm (2/2)
  • An algorithm is composed of a set of steps for
    solving a specific problem. It should satisfy the
    following properties
  • Zero or more inputs
  • At least one output
  • Finiteness (the number of steps should be finite
    and the execution of the steps should terminate)
  • Definiteness (to compute 5/0 is not definite)
  • Effectiveness (to compute v2 as an infinite
    decimal is not effective)

6
A recipe is an algorithm
  • Informally, the concept of an algorithm is often
    illustrated by the example of a recipe.
  • ??????(4??)???12????6-8???2?????2???1?????
    ??????????
  • ??1. ??????????????????????,???????????????????
    ????????,???2. ?????,??,???????,??????????,??????
    3. ?????,?????????,????????????????1??,???????2??
    ???,?????????

7
A flowchart is an algorithm
8
A program is an algorithm
  1. import javax.swing.
  2. public class EuclidGCDApplet extends JApplet
  3. public void init( )
  4. int ??m,??n,?????
  5. String ????1,????2,????
  6. ????1JOptionPane.showInputDialog("??????m?")
  7. ????2JOptionPane.showInputDialog("??????n?")
  8. ??mInteger.parseInt(????1)
  9. ??nInteger.parseInt(????2)
  10. ?????EuclidGCD(??m, ??n)
  11. ????"??"??m"???"??n"???????"?????
  12. JOptionPane.showMessageDialog(null,????)
  13. //??init() ??????

9
Representation of Algorithms
  • ??????????(????????)????(flow chart)????(pseudo
    code)???????(high level programming
    language)???????
  • ??,???????????(?????)?????????? ?
    ????(Euclid)GCD????
  • ????(Euclid)GCD?????????300?????????????,?????????
    ??????(GCD, Greatest Common Divisor)?

10
Euclid GCD Algorithm
Euclid GCD????????????m?n,???????????(????????m?n??????)??E1.???? ??m??n???,????r?E2.???0?? ??r0???,??n?GCD?E3.???? ??mn,nr,?????E1?
11
Pseudo Code
  • ???????????????????????????????,?????????????,????
    ??????????????
  • ????Goodrich?Tamassia??Data Structures and
    Algorithms in JAVA?????????????

12
Pseudo Code
  • ??? Algorithm ????(??1,??2,)?????????????????,A
    lgorithm Euclid-GCD(m, n) ???????????????m?n????E
    uclid-GCD????,?
  • ????? ? ??,????????????????????,m?38
    ????38??????m??
  • ????? ??,???????????????,m38
    ?????38???????m?????

13
Pseudo Code
  • ????? if??then??????? else ???????
    ???,????????????????????????????????????,
  • if m38 then b?38else c?38
  • ?????m???38???,????38??????b???,??????38??????c
    ????

14
Pseudo Code
  • while??? while??do???? ???,????????????????????
    ???????,???????????????????,
  • while m38 do b?38 c?38
  • ?????m???38???,??????38??????b??38??????c????

15
Pseudo Code
  • repeat??? repeat???? until?? ???,???????????????
    ???????????????????????????,???????????,
  • repeat b?38 c?38until m38
  • ????????38??????b??38??????c???,????m???38?????

16
Pseudo Code
  • for??? for??????? do ???? ???,?????????????????
    ??????,
  • for i?3 to 8 do b?38 c?38
  • ?????i???3?8(?i?3?4?5?6?7?8)????,??????38??????b?
    ?38??????c????

17
Pseudo Code
  • ??????? Ai ????A???(index)?i???,???n??????,??
    ????0,1,,n-1???,A5 ????A????5????
  • ????? ??.??(??) ??????,?????????,???????????,M
    ath.random() ?????Math??????random()???
  • ????? return ????? ??????,return 38
    ?????38???

18
Pseudo Code for Euclid Alg.
Algorithm Euclid_GCD(m, n)Input two integers m and nOutput GCD of m and nr?mnwhile r?0 do m?n n?r r?mnreturn n
19
Java Method for Euclid Alg.
  • ????????????????????,?????????,???????????????????
    ????????????????,?????????????????????Java????????
  • int Euclid_GCD(int m, int n)
  • int rmn
  • while (r!0)
  • mn
  • nr
  • rmn
  • return n

20
Java Applet for Euclid Alg.
  • ????(??EuclidGCDApplet.java)
  • //??EuclidGCDApplet.java
  • //????????????,??????GCD??????????(GCD)
  • import javax.swing.
  • public class EuclidGCDApplet extends JApplet
  • public void init( )
  • int ??m,??n,?????
  • String ????1,????2,????
  • ????1JOptionPane.showInputDialog("??????m?")

21
Java Applet for Euclid Alg.
  • ????2JOptionPane.showInputDialog("??????n?
    ")
  • ??mInteger.parseInt(????1)
  • ??nInteger.parseInt(????2)
  • ?????EuclidGCD(??m, ??n)
  • ????"??"??m"???"??n"???????"?????
  • JOptionPane.showMessageDialog(null,????)
  • //??init() ??????

22
Java Applet for Euclid Alg.
  • static int EuclidGCD(int m, int n)
  • int rmn
  • while (r!0)
  • mn
  • nr
  • rmn
  • return n
  • //??EuclidGCD() ??????
  • //??EuclidGCDApplet ??????

23
Java Applet for Euclid Alg.
  • ????(??EuclidGCDApplet.html)
  • lthtmlgt
  • lth1gtEuclidGCDApplet???lth1gt
  • ltapplet code"EuclidGCDApplet.class" width350
    height100gt
  • lt/appletgt
  • lt/htmlgt
  • ????(????????EuclidGCDApplet.html)

24
Correctness and Efficiency
  • ???????,???????????(correctness),????????????????,
    ???????????,??????????????(efficiency)?,??????????
    ???????????????,????????????????????,?????????????
    ???

25
An Alg. for Testing Primes
Algorithm Prime1(n)Input????2????nOutputtrue?false(??n????????)for i?2 to n-1 do if (ni)0 then return falsereturn true
  • ??????,????2??????n,?n???,????Prime1???????????(n
    i)???????((ni)0)??n-2???,?????n??????,?n????,???
    ?Prime1??????????????????1?,?????n??????

26
A Theorem for Testing Primes
?? ???????2???n??,???????? ???(1??)?????n,? n??????
27
Hint
  • ????n??????????,??,16????1?16(11616)?2?8(2816)
    ?4?4(4416)?????????,???????n????,??????????n????
    ,??,??????????n??????????1???????n????????n???????

28
The Other Alg. for Testing Primes
  • ???????????????????Prime2

Algorithm Prime2(n)Input????2????nOutputtrue?false(??n????????)for i?2 to vn do if (ni)0 then return falsereturn true
29
Prime1Applet
  • ????(??Prime1Applet.java)
  • //??Prime1Applet.java
  • //????????????2???,???????????(prime number)
  • import javax.swing.
  • public class Prime1Applet extends JApplet
  • public void init()
  • int ??n
  • String ????,????
  • ????JOptionPane.showInputDialog("?????2???n?"
    )

30
Prime1Applet
  1. ??nInteger.parseInt(????)
  2. boolean ???
  3. long ????????System.currentTimeMillis()
  4. ???Prime1(??n)
  5. long ????????System.currentTimeMillis()
  6. long ???????????????-????????
  7. if(???) ??????n"???"
  8. else ??????n"????"
  9. ????("\n????????"???????"??(milli-seconds)
    ")

31
Prime1Applet
  1. JOptionPane.showMessageDialog(null,????)
  2. //??init() ??????
  3. boolean Prime1(int n)
  4. for (int i2iltn-1i)
  5. if (ni0) return false
  6. return true
  7. //??Prime1Applet ??????

32
Prime1Applet
  • ????(??Prime1Applet.html)
  • lthtmlgt
  • lth1gtPrime1Applet???lth1gt
  • ltapplet code"Prime1Applet.class" width350
    height100gt
  • lt/appletgt
  • lt/htmlgt
  • ????(????????Prime1Applet.html)

33
Prime2Applet
  • ????(??Prime2Applet.java)
  • //??Prime2Applet.java
  • //????????????2???,???????????(prime number)
  • import javax.swing.
  • public class Prime2Applet extends JApplet
  • public void init()
  • int ??n
  • String ????,????
  • ????JOptionPane.showInputDialog("?????2???n?"
    )
  • ??nInteger.parseInt(????)

34
Prime2Applet
  • boolean ???
  • long ????????System.currentTimeMillis()
  • ???Prime2(??n)
  • long ????????System.currentTimeMillis()
  • long ???????????????-????????
  • if(???) ??????n"???"
  • else ??????n"????"
  • ????("\n????????"???????"??(milli-seconds)
    ")

35
Prime2Applet
  • JOptionPane.showMessageDialog(null,????)
  • //??init() ??????
  • boolean Prime2(int n)
  • for (int i2iltMath.sqrt(n)i)
  • if (ni0) return false
  • return true
  • //??Prime2Applet ??????

36
Prime2Applet
  • ????(??Prime2Applet.html)
  • lthtmlgt
  • lth1gtPrime2Applet???lth1gt
  • ltapplet code"Prime2Applet.class" width350
    height100gt
  • lt/appletgt
  • lt/htmlgt
  • ????(????????Prime2Applet.html)

37
  • QA

38
????1-3.???????Prime2??????????

39
????1-2.???????Prime1??????????

40
Result


Write a Comment
User Comments (0)
About PowerShow.com