Docsity

Sorted Vector Sets: Merge Operations and Complexity Comparison, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Worksheet 8: Sorted Vector Set Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 8: Sorted Vector Set
In the previous lesson you explored one reason why it is useful to store values in
sorted order. Doing so allowed the use of the binary search algorithm, which
would speed up searching (albeit at the cost of slowing down insertions). In this
lesson you will explore an entirely different motivation for keeping values ordered.
Two sorted arrays can be easily merged into a new array. Simply walk down both
input arrays in parallel, selecting the smallest element to copy into the new array:
The set operations of union, intersection, difference and subset are each very
similar to a merge. That is, each algorithm can be expressed as a parallel walk
down the two input collections, advancing the index into the collection with the
smallest value, and copying values into a new vector.
void sortedVectorSetIntersect (struct vector *left, struct vector *right, struct vector *to) {
int i = 0;
int j = 0;
while ((i < vectorSize(left)) && (j < vectorSize(right))) {
if (LT(vectorGet(left, i), vectorGet(right, j)) {
i++;
} else if (EQ(vectorGet(left, i), vectorGet(right, j))) {
vectorAdd(to, vectorGet(left, i)); i++; j++;
} else {
j++;
}
pf3
pf4

Partial preview of the text

Download Sorted Vector Sets: Merge Operations and Complexity Comparison and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 8: Sorted Vector Set

In the previous lesson you explored one reason why it is useful to store values in sorted order. Doing so allowed the use of the binary search algorithm, which would speed up searching (albeit at the cost of slowing down insertions). In this lesson you will explore an entirely different motivation for keeping values ordered. Two sorted arrays can be easily merged into a new array. Simply walk down both input arrays in parallel, selecting the smallest element to copy into the new array: The set operations of union, intersection, difference and subset are each very similar to a merge. That is, each algorithm can be expressed as a parallel walk down the two input collections, advancing the index into the collection with the smallest value, and copying values into a new vector. void sortedVectorSetIntersect (struct vector *left, struct vector *right, struct vector *to) { int i = 0; int j = 0; while ((i < vectorSize(left)) && (j < vectorSize(right))) { if (LT(vectorGet(left, i), vectorGet(right, j)) { i++; } else if (EQ(vectorGet(left, i), vectorGet(right, j))) { vectorAdd(to, vectorGet(left, i)); i++; j++; } else { j++; }

} } Take, for example, set intersection. The intersection copies a value when it is found in both collections. Notice that in this abstraction it is more convenient to have the set operations create a new set, rather than modifying the arguments. Union copies the smaller element when they are unequal, and when they are equal copies only one value and advances both pointers (remember that in a set all elements are unique, each value appears only once). The difference copies values from the first collection when it is smaller than the current element in the second, and ignores elements that are found in both collections. Finally there is the subset test. Unlike the others this operation does not produce a new vector, but instead returns false if there are any values in the first collection that are not found in the second. But this is the same as returning false if the element from the left set is ever the smallest value (indicating it is not found in the other set). The parallel walk halts when one or the other array reaches the end. In the merge algorithm there was an additional loop after the parallel walk needed to copy the remaining values from the remaining array. This additional step is necessary in some but not all of the set operations. Can you determine which operations need this step? The following table compares the sorted vector set with a naïve implementation that makes no assumptions concerning the ordering of elements. The naive algorithms each have O(n^2 ) behavior, where n represents the number of elements in the resulting array. What will be the algorithmic execution times for the new algorithms? VectorSet SortedVectorSet unionWith O(n^2 ) O( intersectionWith O(n^2 ) O( differenceWith O(n^2 ) O( subset O(n 2 ) O(

void sortedVectorSetDifference (struct vector *left, struct vector *right, struct vector *to) { } int sortedVectorSetSubset (struct vector *left, struct vector *right) { }