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

DSA Lab Manual-Group A Writeup

The problem statement involves creating an abstract data type (ADT) to implement the set concept. The ADT should support common set operations like add, remove, contains, size, iterator, intersection, union, and difference. The theory section defines an abstract data type as a type whose behavior is defined by its values and operations, without specifying implementation details. Sets are introduced as an unordered collection of unique elements that support operations like membership testing and set operations.

Uploaded by

Hitesh Mali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

DSA Lab Manual-Group A Writeup

The problem statement involves creating an abstract data type (ADT) to implement the set concept. The ADT should support common set operations like add, remove, contains, size, iterator, intersection, union, and difference. The theory section defines an abstract data type as a type whose behavior is defined by its values and operations, without specifying implementation details. Sets are introduced as an unordered collection of unique elements that support operations like membership testing and set operations.

Uploaded by

Hitesh Mali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment 01

Problem Statement:
Consider telephone book database of N clients. Make use of a hash table implementation to
quickly look up client‘s telephone number. Make use of two collision handling techniques and
compare them using number of comparisons required to find a set of telephone numbers

Theory:
Hash tables are an efficient implementation of a keyed array data structure, a structure
sometimes known as an associative array or map. If you're working in C++, you can take
advantage of the STL map container for keyed arrays implemented using binary trees, but this
article will give you some of the theory behind how a hash table works.
Keyed Arrays vs. Indexed Arrays
One of the biggest drawbacks to a language like C is that there are no keyed arrays. In a normal
C array (also called an indexed array), the only way to access an element would be through its
index number. To find element 50 of an array named "employees" you have to access it like this:
1employees[50];
In a keyed array, however, you would be able to associate each element with a "key," which can
be anything from a name to a product model number. So, if you have a keyed array of employee
records, you could access the record of employee "John Brown" like this:

1employees["Brown, John"];
One basic form of a keyed array is called the hash table. In a hash table, a key is used to find an
element instead of an index number. Since the hash table has to be coded using an indexed array,
there has to be some way of transforming a key to an index number. That way is called the
hashing function.
Hashing Functions
A hashing function can be just about anything. How the hashing function is actually coded
depends on the situation, but generally the hashing function should return a value based on a key
and the size of the array the hashing table is built on. Also, one important thing that is sometimes
overlooked is that a hashing function has to return the same value every time it is given the same
key.
Let's say you wanted to organize a list of about 200 addresses by people's last names. A hash
table would be ideal for this sort of thing, so that you can access the records with the people's last
names as the keys.
First, we have to determine the size of the array we're using. Let's use a 260 element array so that
there can be an average of about 10 element spaces per letter of the alphabet.>
Now, we have to make a hashing function. First, let's create a relationship between letters and
numbers:
A --> 0
B --> 1
C --> 2
D --> 3
...
and so on until Z --> 25.
The easiest way to organize the hash table would be based on the first letter of the last name.
Since we have 260 elements, we can multiply the first letter of the last name by 10. So, when a
key like "Smith" is given, the key would be transformed to the index 180 (S is the 19 letter of the
alphabet, so S --> 18, and 18 * 10 = 180).

Since we use a simple function to generate an index number quickly, and we use the fact that the
index number can be used to access an element directly, a hash table's access time is quite small.
A linked list of keys and elements wouldn't be nearly as fast, since you would have to search
through every single key-element pair.
Basic Operations
Following are the basic primary operations of a hash table.
Search − Searches an element in a hash table.
Insert − inserts an element in a hash table.
delete − Deletes an element from a hash table.

DataItem
Define a data item having some data and key, based on which the search is to be conducted in a
hash table.
struct DataItem
{
int data;
int key;
};

Hash Method
Define a hashing method to compute the hash code of the key of the data item.
int hashCode(int key){
return key % SIZE;
}

Search Operation

Whenever an element is to be searched, compute the hash code of the key passed and locate the
element using that hash code as index in the array. Use linear probing to get the element ahead if
the element is not found at the computed hash code.
Example
struct DataItem *search(int key)
{
//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}

return NULL;
}
Insert Operation
Whenever an element is to be inserted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing for empty location, if an
element is found at the computed hash code.
Example
void insert(int key,int data)
{
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell

while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { //go to next cell

++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
Delete Operation
Whenever an element is to be deleted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing to get the element ahead if
an element is not found at the computed hash code. When found, store a dummy item there to
keep the performance of the hash table intact.
Example
struct DataItem* delete(struct DataItem* item) {
int key = item->key;

//get the hash


int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL) {
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}

return NULL;
}
Expected Output
Menu
1.Create Telephone book
2.Display
3.Look up
Enter Choice1
how many entries2
enter Namea
enter number1234567890
enter Named
enter number3216549876
do u want to continue?(1 for continue)1

Menu
1.Create Telephone book
2.Display
3.Look up
Enter Choice2
a 1234567890
0
0
do u want to continue?(1 for continue)1
Menu
1.Create Telephone book
2.Display
3.Look up
Enter Choice3
enter Name to searchd
found at 0
no of comparision1
do u want to continue?(1 for continue)0*/

Conclusion: In this way we have implemented Hash table for quick lookup using Python.
Assignment 02
Problem Statement:
To create ADT that implement the "set" concept.
 Add (new Element) -Place a value into the set,
 Remove (element) Remove the value
 Contains (element) Return true if element is in collection,
 Size () Return number of values in collection Iterator () Return an iterator used to loop
over collection,
 Intersection of two sets,
 Union of two sets,
 Difference between two sets,
 Subset

Theory

Abstract Data type (ADT):

Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
values and a set of operations. The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented. It does not specify how data will
be organized in memory and what algorithms will be used for implementing the operations. It is
called “abstract” because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known as abstraction.

The user of data type does not need to know how that data type is implemented, for example, we
have been using Primitive values like int, float, char data types only with the knowledge that
these data type can operate and be performed on without any idea of how they are implemented.
So a user only needs to know what a data type can do, but not how it will be implemented. Think
of ADT as a black box which hides the inner structure and design of the data type.
If we consider the smartphone. We look at the high specifications of the smartphone, such as:
o 4 GB RAM
o Snapdragon 2.2ghz processor
o 5 inch LCD screen
o Dual camera
o Android 8.0

The above specifications of the smartphone are the data, and we can also perform the following
operations on the smartphone:

o call(): We can call through the smartphone.


o text(): We can text a message.
o photo(): We can click a photo.
o video(): We can also make a video.

The smartphone is an entity whose data or specifications and operations are given above. The
abstract/logical view and operations are the abstract or logical views of a smartphone.

1. class Smartphone
2. {
3. private:
4. int ramSize;
5. string processorName;
6. float screenSize;
7. int cameraCount;
8. string androidVersion;
9. public:
10. void call();
11. void text();
12. void photo();
13. void video();
14. }

SET:
Sets are a type of abstract data type that allows you to store a list of non-repeated values. Their
name derives from the mathematical concept of finite sets.
Unlike an array, sets are unordered and unindexed. You can think about sets as a room full of
people you know. They can move around the room, changing order, without altering the set of
people in that room. Plus, there are no duplicate people (unless you know someone who has
cloned themselves). These are the two properties of a set: the data is unordered and it is not
duplicated.

Sets have the most impact in mathematical set theory. These theories are used in many kinds of
proofs, structures, and abstract algebra. Creating relations from different sets and codomains are
also an important applications of sets.

In computer science, set theory is useful if you need to collect data and do not care about their
multiplcity or their order. As we've seen on this page, hash tables and sets are very related. In
databases, especially for relational databases, sets are very useful. There are many commands
that finds unions, intersections, and differences of different tables and sets of data.

Set Operations
Sets are a collection of unordered elements that are unique. Meaning that even if the data is
repeated more than one time, it would be entered into the set only once. It resembles the sets that
you have learnt in arithmetic. The operations also are the same as is with the arithmetic sets.
Set Operations include Set Union, Set Intersection, Set Difference, Complement of Set, and
Cartesian Product.
Set Union
The union of sets A and B (denoted by A ∪ B) is the set of elements that are in A, in B, or in
both A and B. Hence, A ∪ B = { x | x ∈ A OR x ∈ B }.
Example − If A = { 10, 11, 12, 13 } and B = { 13, 14, 15 }, then A ∪ B = { 10, 11, 12, 13, 14, 15
}. (The common element occurs only once)

Set Intersection
The intersection of sets A and B (denoted by A ∩ B) is the set of elements which are in both A
and B. Hence, A ∩ B = { x | x ∈ A AND x ∈ B }.
Example − If A = { 11, 12, 13 } and B = { 13, 14, 15 }, then A ∩ B = { 13 }.
Set Difference/ Relative Complement
The set difference of sets A and B (denoted by A – B) is the set of elements that are only in A
but not in B. Hence, A - B = { x | x ∈ A AND x ∉ B }.
Example − If A = { 10, 11, 12, 13 } and B = { 13, 14, 15 }, then (A - B) = { 10, 11, 12 } and (B
- A) = { 14, 15 }. Here, we can see (A - B) ≠ (B - A)

Complement of a Set
The complement of a set A (denoted by A’) is the set of elements which are not in set A. Hence,
A' = { x | x ∉ A }.
More specifically, A'= (U - A) where U is a universal set that contains all objects.
Example − If A = { x | x belongs to set of odd integers } then A' = { y | y does not belong to set
of odd integers }

Cartesian Product / Cross Product


The Cartesian product of n number of sets A1, A2, ... An denoted as A1 × A2 ... × An can be
defined as all possible ordered pairs (x1, x2, ... xn) where x1 ∈ A1, x2 ∈ A2, ... xn ∈ A_n
Example − If we take two sets A = { a, b } and B = { 1, 2 },
The Cartesian product of A and B is written as − A × B = { (a, 1), (a, 2), (b, 1), (b, 2)}
The Cartesian product of B and A is written as − B × A = { (1, a), (1, b), (2, a), (2, b)}
Creating a set
Sets are created using the flower braces but instead of adding key-value pairs, you just pass
values to it.

1 my_set = {1, 2, 3, 4, 5, 5, 5} #create set


2 print(my_set)
Output:
{1, 2, 3, 4, 5}

Adding elements
To add elements, you use the add() function and pass the value to it.

1 my_set = {1, 2, 3}
2 my_set.add(4) #add element to set
3 print(my_set)
Output:
{1, 2, 3, 4}

Operations in sets
The different operations on set such as union, intersection and so on are shown below.

1 my_set = {1, 2, 3, 4}
2 my_set_2 = {3, 4, 5, 6}
3 print(my_set.union(my_set_2), '----------', my_set | my_set_2)
4 print(my_set.intersection(my_set_2), '----------', my_set & my_set_2)
5 print(my_set.difference(my_set_2), '----------', my_set - my_set_2)
6 print(my_set.symmetric_difference(my_set_2), '----------', my_set ^ my_set_2)
7 my_set.clear()
8 print(my_set)

 The union() function combines the data present in both sets.


 The intersection() function finds the data present in both sets only.
 The difference() function deletes the data present in both and outputs data present only in
the set passed.
 The symmetric_difference() does the same as the difference() function but outputs the
data which is remaining in both sets.

Output:
{1, 2, 3, 4, 5, 6} ———- {1, 2, 3, 4, 5, 6}
{3, 4} ———- {3, 4}
{1, 2} ———- {1, 2}
{1, 2, 5, 6} ———- {1, 2, 5, 6}
set()

Conclusion: In this way we have implemented set concept using Python.

You might also like