0% found this document useful (0 votes)
5 views

Unit - 4 Searching

The document explains search algorithms, focusing on Linear and Binary search methods. Linear search has O(n) time complexity and checks each element sequentially, while Binary search, applicable only to sorted lists, has O(log n) time complexity and uses a divide-and-conquer approach. Additionally, it discusses differences between various tree structures, including Binary trees, Binary search trees, AVL trees, B trees, and B+ trees, as well as traversal techniques like BFS and DFS.

Uploaded by

harinik842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit - 4 Searching

The document explains search algorithms, focusing on Linear and Binary search methods. Linear search has O(n) time complexity and checks each element sequentially, while Binary search, applicable only to sorted lists, has O(log n) time complexity and uses a divide-and-conquer approach. Additionally, it discusses differences between various tree structures, including Binary trees, Binary search trees, AVL trees, B trees, and B+ trees, as well as traversal techniques like BFS and DFS.

Uploaded by

harinik842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Searching

What is Search?
Search is a process of finding a value in a list of values. In other
words, searching is the process of locating given value position in
a list of values.

Linear Search Algorithm (Sequential Search Algorithm)


• Linear search algorithm finds a given element in a list of elements
with O(n) time complexity where n is total number of elements in
the list.
• This search process starts comparing search element with the first
element in the list. If both are matched then result is element
found otherwise search element is compared with the next
element in the list.
• Repeat the same until search element is compared with the last
element in the list, if that last element also doesn't match, then
the result is "Element not found in the list". That means, the
Linear Search
Linear search is implemented using following steps...
•Step 1 - Read the search element from the user.
•Step 2 - Compare the search element with the first element in
the list.
•Step 3 - If both are matched, then display "Given element is
found!!!" and terminate the function
•Step 4 - If both are not matched, then compare search
element with the next element in the list.
•Step 5 - Repeat steps 3 and 4 until search element is
compared with last element in the list.
•Step 6 - If last element in the list also doesn't match, then
display "Element is not found!!!" and terminate the function.
Example
Consider the following list of elements and the element to be
searched...
Implementation of Linear Search Algorithm using C Programming
Language
#include<stdio.h>
#include<conio.h>

void main(){
int list[20],size,i,sElement;

printf("Enter size of the list: ");


scanf("%d",&size);

printf("Enter any %d integer values: ",size);


for(i = 0; i < size; i++)
scanf("%d",&list[i]);

printf("Enter the element to be Search: ");


scanf("%d",&sElement);

// Linear Search Logic


for(i = 0; i < size; i++)
{
if(sElement == list[i])
{
printf("Element is found at %d index", i);
break;
}
}
if(i == size)
printf("Given element is not found in the list!!!");
getch();

}
Output
Binary search
• Binary search is the search technique which works
efficiently on the sorted lists.
• in order to search an element into some list by using binary
search technique, we must ensure that the list is sorted.

• Binary search follows divide and conquer approach in


which, the list is divided into two halves and the item is
compared with the middle element of the list.
• If the match is found then, the location of middle element
is returned otherwise, we search into either of the halves
depending upon the result produced through the match.
Binary Search Algorithm
• Binary search algorithm finds a given element in a list of elements with
O(log n) time complexity where n is total number of elements in the
list.
• The binary search algorithm can be used with only a sorted list of
elements. That means the binary search is used only with a list of
elements that are already arranged in an order.
• The binary search can not be used for a list of elements arranged in
random order. This search process starts comparing the search
element with the middle element in the list. If both are matched, then
the result is "element found".
• Otherwise, we check whether the search element is smaller or larger
than the middle element in the list.
• If the search element is smaller, then we repeat the same process for
the left sublist of the middle element.
• If the search element is larger, then we repeat the same process for
the right sublist of the middle element.
• We repeat this process until we find the search element in the list or
until we left with a sublist of only one element. And if that element also
doesn't match with the search element, then the result is "Element not
found in the list".
Binary search is implemented using following steps...
•Step 1 - Read the search element from the user.
•Step 2 - Find the middle element in the sorted list.
•Step 3 - Compare the search element with the middle element in the
sorted list.
•Step 4 - If both are matched, then display "Given element is
found!!!" and terminate the function.
•Step 5 - If both are not matched, then check whether the search
element is smaller or larger than the middle element.
•Step 6 - If the search element is smaller than middle element,
repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
•Step 7 - If the search element is larger than middle element, repeat
steps 2, 3, 4 and 5 for the right sublist of the middle element.
•Step 8 - Repeat the same process until we find the search element in
the list or until sublist contains only one element.
•Step 9 - If that element also doesn't match with the search
element, then display "Element is not found in the list!!!" and
terminate the function.
Example
Consider the following list of elements and the element to be
searched...
• BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <=END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT
• Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the
location of the item 23 in the array.
• In 1st step :
BEG = 0
END = 8ron
MID = 4
a[mid] = a[4] = 13 < 23, therefore
• in Second step:
Beg = mid +1 = 5
End = 8
mid = 13/2 = 6
a[mid] = a[6] = 20 < 23, therefore;
• in third step:
beg = mid + 1 = 7
End = 8
mid = 15/2 = 7
a[mid] = a[7]
a[7] = 23 = item;
therefore, set location = mid;
The location of the item will be 7.
Complexity
SN Performance Complexity
1 Worst case O(log n)
2 Best case O(1)
3 Average Case O(log n)
4 Worst case space O(1)
complexity
Implementation of Binary Search Algorithm using C Programmi
Language
void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();

printf("Enter the size of the list: ");


scanf("%d",&size);

printf("Enter %d integer values in Assending order\n", size);

for (i = 0; i < size; i++) if (first > last)


scanf("%d",&list[i]); printf("Element Not found in the list.");
getch();
printf("Enter value to be search: "); }
scanf("%d", &sElement);

first = 0;
last = size - 1;
middle = (first+last)/2;

while (first <= last) {


if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement) {
printf("Element found at index %d.\n",middle);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
Output
Differences between Binary tree and Binary search
tree
Basis for Binary tree Binary search tree
comparison
Definition A binary tree is a non-linear data structure in which a node can A binary search tree is an ordered
have utmost two children, i.e., a node can have 0, 1 or binary tree in which some order is
maximum two children. followed to organize the nodes in a
tree.

Structure The structure of the binary tree is that the first node or the The binary search tree is one of the
topmost node is known as the root node. Each node in a binary types of binary tree that has the value
tree contains the left pointer and the right pointer. The left of all the nodes in the left subtree
pointer contains the address of the left subtree, whereas right lesser or equal to the root node, and
pointer contains the address of right subtree. the value of all the nodes in a right
subtree are greater than or equal to the
value of the root node.

Operations The operations that can be implemented on a binary tree are Binary search trees are the sorted
insertion, deletion, and traversal. binary trees that provide fast insertion,
deletion and search. Lookups mainly
implement binary search as all the
keys are arranged in sorted order.
types Four types of binary trees are Full Binary Tree, Complete There are different types of binary
Binary Tree, Perfect Binary Tree, and Extended Binary Tree. search trees such as AVL trees, Splay
tree, Tango trees, etc.
Differences between Binary Search tree and AVL tree
Binary Search tree AVL tree
Every binary search tree is a binary tree because both the Every AVL tree is also a binary tree because AVL tree also has the utmost
trees contain the utmost two children. two children.

In BST, there is no term exists, such as balance factor. In the AVL tree, each node contains a balance factor, and the value of the
balance factor must be either -1, 0, or 1.

Every Binary Search tree is not an AVL tree because Every AVL tree is a binary search tree because the AVL tree follows the
BST could be either a balanced or an unbalanced tree. property of the BST.

Each node in the Binary Search tree consists of three Each node in the AVL tree consists of four fields, i.e., left subtree, node
fields, i.e., left subtree, node value, and the right subtree. value, right subtree, and the balance factor.

In the case of Binary Search tree, if we want to insert In the case of AVL tree, first, we will find the suitable place to insert the
any node in the tree then we compare the node value node. Once the node is inserted, we will calculate the balance factor of
with the root value; if the value of node is greater than each node.
the root node value then the node is inserted to the right If the balance factor of each node is satisfied, the insertion is completed. If
subtree otherwise the node is inserted to the left subtree. the balance factor is greater than 1, then we need to perform some
Once the node is inserted, there is no need of checking rotations to balance the tree.
the height balance factor for the insertion to be
completed.

Searching is inefficient in BST when there are large Searching is efficient in AVL tree even when there are large number of
number of nodes available in the tree because the height nodes in the tree because the height is balanced.
is not balanced.
Differences between Binary Search tree and AVL tree
Binary Search tree AVL tree

In Binary Search tree, the height or depth of the tree is In AVL tree, the height or depth of the tree is O(logn).
O(n) where n is the number of nodes in the Binary
Search tree.

It is simple to implement as we have to follow the It is complex to implement because in AVL tree, we have to first construct
Binary Search properties to insert the node. the AVL tree, and then we need to check height balance. If the height is
imbalance then we need to perform some rotations to balance the tree.

BST is not a balanced tree because it does not follow the AVL tree is a height balanced tree because it follows the concept of the
concept of the balance factor. balance factor.

Searching is inefficient in BST when there are large Searching is efficient in AVL tree even when there are large number of
number of nodes available in the tree because the height nodes in the tree because the height is balanced.
is not balanced.
Differences between B tree and B+ tree

B tree B+ tree
In the B tree, all the keys and records are stored in both internal In the B+ tree, keys are the indexes stored in the internal nodes
as well as leaf nodes. and records are stored in the leaf nodes.
In B tree, keys cannot be repeatedly stored, which means that In the B+ tree, there can be redundancy in the occurrence of the
there is no duplication of keys or records. keys. In this case, the records are stored in the leaf nodes,
whereas the keys are stored in the internal nodes, so redundant
keys can be present in the internal nodes.
In the Btree, leaf nodes are not linked to each other. In B+ tree, the leaf nodes are linked to each other to provide the
sequential access.
In Btree, searching is not very efficient because the records are In B+ tree, searching is very efficient or quicker because all the
either stored in leaf or internal nodes. records are stored in the leaf nodes.
Deletion of internal nodes is very slow and a time-consuming Deletion in B+ tree is very fast because all the records are stored
process as we need to consider the child of the deleted key also. in the leaf nodes so we do not have to consider the child of the
node.
In Btree, sequential access is not possible. In the B+ tree, all the leaf nodes are connected to each other
through a pointer, so sequential access is possible.
In Btree, the more number of splitting operations are performed B+ tree has more width as compared to height.
due to which height increases compared to width,
In Btree, each node has atleast two branches and each node In B+ tree, internal nodes contain only pointers and leaf nodes
contains some records, so we do not need to traverse till the leaf contain records. All the leaf nodes are at the same level, so we
nodes to get the data. need to traverse till the leaf nodes to get the data.
The root node contains atleast 2 to m children where m is the The root node contains atleast 2 to m children where m is the
order of the tree. order of the tree.
Differences between BFS and DFS
DFS
BFS
Full form BFS stands for Breadth First Search. DFS stands for Depth First Search.
Technique It a vertex-based technique to find the shortest path It is an edge-based technique because
in a graph. the vertices along the edge are explored
first from the starting to the end node.
Definition BFS is a traversal technique in which all the nodes DFS is also a traversal technique in
of the same level are explored first, and then we which traversal is started from the root
move to the next level. node and explore the nodes as far as
possible until we reach the node that has
no unvisited adjacent nodes.
Data Structure Queue data structure is used for the BFS traversal. Stack data structure is used for the BFS
traversal.
Backtracking BFS does not use the backtracking concept. DFS uses backtracking to traverse all
the unvisited nodes.
Number of edges BFS finds the shortest path having a minimum In DFS, a greater number of edges are
number of edges to traverse from the source to the required to traverse from the source
destination vertex. vertex to the destination vertex.
Optimality BFS traversal is optimal for those vertices which DFS traversal is optimal for those
are to be searched closer to the source vertex. graphs in which solutions are away from
the source vertex.
Speed BFS is slower than DFS. DFS is faster than BFS.
Suitability for decision tree It is not suitable for the decision tree because it It is suitable for the decision tree. Based
requires exploring all the neighboring nodes first. on the decision, it explores all the paths.
When the goal is found, it stops its
traversal.
Memory efficient It is not memory efficient as it requires more It is memory efficient as it requires less
memory than DFS. memory than BFS.
Graph Traversal
• A graph traversal is a systematic way of visiting the nodes in a specific order.
• Depth First Traversal
• Breadth first Traversal

• The Breadth First Search (BFS) traversal is an algorithm, which is used to visit
all of the nodes of a given graph.
• In this traversal algorithm one node is selected and then all of the adjacent nodes
are visited one by one.
• After completing all of the adjacent vertices, it moves further to check another
vertices and checks its adjacent vertices again.
BFS
• Step1 : choose any node in the graph, designate it as the search node
& mark it as visited
• Setp 2: Using the adjacency matrix of the graph, find all the
univisted adjacent node and enqueue them into queue Q
• Step 3: then the node is dequeued from the queue. Mark that node as
visited and designate it as the new search node
• Step 4: Repeat the step 2 and 3 using the new search node
• Step 5: this process continues until the queue Q which keeps track of
the adjacent nodes is empty.
Algorithm BFS gives the details.
Procedure BFS(v)

//A breadth first search of G is carried out beginning at vertex v. All


vertices visited are marked as VISITED(I) = 1. The graph G and array
VISITED are global and VISITED is initialised to 0.//
VISITED(v)  1
Initialise Q to be empty //Q is a queue//
loop
for all vertices w adjacent to v do
if VISITED(w) = 0 //add w to queue//
then [call ADDQ(w, Q); VISITED(w)  1] //mark w as VISITED//
end
if Q is empty then return
call DELETEQ(v,Q)
forever
end BFS
Let us consider the same example, given in figure. We start say,
with V1. Its adjacent vertices are V2, V8 , V3 . we visit all one by one. We
pick on one of these, say V2. The unvisited adjacent vertices to V2 are
V4, V5 . we visit both . we back to the remaining visited vertices of V 1 and
pick on one of this, say V3. T The unvisited adjacent vertices to V3 are
V6,V7. There are no more unvisited adjacent vertices of V8, V4, V5, V6 and
V7. Thus the sequence so generated is V V V V V , V V V .
1, 2, 8, 3, 4 5, 6, 7
COMPUTING TIME:

Each vertex visited gets into the queue exactly once, so the loop
forever is iterated at most n times. If an adjacency matrix is used, then the
for loop takes O(n) time for each vertex visited. The total time is, therefore,
O(n2). In case adjacency lists are used the for loop as a total cost of d 1+
……..+dn = O(e) where di = degree(vi). Again, all vertices visited. Together
with all edges incident to from a connected component of G.
DEPTH FIRST SEARCH

The Depth First Search (DFS) is a graph traversal algorithm. In this algorithm
one starting vertex is given, and when an adjacent vertex is found, it moves to
that adjacent vertex first and try to traverse in the same manner.

This procedure is best described recursively as in


Procedure DFS(v)
// Given an undirected graph G = (V.E) with n vertices and an
array visited (n) initially set to zero . This algorithm visits all vertices
reachable from v .G and VISITED are global > //VISITED (v)  1
for each vertex w adjacent to v do
if VISITED (w) =0 then call DFS (w)
end
end DFS
Example
Let us start with V1.
Its adjacent vertices are V2, V8, and V3. Let us pick on v2.
Its adjacent vertices are V1, V4, V5, V1 is already visited . let us pick on V4.
Its adjacent vertices are V2, V8.
V2 is already visited .let us visit V8.
Its adjacent vertices are V4, V5, V1, V6, V7.
V4 and V1 are visited. Let us traverse V5.
Its adjacent vertices are V2, V8. Both are already visited therefore, we back track.
We had V6 and V7 unvisited in the list of V8. We may visit any. We may visit any. We visit V6.
Its adjacent are V8 and V3. Obviously the choice is V3.
Its adjacent vertices are V1, V7 . We visit V7.

All the adjacent vertices of V7 are already visited, we back track and find that we have
visited all the vertices.

Therefore the sequence of traversal is


V1, V2, V4, V5, V6, V3, V7.
This is not a unique or the only sequence possible using this traversal method.

We may implement the Depth First search by using a stack,pushing all unvisited vertices to the one
just visited and poping the stack to find the next vertex to visit.
COMPUTING TIME:
In case G is represented by adjacency lists then the vertices w
adjacent to v can be determined by following a chain of links. Since the
algorithm DFS would examine each node in the adjacency lists at most once
and there are 2e list nodes. The time to complete the search is O (e). if G is
represented by its adjacency matrix. Then the time to determine all vertices
adjacent to v is O(n). since at most n vertices are visited. The total time is
O(n2).

You might also like