Bài giảng cấu trúc dữ liệu và giải thuật 3

481 5 0
Bài giảng cấu trúc dữ liệu và giải thuật 3

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3 Bài giảng cấu trúc dữ liệu và giải thuật 3

Chapter 1: Introduction • Pseudocode • Abstract data type • Algorithm efficiency Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • What is an algorithm? Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • What is an algorithm? – The logical steps to solve a problem Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • What is a program? – Program = Data structures + Algorithms (Niklaus Wirth) Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • The most common tool to define algorithms • English-like representation of the code required for an algorithm Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • Pseudocode = English + Code relaxed syntax being easy to read instructions using basic control structures (sequential, conditional, iterative) Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode Algorithm Header Algorithm Body Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • Algorithm Header: – Name – Parameters and their types – Purpose • what the algorithm does – Precondition • precursor requirements for the parameters – Postcondition • taken action and status of the parameters – Return condition • returned value Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Pseudocode • Algorithm Body: – Statements – Statement numbers • decimal notation to express levels – Variables • important data – Algorithm analysis • comments to explain salient points – Statement constructs • sequence, selection, iteration Cao Hoang Tru CSE Faculty - HCMUT 10 September 2008 Example Algorithm average Pre nothing Post numbers read and their average printed i=0 loop (all data not read) i=i+1 read number sum = sum + number average = sum / i print average return End average Cao Hoang Tru CSE Faculty - HCMUT 10 10 September 2008 Linear Probing • When a home address is occupied, go to the next address (the current address + 1): hp(k, i) = (h(k) + i) MOD m Cao Hoang Tru CSE Faculty - HCMUT 40 01 December 2008 Linear Probing Harry Eagle 166702 Cao Hoang Tru CSE Faculty - HCMUT Hash Function 001 Mary Dodd (379452) 002 Sarah Trapp (070918) 003 Bryan Devaux (121267) 008 John Carver (378845) 306 Tuan Ngo (160252) 307 Shouli Feldman (045128) 002 41 01 December 2008 Linear Probing Harry Eagle 166702 Cao Hoang Tru CSE Faculty - HCMUT Hash Function 001 Mary Dodd (379452) 002 Sarah Trapp (070918) 003 Bryan Devaux (121267) 004 Harry Eagle (166702) 008 John Carver (378845) 306 Tuan Ngo (160252) 307 Shouli Feldman (045128) 002 42 01 December 2008 Linear Probing • Advantages: – quite simple to implement – data tend to remain near their home address (significant for disk addresses) • Disadvantages: – produces primary clustering Cao Hoang Tru CSE Faculty - HCMUT 43 01 December 2008 Quadratic Probing • The address increment is the collision probe number squared: hp(k, i) = (h(k) + i2) MOD m Cao Hoang Tru CSE Faculty - HCMUT 44 01 December 2008 Quadratic Probing • Advantages: – works much better than linear probing • Disadvantages: – time required to square numbers – produces secondary clustering h(k1) = h(k2) ⇒ hp(k1, i) = hp(k2, i) Cao Hoang Tru CSE Faculty - HCMUT 45 01 December 2008 Double Hashing • Using two hash functions: hp(k, i) = (h1(k) + ih2(k)) MOD m Cao Hoang Tru CSE Faculty - HCMUT 46 01 December 2008 Key Offset • The new address is a function of the collision address and the key offset = [key / listSize] newAddress = (collisionAddress + offset) MOD listSize Cao Hoang Tru CSE Faculty - HCMUT 47 01 December 2008 Key Offset • The new address is a function of the collision address and the key offset = [key / listSize] newAddress = (collisionAddress + offset) MOD listSize hp(k, i) = (hp(k, i-1) + [k/m]) MOD m Cao Hoang Tru CSE Faculty - HCMUT 48 01 December 2008 Open Addressing • Hash and probe function: hp: U × {0, …, m − 1} → {0, …, m − 1} set of keys probe numbers addresses 〈hp(k,0), hp(k,1), …, hp(k,m-1)〉 is a permutation of 〈0, 1, …, m-1〉 Cao Hoang Tru CSE Faculty - HCMUT 49 01 December 2008 Linked List Resolution • Major disadvantage of Open Addressing: each collision resolution increases the probability for future collisions ⇒ use linked lists to store synonyms Cao Hoang Tru CSE Faculty - HCMUT 50 01 December 2008 Linked List Resolution 001 Mary Dodd (379452) 002 Sarah Trapp (070918) 003 Bryan Devaux (121267) Harry Eagle (166702) Chris Walljasper (572556) overflow area 008 John Carver (378845) prime area 306 Tuan Ngo 307 Shouli Feldman (045128) Cao Hoang Tru CSE Faculty - HCMUT (160252) 51 01 December 2008 Bucket Hashing • Hashing data to buckets that can hold multiple pieces of data • Each bucket has an address and collisions are postponed until the bucket is full Cao Hoang Tru CSE Faculty - HCMUT 52 01 December 2008 Bucket Hashing Mary Dodd (379452) Sarah Trapp (070918) Harry Eagle (166702) Ann Georgis (367173) 001 002 Bryan Devaux (121267) 003 Chris Walljasper(572556) linear probing Shouli Feldman (045128) 307 Cao Hoang Tru CSE Faculty - HCMUT 53 01 December 2008 Indexing = Hashing ... Tru CSE Faculty - HCMUT 32 10 September 2008 Big-O Notation • f(n) = c.n ⇒ f(n) = O(n) • f(n) = n(n + 1)/2 = n2/2 + n/2 ⇒ f(n) = O(n2) Cao Hoang Tru CSE Faculty - HCMUT 33 10 September 2008 Big-O... matrix3 ) Add matrix1 to matrix2 and place results in matrix3 Pre matrix1 and matrix2 have data size is number of columns and rows in matrix Post matrices added - result in matrix3 r=1... matrix3 ) Add matrix1 to matrix2 and place results in matrix3 Pre matrix1 and matrix2 have data size is number of columns and rows in matrix Post matrices added - result in matrix3 r=1

Ngày đăng: 27/02/2022, 15:43

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan