# ????? (Analyzing Algorithms) - PowerPoint PPT Presentation

Title:

## ????? (Analyzing Algorithms)

Description:

### Introduction to the Design and Analysis of Algorithms -- A strategic approach, 2E, R.C.T. Lee et. al., ... Introduction to Algorithms, Cormen et. al., MIT Press. – PowerPoint PPT presentation

Number of Views:229
Avg rating:3.0/5.0
Slides: 33
Provided by: Jian1155
Category:
Tags:
Transcript and Presenter's Notes

Title: ????? (Analyzing Algorithms)

1
?????(Analyzing Algorithms)
• ??
• ???????? ??Java??, ??? ?, ??????, 2005.
• Introduction to the Design and Analysis of
Algorithms -- A strategic approach, 2E, R.C.T.
Lee et. al., NcGraw Hill, 2005.
• Introduction to Algorithms, Cormen et. al., MIT
Press.

2
?????
• ?????????????????????(resources)?????
• ??(Resources)???(memory),??(communication),??(ban
dwidth),??? (logic gate),??(time)

3
?????
• ???????(running time)???????????????????????
• ????????????(machine-independent) ???

4
???(Complexity)
• ??????
• ?????(time complexity)
• ?????(space complexity)
• ????????????????????,
• ???????,?????????
• ??????time complexity!

5
????
• ?????????????????
• ????(best case)?????????????????????????
• ????(worst case)?????????????????????????
• ????(average case)??????????????????????????

6
????????????
• ??,????????????????(worst-case running time)
• ??
• ????????(upper bound)?
• ??????????
• ?????????????????????(insertion sort)

7
????????????
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??????

8
?????????????
• ??????,????2??????n,?n???,????Prime2???????????(n
i)???????((ni)0)?? -2???,?????n??????,?n????,?
???Prime2??????????????????1?,?????n??????

9
?????Prime1
• ??,????????,?????(worst case)?,???Prime1??????????
????n???????????(best case)?,???Prime1???????????
????n????????????,???Prime1?????(linear)
?????????(??????????)????(constant) ??????????

10
?????Prime2
• ?????????,?????(worst case)?,???Prime2????????????
????n???????????(best case)?,???Prime2???????????
????n????????????,???Prime2??????(square root)
?????????????(constant) ??????????

11
????(Asymptotic Notation)
• ????,???????????(asymptotic notation)??????????,??
??????????????????????,???????(problem
size)???????????
• ???????????(??????)???,???????(???????)???????(???
????)???????????????,??????,????????????????????
• ??????????(??????)????,??????????????????????????
??????????????????????????????,???????????????(???
???)??????,??????????????????????????

12
??(Order)
• ????????????????????,?????????????????(order)?????
,???????????????,?????????????????,???????????????
?????,??????????????,??????????????????
• ??,?????????????n-2,??n???(??????)?,?????????????n
,?????????(???)??(??????????????????????????????)
?????????????35n212n11,??n???(??????)?,?????????
????n2,????????????????????28n31245n2162n321,?
?n????(???????),?????????????n3(??????)?

13
?O??(Big-O Notation)
• ???????O??(Big-O notation)??????????,?O???????????
???????????(order)???,??????????O?????????

????,?????????????????????,????????????????????????(order),????????O????(O ??order??)?
14
???O??
• ?????????O??
• ?? ?O?? (Big-O notation)
• ?f(n)?g(n)????????????????,????????cgt0??????n0???
???n?n0??,f(n)?cg(n)??,????f(n)O(g(n))?(??
?f(n)???Big-O of g(n)?,???????????f of n is of
Big-O of g of n?)?
• ??,??35n212n11??,??c58?n01(58?351211??),???n
?n01?,35n212n11?cn2(58n2)??,??,???35n212n11
O(n2)?

15
?????(Asymptotic Upper Bound)
• Def f(n) O(g(n)) ??(upper bound)"
• iff ? c, n0 ? f(n) ? cg(n) ? n ? n0
• e.g. f(n) 3n2 2
• g(n) n2
• ? n02, c4
• ? f(n) O(n2)
• e.g. f(n) n3 n O(n3)
• e. g. f(n) 3n2 2 O(n3) or O(n100 )

16
?????(Asymptotic Upper Bound)
17
?????(Asymptotic Lower Bound )
• Def f(n) ?(g(n)) ??(lower bound)
• iff ? c, and n0, ? f(n) ? cg(n) ? n ?
n0
• e. g. f(n) 3n2 2 ?(n2) or ? (n)

18
?????(Asymptotic Lower Bound )
19
Theta??
• Def f(n) ?(g(n))
• iff ? c1, c2, and n0, ? c1g(n) ? f(n) ?
c2g(n) ? n ? n0
• e. g. f(n) 3n2 2 ? (n2)

20
Theta??
• f(n) ?(g(n))

21
Theorem
• ??????f(n) ? g(n),
• ?

22
????????
• ?????????????????????????(?????,problem
size)??????????,??????????????????(?????,execution
time)??????????,?????????????????,???????????????
??,?????????????????????????????????????

23
?????
• ????????????????????,??????????????,??????????????
??
• O(1) lt O(log n) lt O( ) lt O(n) lt O(n log n) lt
O(n2) lt O(n3) lt O(2n)O(n!) ? O(nn)

24
??????????O????????
????? ??O???? ??
162 O(1) ?????(constant)??
63log n4 O(log n) ???(??)(sub-linear, logarithmic)??
37 52 O( ) ???(square root)??
n-2 O(n) ??(linear)??
156n81 O(n) ??(linear)??
28n31245n2162n321 O(n3) ??(cubic)??
14?2n457n3248n2-45n81 O(2n) ??(exponential)??
25
???????????????????
log n n n logn n2 n3 2n
0 1.00 1 0 1 1 2
1 1.41 2 2 4 8 4
2 2.00 4 8 16 64 16
3 2.83 8 24 64 512 256
4 4.00 16 64 256 4,096 65,536
5 5.66 32 160 1,024 32,768 4,294,967,296
26
???????????????
27
???????????????(???)
28
???????? vs. ???????
• ??????????O(p(n))????,???p(n) ????????,???????????
???(polynomial time algorithm)?????,?p(n)???????,?
?????????????(exponential time algorithm)

29
(No Transcript)
30
(No Transcript)
31
????? (ceiling) ?????? (floor)
32
????(modular operation)
• ?????a ? ?????n, ??a mod n ? a/n ???
• a mod n a -?a/n?n.
• ?(a mod n) (b mod n), ?????? a ? b (mod n)???a
?b ?mod n (modulo n)????(equivalent)??
• ????a ? b (mod n) ?a ?b ?mod n?????????