0% found this document useful (0 votes)
5 views24 pages

DS Unit-IV

The document provides an overview of graphs and hashing, defining key concepts such as vertices, edges, and various types of graphs. It discusses graph traversal techniques like Depth First Search (DFS) and Breadth First Search (BFS), as well as algorithms for finding Minimum Spanning Trees (MST) including Kruskal's and Prim's algorithms. Additionally, it explains hashing, hash tables, and hash functions, detailing methods for collision resolution in hashing.

Uploaded by

214g1a0589
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)
5 views24 pages

DS Unit-IV

The document provides an overview of graphs and hashing, defining key concepts such as vertices, edges, and various types of graphs. It discusses graph traversal techniques like Depth First Search (DFS) and Breadth First Search (BFS), as well as algorithms for finding Minimum Spanning Trees (MST) including Kruskal's and Prim's algorithms. Additionally, it explains hashing, hash tables, and hash functions, detailing methods for collision resolution in hashing.

Uploaded by

214g1a0589
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/ 24

UNIT-IV

GRAPHS AND HASHING


Introduction

Graph is a non-linear data structure. A graph is a collection of vertices and edges.


Generally a graph G = (V, E) is an ordered pair of sets V and E. The element of V is called
vertices and the element of E is called edges.

Graph G can be defined as G = ( V , E ) Where V = {A, B, C, D, E} and E = {(A,B),


(A,C), (A,D), (B,D), (C,D), (B,E), (E,D)}.

Graph Terminology

i. Vertex: Individual data element of a graph is called as Vertex. Vertex is also known
as node. In the above example A, B, C, D, E are vertices.

ii. Edge: An edge is a connecting link between two vertices. Edge is also known
as Arc. In the above example ((A,B), (A,C), (A,D), (B,D), (C,D), (B,E), (D,E)).

iii. Path: A path is a sequence of alternate vertices and edges that starts at a vertex and
ends at other vertex such that each edge is incident to its predecessor and successor
vertex.

iv. Undirected Graph: A graph with only undirected edges is said to be undirected
graph.

v. Directed Graph: A graph with only directed edges is said to be directed graph.

vi. Mixed Graph: A graph with both undirected and directed edges is said to be mixed
graph

vii. Degree: Total number of edges connected to a vertex is said to be degree of that
vertex.

P CHITRALINGAPPA, SRIT Page 1


viii. In-degree: Total number of incoming edges connected to a vertex is said to be in-
degree of that vertex.

ix. Out-degree: Total number of outgoing edges connected to a vertex is said to be out-
degree of that vertex.

Elementary Graph Operations (Graph Traversal)

Graph traversal is a technique used for a searching vertex in a graph. The graph
traversal is also used to decide the order of vertices is visited in the search process. A graph
traversal finds the edges to be used in the search process without creating loops. That means
using graph traversal we visit all the vertices of the graph without getting into looping path.
There are two graph traversal techniques and they are as follows.

i. DFS (Depth First Search)


ii. BFS (Breadth First Search)
DFS (Depth First Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a
graph without loops. We use Stack data structure with maximum size of total number of
vertices in the graph to implement DFS traversal. We use the following steps to implement
DFS traversal...
Step1 - Define a Stack of size total number of vertices in the graph.
Step2 - Select any vertex as starting point for traversal. Visit that vertex and push it on
to the Stack.
Step3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top
of stack and push it on to the stack.
Step4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
at the top of the stack.
Step5 - When there is no new vertex to visit then use back tracking and pop one
vertex from the stack.
Step6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

P CHITRALINGAPPA, SRIT Page 2


P CHITRALINGAPPA, SRIT Page 3
P CHITRALINGAPPA, SRIT Page 4
P CHITRALINGAPPA, SRIT Page 5
P CHITRALINGAPPA, SRIT Page 6
BFS (Breadth First Search)

BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a
graph without loops. We use Queue data structure with maximum size of total number of
vertices in the graph to implement BFS traversal. We use the following steps to implement
BFS traversal.

Step1 - Define a Queue of size total number of vertices in the graph.

Step2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into

the Queue.

Step3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the

Queue and insert them into the Queue.

Step4 - When there is no new vertex to be visited from the vertex which is at front of the

Queue then delete that vertex.

Step5 - Repeat steps 3 and 4 until queue becomes empty.

Step6 - When queue becomes empty, then produce final spanning tree by removing

unused edges from the graph.

P CHITRALINGAPPA, SRIT Page 7


P CHITRALINGAPPA, SRIT Page 8
P CHITRALINGAPPA, SRIT Page 9
Minimum Cost Spanning Trees

Spanning Trees:
A spanning tree is a subset of Graph G, which has all the vertices covered with
minimum possible number of edges. Hence, a spanning tree does not have cycles and it
cannot be disconnected.
A complete undirected graph can have
maximum nn-2 number of spanning trees,
where n is the number of nodes. In the above
addressed example, 33−2 = 3 spanning trees are
possible.

Minimum Spanning Tree (MST)

A minimum cost spanning tree is a spanning tree of least cost. The cost of a spanning
tree of a weighted undirected graph is the sum of the costs (weights) of the edges in the
spanning tree. Three different algorithms can be used to obtain a minimum cost spanning tree
of a connected undirected graph. All three use an algorithm design strategy called the greedy
method. Those famous algorithms are:

i. Kruskal's Algorithm

ii. Prim's Algorithm

iii. Sollin’s Algorithm

Kruskal's Algorithm

Kruskal’s Algorithm builds the spanning tree by adding edges one by one into a
growing spanning tree. Kruskal's algorithm follows greedy approach and it finds an
edge which has least weight and adds it to the growing spanning tree. The following
steps are used to construct MST.
Step1: Remove all loops and Parallel Edges
Step2: Sort the graph edges with respect to their weights.

Step3: Start adding edges to the MST from the edge with the smallest weight until the

edge of the largest weight.

Step4: Only add edges which doesn't form a cycle, edges which connect only

disconnected components.

P CHITRALINGAPPA, SRIT Page 10


To understand Kruskal's algorithm let us consider the following example

Step 1 - Remove all loops and Parallel Edges


Remove all loops and parallel edges from the given graph.

In case of parallel edges, keep the one which has the least cost associated and
remove all others.

Step 2 - Arrange all edges in their increasing order of weight


The next step is to create a set of edges and weight, and arrange them in an ascending
order of weightage (cost).

Step 3 - Add the edge which has the least weightage


Now we start adding edges to the graph beginning from the one which has the
least weight. Throughout, we shall keep checking that the spanning properties remain
intact. In case, by adding one edge, the spanning tree property does not hold then we
shall consider not including the edge in the graph.

P CHITRALINGAPPA, SRIT Page 11


The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them does
not violate spanning tree properties, so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them again −

Next cost in the table is 4, and we observe that adding it will create a circuit in the
graph. –

We ignore it. In the process we shall ignore/avoid all edges that create a circuit.

We observe that edges with cost 5 and 6 also create circuits. We ignore them and move
on.

Now we are left with only one node to be added. Between the two least cost edges
available 7 and 8, we shall add the edge with cost 7.

By adding edge S, A we have included all the nodes of the graph and we now have
minimum cost spanning tree.

P CHITRALINGAPPA, SRIT Page 12


Prim's Algorithm
Prim’s Algorithm also uses Greedy approach to find the minimum spanning tree.
In Prim’s Algorithm we grow the spanning tree from a starting position. Unlike
an edge in Kruskal's, we add vertex to the growing spanning tree in Prim's. Algorithm
steps for prim’s algorithms.
Step1: Select any connected vertices with minimum weight.
Step2: Select unvisited vertex which is adjacent of visited vertices with minimum weight.
Step3: Repeat Step2 until all vertices are visited without forming cycles.

To understand Kruskal’s algorithm let us consider the following example

Step 1 - Remove all loops and Parallel Edges


Remove all loops and parallel edges from the given graph.

In case of parallel edges, keep the one which has the least cost associated and remove
all others.

Step 2 - Choose any arbitrary node as root node


In this case, we choose S node as the root node of Prim's spanning tree. This
node is arbitrarily chosen, so any node can be the root node.
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S, A and S, C are two edges with
weight 7 and 8, respectively. We choose the edge S, A as it is lesser than the other.

P CHITRALINGAPPA, SRIT Page 13


Now, the tree S-7-A is treated as one node and we check for all edges going out from it.
We select the one which has the lowest cost and include it in the tree.

After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will
check all the edges again. However, we will choose only the least cost edge. In this case,
C-3-D is the new edge, which is less than other edges' cost 8, 6, 4, etc.

After adding node D to the spanning tree, we now have two edges going out of it having
the same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the next step will
again yield edge 2 as the least cost. Hence, we are showing a spanning tree with both
edges included.

We may find that the output spanning tree of the same graph using two different
algorithms is same.
Applications of Minimum spanning tree:

i. Design of networks
ii. Cluster Analysis
iii. Handwriting recognition
iv. Image segmentation
v. Travelling salesman problem
vi. Multi-terminal minimum cut problem
vii. Minimum-cost weighted perfect matching

P CHITRALINGAPPA, SRIT Page 14


Hashing:

➢ Hashing is the process of mapping large amount of data item to smaller table
with the help of hashing function.
➢ Hashing is also known as Hashing Algorithm or Message Digest Function.
➢ It is a technique to convert a range of key values into a range of indexes of an
array.
➢ It is used to facilitate the next level searching method when compared with the
linear or binary search.
➢ Hashing is used with a database to enable items to be retrieved more quickly.
➢ It is used in the encryption and decryption of digital signatures.

What is Hash Table?

➢ Hash table or hash map is a data structure used to store key-value pairs.
➢ It is a collection of items stored to make it easy to find them later.
➢ It uses a hash function to compute an index into an array of buckets or slots from
which the desired value can be found.
➢ It is an array of list where each list is known as bucket.
➢ It contains value based on the key.
➢ Hash table is synchronized and contains only unique elements.

➢ The above figure shows the hash table with the size of n = 10. Each position of
the hash table is called as Slot. In the above hash table, there are n slots in the
table, names = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Slot 0, slot 1, slot 2 and so on. Hash table
contains no items, so every slot is empty.
➢ As we know the mapping between an item and the slot where item belongs in
the hash table is called the hash function. The hash function takes any item in the
collection and returns an integer in the range of slot names between 0 to n-1.

What is Hash Function?

➢ A hash function is simply a mathematical formula that maps the key to some
location in the hash table. The location will be calculated from the key value
itself. This one–to-one correspondence between a key value and an index in the
hash table is known as hashing or address calculation indexing. Thus, you can

P CHITRALINGAPPA, SRIT Page 15


say that the key k hashes to a slot h(k), or h(k) is the hash value of key k. if the
size of the hash table is N, then the index of the hash table ranges from 0 to N-1. A
hash table with N slots is denoted by T[N].
➢ The hash function is returned by the integer is called hash key.
There are various types of hash functions.
1. Division Method
2. Mid Square Method
3. Folded Key Method
1. Division Method
One of the fast hashing functions and most widely accepted is the division
method. In this method the key “k” is divided by the number of slots “N” in the hash
table, and the remainder obtained after division is used as an index in the hash table.
The hash function is: h(k) = k mod N

Consider the following keys are to be inserted in the hash table.


54, 72, 89, 37 and table size is 10

Index Hash Value


0
The hash function is: h(k) = k mod N
1
2 72
54 mod 10 = 4 3
72 mod 10 =2 4 54
89 mod 10 = 9
5
37 mod 10 = 7
6
7 37
8
9 89

2. Mid Square Method


In mid-square hashing the key is squared and the address is selected from the
middle of the square number. Let us see the address calculations or 3 distinct keys and
with the hash function.

Address = middle digits of key2

Key k : 9452
Key square k2 : 89340304
Hash function h(k) : 3403

P CHITRALINGAPPA, SRIT Page 16


3. Folded Key Method
Two folding methods are used
1. Fold shift method
2. Fold boundary method

Fold shift method Fold boundary method


The key value is divided into parts The left and right numbers are folded
whose size matches the size of on a fixed boundary between them and
required address and then added. center number. The two outside values
are reversed
Key=123 456 789
123+456+789 321+456+987
=1368 (discard carry) =1764 (discard carry)
=368 =764

Types of Hashing:

Types of Hashing and


Collision Resolve methods

Open Hashing Closed Hashing


(Closed Addressing) (Open Addressing)

Collision Resolve methods

Chaining
Linear Quadratic Double
Probing Probing Hashing

P CHITRALINGAPPA, SRIT Page 17


Open Hashing (Closed Addressing)

To handle the collision,

➢ This technique creates a linked list to the slot for which collision occurs.

➢ The new key is then inserted in the linked list.

➢ These linked lists to the slots appear like chains.

➢ That is why; this technique is called as separate chaining.

Using the hash function ‘key mod 7’, insert the following sequence of keys in the
hash table- 50, 700, 76, 85, 92, 73 and 101 and Use separate chaining technique
for collision resolution.

Solution:

The given sequence of keys will be inserted in the hash table as-

Step1

➢ Draw an empty hash table.


➢ For the given hash function, the possible range of hash values is [0, 6].
➢ So, draw an empty hash table consisting of 7 buckets as-

Step2

➢ Insert the given keys in the hash table one by one.


➢ The first key to be inserted in the hash table = 50.
➢ Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.
➢ So, key 50 will be inserted in bucket-1 of the hash table as-

Step3

➢ The next key to be inserted in the hash table = 700.


➢ Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
➢ So, key 700 will be inserted in bucket-0 of the hash table as-

P CHITRALINGAPPA, SRIT Page 18


Step4

➢ The next key to be inserted in the hash table = 76.


➢ Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
➢ So, key 76 will be inserted in bucket-6 of the hash table as-

Step5

➢ The next key to be inserted in the hash table = 85.


➢ Bucket of the hash table to which key 85 maps =
85 mod 7 = 1.
➢ Since bucket-1 is already occupied, so collision
occurs.
➢ Separate chaining handles the collision by
creating a linked list to bucket-1.
➢ So, key 85 will be inserted in bucket-1 of the hash
table as-

Step6

➢ The next key to be inserted in the hash table = 92.

➢ Bucket of the hash table to which key 92 maps = 92 mod 7 = 1.

➢ Since bucket-1 is already occupied, so collision occurs.

➢ Separate chaining handles the collision by creating a linked list to bucket-1.

➢ So, key 92 will be inserted in bucket-1 of the hash table as-

P CHITRALINGAPPA, SRIT Page 19


Step7

➢ The next key to be inserted in the hash table = 73.

➢ Bucket of the hash table to which key 73 maps = 73 mod 7 = 3.

➢ So, key 73 will be inserted in bucket-3 of the hash table as-

Step8

➢ The next key to be inserted in the hash table = 101.

➢ Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.

➢ Since bucket-3 is already occupied, so collision occurs.

➢ Separate chaining handles the collision by creating a linked list to bucket-3.

➢ So, key 101 will be inserted in bucket-3 of the hash table as-

P CHITRALINGAPPA, SRIT Page 20


Closed Hashing (Open Addressing)

In open addressing, unlike separate chaining, all the keys are stored inside the
hash table. No key is stored outside the hash table.

Techniques used for open addressing are-

➢ Linear Probing
➢ Quadratic Probing
➢ Double Hashing
Operations in Open Addressing-

➢ Search Operation
➢ Insert Operation
➢ Deletion Operation
Open Addressing Techniques-

1. Linear Probing
In linear probing,

➢ When collision occurs, we linearly probe for the next bucket.


➢ We keep probing until an empty bucket is found.
➢ Insert ki at first free location from (u+i)%m where i=0 to (m-1), Where u is
location (index or address).

Advantage Disadvantage

It is easy to compute. The main problem with linear probing is clustering.

2. Quadratic Probing
In quadratic probing,

➢ Insert ki at first free location from (u+i2) % m where i=0 to (m-1), Where u is
Location (index or address).
➢ We keep probing until an empty bucket is found.

P CHITRALINGAPPA, SRIT Page 21


3. Double Hashing
In double hashing,

➢ Let us consider two hash functions h1(k) and h2(k) two functions.
➢ Insert ki at first free location from (u + v * i) % m where i=0 to (m-1), Where u= is
Location (index or address) and v= h2(k) %m.
➢ It requires more computation time as two hash functions need to be computed.

Using the hash function ‘key mod 7’, insert the following sequence of keys in the
hash table- 50, 700, 76, 85, 92, 73 and 101 to use linear probing technique for
collision resolution.

Step1

➢ Draw an empty hash table.

➢ For the given hash function, the possible range of hash


values is [0, 6].

➢ So, draw an empty hash table consisting of 7 buckets as-

Step2

➢ Insert the given keys in the hash table one by one.

➢ The first key to be inserted in the hash table = 50.

➢ Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.

➢ So, key 50 will be inserted in bucket-1 of the hash table as-

Step3

➢ The next key to be inserted in the hash table = 700.

➢ Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
➢ So, key 700 will be inserted in bucket-0 of the hash table as-

P CHITRALINGAPPA, SRIT Page 22


Step4

➢ The next key to be inserted in the hash table = 76.


➢ Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
➢ So, key 76 will be inserted in bucket-6 of the hash table as-

Step5
➢ The next key to be inserted in the hash table = 85.

➢ Bucket of the hash table to which key 85 maps = 85 mod 7 = 1.


➢ Since bucket-1 is already occupied, so collision occurs.
➢ To handle the collision, linear probing technique keeps
probing linearly until an empty bucket is found.
➢ The first empty bucket is bucket-2.
➢ So, key 85 will be inserted in bucket-2 of the hash table as-

Step6
➢ The next key to be inserted in the hash table = 92.

➢ Bucket of the hash table to which key 92 maps = 92 mod 7 = 1.


➢ Since bucket-1 is already occupied, so collision occurs.
➢ To handle the collision, linear probing technique keeps probing
linearly until an empty bucket is found.
➢ The first empty bucket is bucket-3.
➢ So, key 92 will be inserted in bucket-3 of the hash table as-

Step7
➢ The next key to be inserted in the hash table = 73.

➢ Bucket of the hash table to which key 73 maps = 73 mod 7 = 3.


➢ Since bucket-3 is already occupied, so collision occurs.
➢ To handle the collision, linear probing technique keeps

P CHITRALINGAPPA, SRIT Page 23


probing linearly until an empty bucket is found.
➢ The first empty bucket is bucket-4.
➢ So, key 73 will be inserted in bucket-4 of the hash table as-

Step8
➢ The next key to be inserted in the hash table = 101.

➢ Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.
➢ Since bucket-3 is already occupied, so collision occurs.
➢ To handle the collision, linear probing technique keeps
probing linearly until an empty bucket is found.
➢ The first empty bucket is bucket-5.
➢ So, key 101 will be inserted in bucket-5 of the hash table
as-

Assignment

1. Using the hash function ‘key mod 7’, insert the following sequence of keys in the
hash table- 50, 700, 76, 85, 92, 73 and 101 to use Quadratic probing technique
for collision resolution.

2. Using the hash function ‘key mod 7’, insert the following sequence of keys in the
hash table- 50, 700, 76, 85, 92, 73 and 101 to use Double hashing technique for
collision resolution.

3. Using the hash function h(k)=2k+3 and m=10, insert the following sequence of
keys in the hash table- 3, 2, 9, 6, 11, 13, 7 and 12 to use closed addressing
technique for collision resolution.

4. Using the hash function h(k)=2k+3 and m=10, insert the following sequence of
keys in the hash table- 3, 2, 9, 6, 11, 13, 7 and 12 to use Linear probing technique
for collision resolution.

P CHITRALINGAPPA, SRIT Page 24

You might also like