DSA Unit 4 Notes
DSA Unit 4 Notes
PES UNIVERSITY
UE19CS202: Data Structures and its Applications (4-0-0-4-4)
Abstract
www.pes.edu
Applications of BFS and DFS:
Connectivity of graph
A graph is said to be connected if there is a path between every pair of vertex..
A graph with multiple disconnected vertices and edges is said to be
disconnected. To check connectivity of a graph, we traverse all nodes using
graph traversal algorithm like BFS and DFS. On completion of traversal, if there
is any node, which is not visited, then the graph is disconnected.
Connected graph:
Below graph is an example for connected graph since it is possible to traverse
from one vertex to any other vertex. For example, one can traverse from
vertex c to vertex d using the path c-d-e. Similarly we can traverse from vertex
a to e using the path a-c-d-e
Disconnected
The graph shown below is an example for disconnected graph because there
exist no path from b to e.
www.pes.edu
• To check whether a graph is connected or not, we traverse the graph
using either bfs traversal or dfs traversal method
• After the traversal if there is at-least one node which is not marked as
visited then that graph is disconnected graph
Procedure to check the whether a graph is connected or not using
adjacency matrix
• Read the adjacency matrix .
• Create a visited [] array. Start DFS/BFS traversal method from any
arbitrary vertex and mark the visited vertices in the visited [] array.
• Once DFS/BFS is completed check the visited [] array. If there is at-least
one vertex which is marked as unvisited then the graph is disconnected
otherwise it is connected.
Begin
define visited array
for all nodes u in the G, do
mark all nodes unvisited
traversal (u, visited)
if unvisited , then
www.pes.edu
return false
done
return true
End
traversal(u, visited)
Input – Any arbitrary node u as the start node and the visited node to mark
which node is visited
Output: Traverse all connected vertices.
Begin
mark u as visited
for all nodes v, if it is adjacent with u, do
if v is not visited, then
traversal(v, visited)
done
End
www.pes.edu
To check whether the below graph is connected we give the adjacency matrix
for the below graph as input.
graph
A B C D E
A 0 1 1 0 0
B 1 0 1 1 0
C 1 1 0 1 1
D 0 1 1 0 1
E 0 0 1 1 0
www.pes.edu
define visited array
for all nodes u in the G, do
mark all nodes unvisited
traversal (u, visited)
if unvisited , then
return false
done
return true
End
traversal(u, visited)
Input – Any arbitrary node u as the start node and the visited node to mark
which node is visited
Output: Traverse all connected vertices.
Begin
mark u as visited
for all nodes v, if it is adjacent with u, do
if v is not visited, then
traversal(v, visited)
done
End
www.pes.edu
graph
Adjacency matrix
A B C D
A 0 1 1 0
B 0 0 1 0
C 0 0 0 1
D 1 0 0 0
www.pes.edu
make x as visited
insert x into a queue Que
until the Que is not empty, do
u = node taken out from queue
for each vertex v of the graph G, do
if the u and v are connected, then
if u is not visited, then
make u as visited
insert u into the queue Que.
done
done
End
connected(G)
Input − The directed graph.
Output − True if the graph is connected otherwise returns False.
Begin
define visited array
for all nodes u in the G, do
mark all nodes as unvisited
traversal(u, visited)
if unvisited , then
return false
done
return true
www.pes.edu
End
We input the adjacency matrix for the below graph as and the The output for
the above directed graph is connected.
A B C D
A 0 1 1 0
B 0 0 1 0
C 0 0 0 1
D 1 0 0 0
www.pes.edu
Connectivity of undirected graph using BFS
Algorithm:
traversal(x, visited)
Input: The arbitrary node x as start node and the visited node to mark which
node is visited.
Begin
make x as visited
insert x into a queue Que
until the Que is not empty, do
u = node that is taken out from the queue
for each vertex v of the graph G, do
if the u and v are connected, then
if u is not visited, then
make u as visited
insert u into the queue Que.
done
done
End
Connected(G)
Input − The directed graph.
Output − True if the graph is connected otherwise returns False.
www.pes.edu
Begin
define visited array
for all nodes u in the G, do
mark all nodes as unvisited
traversal(u, visited)
if unvisited , then
return false
done
return true
End
graph
A B C D E
A 0 1 1 0 0
B 1 0 1 1 0
C 1 1 0 1 1
D 0 1 1 0 1
E 0 0 1 1 0
www.pes.edu
The output for the below undirected graph using BFS is connected.
graph
A B C D
A 0 1 1 0
B 0 0 1 0
C 0 0 0 0
D 0 0 0 0
www.pes.edu
TRIE Trees
Introduction
Strings can essentially be viewed as the most important and common topics for a variety of
programming problems. String processing has a variety of real world applications too, such as:
Search Engines
Genome Analysis
Data Analytics
All the content presented to us in textual form can be visualized as nothing but just strings.
Tries:
Tries are an extremely special and useful data-structure that are based on the prefix of a string.
They are used to represent the “Retrieval” of data and thus the name Trie.
Prefix : What is prefix:
The prefix of a string is nothing but any n letters n≤|S| that can be considered beginning strictly
from the starting of a string. For example , the word “abacaba” has the following prefixes:
a
ab
aba
abac
abaca
abacab
A Trie is a special data structure used to store strings that can be visualized like a graph. It
consists of nodes and edges. Each node consists of at max 26 children and edges connect each
parent node to its children. These 26 pointers are nothing but pointers for each of the 26 letters
of the English alphabet A separate edge is maintained for every edge.
Strings are stored in a top to bottom manner on the basis of their prefix in a trie. All prefixes of
length 1 are stored at until level 1, all prefixes of length 2 are sorted at until level 2 and so on.
For example , consider the following diagram
Now, one would be wondering why to use a data structure such as a trie for processing a single
string? Actually, Tries are generally used on groups of strings, rather than a single string. When
given multiple strings , we can solve a variety of problems based on them.
For example, consider an English dictionary and a single string s, find the prefix of maximum
length from the dictionary strings matching the string s. Solving this problem using a naive
approach would require us to match the prefix of the given string with the prefix of every other
word in the dictionary and note the maximum. The is an expensive process considering the
amount of time it would take. Tries can solve this problem in much more efficient way.
Before processing each Query of the type where we need to search the length of the longest
prefix, we first need to add all the existing words into the dictionary. A Trie consists of a special
node called the root node. This node doesn't have any incoming edges. It only contains 26
outgoing edfes for each letter in the alphabet and is the root of the Trie.
So, the insertion of any string into a Trie starts from the root node. All prefixes of length one are
direct children of the root node. In addition, all prefixes of length 2 become children of the
nodes existing at level one.
TRIE tree is a digital search tree, need not be implemented as a binary tree.
•Each node in the tree can contain ‘m’ pointers –corresponding to ’m‘ possible symbols
in each position of the key.
•Generally used to store strings.
A trie, pronounced “try”, is a tree that exploits some structure in the keys
-e.g. if the keys are strings, a binary search tree would compare the entire strings but a
triewould look at their individual characters
-A trieis a tree where each node stores a bit indicating whether the string spelled out to this
point is in the set
struct trienode {
struct trienode *child[255];
int endofword;
};
// create a trienode – getnode function does the job.
Function to insert a node / character into the trie tree using the
function insert.
For every character read getnode function and store the link in the
child variable
curr = root;
for(i=0;key[i]!='\0';i++)
{ index=key[i];
if(curr->child[index]==NULL)
curr->child[index]=getnode();
curr=curr->child[index];
}
curr->endofword=1;
}
for(i=0;i<255;i++)
{
if(curr->child[i]!=NULL)
{
word[length++]=i;
if(curr->child[i]->endofword==1)
{
//print the word
printf("\n");
for(j=0;j<length;j++)
printf("%c",word[j]);
}
display(curr->child[i]);
}
}
length--;
return;
}
To search for a given string, use the function search as shown below.
To, delete a given string, use the function delete_trie as shown below.
The function searches for a given string in the tree. If the string does not exist then it displays
string not found. Otherwise, the word has to be deleted with respect to the following cases:
Case 1: As the word is searched character by character in the trie, the index and the
addresses of the nodes are stored on the stack if a match is found. At the end, endofword is
set to 0.
Now, to delete the word, first pop the top of the stack, if it has -1 as the index it does nothing
as it is the end of the word. Otherwise, it does nothing if it is a root node of the trie tree.
Otherwise it ill delete the node if the node doesnot have any descendents (child nodes).
void delete_trie(struct trienode *root,char *key)
{
int i,k,index;
struct trienode *curr;
struct stack x;
curr =root;
for(i=0;key[i]!='\0';i++)
{ index=key[i];
if(curr->child[index]==NULL)
{
printf("Word not found..");
return;
}
push(curr,index);
curr=curr->child[index];
}
curr->endofword=0;
push(curr,-1);
while(1)
{
x=pop();
if(x.index!=-1)
x.m->child[x.index]=NULL;
if(x.m==root)//if root
break;
k=check(x.m);
if((k>=1)||(x.m->endofword==1))break;
else free(x.m);
}
return;
}
# of Hours : 56
Chapter
Class # Title/Reference Topics Covered Reference Page
Literature Chapter Numbers
Suffix Trees: Definition, Introduction of Trie
38. T1 : Chapter 7 465, 467
Trees, suffix trees
39. Implementation of Trie trees-insert operations, T2 : Chapter 10 457 - 461
Implementation of Trie trees-delete and search
40. T2 : Chapter 10 457-461
operations.
41. Unit-5: Application: URL decoding
Suffix Tree ,
Hashing T1: 468-470
42. Techniques Hash: definition, hash function, hash table.
T2: 329, 345-
T1: Chapters
353
7(7.3,7.4)
R1: Chapter T1:488-491
43. Collision Handling: Separate Chaining
8(8.1,8.6) T1: Chapter 7 T2:345-353
10(10.2,10.3)
T2 Chapter 8
44. Collision Handling: Open Chaining T1:471-473
T2:345-353
Literature
Publication Information
Book Type Code Title & Author
Edition Publisher Year
Data Structures using C & C++,
YedidyahLangsam, Moshe J. Augenstein,
Text Book T1 Aaron M. Tenenbaum 2nd Pearson Education 2015
Data Structures and Program Design in C,
Reference Robert L. Kruse, Clovis L. Tando, Bruce P.
R1 2nd Pearson Education 2007
Book Leung
UE19CS202: DATA STRUCURES AND ITS APPLICATIONS (4-0-0-4)
Department of Computer Science & Engineering
PES UNIVERSITY
# of Hours : 56
Chapter
Class # Title/Reference Topics Covered Reference Page
Literature Chapter Numbers
Suffix Trees: Definition, Introduction of Trie
38. T1 : Chapter 7 465, 467
Trees, suffix trees
39. Implementation of Trie trees-insert operations, T2 : Chapter 10 457 - 461
Implementation of Trie trees-delete and search
40. T2 : Chapter 10 457-461
operations.
41. Unit-5: Application: URL decoding
Suffix Tree ,
Hashing T1: 468-470
42. Techniques Hash: definition, hash function, hash table.
T2: 329, 345-
T1: Chapters
353
7(7.3,7.4)
R1: Chapter T1:488-491
43. Collision Handling: Separate Chaining
8(8.1,8.6) T1: Chapter 7 T2:345-353
10(10.2,10.3)
T2 Chapter 8
44. Collision Handling: Open Chaining T1:471-473
T2:345-353
Literature
Publication Information
Book Type Code Title & Author
Edition Publisher Year
Data Structures using C & C++,
YedidyahLangsam, Moshe J. Augenstein,
Text Book T1 Aaron M. Tenenbaum 2nd Pearson Education 2015
Data Structures and Program Design in C,
Reference Robert L. Kruse, Clovis L. Tando, Bruce P.
R1 2nd Pearson Education 2007
Book Leung