CIE III
CIE III
CIE III
Show the adjacency matrix and adjacency list representation for the
below graph.
A graph, G, consists of two sets: a finite, nonempty set of vertices, and a finite, possibly
empty set of edges. V(G) and E(G) represent the sets of vertices and edges of G, respectively.
Alternately, we may write G = (V, E) to represent a graph.
Graphs are represented in 3 different ways: adjacency matrices, adjacency lists, and
adjacency multilists.
i. Adjacency Matrix
Let G = (V, E) be a graph with n vertices, m > 1. The adjacency matrix of G is a two-
dimensional n xn array, say adj-mat. If the edge (Vj, Vj) is in E(G), adj-mat[i][j] = 1.
If there is no such edge in E(G), adj~mat[i][j] = 0.
i. Depth-First Search
Begin the search by visiting the start vertex v o If v has an unvisited neighbor,
traverse it recursively o Otherwise, backtrack. We begin by visiting the start vertex v.
Next an unvisited vertex w adjacent to v is selected, and a depth-first search from w is
initiated. When a vertex u is reached such that all its adjacent vertices have been visited,
we back up to the last vertex visited that has an unvisited vertex w adjacent to it and
initiate a depth-first search from w.
The search terminates when no unvisited vertex can be reached from any of the
visited vertices. DFS traversal of a graph, produces a spanning tree as final result.
Spanning Tree is a graph without any loops. We use Stack data structure with maximum
size of total number of vertices in the graph to implement DFS
3. Folding Method:
Step 1: Divide the key value into a number of parts. That is, divide k into parts k1,
k2, ..., kn, where each part has the same number of digits except the last part which
may have lesser digits than the other parts.
Step 2: Add the individual parts. That is, obtain the sum of k1 + k2 + ... + kn. The
hash value is produced by ignoring the last carry, if any.
4. What is Collision. What are the methods to resolve collision? Explain Linear probing
with example?
Figure shows a hash table in which each key from the set K is mapped to locations
generated by using a hash function. Note that keys k2 and k6 point to the same memory
location. This is known as collision. That is, when two or more keys map to the same
memory location collision occurs.
A method used to solve the problem of collision, also called collision resolution
technique, is applied. The two most popular methods of resolving collisions are:
1. Collision Resolution by Linear Probing (open addressing)
2. Quadratic Probing
3. Double Hashing
4. Rehashing
5. Chaining
Example: Consider a hash table of size 10. Using linear probing, insert the keys 72, 27,
36, 24, 63, 81, 92, and 101 into the table.
Solution: Let H(k) = k mod
m, m = 10 Initially hash
table will be
0 1 2 3 4 5 6 7 8 9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
H(72) = 72 mod 10 = 2
0 1 2 3 4 5 6 7 8 9
-1 -1 72 -1 -1 -1 -1 -1 -1 -1
H(27) = 27 mod 10 = 7
0 1 2 3 4 5 6 7 8 9
-1 -1 72 -1 -1 -1 -1 27 -1 -1
H(36) = 36 mod 10 = 6
0 1 2 3 4 5 6 7 8 9
-1 -1 72 -1 -1 -1 36 27 -1 -1
H(24) = 24 mod 10 = 4
0 1 2 3 4 5 6 7 8 9
-1 -1 72 -1 24 -1 36 27 -1 -1
H(63) = 63 mod 10 =3
0 1 2 3 4 5 6 7 8 9
-1 -1 72 63 24 -1 36 27 -1 -1
H(81) = 81 mod 10 =1
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 -1 36 27 -1 -1
H(92) = 92 mod 10 =2
Collision occurred since 2 is already filled. CEC So go to next position – 3, which is also
already filled, go to next position – 4 which is also already filled. So go to 5 – which is
not filled – so insert the key 92 in position 5.
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 92 36 27 -1 -1
H(101) = 101 mod 10 = 1
Collision occurred since 1 is already filled. Do linear probing and the next position free is
8, so insert key 101 in position 8.
0 1 2 3 4 5 6 7 8 9
-1 81 72 63 24 92 36 27 101 -1
Dynamic hashing is a data structure technique that allows for efficient management of
hash tables with a changing number of records. Dynamically increases the size of the hash
table as collision occurs.
Here, the 2 bit addresses are the actual addresses of these pages (actually they are an
offset of some base address). Thus, the hash function delivers the actual address of a page
containing the key. Moreover, every value produced by the hash function must point to an
actual page. In contrast to the directory scheme where a single page might be pointed at by
several directory entries, in the directoryless scheme there must exist a unique page for every
possible address. Figure shows a simple trie and its mapping to contiguous memory without a
directory.
Now when a page overflows, we could double the size of the address space, but this is
wasteful. Instead, whenever an overflow occurs, we add a new page to the end of the file, and
divide the identifiers in one of the pages between its original page and the new page.
6. What is Priority Queue? Write the functions to implement Maximum Priority Queue
with an example.
i) Insert into Max Priority Queue.
ii)Delete into Max Priority Queue.
A priority queue is a type of queue that arranges elements based on their priority
values. Elements with higher priority values are typically retrieved or removed before
elements with lower priority values. Each element has a priority value associated with it.
When we add an item, it is inserted in a position based on its priority value.
There are two types of priority queues based on the priority of elements.
If the element with the smallest value has the highest priority, then that priority queue
is called the min priority queue.
If the element with a higher value has the highest priority, then that priority queue is
known as the max priority queue.
The main operations performed on a leftist tree include insert, extract-min and merge.
The insert operation simply adds a new node to the tree, while the extract-min operation
removes the root node and updates the tree structure to maintain the leftist property. The
merge operation combines two leftist trees into a single leftist tree by linking the root nodes
and maintaining the leftist property.
A leftist tree is a binary tree with properties:
1. Normal Min Heap Property : key(i) >= key(parent(i))
2. Heavier on left side : dist(right(i)) <= dist(left(i)). Here, dist(i) is the number of edges
on the shortest path from node i to a leaf node in extended binary tree representation
(In this representation, a null child is considered as external or leaf node). The shortest
path to a descendant external node is through the right child. Every subtree is also a
leftist tree and dist( i ) = 1 + dist( right( i ) ).
Example: The below leftist tree is presented with its distance calculated for each node with
the procedure mentioned above. The rightmost node has a rank of 0 as the right subtree of
this node is null and its parent has a distance of 1 by dist( i ) = 1 + dist( right( i )). The same is
followed for each node and their s-value( or rank) is calculated.
8. Discuss AVL Tree with an example. Write a Function for insert into an AVL tree.
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees for any node cannot be more than one.
The difference between the heights of the left subtree and the right subtree for any node is
known as the balance factor of the node.
Example:
Operations on an AVL Tree:
Insertion
Deletion
Searching
Following are two basic operations that can be performed to balance a BST without
violating the BST property (keys(left) < key(root) < keys(right)).
Left Rotation
Right Rotation
Given a sorted array key [0.. n-1] of search keys and an array freq[0.. n-1] of
frequency counts, where freq[i] is the number of searches for keys[i]. Construct a binary
search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is the level of that node
multiplied by its frequency. The level of the root is 1.
Examples:
Input: keys[] = {10, 12}, freq[] = {34, 50}
There can be following two possible BSTs
10 12
\ /
12 10