Data Structure ???? - PowerPoint PPT Presentation

About This Presentation
Title:

Data Structure ????

Description:

Title: PowerPoint Author: james Last modified by: ccw Created Date: 7/29/2003 1:54:13 AM Document presentation format: Company – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 36
Provided by: James504
Category:

less

Transcript and Presenter's Notes

Title: Data Structure ????


1
Data Structure????
  • ??? ???
  • ?0321(??? 02-04)

2
????
  • ???????????????,?Stacks, Queues, Linked Lists,
    Trees, Hash, Graph?? ???????????????????
    ??????????(algorithm)???????????????????????????,?
    ?????????????????????(Complexity)???????????????,?
    ????????????,???????????????

3
?????????
  • Data Structures and Program Design in C,Kruse,
    Tondo, Leung, ????,2597-1300x338
  • ??????C??,????. ????(02)2789-0561
  • ????????????? http//faculty.pccu.edu.tw/cweng/
  • ??????????????,???????????????

4
?????????
  • ???? 30
  • ???? 40
  • ???? 30(?????????)
  • ???12001300
  • ???14001600
  • ???12001300
  • ???15001700

5
??? ??????????
  1. ???????
  2. ???(Algorithm)
  3. ??(program)?????(programming)
  4. ????????

6
1. ???????
  • ??(Data),???????????????(Word)???(Number)???(Sym
    bol)???(Graph)?,??????????????????????????????????
    ???????????????????????

7
????
  • ????????????????,??????????????????????????????,?
    ????????????,??????????(Information)

8
????
  • ?????????????,????????????(algorithm)???(logic)???
    ??????????????(relationship)?
  • ???????????????????????????????????,??????????????
    ???,????????????????????? ?

9
??????????????
  • ???????????????????,?????????????
  • ???????????????,???????????,???????????????(Data)
    ?????????????,????????????????(Information)?

10
2.???(Algorithm)
  • ?????????????????
  • ???????????????????????????????
  • ????????????????????????????,????????????????????
    ????

11
???????
  • ??(Input)0????????,????????????????
  • ??(Output)??????????,??????????
  • ???(Definiteness)?????????????????????
  • ???(Finiteness)????????????,?????????
  • ???(Effectiveness)???????,????????????????

12
??????????
  • ??????????????
  • ???????????????,??????SPARKS?PASCA-LIKE????
  • ??????????????????
  • ????????)??????????????????,??????????
  • ??????????????????????????????,??C???C???Java??
    ?Visual Basic???,??????C????????

13
??????????????
  • ???????????,????????????????,??????????????????,?
    ????????(waiting loop)?????????,??????????????????
    ???
  • ??????????????????,???????????????,?????????????

14
3. ??(program)?????(programming)
  • ?????????
  • ??????????????????,??????????
  • ????????,?????????,??????????????????????
  • ???????????????????,????????????
  • ??????????,?????????
  • ?????????????????????,???????????????????????

15
???????
??????????(Basic Structure)
?????? ???
???? ???????
???? ?????????
???? ??????????????????
16
???????????
  • ?????????????????,???????????????????????????????
    ??,???????
  • ?????????????,????????,?????????,??????????,?????
    ????

17
?????????
  • ??????(atomic data type)?????????(physical data
    type)
  • ???????(structure data type)?????????(virtual
    data type)
  • ??????(Abstract Data TypeADT)

18
??????
  • ?????????,????????????????????????,???????????????
    ?????C????????????(int)???(char)???????(float)????
    ???(double)?

19
???????
  • ???????????,?????????????????,????(string)???(set)
    ???(array)?

20
??????
  • ????????????,ADT????????????????????????????,?????
    ???ADT?????,???????????????(stack)???(queue)??????
    ??ADT???

21
4. ????????
  • Given a problem, there may be several possible
    implementations.
  • Efficiency is the most important consideration
    including time and space.
  • Complexity Theory to estimate the time and space
    needed for a program. Its machine independent.
  • Space Complexity of a program the amount of
    memory space needed to complete a program.
  • Time Complexity of a program the amount of
    computation (computer) time needed to complete a
    program.

22
Space Complexity S(p)S(p)ScSp(I)
  • Sc (Fix space requirement) including instruction
    space, constants, simple variables, and fix-size
    structures. The Sc is independent of the number
    and size of inputs and outputs. e.g. int i,
    sum0, A100
  • Sp(I) (Variable space requirement) depends on a
    particular instance I of a problem. Instance I
    may be a function of number, size, or values of
    inputs and outputs. e.g. int n //score list
    of a course

23
Time Complexity T(p)T(p)TcTp(I)
  • Tc (Fix time requirement) compile time, Tc is
    independent of any instance of the problem.
  • Tp(I) (Variable time requirement) execution time,
    depends on a particular instance I of a problem.
    e.g. matrix
    multiplication C A B
  • (0,0)
    0,1 0,2 0,3 0,n 0,0
  • 1,0


  • 2,0
  • n,0
  • (n) (n) gt 2n 2n nn an2bnc gt 2n3
    an2bnc

24
Asymptotic Notations- for measuring space and
time complexities
  • O(Big-oh)
  • O(omega)
  • ?(theta)

25
Big-oh???
  • O(g(n))???????????????????????????g(n),???????????
    ???????(space or time complexity)?O(g(n))(??big-oh
    of g(n)?order is g(n))?
  • Definition The function f(n) is said to be of
    order at most g(n) if there are positive
    constants c and n0 such that f(n)ltcg(n) for all
    n, ngtn0.
  • Therefore, Big oh is the smallest upper bound
    of f(n).

26
???Big-oh?????
  • O(1)???? (constant)
  • O(log2n)?? (logarithmic)
  • O(log22n)?? (log squared)
  • O(n)???? (linear)
  • O(nlog2n)??n log n
  • O(n2)???? (quadratic)
  • O(n3)???? (cubic)
  • O(2n)???? (exponential)

27
(No Transcript)
28
O(omega)???
  • O?????????????,???Big-oh????????????,?O???????????
    ??????O???
  • Definition The function f(n) is said to be of
    order at least g(n) if there are positive
    constants c and n0 such that f(n)gtcg(n) for all
    n, ngtn0.
  • Therefore, Big oh is the largest lower bound of
    f(n).

29
?(theta)???
  • ????Big-O?O?????????????????
  • Definition The function f(n) is ?(g(n)) iff
    there exists positive constants c1, c2 and n0 ,
    such that c1 g(n) ltf(n)lt c2 g(n) for all n,
    ngtn0.
  • Therefore, ? is both the smallest upper bound
    and the greatest lower bound of f(n).

30
Examples for asymptotic notations
  • 1. 3n 2 ?(n), ?3n 2 ? 4n, for all n ? 2, c
    4.
  • 2. 3n 3 ?(n), ?3n 3 ? 4n, for all n ? 3, c
    4.
  • 3. 3n 3 ?(n2), ?3n 2 ? 3n2, for all n ? 2, c
    3.
  • (2) is correct. (3) is wrong. Big oh should be
    a smaller function of n.
  • 4. 10n2 4n 2 ?(n2), ?10n2 4n 2? 11n2, for
    all n ? 5, c 11.
  • 5. 6.2n n2 ?(2n), ?6.2n nn? 7.2n, for all
    n ? 4, c 7.
  • 6. 3n 3 O(n), ?3n 3 ? 3n, for all n ? 1, c
    3.
  • 7. 3n 3 O(1), ?3n 3 ? 3, for all n ? 1, c
    3.
  • (6) is correct. (7) is wrong. Omega should be a
    larger function of n.
  • 8. 3n 2 T(n) ?3n?3n 2 ? 4n, for all n ? 2, c1
    3, c2 4 , and n0 2.

31
Example for S(p) addup values of n elements in
an array called list.
  • main( ) float addup(float list , int n)
  • float listn, temp 0.0 float temp 0.0
  • int i int i
  • for( i0 i lt n i) for( i0 i lt n i)
  • temp listi temp listi
  • return temp return temp
  • Smain(n) c n O(n) Saddup(n) c O(1)
  • Other languages may need to pass the whole
    array, but addup passes only addresses of the
    1st element and the size of array.

32
Example for T(p) use step count instead of
execution time.
  • 1. on-line step count
  • float sum(float list , int n) / calculate
    the sum of an array /
  • float tmp0.0 count / tmp assignment /
  • int i
  • for ( i0 i ltn i )
  • count / for loop /
  • tmplisti count adding up /
  • count / last time to check for loop and
    fails /
  • return tmp count / for return /
  • gt step count 2n 3 gt Tsum(n) O(n)

33
  • 2. Tabular method
  • void add( int a , int b , int c ,
    int row, int col)
  • Step per exec. freq. total
  • int i, j 0 0
    0
  • for ( i0 iltrow I) 1
    row 1 row1
  • for ( j0 jltcol j) 1
    row(col1) rowcol row
  • cij aijbij 1
    rowcol rowcol
  • In total, we have step count 2rowcol 2row 1
    .
  • gt Tadd(row, col) O(rowcol)
  • Thus, if row gtgtcol, one may want to exchange i
    and j to reduce the step count and execution
    time.

34
  • 3. Execution time Measurement
  • include lttime.hgt 1. Clock 2. Time
  • Before execution, use start clock() start
    time(NULL)
  • After execution, use stop clock() stop
    time(NULL)
  • Type return clock_t time_t
  • result in sec. (stop - start)/
    CLK_TCK difftime(stop, start)
  • When measuring the execution of a program, we
    have
  • CLK_TCK 18.2, beginclk 66, stopclk 193,
    diffclk 127, time 6.98
  • begintime825316157, stoptime 825316164,
    difftime 7
  • Note 1.The time is begin at around 1970.
  • 2. Use ld to print out a long integer

35
trade-off between program space and execution time
  • Example Two ways to interchange two elements,
    using a functions or a macro
  • 1. using a function 2. using a macro
  • void swap (int x, int y) define swap(a,b,t)
    ((t)(a), (a)(b), (b)(t))
  • int temp swap(p, q, temp)
  • temp x
  • x y Disadv duplicated macros may takes
    lots of space.
  • y temp Adv no calls returns,
    execution time is reduced.
  • Adv single copy of function gt save space.
  • Disadv calls returns gt waste time.
  • Therefore, a careful evaluation of the
    trade-offs among various aspects before the
    implementation of a solution is important.
Write a Comment
User Comments (0)
About PowerShow.com