Kiến trúc máy tính - P12-2 pps

16 588 0
Kiến trúc máy tính - P12-2 pps

Đ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

Sorting 1 Hàng đợi ưu tiên (Priority Queues) Sorting 2 Cấu trúc dữ liệu ưu tiên • Giả sử rằng mức độ ứu tiên thể hiện bằng số • Cũng có thể là một đối tượng bất kỳ, nhưng chúng có thể so sánh được với nhau • Giá trị nhỏ nhất có mức ưu tiên cao nhất • Các giá trị khác là như nhau Sorting 3 Hàng đợi ưu tiên (Priority queue) Là cấu trúc dữ liệu cho việc lưu trữ mọt tập các phần tử có ion of prioritized elements Hỗ trợ chèn thêm phần tử bất kỳ Hỗ trợ loại bỏ phần tử với mức ưu tiên cao nhất tại bất kỳ thời điểm nào. Không như những cấu trúc dữ liệu dựa trên địa chỉ (stacks queues) vì nó không định nghĩa vị trí cho người sử dụng Sorting 4 Cài đặt Bằng danh sách – Đơn giản nhưng không hiệu quả Bằng cây heap – hiệu quả hơn (thời gian yêu cầu là hàm log) Sorting 5 Hàng đợi ưu tiên ADT Một hàng đợi ưu tiên lưu trữ một tập các phần tử Mỗi phần tử là một cặp (key, value) Các phương thức chính của Hàng đợi ưu tiên ADT  insert(k, x) Chèn một phần tử với khóa k và giá trị x  removeMin() Loại bỏ và trả lại phần tử có khóa nhỏ nhất Thêm vào một số phương thức  min() trả lại phần tử có khóa nhỏ nhất nhưng không loại bỏ nó đi  size(), isEmpty() Các ứng dụng:  Các máy bay dự phòng  Đấu giá  Thị trường chứng khoán (Stock market) Sorting 6 Quan hệ thứ tự toàn phần Các khóa trong hàng đợi ưu tiên có thể là các đối tượng bất kỳ và trên nó được định nghĩa một thứ tự Hai phần tử phân biệt trong một hàng đợi ưu tiên có thể có khóa giống nhau Các khái niệm toán học về quan hệ thự tự ≤ toàn phần  T/c phản xạ: x ≤ x  T/c phẩn đối xứng: x ≤ y ∧ y ≤ x ⇒ x = y  T/c bắc cầu: x ≤ y ∧ y ≤ z ⇒ x ≤ z Sorting 7 Phần tử ADT (§ 7.1.2) Một phần tử trong hàng đợi ưu tiên đơn giản là một cặp key- value. Hàng đợi ưu tiên lưu trữ các cặp cho phép thêm vào và loại bỏ đi dựa trên các khóa. Các phương thức:  key(): trả lại giá trị khóa của phần tử  value(): trả lại giá trị của phần tử Cài đặt trong C++ template <class Object1, class Object2> class Entry { private: Object1 value; Object2 key; public: Object key(); Object value(); }; Sorting 8 Toán tử so sánh ADT (§ 7.1.2) A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses an auxiliary comparator The comparator is external to the keys being compared When the priority queue needs to compare two keys, it uses its comparator The primary method of the Comparator ADT:  compare(x, y): Returns an integer i such that i < 0 if a < b , i = 0 if a = b , and i > 0 if a > b ; an error occurs if a and b cannot be compared. Sorting 9 Example Comparator Lexicographic comparison of 2-D points: /** Comparator for 2D points under the standard lexicographic order. */ public class Lexicographic implements Comparator { int xa, ya, xb, yb; public int compare(Object a, Object b) throws ClassCastException { xa = ((Point2D) a).getX(); ya = ((Point2D) a).getY(); xb = ((Point2D) b).getX(); yb = ((Point2D) b).getY(); if (xa != xb) return (xb - xa); else return (yb - ya); } } Point objects: /** Class representing a point in the plane with integer coordinates */ public class Point2D { protected int xc, yc; // coordinates public Point2D(int x, int y) { xc = x; yc = y; } public int getX() { return xc; } public int getY() { return yc; } } Sorting 10 Priority Queue Sorting (§ 7.1.4) We can use a priority queue to sort a set of comparable elements 1. Insert the elements one by one with a series of insert operations 2. Remove the elements in sorted order with a series of removeMin operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P ← priority queue with comparator C while ¬S.isEmpty () e ← S.removeFirst () P.insert (e, 0) while ¬P.isEmpty() e ← P.removeMin().key() S.insertLast(e) [...]... (g) (2) (2,3) (2,3,4,5,7,8,9) (3,4,5,7,8,9) (4,5,7,8,9) () Sorting Priority queue P () 15 In-place Insertion-sort Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place A portion of the input sequence itself serves as the priority queue For in-place insertion-sort   We keep sorted the initial portion of the sequence We can use swaps instead of modifying... 11 Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort: 1 2 Inserting the elements into the priority queue with n insert operations takes O(n) time Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to 1 + 2 + …+ n Selection-sort runs in... Sorting 12 Selection-Sort Example Input: Sequence S (7,4,8,2,5,3,9) Priority Queue P () Phase 1 (a) (b) (g) (4,8,2,5,3,9) (8,2,5,3,9) () (7) (7,4) Phase 2 (a) (b) (c) (d) (e) (f) (g) (2) (2,3) (2,3,4) (2,3,4,5) (2,3,4,5,7) (2,3,4,5,7,8) (2,3,4,5,7,8,9) (7,4,8,5,3,9) (7,4,8,5,9) (7,8,5,9) (7,8,9) (8,9) (9) () Sorting (7,4,8,2,5,3,9) 13 Insertion-Sort Insertion-sort is the variation of PQ-sort where the... implemented with a sorted sequence Running time of Insertion-sort: 1 Inserting the elements into the priority queue with n insert operations takes time proportional to 1 + 2 + …+ n 2 Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Insertion-sort runs in O(n2) time Sorting 14 Insertion-Sort Example Input: Sequence S (7,4,8,2,5,3,9) Phase...Sequence-based Priority Queue Implementation with an unsorted list Implementation with a sorted list 4 1 5 2 3 1 Performance:   insert takes O(1) time since we can insert the item at the beginning or end of . (2 , 3 , 4 , 5 , 7 , 8 , 9) () Sorting 16 In-place Insertion-sort Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place A portion of the input sequence. (2 , 3 , 4 , 5 , 7 , 8 , 9) () Sorting 14 Insertion-Sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort: 1. Inserting the elements. 4 5 Sorting 12 Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort: 1. Inserting the elements

Ngày đăng: 12/08/2014, 17:20

Từ khóa liên quan

Mục lục

  • Hàng đợi ưu tiên (Priority Queues)

  • Cấu trúc dữ liệu ưu tiên

  • Hàng đợi ưu tiên (Priority queue)

  • Cài đặt

  • Hàng đợi ưu tiên ADT

  • Quan hệ thứ tự toàn phần

  • Phần tử ADT (§ 7.1.2)

  • Toán tử so sánh ADT (§ 7.1.2)

  • Example Comparator

  • Priority Queue Sorting (§ 7.1.4)

  • Sequence-based Priority Queue

  • Selection-Sort

  • Selection-Sort Example

  • Insertion-Sort

  • Insertion-Sort Example

  • In-place Insertion-sort

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

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

Tài liệu liên quan