0% found this document useful (0 votes)
23 views

Data Structure FF - Red

The document discusses sets and multisets in data structures. It defines a set as a collection of unique elements where each element occurs only once. It provides examples of set operations like insertion, deletion, searching. A multiset is similar but allows multiple occurrences of the same element. It gives examples showing how elements can be inserted and removed from multisets.

Uploaded by

nurayjumayeva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Data Structure FF - Red

The document discusses sets and multisets in data structures. It defines a set as a collection of unique elements where each element occurs only once. It provides examples of set operations like insertion, deletion, searching. A multiset is similar but allows multiple occurrences of the same element. It gives examples showing how elements can be inserted and removed from multisets.

Uploaded by

nurayjumayeva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

MINISTRY OF EDUCATION OF THE REPUBLIC OF AZERBAIJAN

Baku Engineering University and INHA University


Faculty: Engineering
Department: Computer and Information Technologies
Major: Information Technology

Report Accepted:

Mark (Score):

Head teacher of department

Hasanov Ali Vahid

(sign ,date) (S.N.N)

Subject: « Data Structure »

Individual activity

« Set, multiset »

REPORT

2nd year student Nuray Jumayeva Rashad


(S.N.N)

(sign ,date)
2

«CONTENTS»
• Introduction……………………………………………………………3
1. What is Set?………………………………………………….....3
2. Properties of Set………………………………………………..6
3. Examples about Set………………………………………….....7
4. Example problems: N-1……………………………………….10
N-2………………………………………………………………11
5. What is Multiset?......…………………………………………..13
6. Properties of Multiset………………………………………….14
7. Example about Multiset………………………………………..15
8. Example problems: N-1………………………………………...16

• Result ………………………………………………………………….18

• References……………………………………………………………..19
3

«INTRODUCTION»

What is set?

In programming, a set is a fascinating data structure that holds a collection


of unique elements, where each element occurs only once. It's like a bag that
doesn’t allow duplicates. One of the key aspects of sets is their uniqueness—no
element in a set can be repeated. This feature makes them super handy when you
need a collection of distinct values.

Programming languages like C++ offer sets as part of their standard libraries,
allowing programmers to work with these collections seamlessly. Python, Java,
and many other languages have their versions of sets too. Sets are often
implemented using hash tables or balanced trees, which make operations like
insertion, deletion, and searching quite efficient, even for large sets. Sets maintain
an order based on a specific criterion—usually, elements are sorted in ascending
order by default. This ordering makes certain operations easier to perform. Sets
support various operations like adding elements, removing elements, checking for
the existence of an element, and performing set operations like union, intersection,
and difference. Iterators allow you to loop through the elements of a set, which is
super useful for processing data or performing specific actions on each unique
item. Think of a unique list of student IDs in a class or tracking unique email
addresses in a database—sets are perfect for scenarios where you need distinct
values without any repetition. The efficiency of sets comes from their ability to
perform operations swiftly, especially in cases where you need to quickly
4

determine if an element exists or add new unique items. Sets are quite different
from multisets (or bags), as multisets allow duplicates while sets strictly maintain
uniqueness. Sets are like your go-to toolbox. They're handy for all sorts of tasks—
whether you're removing duplicates from a list or checking for unique elements in
a dataset. Sets are an elegant and efficient way to manage collections of unique
elements in programming. They provide a structured and ordered approach to
handling data, ensuring each value appears only once.

An associative container that contains a sorted set of unique objects pf type Key.

• Need to include<set> header file.


• A user-provided compare can be supplied the change the ordering (sorting).
• Search, removal, and insertion times are logarithmic.
• Usually implemented as RB Trees.

Template<

class key,

class Compare= std::less <Key>,

class Allocator=std::<Key>,

>class set;

Set functions

size()

• clear()
• count(),find()
5

• empty()
• insert(key),insert(it1,it2),insert(init_list)
• erase(it),erase(it1,it2),erase(key)
• upper_bound(key),lower_bound(key)
• =

Insert()

Insert()

insert(Key key)

• Insert key
• Returns pair consisting of iterator to inserted element (or element present)
and bool set to true if insertion took place
insert(iterator pos1,iterator pos2)
• Inserts elements in range(pos1,pos2)
• Returns nothing(void)

insert(initializer_list l)
• Inserts elemenents from initilazer list
• Returns nothing(void)

Erase()

Erase(iterator pos)

• Removes element at position pos


• returns iterator following last element removed

Erase(iterator pos1,pos2)
6

• removes elements in range[ pos1,pos2)


• returns iterator following last removed

Erase(key)

• removes elements with key (if present) key


• returns number of elements removed

Properties:

Ordered collection: Elements in a set are sorted in a specific order defined by the
sorting criterion. By default, it uses the less-than operator (operator<) for ordering.

Uniqueness: Each element in a set is unique, meaning duplicates are not allowed.
When inserting an element that already exists, it won't be added again.

Dynamic Sizing: Sets are dynamic in size, meaning elements can be inserted and
removed at runtime. The size of the set changes based on the operations performed.

Search and Access: Elements can be efficiently searched within a set using its
member functions like find() which has a time complexity of O(log n) for ordered
sets.

Iterators: Sets support iterators allowing traversal through the elements in a


sequence. Iterators can be used to access, modify, or remove elements.

Performance: The underlying data structure of std::set is usually implemented as


a balanced binary search tree (typically a red-black tree), which provides efficient
operations like insertion, deletion, and search with logarithmic time complexity.
7

Example 1:

We started with a group of numbers in a special order, added more numbers to it,
and saw how it keeps only unique ones, ignoring repeats.

And the output is:

Example 2:

We began by creating a set filled with different integers. We checked the size of
the set before and after adding elements. Then, we showcased the unique numbers
within it. Afterward, we removed a particular value and showed the modified set.
8

So , we removed the number 10 from the set.

Now, let’s use upper_bound(key), lower_bound(key) set functions.


9
10

Example problems:
N-1

Code:
11

Explanation:
This program in C++ counts how many different numbers you type in. It keeps
track of the unique numbers you enter using something called a "set." First, it asks
how many sets of numbers you want to type. Then, for each set, it asks how many
numbers you'll type and then records those numbers. Finally, it shows you how
many different numbers you entered altogether.

N-2
12

Code:

Explanation:

This program in C++ collects different numbers you type in and shows them back
to you. It asks how many numbers you want to type. Then, you type those numbers
one by one, and it remembers only the different ones. After you finish typing, it
shows all the different numbers you entered.
13

What is Multiset?

A multiset is a type of mathematical set that allows for multiple occurrences


of elements. It's also known as a bag or a multiset.
Some basic function associated with multiset (the majority of these function
are included in the set): begin(), end(), size(), max_size(), empty(), insert(x),
clear(), erase(x) and etc.
In a traditional set, each element is unique and occurs only once. For
example, in the set {1, 2, 3}, each number appears only once. However, in a
multiset, elements can occur multiple times. For instance, in the multiset {1,
1, 2, 2, 2, 3}, the number 1 appears twice, the number 2 appears three times,
and the number 3 appears once.
They're like versatile containers, critical in various fields—be it computer
science, mathematics, or practical everyday applications. Their significance
lies in scenarios where counting the occurrences of elements holds
importance.
What makes multisets stand out is their ability to hold multiple instances of
the same element, all while offering operations akin to those of sets—
insertion, deletion, finding elements, and traversing through the collection.
In computer science, they're the backbone of algorithms, assisting in tasks
ranging from frequency analysis in datasets to optimizing search operations.
Consider scenarios like managing inventory, where counting the quantity of
identical items is crucial. Multisets shine here by keeping track of these
multiple occurrences efficiently.
Languages like C++ provide tools like std::multiset as part of their standard
libraries, making it easier for programmers to work with these structures.
14

The significance of multisets lies in their ability to handle duplicates


effortlessly. Think of counting votes in an election or analyzing text data—
these situations often require tallying repetitive occurrences.
Efficient algorithms for multisets are crucial, ensuring optimal memory
usage and swift handling of vast amounts of data. The choice of underlying
data structures, such as trees or hash tables, affects performance.
From text processing in natural language analysis to database queries and
IoT applications, multisets play a fundamental role in handling data,
providing insights, and optimizing various operations.
Designing efficient algorithms for multisets involves a delicate balance—
minimizing memory usage while optimizing time complexities, especially
for large datasets or real-time updates.
Frequency histograms, network traffic analysis, and error detection—all
benefit from the straightforward representation of element occurrences that
multisets offer.
Properties:
• Allows duplicate elements.
• Does not enforce a particular order of elements.

Often implemented using balanced binary search trees (like Red-Black Trees)
or hash tables.

• Frequently used in scenarios where counting occurrences is important (e.g.,


frequency analysis, counting elements in a dataset).

Operations on Multiset:

• Insertion: Adding elements into the multiset.


• Deletion: Removing elements, possibly reducing their count.
15

• Counting: Determining the number of occurrences of a particular element.


• Iteration: Accessing and traversing elements in the multiset.
• Union, Intersection, Difference: Combining multisets similar to set
operations, considering multiplications.
Example:
16

In here we created a set of integers, add elements, count occurrences of a


specific element, and remove elements from the multiset.

Example Problems:
N-1
17

Code:
18

«RESULT»

std::set

Think of std::set like a box for toys. It keeps only one of each toy and puts them in
order, like arranging toys from small to big. So, if you have the same toy twice, it
won't go in. It's helpful when you want things to stay organized and need to find
something quickly.

std::multiset

Now, std::multiset is more like a box where you can put as many same toys as you
want. It also keeps toys in order, so finding a specific toy is still easy. It's handy
when you're okay with having lots of the same toy and still want them sorted.

«RECOMMENDATION»

‘std::set’ and ‘std::multiset ‘

These toy boxes offer some cool stuff:

• Easy to look through: You can easily go through all the toys inside.:
• Same tools: They both have similar tools to add, remove, or find toys,
making them easy to use.
• Part of the Toy Collection: They're part of the same toy collection kit, so
they work the same way in any toy set.
19

• Useful in different situations: You can use them in different toy situations,
whether you want an organized collection with no duplicates or a sorted one
that allows many of the same toy.
• Can change how they sort: You can use them in different toy situations,
whether you want an organized collection with no duplicates or a sorted one
that allows many of the same toy.

«REFERENCES»

• www.geeksforgeeks.org

• www.scaler.com

• www.w3schools.com

• www.eolymp.com

• www.wikipedia.org

• Data Structures and Algorithm Analysis in C++

You might also like