Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Data Structures

Description:

????? BIG O ??? loop ????? (???) ?????????????? Nested loop ?????????? ... Maximum Subsequence Sum, ???????????????? code ?????? Big O ???????? ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 53
Provided by: cpEngC
Category:
Tags: big | data | structures

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures
  • ???????????????????????????
  • Algorithm ??? ????????????????????????????????????
    ??????

2
Introduction
  • ????????????????????????????
  • ?????? code ???? ? ???????
  • ?????????????????????????????????????????????

3
???????????? ????????????????????? 5 ????? (1 ???
5 ) ???????????????????
Linked ist
  • P

1
2
3
5
4
Tree (Binary Search Tree)
4
?????????????????? (???)
  • Heap

???????????????????????????? ?????????????????????
?????????????????????????????????????????
???????? Heap ?????????????
5
??????????????????????????
  • Big O
  • ???
  • ????? c ??? N0 ??????????????? NgtN0
  • ??????????????? T(N) ?????????????

6
???????????????? BIG O
  • ??? T(N) 339N ??? f(N) NN
  • ?????? N0 339 ??? C 1
  • ?????????????????????????? 339N0 lt 1(N0N0)
  • ?????????????????? -gt T(N) lt 1(340N)
    ??????????????

7
???????????????? BIG O (???)
  • ??????? T(N)O(N) ??????????????
    ???????????????????
  • ?????????????????????????? ?????????????????????
    ????????????????????????????????????????????
    ??????? ?????? O(N) ?????????
  • ???? O(N) ??????????????????????????????????
    execute ??? code ???????????????????????

8
????????????????????????? ??????????
  • sigmaOfSquare(int n) // calculate
  • 1               int tempSum
  • 2               tempSum 0
  • 3                for (int i1iltni)
  • 4               tempSum ii
  • 5               return tempSum

1 unit (declare only)
1 unit (assignment)
n1 unit
n unit
1 unit
??? ??? ??? assignment ??????? n ??? 3n unit
1 unit (return)
?????? 5n5 unit
9
??????????????????????????????
  • ????????????????????????????????????? running
    time ??????? ??????? ??????????? ?
    ????????????????? ??????? ?????????? running time
    ??????????????????????????????????????????????????
    ????? ? ??????? ?????????????? Big O ???????
  • ?????????????? ??????????? loop ????????? run
    ????????????????????????????????????????? loop
    ??????????????? ???????? n ?????
  • ?????? ?????????????? loop ?????????? ????Big O
    O(n)
  • ???????????? 5n5 ?????? O(n) (????????? 5n5lt
    6n)

10
????? BIG O ??? loop ?????
  • For loop ??? Big O ??? ????????????? loop ?????
  • Nested loop
  • 1      for (i 1 i lt n i)
  • 2           for (j 1 j lt n j)
  • statements

n ???
n ???
Big O ?????? O(n2)
11
????? BIG O ??? loop ????? (???)
  • ?????????????? Nested loop ??????????
  • ??? T1(N)O(f(N)) ??? T2(N) O(g(N)) ????????
  • T1(N) T2(N) O(f(N)g(N))
  • ??????????????? f(n) g(n) n
  • ?????????? O(n2)

12
????? BIG O ??? loop ????? (??? 2)
  • Statement ????????????????????????
  • 1         for (i 0 i lt n i)
  • 2             statement1
  • 3         for (j 0 j lt n j)
  • 4                 for (k 0 k lt n k)
  • 5 statement2

O(n)
O(n2)
????????????????? ??????? O(n2)
13
????? BIG O ??? loop ????? (??? 3)
  • ????????????? running time ??? Statement
    ??????????????
  • ??? T1(N)O(f(N)) ??? T2(N) O(g(N)) ????????
  • T1(N) T2(N) max(O(f(N),O(g(N)))
  • ??????????? f(n) O(n), g(n) O(n2)
  • ?????????? O(n2)

14
????? BIG O ??? loop ????? (??? 4)
  • ???????????????????
  • 1             if (condition)
  • 2                   Statement1
  • 3             Else
  • 4                   Statement2

O(f(n))
O(g(n))
????????????????? ??????? max(O(f(n),O(g(n)))
15
????? BIG O ??? recursion
  • 1mymethod (int n)
  • 2 if (n 1)
  • 3       return 1
  • 4        else
  • 5             return 2mymethod(n 1) 1
  • 6       
  • 7

n ?????, big O O(n)
16
Maximum Subsequence Sum, ???????????????? code
?????? Big O ????????
  • Maximum Subsequence Sum ???
  • ?????????????? A1,A2, ??? An
  • Maximum Subsequence Sum ??????
    ????????????????? (????????????????
    ????????????????????)
  • ???????????? -2, 11, -6, 16, -5, 7
  • ???????? 11, -6, 16 ??? 21 (????? ?????????????)
    ??????????????? ????? ??????? 11, -6, 16, -5, 7
    ?????????????? 23 ?????????? Maximum Subsequence
    Sum

??????
17
?????????? max sub sum ?????? 1
  • 1 int maxSubSum01 ( int a)
  • 2 int maxSum 0
  • 3         for (int i 0 i lt a.length i)
  • 4          for (int j i j lt a.length j)
  • 5             int theSum 0
  • 6                   for (int k i k lt j
    k)
  • 7             theSum ak
  • 8                  
  • 9                   if (theSum gt maxSum)
  • 10         maxSum theSum
  • 11               
  • 12               
  • 13         return maxSum
  • 14      
  • 15

index ???
index ???????
???????????????????????
???????????????????????
18
?????????? max sub sum ?????? 1 (???)
  • ??????? 1 ????? big O O(n3) ????????????
  • ??????????????????? ??????????????????????????????
    ????
  • ???????????????? 0 ???2 ?????? ???????????????????
    ??? 0 ??? 3 ???????????????????????
    ???????????????????????????????? 0 ???2 ???

19
?????????? max sub sum ?????? 2
  • 1             int maxSubSum02 (int a)
  • 2             int maxSum 0
  • 3                   for (int i 0 i lt
    a.length i)
  • 4             int theSum 0
  • 5                   for (int j i j lt
    a.length j)
  • 6             theSum aj
  • 7                   if (theSum gt
    maxSum)
  • 8             maxSum theSum
  • 9            
  • 10               
  • 11               
  • 12         return maxSum
  • 13             

????????
???????????????????????????????
BIG O O(n2)
20
?????????? max sub sum ?????? 2 (???)
-2
11
-6
4
  • ????? i0, j0 theSum -2
    maxSum 0
  •  
  • ????? i0, j1 theSum -2 11
    9 maxSum ???????? 9
  •  
  • ????? i0, j2 theSum 9 (-6)
    3 maxSum ????????? 9
  •  
  • ????? i0, j3 theSum 3 4
    maxSum ????????? 9

21
?????????? max sub sum ?????? 3
  • ??? divide and conquer
  • ????????????????
  • ???????????????????????? ????
  • ??????????????????????? ????
  • ??????????????? (?????????????????????????????????
    ???????????????)

22
?????????? max sub sum ?????? 3 (???)
1 -2 7 -6 2 8 -5 4
Max sub sum ?????????? 7
Max sub sum ?????????? 10
??????????????????????????? (-6) ??? 1
?????????????????????? (2) ??? 10
Max sub sum ?????????????????????????? ??? 1 10
11 ???????????
23
?????????? max sub sum ?????? 3 (??? 2)
  • 1int maxSumDivideConquer (int array, int
    leftindex, int rightindex
  • 2 // ????????????????????????????????????????????
    ??????????????? ???????????????????????????
  • 3 if (leftindex rightindex) // Base
    Case
  • 5        if (arrayleftindex gt 0 )
  • 6             return arrayleftindex
  • 7             else
  • 8                   return 0 // maxSubSum
    ?????????????? 0 ????????
  • 9       
  • 10     int centerindex (leftindex
    rightindex)/2
  • 12     int maxsumleft maxSumDivideConquer(array
    , leftindex, centerindex)
  • 13 int maxsumright maxSumDivideConquer (
    array, centerindex 1, right)

T(n)
T(n/2)
T(n/2)
24
?????????? max sub sum ?????? 3 (??? 3)
  • 14     int maxlefthalfSum 0, lefthalfSum 0
  • 15        // ????????????????????????????????????
    ??????????????????????????
  • 16     for (int i center i gt leftindex i--)
  • 17             lefthalfSum lefthalfSum
    arrayi
  • 18             if (lefthalfSum gt maxlefthalfSum)
  • 19             maxlefthalfSum lefthalfSum
  • 20             
  • 21     

O(n/2)
25
?????????? max sub sum ?????? 3 (??? 4)
  • 22 int maxrighthalfSum 0, righthalfSum 0
  • 23    // ?????????????????????
    ??????????????????????????????????????
  • 24   for (int i centerindex 1 i lt
    rightindex i)
  • 25    righthalfSum righthalfSum array i
  • 26             if (righthalfSum gt
    maxrighthalfSum)
  • 27             maxrighthalfSum righthalfSum
  • 28              
  • 29   

O(n/2)
26
?????????? max sub sum ?????? 3 (??? 5)
  • 30 // ????????? max ??????? 3 ???? ???
    ????????????, ???????????, ????????????????? 2
    ?????
  • 31 return max3 (maxsumleft, maxsumright,
    maxlefthalfSum maxrighthalfSum)
  • ??????? ????????????????????
  • T(n) 2T(n/2) 2O(n/2)

????????? max ????????? ????????????
??????????????
27
?????????? max sub sum ?????? 3 (??? 6)
  • ?????????? unit time ?????? ?????????????? BIG O
    ???
  • T(n) 2T(n/2) 2O(n/2)
  • 2T(n/2) O(n)
  • 2T(n/2) cn
  • ??? n ????????????

????? O(n) lt cn ????????
(1)
28
?????????? max sub sum ?????? 3 (??? 7)
  • ???????????????? ?????

(2)
(3)
(X)
29
?????????? max sub sum ?????? 3 (??? 8)
  • ???????? (1) (2) (3) .. (x) ?????
  • ??????????????????????????????????? ???? c
    ???????????????????? log2 n ?????????????
  • ??? n ?????? ?????
  • ???????? T(1) ???????????? ????????????????????
    T(n) ?? Big O O(n log n)

30
?????????? max sub sum ?????? 4
  • ???????????????? 2 ?????????? ????????????????????
    ?????
  • ????????? maximum subsequence sum
    ????????????????? ????????? 3, -5, 1, 4, 7, -4
  • ??? -5 ??????????????? sequence ??????????????
    ?????????????????????????????? (?????????????? 1)
    ??????????????????????????? (?????????????????????
    ?????? maximum subsequence sum ??????????????????
    0)

31
?????????? max sub sum ?????? 4 (???)
  • ??????????????????
  • subsequence ???????????????? ?????????????????????
    ??????? maximum subsequence sum ???
  • ????????????????????????????????? ?????? i ????
    index ???????????? subsequence ??? j ???? index
    ???????????????? subsequence (????????????????????
    ?? subsequence ???????????) ??? p ???? index ?? ?
    ??????? i1 ??? j

3 4 1 -3 -9 1 5
i
j
p
32
?????????? max sub sum ?????? 4 (??? 2)
  • ??????????????? (???)
  • ??????????????????????????????????? j ????? ????
  • ??? aj ?????? ??????????? maximum subsequence
    sum ????????? ?????? maximum subsequence sum
    ???????????????????
  • ??? aj ??????? ????????????? aiaj
    ?????????????? aiaj ??????? ????????
    aiaj ??????? ?????? ???????????????????????
    ????? maximum subsequence sum ????????????????
    ?????????????????? aj ??????? ??????????????
    ???????????????????? ????? subsequence
    ?????????????? ????????????????????? j
    ??????????? ?????????????? i ???

33
?????????? max sub sum ?????? 4 (??? 3)
  • ?????????????? i ???????????????????????????
  • ??????????? ????????? aj ????????????????
    aiaj ??????????? ??????????? ????????? i
    ????????????????????????? i ??? p
    ??????????????????????? ai ??? ap (??? p
    ????????????????????? i ??? j) ???????????????????
    ???????? ??????? ????????? j ??? i ???????????
    ???????????????????? max sub sum ???
  • ??????????????????? max sub sum
    ?????????????????? ??????????????????????????????
    ??????????? j1 ????????????? i ????? j1 ???

3 4 1 -3 -9 1 5
i
j
p
34
?????????? max sub sum ?????? 4 (??? 4)
  • 1    int maxsubsumOptimum (int array)
  • 2     int maxSum 0, theSum 0
  • 3         for (int j 0 j lt a.length j)
  • 4          theSum theSum array j
  • 5                   if ( theSum gt maxSum)
  • 6             maxSum theSum
  • 7                   else if (theSum lt 0) //
    ?????? aj ????????????
  • 8                  
    //sequence ??????
  • 9                     theSum 0 //
    ?????????????????????????
  • 10 // j1
  • 11               
  • 12               
  • 13       return maxSum
  • 14  

35
????? log ?? BIG O
  • ???????????????????????? (O(1))
    ??????????????????????????????????? ? ???? ? ???
    (??????? 3 ??? maximum subsequence sum)
    ????????????? Big O ???? O(log n)
  • ??????????????????????????????????????????????
    ?????????????????????????????????? O(n) ????

36
?????????????????????? O(log n)
  • ?? 5 ?????????????????????????????????????????????
    ????????????
  • ????????????????????????????????????? (Big O ???
    O(n)) ?????????????
  • ??????????????????????????????? (Binary Search)

37
  • int binarySearch (int a, int x)
  • int left 0, right a.length 1
  • while (left ltright)
  • int mid (left right)/2
  • if (amid lt x )
  • left mid 1
  • else if (amid gt x)
  • right mid 1
  • else
  • return mid
  • return -1 // ?????????????????????????

Big O O(log2 n)
38
???????? O(log n) (???)
  • ???????????????

long gcd (long m , long n) while (n!0)
long rem mn m n n
rem return m
???? Big O ??????????
????????????????????? Big O (????????) ??????????
????????????????????????????????????
39
????? big O ????????????????????
  • ???????????????????????
  • ??? M gt N ???? M mod N lt M/2 ???????????????????
  • ??? N lt M/2 ???????????????????????? N
    ??????????? ?????????????????? M/2 ?????????
  • ??? N gt M/2 M ??? N ????? 1 ?????? M-N ??????? 1
    ?????? M (gt M/2) ??????????? ????????????? M/2
    ?????????
  • ??????????? ???????????? n ???????????????? m
    ????????????????? (n2) ???????????? (n2)
    ???????????????????????????????????????????? n
    ???????????????????????????????? ???????
    ??????????????????????????????????? ?

40
???????? O(log n) (???2)
  • ????????????? xn ????????????????????????
  • long power (long x, int n)
  • if (n0)
  • return 1
  • if (isEven (n))
  • return power (xx, n/2)
  • else
  • return power (xx, n/2)x

Big O ??? O (log2 n) ???????????????? 2 ?????
(??????) ????????????????????????? method
41
?????????????? O(log n)
  • logk n O(n) ????? k ????????????
  • (????????????????? logarithm function
    ?????????????????????)
  • ????????? f(n) loga n ????? big O ???? O(logb
    n) (a ??? b ????????????????????? 1)
  • (??????? log function ?? growth rate ???????)

42
?????????????????? log ?? growth rate ???????
  • ??? ???

43
Runtime -????(??)?????(????)
  • c
  • log n
  • logk n
  • n
  • n log n
  • n2
  • n3
  • 2n

44
????????? ? ??????????? Big O
  • Big Omega ( )
  • T(N) (g(N)) ????????????? C ??? N0
    ????
  • T(N) gt C g(N) ??? NgtN0
  • ???????? ??? f(N) (N2) ????????? f(N)
    (N) (N1/2) ????????????????????????????????
    ?????????

45
????????? ? ??????????? Big O (???)
  • Big Theta ( )
  • T(N) (h(N)) ?????????? T(N) O(h(N)) ???
    T(N) (h(N))
  • (????? c1, c2, N0 ??? c1h(N) lt T(N) lt c2h(N)
    ?????? N gt N0

46
????????? ? ??????????? Big O (??? 2)
  • small O
  • T(N) o(p(N)) ??? T(N) O(p(N)) ??? T(N)
    (p(N))

47
?????????????????????????
  • T(N) O(f(N)) ??????????????????? f(N)
    (T(N))
  • ???????????????????? f(N) ???? upper bound ???
    T(N) ??? T(N) ??????????????? lower bound ???
    f(N)
  • f(N) N2 ??? g(N) 2N2 ????? Big O ??? Big
    ??????? ??????? f(N) (g(N))
  • f(N) N2 ?? Big O ?????????? (???? O(N3), O(N4))
    ???????????????????? O(N2)
  • ????????????????? f(N) (N2) ?????????????
    ??? Big O -??? N2 ??????? Big O ???????????

48
???????????? ???????????????????????
  • ??? T(N) ??? Polynomial degree k ????
  • T(N) (Nk)
  • ??????????? ??? T(N) 5N4 4N3 N ???????????
    T(N) (N4)

49
Best case Worst case ??? Average case
  • worst case ??? ???????????? run
    ????????????????????????????
  • ???? best case ??? ????????? run
    ?????????????????????????
  • average case (??????????) ????
  • ?????????? input (??????????) ???????????
  • ??????????? input ????????????????????????? (unit
    time) ????? run
  • ????????? (average case running time)
    ????????????? unit time ?????? input / ?????
    input

50
Average case (???)
  • ????????????????????????????? ????????????????????
    ?
  • input ?????????????????????????????????
  • ????????????????????????????
  • ?????????????????????????????????????? input
    ??????? ? ????????????????????
  • Average case ????????????????
    inputi ??? unit time ???????? inputi

51
????????????? Average case
  • ??????????????? x ?????????????? n
  • Best case ???????????????? x ?????????
  • Worst case ????????????? x ???????????????????????
    ?? (?????????????????)
  • Average case
  • ??????????????????????????????????????? x ???????
  • ??????????? ???????? x ????????????????????? ???
    1/n

52
????????????? Average case (???)
  • Average Case running time 1/n
    (?????????????????????????????? x ??????????? 1)
    1/n (?????????????????????????????? x
    ??????????? 2) ... 1/n (???????????????????
    ??????????? x ??????????? n ?????????????????)
  • (1 2 n) / n (n1)/2
  • O(n) big O ??? worst
    case
Write a Comment
User Comments (0)
About PowerShow.com