generic set algorithms n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Generic Set Algorithms PowerPoint Presentation
Download Presentation
Generic Set Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 48

Generic Set Algorithms - PowerPoint PPT Presentation


  • 103 Views
  • Uploaded on

Generic Set Algorithms. 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. Assumptions

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Generic Set Algorithms' - kynthia


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
generic set algorithms1
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
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
set operations example
struct ltstr {

bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; }

};

int main() {

const int N = 6;

const char* a[N] = {"isomer", "ephemeral", "prosaic", "nugatory", "artichoke", "serif"};

const char* b[N] = {"flat", "this", "artichoke", "frigate", "prosaic", "isomer"};

set<const char*, ltstr> A(a, a + N);

set<const char*, ltstr> B(b, b + N);

set<const char*, ltstr> C;

cout << "Set A: ";

copy(A.begin(), A.end(), ostream_iterator<const char*>(cout, " "));

cout << endl;

cout << "Set B: ";

copy(B.begin(), B.end(), ostream_iterator<const char*>(cout, " "));

cout << endl; cout << "Union: "; set_union(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator<const char*>(cout, " "), ltstr());

cout << endl;

cout << "Intersection: ";

set_intersection(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator<const char*>(cout, " "), ltstr());

cout << endl;

set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()), ltstr());

cout << "Set C (difference of A and B): "; copy(C.begin(), C.end(), ostream_iterator<const char*>(cout, " "));

cout << endl;

}

Set Operations Example
set algorithm complexity
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
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 illustrated

B1

E1

B2

E2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {}

set union illustrated1

B1

E1

B2

E2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a}

set union illustrated2

B1

E1

B2

E2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a, b}

set union illustrated3

B1

E1

B2

E2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a, b, c}

set union illustrated4

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}

set union illustrated5

B1

E1

E2

B2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a, b, c, d}

set union illustrated6

B1

E1

E2

B2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a, b, c, d, g}

set union illustrated7

B1

E1

E2

B2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a, b, c, d, g, m}

set union illustrated8

E1

B1

E2

B2

D

Set Union Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Union Set: {a, b, c, d, g, m}

set union
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

*D = *B1;

++B1, ++B2;

}

}

// Handle the tail

while (B1 != E1) { *D++ = *B1++; }

while (B2 != E2) { *D++ = *B2++; }

}

set merge illustrated

B1

E1

B2

E2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Merged Set: {}

set merge illustrated1

B1

E1

B2

E2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Merged Set: {a}

set merge illustrated2

B1

E1

B2

E2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Merged Set: {a, a}

set merge illustrated3

B1

E1

B2

E2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Merged Set: {a, a, b}

set merge illustrated4

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}

set merge illustrated5

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}

set merge illustrated6

B1

E1

E2

B2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2:{a, b, d}

Merged Set: {a, a, b, c, d}

set merge illustrated7

B1

E1

E2

B2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2:{a, b, d}

Merged Set: {a, a, b, c, d, g}

set merge illustrated8

B1

E1

E2

B2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2:{a, b, d}

Merged Set: {a, a, b, c, d, g, m}

set merge illustrated9

E1

B1

E2

B2

D

Set Merge Illustrated

Set 1: {a, c, g, m}

Set 2:{a, b, d}

Merged Set: {a, a, b, c, d, g, m}

set merge
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 intersection illustrated

B1

E1

B2

E2

D

Set Intersection Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Intersection Set: {}

set intersection illustrated1

B1

E1

B2

E2

D

Set Intersection Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Intersection Set: {a}

set intersection illustrated2

B1

E1

B2

E2

D

Set Intersection Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Intersection Set: {a}

set intersection illustrated3

B1

E1

B2

E2

D

Set Intersection Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Intersection Set: {a}

set intersection illustrated4

B1

E1

B2

E2

D

Set Intersection Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Intersection Set: {a}

set intersection illustrated5

D

Set Intersection Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Intersection Set: {a}

B1

E1

E2

B2

set intersection
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;

++B1;

++B2;

}

}

}

set difference illustrated

B1

E1

E2

B2

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {}

set difference illustrated1

B1

E1

E2

B2

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {}

set difference illustrated2

B1

E1

E2

B2

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {}

set difference illustrated3

B1

E1

E2

B2

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {c}

set difference illustrated4

B1

E1

E2

B2

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {c}

set difference illustrated5

B1

E1

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {c}

E2

B2

set difference illustrated6

B1

E1

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {c, g}

E2

B2

set difference illustrated7

D

Set Difference Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Difference Set: {c, g, m}

E1

B1

E2

B2

set difference
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;

++B2;

}

}

while (B1 != E1) { *D++ = *B1++; }

}

set containment illustrated

B1

E1

B2

E2

Set Containment Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

set containment illustrated1

B1

E1

B2

E2

Set Containment Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

set containment illustrated2

B1

E1

B2

E2

Set Containment Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

set containment illustrated3

B1

E1

B2

E2

Set Containment Illustrated

Set 1: {a, c, g, m}

Set 2: {a, b, d}

Return 0 (false)

set containment
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;

++B2;

}

}

if (B1 == E1) return 1;

return 0;

}