資工研二 翁靜美. L 1 : 1, 2, 5, 6L 2 : 3, 4 124365 Input:Two sorted lists, L 1 = (a 1, a 2,..., a n1 ) and L 2 = (b 1, b 2,..., b n2 ). Output:A sorted list.

Slides:



Advertisements
Similar presentations
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Advertisements

Introduction to Algorithms
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
Introduction to Algorithms 6.046J/18.401J
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
CS16: Introduction to Data Structures & Algorithms
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
CSE 373 Data Structures and Algorithms
Problems and Their Classes
Subtraction: Adding UP
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Splay Trees Binary search trees.
Chapter 12 Binary Search Trees
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Distributed Computing 9. Sorting - a lower bound on bit complexity Shmuel Zaks ©
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
COL 106 Shweta Agrawal, Amit Kumar
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 9.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Heapsort By: Steven Huang. What is a Heapsort? Heapsort is a comparison-based sorting algorithm to create a sorted array (or list) Part of the selection.
Chapter 3 The Greedy Method 3.
Optimal Merging Of Runs
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
© 2004 Goodrich, Tamassia Greedy Method and Compression1 The Greedy Method and Text Compression.
A Data Compression Algorithm: Huffman Compression
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
DL Compression – Beeri/Feitelson1 Compression דחיסה Introduction Information theory Text compression IL compression.
The Complexity of Algorithms and the Lower Bounds of Problems
The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
3 -1 Chapter 3 The Greedy Method 3 -2 A simple example Problem: Pick k numbers out of n numbers such that the sum of these k numbers is the largest.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
The 2-Way Merge Problem 學生:曾羽銘.
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Foundation of Computing Systems
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
CSCI 62 Data Structures Dr. Joshua Stough October 23, 2008.
Advanced Sorting 7 2  9 4   2   4   7
Tries 07/28/16 11:04 Text Compression
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Binary search tree. Removing a node
The Greedy Method and Text Compression
The Greedy Method and Text Compression
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Optimal Merging Of Runs
Optimal Merging Of Runs
Huffman Coding.
© 2013 Goodrich, Tamassia, Goldwasser
Trees Addenda.
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Podcast Ch23d Title: Huffman Compression
CSE 589 Applied Algorithms Spring 1999
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

資工研二 翁靜美

L 1 : 1, 2, 5, 6L 2 : 3,

Input:Two sorted lists, L 1 = (a 1, a 2,..., a n1 ) and L 2 = (b 1, b 2,..., b n2 ). Output:A sorted list consisting of elements in L 1 and L 2. Begin i : = 1 j : = 1 do compare a i and b j if a i > b j then output b j and j : = j + 1 else output a i and i : = i + 1 while (i  n 1 and j  n 2 ) if i > n 1 then output b j, b j+1,..., b n2, else output a i, a i+1,..., a n1. End. The worst case of merge sort L 1 : L 2 : The number of comparison required is m+n-1

 If more than two sorted lists are to be merged, we can still apply the linear merge algorithm.  These merging processes are called 2-way merge because each merging step only merges two sorted lists.

L2L2 30 elementsL3L3 10 elementsL1L1 50 elements L 1 +L 2 80 elements L 1 +L 2 +L 3 90 elements = = 89 the number of comparisons required in this merging sequence is 168

L2L2 30 elementsL3L3 10 elementsL1L1 50 elements L 2 +L 3 40 elements L 2 +L 3 +L 1 90 elements = = 89 the number of comparisons required is only 128

 There are m sorted lists.  Each of them consists of n i elements.  What is the optimal sequence of merging process to merge these sorted lists together by using the minimum number of comparisons?

Input:m sorted lists, L i, i = 1, 2,..., m, each L i consisting of n i elements. Output:An optimal 2-way merge tree. Step 1: Generate m trees, where each tree has exactly one node (external node) with weight n i. Step 2: Choose two trees T 1 and T 2 with minimal weights. Step 3: Create a new tree T whose root has T 1 and T 2 as its subtrees and weight is equal to the sum of weights T 1 and T 2. Step 4: Replace T 1 and T 2 by T. Step 5: If there is only one tree left, stop and return; otherwise, go to Step 2.

 For the given m numbers n 1, n 2, …, n m, we can construct a min-heap to represent these numbers where the root value is smaller than the values of its sons.  The main loop is executed (n-1) times:  Tree reconstruction after removing the root, which has the smallest value, can be done in O(log n) time.  The insertion of a new node into a min-heap also can be done in O(log n) time.  The total time to generate an optimal extended binary tree is O(n log n).

 In telecommunication, how do we represent a set of messages, each with an access frequency, by a sequence of 0’s and 1’s?  To minimize the transmission and decoding costs, we may use short strings to represent more frequently used messages.  This problem can by solved by using the 2-way merge algorithm.

A:2 B:3 E:13 G:18 C:5 D:8 F:15 A:2 B:3 5 5 C:5 10 D:8 18 E:13 F:15 28 G: A Huffman Code Tree Huffman Codes A10100 B10101 C1011 D100 E00 F01 G11

SymbolsfrequenciesHuffmanASCIIReduced Bits A B C D E F G