• Tidak ada hasil yang ditemukan

Design and Analysis

N/A
N/A
Sohuturon Fernando

Academic year: 2024

Membagikan " Design and Analysis"

Copied!
16
0
0

Teks penuh

(1)

DESIGN & ANALYSIS OF ALGORITHM

PRIORITY QUEUE

Informatics Department

Parahyangan Catholic University

(2)

PRIORITY QUEUE

A priority queue is a data structure that

supports two basic operations: inserting a new item and removing element with the largest (or smallest) key

Example : queue in hospital prioritize patient with emergency issue

(3)

PRIORITY QUEUE

A few more operations:

Finding element with the largest key

i.e., which patient has the most severe issue ?

Removing and arbitrary element

i.e., maybe someone decides to leave the hospital

Changing the priority of an arbitrary element

i.e., someone suddenly needs an immediate help !

Joining two priority queues

i.e., received several transfer patients from other hospital

(4)

PRIORITY QUEUE OPERATIONS

More specifically, a priority queue maintains a set S of elements. Each element has a key.

A max priority queue supports the following basic operations :

INSERT(x)

inserts element x to S

MAXIMUM()

returns the element in S that has the largest key

EXTRACT-MAX()

removes and returns the element in S that has the largest key

INCREASE-KEY(x, k)

increase the key of element x to k

(assume the current key of x is at least as large as k)

Removing an element is the same as increasing its

key to ∞, then do extract- max

Removing an element is the same as increasing its

key to ∞, then do extract- max

(5)

PRIORITY QUEUE OPERATIONS

Alternatively, a min priority queue supports the following operations:

INSERT(x)

inserts element x to S

MINIMUM()

returns the element in S that has the largest key

EXTRACT-MIN()

removes and returns the element in S that has the smallest key

DECREASE-KEY(x,k)

decrease the key of element x to k

(assume the current key of x is at least as small as k)

Removing an element is the same as

decreasing its key to -∞, then do extract-

min

Removing an element is the same as

decreasing its key to -∞, then do extract-

min

(6)

VARIOUS IMPLEMENTATION

Priority Queue is an ADT, means that we can implement it with various technique:

Array

Ordered Array

Unordered Array

Linked List

Ordered Linked List

Unordered Linked List

Heap (covered later)

Binomial Heap

Fibonacci Heap

etc.

Now we discuss how to implement a max priority queue that only stores keys.

Implementation of min priority queue is very similar, and left

as an exercise 

Later we shall see how to

link” a priority queue’s element to an outside object.

Now we discuss how to implement a max priority queue that only stores keys.

Implementation of min priority queue is very similar, and left

as an exercise 

Later we shall see how to

link” a priority queue’s element to an outside object.

(7)

IMPLEMENTATION #1

ORDERED ARRAY

Since an array has a fixed capacity, priority queue implemented using an array is also limited.

length = array’s length (=capacity)

size = the number of elements in priority queue

Example:

priority queue of length 10 with size=7

MAXIMUM and EXTRACT-MAX are O(1)

2 4 5 7 9 1

2 1 3

size = position of max element

(8)

IMPLEMENTATION #1

ORDERED ARRAY

INSERT

Similar to inserting an element on insertion sort

INCREASE-KEY is similar to INSERT, only to a different swap direction

2 4 5 7 9 1

2 1 3 2 4 5 7 9 1

2 1

3 1 0 2 4 5 7 9 1

2 1

0 1 3 2 4 5 7 9 1

0 1

2 1 3

INSERT(10)

10<13  swap

10<12  swap

10≮9  stop

Time complexity is O(n)

Time complexity is O(n)

(9)

IMPLEMENTATION #2

UNORDERED ARRAY

Example:

priority queue of length 10 with size=7

INSERT is simply adding the new element on an empty spot  O(1)

INCREASE-KEY is simply changing the key’s value  O(1)

EXTRACT-MAX and MAXIMUM requires sequential through the entire array  O(n)

5 4 2 1 1

1 6 7

size ≠ position of max element !!

(10)

EXERCISE

Write the pseudocode for the following operations on an unordered array priority queue !

Global variables are:

A = array of integers

length = maximum capacity of A

size = # of elements in priority queue, initially 0

INSERT(x)

MAXIMUM()

EXTRACT-MAX()

INCREASE-KEY(i, k)

increase the key of the element stored at index i to the new value k. Assume the old key is ≤ k

(11)

Node of linked list can be ordered in ascending

manner, and we keep track of “tail” pointer as the maximum element of the priority queue.

Alternatively, order the linked list in descending manner, thus the “head” is the maximum element of the priority queue.

IMPLEMENTATION #3

ORDERED LINKED LIST

3

head tail = max

6 8

8 head = max

6 3

(12)

Since a linked list can have unlimited* nodes, keeping track of length variable is not

necessary.

*of course they are limited by computer’s memory space

INSERT is similar to inserting on an ordered array

IMPLEMENTATION #3

ORDERED LINKED LIST

8 head = max

6 3

5

5 < 8

5 < 8 5 < 65 < 6 5 ≮ 35 ≮ 3

What is the time complexity

?

What is the time complexity

?

(13)

How do we INCREASE-KEY ?

Need a doubly linked-list to be able to move an element backward !

IMPLEMENTATION #3

ORDERED LINKED LIST

8 head = max

6 5 3

increase this element’s key to

7 !

increase this element’s key to

7 !

8 head = max

6 5 3

(14)

EXERCISE

Write the pseudocode of

INCREASE-KEY(Node x, int newkey)

Assume that a Node has the following attribute:

key

next

prev

(15)

In an unordered linked-list, head element is not necessarily the maximum of the priority queue.

New element can be inserted anywhere in the list, thus we can use the O(1) INSERT- FIRST method.

However, finding the maximum element

requires sequential search through the entire list  O(n)

IMPLEMENTATION #4

UNORDERED LINKED LIST

4 head ≠ max

7 5 2

(16)

COMPARISON

Array Linked-List Heap

unordere

d ordered unordere

d ordered

INSERT O(1) O(n) O(1) O(n) O(lgn)

MAXIMUM O(n) O(1) O(n) O(1) O(1)

EXTRACT-MAX O(n) O(1) O(n) O(1) O(lgn)

INCREASE-KEY O(1) O(n) O(1) O(n) O(lgn)

Referensi

Dokumen terkait

User Interface/User Experience UI/UX Analysis & Design of Mobile Banking App for Senior Citizens: A Case Study in Sarawak, Malaysia Elizabeth Ubam Faculty of Computer Science &

In evolving the core courses, it was agreed that courses should cover the essential subjects of computer science and engineering, namely the following: algorithms, data structure, and

From Analysis Model to Design Model • Each element of the analysis model provides information that is necessary to create the four design models – The data/class design transforms

Introduction to Computer Science Dat a St ruct ures and Algorit hms Digit al Logic and Syst em Design Artificial Intelligence Computer Networks Machine Learning Advanced Data

vii DESIGN AND STRUCTURE ANALYSIS OF TELA-TELA CUTTING MACHINE Name : Andri Nurhisyam Nim : 2204181172 Supervisior : Alfansuri, ST.,M.Sc ABSTRACT This research is a design and

This document discusses the data analytics life cycle and highlights the importance of a structured process for conducting data science

A design document providing multiple views and dimensions of a building or structure, including a top view, front view, and other

The document discusses the importance of testing for stationarity in time series data and the consequences of not doing so, particularly in the context of regression