Data Structures-3
Data Structures-3
Syllabus
• Graphs: Basic Terminology, Types of Graphs, Graphs
representations – adjacency matrix, adjacency list; Traversals
Module-2 - breath first search and depth first search; Applications of
graphs.
Unit-2: Graphs
• Hashing: Introduction, Different hash functions, collision:
avoidance and handling methods.
1
14-05-2025
2
14-05-2025
3
14-05-2025
4
14-05-2025
Digraph Terminology
• Weakly connected digraph A directed graph is said to be weakly
REPRESENTATION OF GRAPHS
connected if it is connected by ignoring the direction of edges.
That is, in such a graph, it is possible to reach any node from any • There are three common ways of storing graphs in the computer’s
other node by traversing edges in any direction (may not be in
the direction they point). The nodes in a weakly connected
memory.
directed graph must have either out-degree or in-degree of at • They are:
least 1.
• Sequential representation by using an adjacency matrix.
• Simple directed graph A directed graph G is said to be a simple
directed graph if and only if it has no parallel edges. However, a • Linked representation by using an adjacency list that stores the
simple directed graph may contain cycles with the exception that neighbours of a node using a linked list.
it cannot have more than one loop at a given node.
• Adjacency multi-list which is an extension of linked representation.
• Parallel/Multiple edges Distinct edges which connect the same
end-points are called multiple edges. That is, e = (u, v) and e' =
(u, v) are known as multiple edges of G. In Fig. 13.5(a), e3 and e5
are multiple edges connecting nodes C and D.
• In an adjacency matrix, the rows and columns are labelled by graph vertices. An entry
aij in the adjacency matrix will contain 1, if vertices vi and vj are adjacent to each other.
• However, if the nodes are not adjacent, aij will be set to zero.
5
14-05-2025
6
14-05-2025
Example Example
• We use an undirected graph with 5 vertices. • start from vertex 0, the BFS algorithm starts by putting it in the Visited
list and putting all its adjacent vertices in the queue.
Example Example
• Next, visit the element at the front of queue i.e. 1 and go to its • Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the
adjacent nodes. Since 0 has already been visited, visit 2 instead. back of the queue and visit 3, which is at the front of the queue.
7
14-05-2025
Example Example
• Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the • Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is
back of the queue and visit 3, which is at the front of the queue. already visited. We visit it.
Example-2 Example-2
• Find the BFS traversal for the given node. • Find the BFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visit A and write its adjacent nodes B, C, and D in the queue Next, visit the first node from the Queue B and write its adjacent nodes E in queue
Visited A Visited A B
Queue B C D Queue C D E
8
14-05-2025
Example-2 Example-2
• Find the BFS traversal for the given node. • Find the BFS traversal for the given node.
Consider A as Source node Consider A as Source node
Next, visit the first node from the Queue C and insert its unvisited adjacent nodes G in the queue Next, visit the first node from the Queue D and insert its unvisited adjacent nodes in the queue
Visited A B C Visited A B C D
Queue D E G Queue E G
Example-2 Example-2
• Find the BFS traversal for the given node. • Find the BFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B C D E Visited A B C D E G
Queue G F Queue F H I
9
14-05-2025
Example-2 Example-2
• Find the BFS traversal for the given node. • Find the BFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B C D E G F Visited A B C D E G F H
Queue H I Queue I
Queue
10
14-05-2025
Example Example
• Given an undirected graph with 5 vertices. • Start from vertex 0, the DFS algorithm starts by putting it in the
Visited list and putting all its adjacent vertices in the stack.
11
14-05-2025
Example Example
• Next, visit the element at the top of the stack, i.e. 1 and go to its • Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the
adjacent nodes. Since 0 has already been visited, we visit 2 instead. top of the stack and visit it.
Example Example
• Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the • After we visit the last element 3, it doesn't have any unvisited adjacent
top of the stack and visit it. nodes, so we have completed the Depth First Traversal of the graph.
12
14-05-2025
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A Visited A B
Stack Stack
TOP B TOP E
C C
D D
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B E Visited A B E C
Stack Stack
C G
F F
C C
D D
13
14-05-2025
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B E C G Visited A B E C G F
C
F Stack H Stack
H
H
I
I
F
F
C
C
D
D
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B E C G F H Visited A B E C G F H
E
I Stack I Stack
H H
I I
F F
C C
D D
14
14-05-2025
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B E C G F H I Visited A B E C G F H I
Stack Stack
H
I I
F F
C C
D D
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B E C G F H I Visited A B E C G F H I
Stack Stack
F
C C
D D
15
14-05-2025
Example-2 Example-2
• Find the DFS traversal for the given node. • Find the DFS traversal for the given node.
Consider A as Source node Consider A as Source node
Visited A B E C G F H I Visited A B E C G F H I D
Stack Stack
D D
Applications of DFS
• Space complexity The space complexity of a depth-first search is • Depth-first search is useful for:
lower than that of a breadth-first search. • Finding a path between two specified nodes, u and v, of an
• Time complexity The time complexity of a depth-first search is unweighted graph.
proportional to the number of vertices plus the number of edges in • Finding a path between two specified nodes, u and v, of a weighted
the graphs that are traversed. graph.
• The time complexity can be given as (O(|V| + |E|)). • Finding whether a graph is connected or not.
• Computing the spanning tree of a connected graph.
16
14-05-2025
INTRODUCTION
• We discussed two search algorithms: linear search and binary search.
• Linear search has a running time proportional to O(n), while binary
Module-2 search takes time proportional to O(log n),
Where, n is the number of elements in the array.
Unit-2 Hashing
• Binary search and binary search trees are efficient algorithms to
search for an element.
• But what if we want to perform the search operation in time
proportional to O(1)?
1
14-05-2025
Hash Table
• Hash table is a data structure in which keys are mapped to array Cont..
positions by a hash function.
• In the example discussed here we will use a hash function that • However, when the set K of keys that are actually used is smaller than
extracts the last two digits of the key. the universe of keys (U), a hash table consumes less storage space.
• Therefore, we map the keys to array locations or array indices. • The storage requirement for a hash table is O(k), where k is the
• A value stored in a hash table can be searched in O(1) time by using a number of keys actually used.
hash function which generates an address from the key (by producing • In a hash table, an element with key k is stored at index h(k) and not
the index of the array where the value is stored). k.
• It means a hash function h is used to calculate the index at which the
element with key k will be stored.
2
14-05-2025
3
14-05-2025
Drawbacks Example
• A potential drawback of the division method is that while using this • Calculate the hash values of keys 1234 and 5462.
method, consecutive keys map to consecutive hash values. • Solution Setting M = 97, hash values can be calculated as:
• On one hand, this is good as it ensures that consecutive keys do not • h(1234) = 1234 % 97 = 70
collide, but on the other, it also means that consecutive array
locations will be occupied. • h(5642) = 5642 % 97 = 16
• This may lead to degradation in performance.
4
14-05-2025
5
14-05-2025
Example COLLISIONS
• Collisions occur when the hash function maps two different keys to
the same location.
• Obviously, two records cannot be stored in the same location.
6
14-05-2025
• Where m is the size of the hash table, h¢(k) = (k mod m), and i is the
probe number that varies from 0 to m–1.
• Therefore, for a given key k, first the location generated by [h¢(k) mod
m] is probed because for the first time i=0.
• If the location is free, the value is stored in it, else the second probe
generates the address of the location given by [h¢(k) + 1]mod m.
7
14-05-2025
Example Example
Example Example
8
14-05-2025
Example Example
9
14-05-2025
• Where 𝑚 is the size of the hash table, ℎ (𝑘) and ℎ (𝑘) are two hash functions given as
ℎ (𝑘) = 𝑘 𝑚𝑜𝑑 𝑚
ℎ (𝑘) = 𝑘 𝑚𝑜𝑑 𝑚′,
• 𝑖 is the probe number that varies from 0 to m–1, and 𝑚′ is chosen to be less than m.
• We can choose 𝑚′ = 𝑚– 1 𝑜𝑟 𝑚– 2.
Example Example
10
14-05-2025
Example
Rehashing
• When the hash table becomes nearly full, the number of collisions
increases, thereby degrading the performance of insertion and search
operations.
• Now T[6] is occupied, so we cannot store the key 101 in T[6]. • In such cases, a better option is to create a new hash table with size double
• Therefore, try again for the next location with probe i = 2.
of the original hash table.
• Repeat the entire process until a vacant location is found. • All the entries in the original hash table will then have to be moved to the
new hash table.
• we have to probe many times to insert the key 101 in the hash table.
• Although double hashing is a very efficient algorithm, it always requires m to be a • This is done by taking each entry, computing its new hash value, and then
prime number. inserting it in the new hash table.
• In our case m=10, which is not a prime number, hence, the degradation in • Though rehashing seems to be a simple process, it is quite expensive and
performance. must therefore not be done frequently.
• Thus, we can say that the performance of the technique is sensitive to the value
of m.
Example
Collision Resolution by Chaining
• In chaining, each location in a hash table stores a pointer to a linked
list that contains all the key values that were hashed to that location.
• That is, location l in the hash table points to the head of the linked list
of all the key values that hashed to l.
• However, if no key value hashes to l, then location l in the hash table
contains NULL.
11
14-05-2025
Example Example
12
14-05-2025
APPLICATIONS OF HASHING
• To implement compiler symbol tables in C++. The compiler uses a
• symbol table to keep a record of all the user-defined symbols in a C++
program. Hashing facilitates
• the compiler to quickly look up variable names and other attributes
associated with symbols.
• Hashing is also widely used for Internet search engines.
13