Generic Set Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Generic Set Algorithms

Description:

Input ranges determined by input iterators. Output start determined by output iterator ... Unsorted input ranges. O(size2) to iterate through range for each element ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 61
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Generic Set Algorithms


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

2
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)

3
Set AlgorithmsAssumptions and Outcomes
  • Assumptions
  • 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

4
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

5
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

6
Set Union
  • template ltclass I1, class I2, class I3gt
  • void g_set_union(I1 B1, I1 E1, I2 B2, I2 E2, I3
    D)
  • for ( B1 ! E1 B2 ! E2 D)
  • if (B1 lt B2)
  • D B1
  • B1
  • else if (B2 lt B1)
  • D B2
  • B2
  • else // disallow duplicates
  • D B1
  • B1, B2
  • while (B1 ! E1) D B1
  • while (B2 ! E2) D B2

7
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set

8
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a

9
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a

10
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b

11
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b

12
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c

13
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c

14
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c, d

15
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c, d

B2, E2
16
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c, d, g

B2, E2
17
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c, d, g

B2, E2
18
Set Union Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Union Set a, b, c, d, g, m

B2, E2
19
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
20
Set Merge
  • template ltclass I1, class I2, class I3gt
  • void g_set_merge(I1 B1, I1 E1, I2 B2, I2 E2, I3
    D)
  • while (B1 ! E1 B2 ! E2)
  • if (B2 lt B1)
  • D B2
  • else // allow duplicates
  • D B1
  • while (B1 ! E1) D B1
  • while (B2 ! E2) D B2

21
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set

22
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a

23
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a

24
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a

25
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a

26
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b

27
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b

28
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c

29
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c

30
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c, d

31
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c, d

B2, E2
32
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c, d, g

B2, E2
33
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c, d, g

B2, E2
34
Set Merge Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Merged Set a, a, b, c, d, g, m

B2, E2
35
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
36
Set Intersection
  • template ltclass I1, class I2, class I3gt
  • void g_set_intersection(I1 B1, I1 E1, I2 B2, I2
    E2, I3 D)
  • while (B1 ! E1 B2 ! E2)
  • if (B2 lt B1) // B2 not in set 1
  • B2
  • else if (B1 lt B2) // B1 not in set 2
  • B1
  • else
  • D B1
  • B2

37
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set

38
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

39
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

40
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

41
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

42
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

B2, E2
43
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

B2, E2
44
Set Intersection Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Intersection Set a

B1, E1
B2, E2
45
Set Difference
  • template ltclass I1, class I2, class I3gt
  • void g_set_difference(I1 B1, I1 E1, I2 B2, I2 E2,
    I3 D)
  • while (B1 ! E1 B2 ! E2)
  • if (B2 lt B1) // B2 not in set 1
  • B2
  • else if (B1 lt B2) // B1 not in set 2
  • D B1
  • else
  • B1
  • B2
  • while (B1 ! E1) D B1

46
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set

47
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set

48
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set

49
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c

50
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c

51
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c

B2, E2
52
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c, g

B2, E2
53
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c, g

B2, E2
54
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c, g, m

B2, E2
55
Set Difference Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Difference Set c, g, m

B1, E1
B2, E2
56
Set Containment
  • template ltclass I1, class I2, class I3gt
  • void g_subset_of(I1 B1, I1 E1, I2 B2, I2 E2)
  • while (B1 ! E1 B2 ! E2)
  • if (B1 lt B2) // B1 not in set 2
  • return 0
  • else if (B2 lt B1) // B2 not in set 1
  • B2
  • else
  • B1
  • B2
  • if (B1 E1) return 1
  • return 0

57
Set Containment Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d

58
Set Containment Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d

59
Set Containment Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d

60
Set Containment Illustrated
  • Set 1 a, c, g, m
  • Set 2 a, b, d
  • Return 0
Write a Comment
User Comments (0)
About PowerShow.com