Cormen, Leiserson, Rivest, Introduction to Algorithms . - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Cormen, Leiserson, Rivest, Introduction to Algorithms .

Description:

Cormen, Leiserson, Rivest, Introduction to Algorithms . ... Rivest, Introduction to Algorithms . * ( ) : S = 0; ... – PowerPoint PPT presentation

Number of Views:511
Avg rating:3.0/5.0
Slides: 33
Provided by: DanG114
Category:

less

Transcript and Presenter's Notes

Title: Cormen, Leiserson, Rivest, Introduction to Algorithms .


1
???? ??????
Lecture1 of Geiger Itais slide
brochure www.cs.technion.ac.il/dang/courseDS
???? ??????
CS-234218
???? ????? ??? 737 ???? ???? ? 1030- 1130
http//webcourse.cs.technion.ac.il/234218//
??? ?????? ?????? (???? ?? ?????) Cormen,
Leiserson, Rivest, Introduction to Algorithms .
?GeigerItai,2001
2
???? ??????
Lecture1 of Geiger Itais slide
brochure www.cs.technion.ac.il/dang/courseDS
????? ?????
  • ????????? ?? ???? ?????? ????????? ???????
  • ????? ???? ?????? ??????
  • ????? ???? ?????? ?????? ?????

??????? ????? ?????? ??????, ???, ?????, ???
????????, ???? ?????...
??????? ???????? ????, ????? ??????? ?????,
????? ?????, ???????? ???? ????, ????
?GeigerItai,2001
3
?????? ?????
  1. ???? ?????? ??????? ???????? ???????????
  2. ?????? ??????? ???????
  3. ???? ???? ?????
  4. ??? AVL
  5. ??? 3-2
  6. ?????? ???????
  7. ?????? ?????
  8. ????? ?????? ????
  9. ????
  10. ????? ????????
  11. ?????
  12. ????? ????

4
?????? ???????
????? ?????? ???????? (Abstract Data Type ADT)
??? ???? ?? ?????? ?????? ?? ????? ??????.
????? ?? ???? ?????? ??? ???? ?????????, ??? ???
?????, ?????? ?? ??????? ?? ???? ???????.
????? ??? ???????
??? ??? ???? ?? ????? ?-? top
8
7
?????
top -1 ????? ????top top 1 Atop x
??? ???????if top gt-1 returnAtop
????? ???? top top 1

6
top
5
4
3
2
1
0
?? ! ??? ????? ?? ?????? ??????? ???? ??????.
5
?????? ?????? ?????? ????????
Last In -- First Out LIFO ????? ????
????? ????
?????? (Stack) ?????? ?"? ??????? ?????
create(S) ????? ?????? S ???? ????. push(S,x)
????? ???? ??? ??? x ???? ??????? S. top(S)
????? ?? ????? ????? ??????? S (??????? ????
?????). pop(S) ????? ?? ????? ????? ???????
S. is-empty(S) ????? true ?? ??????? S ????
?-false ????.
is-emptyStacks ? true,false
???????
6
?????? ?????? ?????? ???????? (????)
?????? ??????? ??????? ?? ?????? ????? 1. ????
???? pop, top ?? ?? ?????? ?? ????. 2. ??? ????
create(S), is-empty(S) ????? ??? true. 3. ????
????? push, ?????? pop, ??????? ?? ?????.
is-empty(create(S)) true
??? 2
pop(push(S,x)) S
??? 3
?? ????? ??????? ?? ???????.
??????
is_empty(pop(push(create(S))))true
?? ????? ???? ????? ?? ??????? ?????? ?? ??????.
7
8
2
5
2
7
8
Q
P
Create(Q)
Push(Q,2)
Push(Q,5)
Push(Q,7)
Pop(Q)
Create(P)
Push(Q,8)
Is-Empty(P)
Push(P,8)
Is-Empty(Q)
Push(P,2)
Pop(Q)
8
?????? ??????
  1. ????? ????? ???? (??? ??????).
  2. ????? ????? ????? ??????.

define NULL 0 typedef struct node DATA_TYPE
info struct node next NODE typedef NODE
STACK
????? ????
????? ?????? ????
void create (STACK s) (s) NULL
9
????? ????
????? push(s,x)
void push ( STACK s, DATA_TYPE x) NODE
P P malloc (sizeof (NODE)) P -gt info x P
-gt next (s) (s) p
void push ( STACK s, DATA_TYPE x) NODE
P P malloc (sizeof (NODE)) P -gt info x P
-gt next (s) (s) p
void push ( STACK s, DATA_TYPE x) NODE
P P malloc (sizeof (NODE)) P -gt info x P
-gt next (s) (s) p
void push ( STACK s, DATA_TYPE x) NODE
P P malloc (sizeof (NODE)) P -gt info x P
-gt next (s) (s) p
void push ( STACK s, DATA_TYPE x) NODE
P P malloc (sizeof (NODE)) P -gt info x P
-gt next (s) (s) p
10
????? ?????? ????
????? pop(s)
void pop ( STACK s) STACK t t (s) ?
next free (s) (s) t
????? top(s)
DATA_TYPE top ( STACK s) return (s) ? info

11
????? ????? ??????
First In -- First Out FIFO ????? ????
????? ????
??? (Queue) ????? ?"? ??????? ????? create(Q)
????? ??? ???. head(Q) ????? ?? ??? ????? ?????
???? Q (???? ???? ?????). enqueue(Q,x) ?????
???? ?? ??? x ???? ???? Q. dequeue(Q) ????? ??
????? ????? ???? Q. is-empty(Q) ????? true ??
???? Q ??? ?-false ????.
12
????? ?? ??? ????? ????
head(Q) Qf enqueue(Q,x) Qr
x r (r1)
n dequeue(Q) f (f1) n is_empty(Q)
f r create(Q) f r 0
???? Q ?? n ?????? ?? ??? ???????
r ????? ?? ???? ????? ????? ??? ???? (rear) f
????? ?? ???? ????? ????? ???? (front)
?????? ?????? ??????? ?????????? ????? mod n(
n). ???? ??? ??. f (r 1) n ?? ????? "??
???" ????enqueue ?"?? ???" ???? .dequeue
???????
???
n9
13
enqueue(Q,4) enqueue(Q,5) enqueue(Q,9) enqueue(Q,2
) enqueue(Q,1) enqueue(Q,9)
Creat(Q)
7 6 5 4 3 2 1 0
5
r
3
9
1
dequeue(Q) dequeue(Q)
2
9
9
head(Q)?
f
r
5
enqueue(Q,3) enqueue(Q,5) enqueue(Q,1)
r
4
1
f
14
????? ??? ?"? ????? ??????
????? ???? ???? (last)
last
????? ???? ???? (first) . ???? ?? ??? ???? ??????
???? ??????? ?????? last.
15
????? ???? ADT ????????
?????? ?-ADT ???? ?? ??????? ??????? ??
???????. ???? ???? ????? ?? ??????.
Abstract Data Type ADT
  • ????? ?????? ??????? ?"?
  • ??????
  • ??? ???? ???? ?????? ???????, ??? ?????.
  • ???? ???? ??????? ??????.
  • ????? ??????? (????? ????? ???? ?????? ????? ).

16
??? ???? ?? ????????
??? ???? ?? ???????? A ???? ??? x ????? ?-TA(x) .
??? ????? ???? ?"? ???? ?????? ????? ??????????
???? ?? ??? ????. ??? ?? ????? ?????? ???????
??? ???????. (????, ???? ????? ???). ????? ?? ???
x ????? ?- x. ?????? ??????? ?????????????
????? ???? x, ???? ???? ??? ???? ????? ?????. ???
????? ????? ?????(worst case) ?? ???????? A
???? ??? ?????? n ????? ?"? TA(n) max
TA(x) xn
17
????? 1 ???? ???? a
sum 0 for (i 0 i lt n i) sum sum ai
A
??? ????? ?? ???????? ?? ???? ?? ??? n ??? TA(n)
c1?n c2 ???? c1,c2 ?? ?????? ???????
?????? ??????? ???? ?????.
18
??? ???? ?? ????????
????? 2
sum 0 for (i 0 i lt n i) if sum lt
ai1 sum sum ai else terminate
??? ??? ????? ?? ?????????? ??? ????? ????? ?????
?? ???????? ?? ????? TA(n) c1?n c2
19
???????? ??????? O
????? ???? f(n), g(n) ???????? ???????. ????
????????? f(n) ????? ?????? ????????? O(g(n)) ??
?????? ?????? n0,c ?? ???? n0 ? n ?????? f(n)
? c?g(n)
??? ?? ???? ?- g(n) ????? ??? ????? ?????????
???????? f(n) ????? ??? ?"? f(n) O(g(n)) ?????
?????? ????? f(n) ? O(g(n)).
f(n)
????? ?????? ?? ???? ???????? f(n) ??? ???????
???? ???? ??????, ???? ??? ????? ?? ????????,
???? g(n) ????? ???? ??????.
20
??????? ?????????????
???? ???? k ???? ??????
????? ???? ?????? c, n0, ?? ?? ???? n0 ? n
?????? f(n) ? c?nk.
21
??????? ??????
22
???????
?????? ?????? ?????? ??????. ??????!
23
???????? ???
????? ?????? ???? ????? ????. ????? ???????
T(n) lt c?n 1 ??????T(n) O(n) .
24
???????? ??? (????)
  • ????? ?????? ????? ????? ?? x ????? ????? ?? n
    ??????.
  • ??? ??? ?????? ?? x ????? ?????? ?????
    ???????????.
  • ?? ????? ?????? ???? ?- x ?????? ????.
  • ?? ????? ?????? ???? ?- x ??????? ?? ??? ?????
    ????? ?? ??????? ??????.
  • ?? ????? ?????? ??? ?- x ??????? ?? ??? ?????
    ????? ?? ??????? ???????.

????? ????? ?? ?? n ?? ???? ?? 2 . ????? ??
????? ????? ???????? ????? ???? ????? ???? ??????
(??? 4)Cormen, Leiserson, Rivest, Introduction
to Algorithms .
25
???????? ??? (????)
????? ?????? S 0 for ( i 1 i lt n i
) for ( j 0 jlt n j i ) S
?????? ??? ??? ?????? ???????. T(n) ? n (n-1) .
?????? ????? ???? i 1,
??????? n ??????. ,i 2
?n/2? ,i 3
?n/3? ??? i n
, n/n 1
???? ??? ???? ??? ????? ????? ?? ?? ????????
????? ??????T(n) O( n?log n)
26
??? ????? ?????????
????? ???? f(n), g(n) ???????? ???????. ????
????????? f(n) ????? ?????? ????????? ?(g(n))
(?????) ?? ?????? ?????? n0,c ?? ???? n0 ? n
?????? f(n) ? c?g(n)
27
??????? ?????????????
?????? ???? k ???? ??????
????? ???? ?????? c, n0, ?? ?? ???? n0 ? n
??????f(n) c?nk?.
????
28
??? ???? ?????????
????? ???? f(n), g(n) ???????? ???????. ????
??????? f(n)?(g(n)) (???) ?? f(n)O(g(n)) ???
f(n) ?(g(n)).
c2 g(n)
f(n)
c1 g(n)
f(n) ?(g(n))
n0
????? (??????????) ???? ??????? f(n)?(g(n)) ??
?????? ?????? n0,c1,c2 ?? ???? n0 ? n ??????
c1?g(n) ? f(n) ? c2?g(n).
?????
29
?????? o ???
????? ???? f(n), g(n) ???????? ???????. ????
????????? f(n) ????? ?????? ????????? o(g(n)) ??
??? ???? c ???? ???? n0 ?? ???? n0 ? n ??????
f(n) ? c?g(n)
????? (??????????) ???? ??????? f(n)o(g(n))
??
??????? log n o(n), n-100 ? o(n)
30
?????? ?????? ??????????
??? ?????? ???????? ??????????? ???? 1. ??????
????? ??????? 2. ????? ????? ????? ???? ????.
??? ????? ???????? ??? ????? ?????. ??? ??????
?? ?????? ??????? ???? ?????? ?????? ???? ????
T(n) n2 ?? ??? ?????? ???? ???? ???? ?? 1080
T(n) ???? ????????? ?????? ??? ??????? ?????
??? n ???? ???? ????? ??????? ?-1040 . ?????
???? ????? ???????? n0,c ???????? ???????
??????????? O, ?, ? ???? "??????".
31
?????. ???? ????????? A ?? ???? TA(n) 100 n
????????? B ?? ???? TB(n) 5nlog2n . ??????
????????? ???? ???????? A ???? ??????????
???????, ??? ???? ????? ???????? n lt 220 ????
???????? B.
220100?
A
B
220
32
???? ????? 1993 ????? ???????? ???? ?-C ???? ??
????????? ???.
Void func (int n) float x, delta int i for
(i 1 i lt n i) delta 1.0 / i x
i while ( x gt 0 ) x x delta
main() int n scanf(d,n) func(n)
Write a Comment
User Comments (0)
About PowerShow.com