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

Sets and Dictionary: Operations (Methods) On Dictionaries

Sets and dictionaries are useful data structures for storing unordered groups of unique items and ordered pairs of keys and values. A dictionary is like a set but associates a value with each unique key. Dictionaries can be implemented as ordered or unordered sequences. Common operations on dictionaries include inserting and retrieving items by key, removing items, and checking the size. Sets are similar to dictionaries but only contain unique keys without associated values. Disjoint-set data structures track partitions of elements and support union and find operations.

Uploaded by

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

Sets and Dictionary: Operations (Methods) On Dictionaries

Sets and dictionaries are useful data structures for storing unordered groups of unique items and ordered pairs of keys and values. A dictionary is like a set but associates a value with each unique key. Dictionaries can be implemented as ordered or unordered sequences. Common operations on dictionaries include inserting and retrieving items by key, removing items, and checking the size. Sets are similar to dictionaries but only contain unique keys without associated values. Disjoint-set data structures track partitions of elements and support union and find operations.

Uploaded by

Gagan Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sets and Dictionary

Sets are groups of items, unordered with duplicates removed. The keys of a dictionary form a set,
but each key has an associated value; these values could be duplicated within a dictionary.
A dictionary is a general-purpose data structure for storing a group of objects . A dictionary is an
ordered or unordered list of key-element pairs, where keys are used to locate elements in the list.

Example: consider a data structure that stores bank accounts; it can be viewed as a dictionary,
where account numbers serve as keys for identification of account objects.

A dictionary is also called a hash, a map, a hashmap in different programming languages. They
are all the same thing: a key-value store.. Keys in a dictionary must be unique.

Implementations of an ordered dictionary are binary search trees and AVL trees which are
binary search trees of a special type. Implement an unordered dictionary is by a hash table

Operations (methods) on dictionaries:

1. Size () :- Returns the size of the dictionary


2. Empty ():- Returns true is the dictionary is empty
3. FindItem (key):- Locates the item with the specified key. If no such key exists,
then , NO_SUCH_KEY is returned. If more than one item with the specified key exists,
an arbitrary item is returned.
4. FindAllItems (key):- Locates all items with the specified key. If no such key exists,
then, NO_SUCH_KEY is returned.
5. RemoveItem (key):- Removes the item with the specified key
6. RemoveAllItems (key):- Removes all items with the specified key
7. InsertItem (key, element):- Inserts a new key-element pair

Sets and dictionaries are ideal data structures to be used when your data has
not in order, but does have a unique object that can be used to reference it. This reference object
is called the “key,” while the data is the “value.” Dictionaries and sets are almost identical,
except that sets do not actually contain values: a set is simply a collection of unique keys. As the
name implies, sets are very useful for doing set operations.

Implementations of the Dictionary ADT:-

Dictionaries are ordered or unordered lists. The easiest way to implement a list is by means of an
ordered or unordered sequence.

1. Unordered sequence implementation :- Items are added to the initially empty


dictionary as they arrive. InsertItem(key, element) method is O(1) no matter whether the
new item is added at the beginning or at the end of the dictionary. FindItem(key),
FindAllItems(key), RemoveItem(key) and RemoveAllItems(key) methods have O(n)
efficiency.
2. Ordered sequence implementation:- Items are added to the initially empty dictionary
in increasing order of their keys. InsertItem(key, element) method is O(n),because a
search for the proper place of the item is required. If the sequence is implemented as an
ordered array, RemoveItem(key) and RemoveAllItems(key) take O(n) time, because all
items following the item removed must be shifted to fill in the gap.

Example of unordered dictionary:-

Consider an empty unordered dictionary and the following set of operations:

Operation Dictionary Output

InsertItem(5,A) {(5,A)}

InsertItem(7,B) {(5,A), (7,B)}

InsertItem(2,C) {(5,A), (7,B), (2,C)}

InsertItem(8,D) {(5,A), (7,B), (2,C), (8,D)}

InsertItem(2,E) {(5,A), (7,B), (2,C), (8,D), (2,E)}

FindItem(7) {(5,A), (7,B), (2,C), (8,D), (2,E)} B

FindItem(4) {(5,A), (7,B), (2,C), (8,D), (2,E)} NO_SUCH_KEY

FindItem(2) {(5,A), (7,B), (2,C), (8,D), (2,E)} C

FindAllItems(2) {(5,A), (7,B), (2,C), (8,D), (2,E)} C, E

Size() {(5,A), (7,B), (2,C), (8,D), (2,E)} 5

RemoveItem(5) {(7,B), (2,C), (8,D), (2,E)} A

RemoveAllItems(2) {(7,B), (8,D)} C, E

FindItem(4) {(7,B), (8,D)} NO_SUCH_KEY

Example of ordered dictionary:-


Consider an empty unordered dictionary and the following set of operations:

Operation Dictionary Output

InsertItem(5,A) {(5,A)}

InsertItem(7,B) {(5,A), (7,B)}

InsertItem(2,C) {(2,C), (5,A), (7,B)}

InsertItem(8,D) {(2,C), (5,A), (7,B), (8,D)}

InsertItem(2,E) {(2,C), (2,E), (5,A), (7,B), (8,D) }

FindItem(7) {(2,C), (2,E), (5,A), (7,B), (8,D) } B

FindItem(4) {(2,C), (2,E), (5,A), (7,B), (8,D) } NO_SUCH_KEY

FindItem(2) {(2,C), (2,E), (5,A), (7,B), (8,D) } C

FindAllItems(2) {(2,C), (2,E), (5,A), (7,B), (8,D) } C, E

Size() {(2,C), (2,E), (5,A), (7,B), (8,D) } 5

RemoveItem(5) {(2,C), (2,E), (7,B), (8,D)} A

RemoveAllItems(2) {(7,B), (8,D)} C, E

FindItem(4) {(7,B), (8,D)} NO_SUCH_KEY

Sets With Merge-Find Operations.

A disjoint-set data structure (also called a union–find data structure or merge–find set) is a
data structure that keeps track of a set of elements partitioned into a number of disjoint (non-
overlapping) subsets. A union-find algorithm is an algorithm that performs three useful
operations on such a data structure:

1. The Make-Set operation makes a new set by creating a new element.

2. The Find-Set operation determine which subset a particular element is in. This can be
used for determining if two elements are in the same subset.

3. The Union operation joins two subsets into a single subset.

Union-Find Algorithm can be used to check whether an undirected graph contains cycle or not.

You might also like