0% found this document useful (0 votes)
29 views22 pages

DSA Unit 4 Notes

Uploaded by

pes2ug23cs007
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)
29 views22 pages

DSA Unit 4 Notes

Uploaded by

pes2ug23cs007
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/ 22

Department of Computer science and Engineering

PES UNIVERSITY
UE19CS202: Data Structures and its Applications (4-0-0-4-4)

Applications of BFS and DFS

Abstract

Applications of BFS, Applications of DFS, Implementation of Connectivity of


graph in directed and undirected graphs.

Dr.Sandesh and Saritha


[email protected]
[email protected]

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.

Connectivity of undirected graph using DFS


We choose one vertex as an arbitrary node and traverse from that node.
Algorithm:
connected (G)
Input − undirected graph.
Output − Returns True if the graph is connected otherwise False.

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

Output: For the below graph is connected

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

Connectivity of directed graph using DFS


We choose one vertex as an arbitrary node and traverse from that node.
Algorithm:
connected (G)
Input − directed graph.
Output − Returns True if the graph is connected otherwise False.
Begin

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

Output for the above directed graph is connected graph

Connectivity of directed graph using BFS


Algorithm:
traversal(x, visited)
Input: the arbitrary node x as start node
Output: Traverse all connected vertices.
Begin

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.

Output: Traverse all connected vertices.

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

The output for the above graph is disconnected.

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

• If the keys are numeric, there would be 10 pointers in a node.


• Consider the SSN number as shown.

Operations on TRIE TREES:


1. Insert a node into a TRIE TREE.
The code for the same is as follows:
// create a trie node using the structure definition as given below with 2 fields:
1. Array of pointers of size 255. Since, the no of characters are 255.
Variables:
child - array of pointers to structure trienode.
endofword – to see whether it is end of the string or the word.

struct trienode {
struct trienode *child[255];
int endofword;
};
// create a trienode – getnode function does the job.

struct trienode* getnode()


{
struct trienode* temp;
int i;
temp=(struct trienode *)(malloc(sizeof( struct trienode)));
for(i=0;i<255;i++)
temp->child[i]=NULL; // initially all the variables are assigned NULL
temp->endofword=0; // end of word is assigned to 0.
return temp;
}

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

void insert(struct trienode* root, char *key)


{
struct trienode *curr;
int i, index;

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;
}

// to display the trie tree, the function display is used.

void display(struct trienode *curr)


{int i,j;

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.

int search(struct trienode * root, char *key)


{
int i,index;
struct trienode *curr;
curr=root;
for(i=0;key[i]!='\0';i++)
{ index=key[i];
if(curr->child[index]==NULL)
return 0;
curr=curr->child[index];
}
if(curr->endofword==1)
return 1;
return 0;
}

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;
}

The function checks whether it has any descendents or not. If a node


has descendents then it returns count of the number of descendents
otherwise returns 0.

int check(struct trienode *x)


{
int i,count=0;
for(i=0;i<255;i++)
{
if(x->child[i]!=NULL) count++;
}
return count;
}
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

45. Double hashing, Rehashing T1:473


T2:345-353
Application of hashing in Cryptography.
46.
Summary of Data Structures.
47.

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

45. Double hashing, Rehashing T1:473


T2:345-353
Application of hashing in Cryptography.
46.
Summary of Data Structures.
47.

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

You might also like