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

Aim & Algorithm

Uploaded by

Thanges Waran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Aim & Algorithm

Uploaded by

Thanges Waran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Linear search

AIM: The aim of linear search is to find the position of a particular element in a given array or
list.

Algorithm:

1. Start from the first element of the array.


2. Compare the current element with the target element.
3. If the current element is equal to the target element, return the index of the
current element.
4. If the current element is not equal to the target element, move to the next
element in the array and repeat steps 2-3.
5. If the end of the array is reached and the target element has not been found,
return a "not found" signal.

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:

1. Start with the middle element of the array.


2. If the middle element is equal to the target element, return the index of the
middle element.
3. If the middle element is less than the target element, search the right half of
the array by setting the starting index to the middle index + 1 and repeating
from step 1.
4. If the middle element is greater than the target element, search the left half of
the array by setting the ending index to the middle index - 1 and repeating
from step 1.
5. If the starting index becomes greater than the ending index, the target
element is not in the array and the algorithm should return a "not found"
signal.

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:

1. Start with the second element in the array, denoted by i.


2. Compare the element at index i with the element to its left, denoted by j=i-1.
3. If the element at index j is greater than the element at index i, swap the
elements and decrement j.
4. Repeat step 3 until either j < 0 or the element at index j is less than or equal
to the element at index i.
5. Increment i and repeat steps 2-4 until all elements in the array have been
processed.
6.BFS

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:

1. Initialize a matrix D of size n x n, where n is the number of nodes in the graph,


and D[i][j] represents the shortest distance between nodes i and j.
2. Initialize D[i][j] to the weight of the edge between nodes i and j if there is an
edge, and infinity otherwise.
3. For each intermediate node k from 1 to n, do the following: a. For each pair of
nodes i and j, if the shortest path from i to j passes through k, update D[i][j] to
the sum of the distances from i to k and from k to j.
4. When all intermediate nodes have been considered, the matrix D contains the
shortest distances between all pairs of nodes.
11.Floyd warshall

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:

1. Initialize a matrix D of size n x n, where n is the number of nodes in the graph,


and D[i][j] represents the shortest distance between nodes i and j.
2. Initialize D[i][j] to the weight of the edge between nodes i and j if there is an
edge, and infinity otherwise.
3. For each intermediate node k from 1 to n, do the following: a. For each pair of
nodes i and j, if the shortest path from i to j passes through k, update D[i][j] to
the minimum of the distances from i to k and from k to j, plus the distance
from k to j.
4. When all intermediate nodes have been considered, the matrix D contains the
shortest distances between all pairs of nodes, even in graphs with negative
edge weights.

12.Find min and max

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:

1. Initialize a variable min to the first element in the collection.


2. For each element in the collection: a. If the element is less than min, set min to
the element.
3. After iterating over all elements, min will contain the smallest element in the
collection.

To find the maximum element in an array or a list, you can use the following
algorithm:

1. Initialize a variable max to the first element in the collection.


2. For each element in the collection: a. If the element is greater than max, set
max to the element.
3. After iterating over all elements, max will contain the largest element in the
collection.
13.

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.

14.N Queen problem

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.

1. Start with an empty chessboard of size NxN.


2. Place a queen in the first column of the first row.
3. Move to the next row and try to place a queen in the first column of that row.
4. If it is not possible to place a queen in the first column, try the next column
until a valid placement is found or all columns have been tried.
5. If a valid placement is found, move to the next row and repeat steps 4 and 5
until all N queens have been placed on the board.
6. If no valid placement is found, backtrack to the previous row and try the next
column until a valid placement is found or all columns have been tried.
7. If all possible solutions have been found, add the solution to the list of
solutions.

15.TSP

AIM: The Traveling Salesman Problem (TSP) is a well-known optimization problem in


computer science that involves finding the shortest possible route that visits all cities in a
given list exactly once and returns to the starting city. The aim of the problem is to find
the optimal solution for a given set of cities.
Algorithm:

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.

16. Kth SMALLEST ELEMENT

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.

You might also like