Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Algorithm Analysis
Advanced Data Structure
Chung-Ang University, Jaesung Lee
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue, Heap and Heap Sort
2
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Max Heap data structure
3
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Representation of Heap Tree
4
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Representation of Heap Tree
5
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Representation of Heap Tree• Let us consider the following elements arranged in the form of array as follows:
6
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Representation of Heap Tree• A heap tree represented using a single array looks as follows:
7
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Operations on heap tree• Insertion
• Deletion and
• Merging
8
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree
9
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree
10
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• The algorithm Max_heap_insert to insert a data into a max heap tree is as follows:
11
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
12
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
13
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
14
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
15
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
16
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
17
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
18
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Insertion into a heap tree• Insert 40, 80, 35, 90, 45, 50, 70
19
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Deletion of a node from heap tree• The algorithm for deleting root node is as follows:
20
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Deletion of a node from heap tree• And the algorithm for “adjust” function is as follows:
21
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Deletion of a node from heap tree
22
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Merging two heap trees
23
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Applications of heap tree
24
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort
• (Step 1) Build a heap tree with the given set of data.
• (Step 2-a) Remove the top most item (the largest) and replace it with the last element in the heap.
• (Step 2-b) Re-heapify the complete binary tree.
• (Step 2-c) Place the deleted node in the output.
• (Step 3) Continue Step 2 until the heap tree is empty.
25
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• A Heap Sort Algorithm
26
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• A Heap Sort Algorithm
27
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• A Heap Sort Algorithm
28
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Time complexity
29
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
30
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
31
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
32
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
33
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
34
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
35
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Example
36
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Priority queue implementation using heap tree
37
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Heap Sort• Priority queue implementation using heap tree
38
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Binary Search Trees
39
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Binary Tree Searching
40
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Why use binary search trees?
41
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Why use binary search trees?
42
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Why use binary search trees?
43
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Why use binary search trees?
44
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Why use binary search trees?
45
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Why use binary search trees?
46
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Inserting nodes into a Binary Search Tree
47
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Deleting nodes from a Binary Search Tree
48
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Deleting nodes from a Binary Search Tree
49
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Deleting nodes from a Binary Search Tree
50
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Deleting nodes from a Binary Search Tree
51
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Priority Queue• Deleting nodes from a Binary Search Tree
52
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Dictionary• Dictionary
53
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Dictionary• Dictionary (Example)
54
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Dictionary• Dictionary (Example)
55
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Dictionary
56
• Get operation
• Put operation
• Remove operation
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Relation between Dictionary and Set
• A set is an unordered collection (possibly empty) of distinct items.
• We can implement a set as a bit vector over the universal set.
• We can implement a set with a list structure (with insertion constraints).
• A multiset or bag is a set without the uniqueness constraint.
• Basic operations of a multiset: Search, Insert, Delete.
• A basic data structure that accomplishes these operations is dictionary.
• Sometimes we need to dynamically partition some n-element set into a collection of disjoint sets.
• Sometimes we need to take the union or intersection of sets.
57
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Set and Disjoint Set
• Disjoint Set Operations: Union and Find
58
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Disjoint Set Union (example)
• �� = {1,7,8,9}
• �� = {2,5,10}
• �� ∪ S� = {1,2,5,7,8,9,10}
59
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Find (example)
• �� = {1,7,8,9}
• �� = {2,5,10}
• �� = 3,4,6
• ����(4) = ��• ����(5) = ��• ����(7) = ��
60
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Set Representation (example)
• �� = {1,7,8,9}
• �� = {2,5,10}
• �� = 3,4,6
61
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Disjoint Union
62
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Find
63
�� �� ��
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Union and Find Algorithms (example)
• For the following sets, the array representation is as shown below.
64
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Algorithm for Union operation
65
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Algorithm for Find operation
66
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Analysis of SimpleUnion(i,j) and SimpleFind(i)
• For example, consider the sets:
67
Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Disjoint Set Operations• Analysis of SimpleUnion(i,j) and SimpleFind(i)
• The sequence of Union operations results the degenerate tree as below.
• Time complexity is � ∑ ����� = � �� .
68