Title: Algorithmics
1Algorithmics
- The discipline of algorithms
2Abu 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.).
3al-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)??????
4What 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
5What 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)
6A 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??
???,?????????
7A flowchart is an algorithm
8A program is an algorithm
- import javax.swing.
- public class EuclidGCDApplet extends JApplet
- public void init( )
- int ??m,??n,?????
- String ????1,????2,????
- ????1JOptionPane.showInputDialog("??????m?")
- ????2JOptionPane.showInputDialog("??????n?")
- ??mInteger.parseInt(????1)
- ??nInteger.parseInt(????2)
- ?????EuclidGCD(??m, ??n)
- ????"??"??m"???"??n"???????"?????
- JOptionPane.showMessageDialog(null,????)
- //??init() ??????
9Representation of Algorithms
- ??????????(????????)????(flow chart)????(pseudo
code)???????(high level programming
language)??????? - ??,???????????(?????)?????????? ?
????(Euclid)GCD???? - ????(Euclid)GCD?????????300?????????????,?????????
??????(GCD, Greatest Common Divisor)?
10Euclid GCD Algorithm
Euclid GCD????????????m?n,???????????(????????m?n??????)??E1.???? ??m??n???,????r?E2.???0?? ??r0???,??n?GCD?E3.???? ??mn,nr,?????E1?
11Pseudo Code
- ???????????????????????????????,?????????????,????
?????????????? - ????Goodrich?Tamassia??Data Structures and
Algorithms in JAVA?????????????
12Pseudo Code
- ??? Algorithm ????(??1,??2,)?????????????????,A
lgorithm Euclid-GCD(m, n) ???????????????m?n????E
uclid-GCD????,? - ????? ? ??,????????????????????,m?38
????38??????m?? - ????? ??,???????????????,m38
?????38???????m?????
13Pseudo Code
- ????? if??then??????? else ???????
???,????????????????????????????????????, - if m38 then b?38else c?38
- ?????m???38???,????38??????b???,??????38??????c
????
14Pseudo Code
- while??? while??do???? ???,????????????????????
???????,???????????????????, - while m38 do b?38 c?38
- ?????m???38???,??????38??????b??38??????c????
15Pseudo Code
- repeat??? repeat???? until?? ???,???????????????
???????????????????????????,???????????, - repeat b?38 c?38until m38
- ????????38??????b??38??????c???,????m???38?????
16Pseudo 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????
17Pseudo Code
- ??????? Ai ????A???(index)?i???,???n??????,??
????0,1,,n-1???,A5 ????A????5???? - ????? ??.??(??) ??????,?????????,???????????,M
ath.random() ?????Math??????random()??? - ????? return ????? ??????,return 38
?????38???
18Pseudo 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
19Java Method for Euclid Alg.
- ????????????????????,?????????,???????????????????
????????????????,?????????????????????Java????????
- int Euclid_GCD(int m, int n)
- int rmn
- while (r!0)
- mn
- nr
- rmn
-
- return n
20Java 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?")
21Java Applet for Euclid Alg.
- ????2JOptionPane.showInputDialog("??????n?
") - ??mInteger.parseInt(????1)
- ??nInteger.parseInt(????2)
- ?????EuclidGCD(??m, ??n)
- ????"??"??m"???"??n"???????"?????
- JOptionPane.showMessageDialog(null,????)
- //??init() ??????
22Java Applet for Euclid Alg.
- static int EuclidGCD(int m, int n)
- int rmn
- while (r!0)
- mn
- nr
- rmn
-
- return n
- //??EuclidGCD() ??????
- //??EuclidGCDApplet ??????
23Java Applet for Euclid Alg.
- ????(??EuclidGCDApplet.html)
- lthtmlgt
- lth1gtEuclidGCDApplet???lth1gt
- ltapplet code"EuclidGCDApplet.class" width350
height100gt - lt/appletgt
- lt/htmlgt
- ????(????????EuclidGCDApplet.html)
24Correctness and Efficiency
- ???????,???????????(correctness),????????????????,
???????????,??????????????(efficiency)?,??????????
???????????????,????????????????????,?????????????
???
25An 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??????
26A Theorem for Testing Primes
?? ???????2???n??,???????? ???(1??)?????n,? n??????
27Hint
- ????n??????????,??,16????1?16(11616)?2?8(2816)
?4?4(4416)?????????,???????n????,??????????n????
,??,??????????n??????????1???????n????????n???????
28The 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
29Prime1Applet
- ????(??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?"
)
30Prime1Applet
- ??nInteger.parseInt(????)
- boolean ???
- long ????????System.currentTimeMillis()
- ???Prime1(??n)
- long ????????System.currentTimeMillis()
- long ???????????????-????????
- if(???) ??????n"???"
- else ??????n"????"
- ????("\n????????"???????"??(milli-seconds)
")
31Prime1Applet
- JOptionPane.showMessageDialog(null,????)
- //??init() ??????
- boolean Prime1(int n)
- for (int i2iltn-1i)
- if (ni0) return false
- return true
-
- //??Prime1Applet ??????
32Prime1Applet
- ????(??Prime1Applet.html)
- lthtmlgt
- lth1gtPrime1Applet???lth1gt
- ltapplet code"Prime1Applet.class" width350
height100gt - lt/appletgt
- lt/htmlgt
- ????(????????Prime1Applet.html)
33Prime2Applet
- ????(??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(????)
34Prime2Applet
- boolean ???
- long ????????System.currentTimeMillis()
- ???Prime2(??n)
- long ????????System.currentTimeMillis()
- long ???????????????-????????
- if(???) ??????n"???"
- else ??????n"????"
- ????("\n????????"???????"??(milli-seconds)
")
35Prime2Applet
- JOptionPane.showMessageDialog(null,????)
- //??init() ??????
- boolean Prime2(int n)
- for (int i2iltMath.sqrt(n)i)
- if (ni0) return false
- return true
-
- //??Prime2Applet ??????
36Prime2Applet
- ????(??Prime2Applet.html)
- lthtmlgt
- lth1gtPrime2Applet???lth1gt
- ltapplet code"Prime2Applet.class" width350
height100gt - lt/appletgt
- lt/htmlgt
- ????(????????Prime2Applet.html)
37 38????1-3.???????Prime2??????????
39????1-2.???????Prime1??????????
40Result