Generic Set Algorithms

Slides:



Advertisements
Similar presentations
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Advertisements

Sorting Really Big Files Sorting Part 3. Using K Temporary Files Given  N records in file F  M records will fit into internal memory  Use K temp files,
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Analysis of Algorithms
CS 540 Database Management Systems
Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Advanced Topics in Algorithms and Data Structures Lecture 6.1 – pg 1 An overview of lecture 6 A parallel search algorithm A parallel merging algorithm.
The Selection Problem. The selection problem Input: A set S of n elements Output: The k-th smallest element of S The median problem: to find the -th smallest.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
© 2004 Goodrich, Tamassia Sets1. © 2004 Goodrich, Tamassia Sets2 Set Operations (§ 10.6) We represent a set by the sorted sequence of its elements By.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
June Searching CE : Fundamental Programming Techniques 16 Searching1.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Sorting Algorithms: Implemented using ARM Assembly
Sorting Chapter 12 Objectives Upon completion you will be able to:
Insertion Sort By Daniel Tea. What is Insertion Sort? Simple sorting algorithm Builds the final list (or array) one at a time – A type of incremental.
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Generic Programming Using the C++ Standard Template Library.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
1 Iterators Good reference site:
Math – What is a Function? 1. 2 input output function.
Introduction to Algorithms: Verification, Complexity, and Searching (2) Andy Wang Data Structures, Algorithms, and Generic Programming.
1 More Merge Sort Java implementation Time complexity Generic merging and sets.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sit-In Lab 1 Twenty-Forty-Eight
Sets and Multimaps 9/9/2017 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Recursion.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Sort Algorithm.
Sorting Algorithms Sections 7.1 to 7.4.
Searching – Linear and Binary Searches
David Kauchak cs201 Spring 2014
Quick Sort and Merge Sort
Insertion Sort Sorted Unsorted
10.6 Shell Sort: A Better Insertion
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Associative Structures
Algorithm design and Analysis
Selection Sort Sorted Unsorted Swap
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Sorting … and Insertion Sort.
Medications Utilities – Mass Void Medications
Recursive Linked List Operations
Containers and the Standard Template Library (STL)
Merge Sort Michael Morton.
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Algorithms Lakshmish Ramaswamy.
Generic Set Algorithms
A G L O R H I M S T A Merging Merge.
Copyright © Aiman Hanna All rights reserved
Sequences 4/10/2019 4:55 AM Sets Sets.
A G L O R H I M S T A Merging Merge.
A G L O R H I M S T A Merging Merge.
WJEC GCSE Computer Science
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Chapter 11 Sets, and Selection
Presentation transcript:

Generic Set Algorithms Andy Wang Data Structures, Algorithms, and Generic Programming

Generic Set Algorithms Very useful software tools A part of the STL specification Implement set theory Union Intersection Difference Containment Merge Runtime complexity in O(size)

Set Algorithms—Assumptions and Outcomes Input ranges determined by input iterators Output start determined by output iterator Input ranges are sorted Outcomes Output range is sorted Output range is the set operation applied to the input ranges

Set Algorithm Complexity Unsorted input ranges O(size2) to iterate through range for each element {g, c, m, y, x, h, a} union {h, w, a, b} Sorted input rages O(size) to iterate through each range once {a, c, g, h, m, x, y} union {a, b, h, w}

Sorted Range Control Structure Compare current elements from each input range Perform action based on comparison Increment past elements used for action Continue until an input range is exhausted Deal with tail of remaining range

Set Union template <class I1, class I2, class I3> void g_set_union(I1 B1, I1 E1, I2 B2, I2 E2, I3 D) { for (; B1 != E1 && B2 != E2; ++D) { if (*B1 < *B2) { *D = *B1; ++B1; } else if (*B2 < *B1) { *D = *B2; ++B2; } else { // disallow duplicates ++B1, ++B2; } while (B1 != E1) { *D++ = *B1++; } while (B2 != E2) { *D++ = *B2++; }

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c, d} B1 E1 B2 E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c, d} B1 E1 B2, E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c, d, g} B1 E1 B2, E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c, d, g} B1 E1 B2, E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c, d, g, m} B1 E1 B2, E2 D

Set Union Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Union Set: {a, b, c, d, g, m} B1, E1 B2, E2 D

Set Merge template <class I1, class I2, class I3> void g_set_merge(I1 B1, I1 E1, I2 B2, I2 E2, I3 D) { while (B1 != E1 && B2 != E2) { if (*B2 < *B1) { *D++ = *B2++; } else { // allow duplicates *D++ = *B1++; } while (B1 != E1) { *D++ = *B1++; } while (B2 != E2) { *D++ = *B2++; }

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c, d} B1 E1 B2 E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c, d} B1 E1 B2, E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c, d, g} B1 E1 B2, E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c, d, g} B1 E1 B2, E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c, d, g, m} B1 E1 B2, E2 D

Set Merge Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Merged Set: {a, a, b, c, d, g, m} B1, E1 B2, E2 D

Set Intersection template <class I1, class I2, class I3> void g_set_intersection(I1 B1, I1 E1, I2 B2, I2 E2, I3 D) { while (B1 != E1 && B2 != E2) { if (*B2 < *B1) { // *B2 not in set 1 ++B2; } else if (*B1 < *B2) { // *B1 not in set 2 ++B1; } else { *D++ = *B1++; }

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {} B1 E1 B2 E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1 E1 B2 E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1 E1 B2 E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1 E1 B2 E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1 E1 B2 E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1 E1 B2, E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1 E1 B2, E2 D

Set Intersection Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Intersection Set: {a} B1, E1 B2, E2 D

Set Difference template <class I1, class I2, class I3> void g_set_difference(I1 B1, I1 E1, I2 B2, I2 E2, I3 D) { while (B1 != E1 && B2 != E2) { if (*B2 < *B1) { // *B2 not in set 1 ++B2; } else if (*B1 < *B2) { // *B1 not in set 2 *D++ = *B1++; } else { ++B1; } while (B1 != E1) { *D++ = *B1++; }

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {} B1 E1 B2 E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {} B1 E1 B2 E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {} B1 E1 B2 E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c} B1 E1 B2 E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c} B1 E1 B2 E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c} B1 E1 B2, E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c, g} B1 E1 B2, E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c, g} B1 E1 B2, E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c, g, m} B1 E1 B2, E2 D

Set Difference Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Difference Set: {c, g, m} B1, E1 B2, E2 D

Set Containment template <class I1, class I2, class I3> void g_subset_of(I1 B1, I1 E1, I2 B2, I2 E2) { while (B1 != E1 && B2 != E2) { if (*B1 < *B2) { // *B1 not in set 2 return 0; } else if (*B2 < *B1) { // *B2 not in set 1 ++B2; } else { ++B1; } if (B1 == E1) return 1;

Set Containment Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} B1 E1 B2 E2

Set Containment Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} B1 E1 B2 E2

Set Containment Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} B1 E1 B2 E2

Set Containment Illustrated Set 1: {a, c, g, m} Set 2: {a, b, d} Return 0 B1 E1 B2 E2