Aim & Algorithm
Aim & Algorithm
Linear search
AIM: The aim of linear search is to find the position of a particular element in a given array or
list.
Algorithm:
2.Binary search
AIM: The aim of binary search is to find the position of a particular element in a sorted array
or list using a divide and conquer strategy.
Algorithm:
3.Pattern Search
AIM: The aim of pattern search is to find all occurrences of a given pattern within a
larger text or string.
Algorithm:
1. Take the text and the pattern as inputs.
2. Determine the length of the pattern, denoted by m.
3. Iterate through the text, comparing each substring of length m with the
pattern.
4. If a substring is equal to the pattern, record the starting position of the
substring in the text.
5. Continue iterating through the text until all possible substrings of length m
have been checked.
6. Return the list of all starting positions where the pattern occurs in the text.
4.Heap sort
AIM: The aim of heap sort is to sort an array or list of elements in ascending or
descending order using a heap data structure.
Algorithm:
1. Build a max-heap from the array. This can be done by iterating from the
middle of the array to the beginning, and for each element, heapify it by
comparing it with its children and swapping it with the larger child if
necessary.
2. Extract the maximum element from the heap and place it at the end of the
array. Reduce the heap size by one.
3. Heapify the remaining elements of the heap to maintain the heap property.
4. Repeat steps 2-3 until the heap is empty.
5. The sorted array is obtained by reversing the order of the elements that were
extracted from the heap.
5.Insertion sort
AIM: The aim of insertion sort is to sort an array or list of elements in ascending or
descending order using an incremental approach.
Algorithm:
AIM: The aim of Breadth-First Search (BFS) is to traverse or search through a graph or tree
data structure, visiting all the nodes or vertices at each level before moving on to the next
level.
Algorithm:
1. Create a queue data structure and add the starting node to the queue.
2. Mark the starting node as visited.
3. While the queue is not empty, do the following:
• Dequeue a node from the queue and examine it.
• For each unvisited neighbor of the node, mark it as visited and add it to
the queue.
4. Repeat step 3 until the queue is empty.
7.DFS
AIM: The aim of Depth-First Search (DFS) is to traverse or search through a graph or tree
data structure, visiting all the nodes or vertices by exploring as far as possible along each
branch before backtracking.
Algorithm:
1. Create a stack data structure and add the starting node to the stack.
2. Mark the starting node as visited.
3. While the stack is not empty, do the following:
4. Pop a node from the stack and examine it.
5. For each unvisited neighbor of the node, mark it as visited and add it to the
stack.
6. Repeat step 3 until the stack is empty.
8.Dijkstra algorithm
AIM: The aim of Dijkstra's algorithm is to find the shortest path between a source node
and all other nodes in a weighted graph.
Algorithm:
1. Initialize all distances to infinity except the starting node, which is assigned a
distance of 0.
2. Select the node with the smallest distance from the starting node. This node
will be the current node.
3. For each neighbor of the current node, calculate the distance from the starting
node to the neighbor through the current node. If this distance is smaller than
the neighbor's current distance, update the neighbor's distance.
4. Mark the current node as visited.
5. Repeat steps 2-4 until all nodes have been visited or the destination node has
been reached.
6. The shortest path from the starting node to any other node is the distance
assigned to that node at the end of the algorithm.
9.Prims algorithm
AIM: The aim of Prim's algorithm is to find the minimum spanning tree (MST) of a
weighted undirected graph. The minimum spanning tree is the subset of the edges
that connects all the vertices in the graph with the minimum total weight.
Algorithm:
1. Initialize a tree T with a single node, chosen arbitrarily from the graph.
2. Initialize a priority queue Q with all the edges of the graph that connect the
chosen node to its neighbors, with the edge weights as the priorities.
3. While Q is not empty, do the following: a. Extract the edge with the smallest
weight from Q. b. If the edge connects a node in the MST to a node not yet in
the MST, add the edge and the new node to T. c. Add all edges that connect
the new node to nodes not yet in T to Q.
4. When all nodes are in T, the MST is complete.
10.floyd algorithm
AIM: The aim of Floyd's algorithm is to find the shortest paths between all pairs of
nodes in a weighted graph, where the weight of each edge represents the cost of
traversing that edge.
Algorithm:
AIM: The aim of the Floyd-Warshall algorithm is to find the shortest paths between
all pairs of nodes in a weighted graph, where the weight of each edge represents the
cost of traversing that edge, even in graphs with negative edge weights.
Algorithm:
AIM: The aim of finding the minimum and maximum elements in an array or a list is
to determine the smallest and largest elements in the collection, respectively.
Algorithm:
To find the minimum element in an array or a list, you can use the following
algorithm:
To find the maximum element in an array or a list, you can use the following
algorithm:
AIM: The aim of the Merge Sort algorithm is to sort a list of elements in ascending or
descending order.
Algorithm:
1. If the list contains only one element, return the list (it is already sorted).
2. Divide the unsorted list into two sublists of about equal size.
3. Sort each sublist recursively by applying the Merge Sort algorithm to it.
4. Merge the two sorted sublists back into one sorted list: a. Create a new list to
hold the merged elements. b. Compare the first element of each sublist. c.
Add the smaller element to the new list and remove it from its original list. d.
Repeat steps b and c until one sublist is empty. e. Add the remaining elements
of the non-empty sublist to the new list.
5. Return the merged list.
AIM: The N-Queen problem is a classic problem in computer science that involves
finding all possible ways to place N queens on an NxN chessboard, such that no two
queens threaten each other. The aim of the problem is to find all possible solutions
for a given value of N.
15.TSP
1. Create a table to hold the minimum distance between all pairs of cities.
2. Initialize the table so that the distance between any two cities not connected
by an edge is infinite.
3. For each subset of cities that includes the starting city and exactly one other
city: a. Calculate the minimum distance between the two cities and store it in
the table.
4. For each subset of cities that includes the starting city and two or more other
cities: a. For each city in the subset other than the starting city: i. Calculate the
distance between the starting city and the current city. ii. Calculate the
minimum distance from the current city to any other city in the subset, using
the previously calculated values in the table. iii. Store the minimum distance in
the table.
5. Find the minimum distance tour that visits all cities exactly once and returns to
the starting city by iterating over all possible tours and selecting the one with
the shortest total distance.
AIM: The aim of the Kth Smallest Element algorithm is to find the Kth smallest
element in a given list of elements.
Algorithm:
1. Select a pivot element from the list (usually the first or last element).
2. Partition the list into two sublists: elements smaller than the pivot and
elements larger than the pivot.
3. If the number of elements in the sublist of smaller elements is greater than or
equal to K, recursively apply the algorithm to this sublist and return the Kth
smallest element.
4. If the number of elements in the sublist of smaller elements is less than K,
recursively apply the algorithm to the sublist of larger elements and return the
(K - M)th smallest element, where M is the number of elements in the sublist
of smaller elements.