Algorithm Unit 1
Algorithm Unit 1
PRIM’S ALGORITHM
Prim's Algorithm is a greedy algorithm that is used to find the
minimum spanning tree from a graph. Prim's algorithm finds the subset of
edges that includes every vertex of the graph such that the sum of the
weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the
adjacent nodes with all the connecting edges at every step. The edges with
the minimal weights causing no cycles in the graph got selected.
PSEUDOCODE
prim(graph):
# Initialize an empty set to hold the vertices in the minimum spanning tree
# Add the edges connected to the vertex to the set of edges to consider
for edge in edges connected to minEdge:
if edge is not in mst:
edges.add(edge)
2.
3. KRUSKAL ALGORITHM
A minimum spanning tree (MST) or minimum weight
spanning tree for a weighted, connected, undirected graph
is a spanning tree with a weight less than or equal to the
weight of every other spanning tree.
Step 1: Sort all edges in increasing order of their edge weights.
Step 3: Check if the new edge creates a cycle or loop in a spanning tree.
Step 4: If it doesn’t form the cycle, then include that edge in MST.
Otherwise, discard it.
PSEUDOCODE
KRUSKAL(G):
A=∅
For each vertex v ∈ G.V:
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A
Start
linear_search ( array, value)
For each element in the array // loop the element till the size
end if
end for
end
Step 1 − Select the middle item in the array and compare it with the key
value to be searched. If it is matched, return the position of the median.
Step 2 − If it does not match the key value, check if the key value is either
greater than or less than the median value.
Step 3 − If the key is greater, perform the search in the right sub-array; but
if the key is lower than the median value, perform the search in the left sub-
array.
Step 4 − Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array
becomes 1.
Step 5 − If the key value does not exist in the array, then the algorithm
returns an unsuccessful search.
PSEUDOCODE
function binary_search(list, target):
left = 0
right = length(list) - 1
while left <= right:
mid = (left + right) // 2
if list[mid] == target:
return mid
elif list[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Unlike binary search, which always selects the middle element, Interpolation
Search makes a more intelligent guess based on the distribution of the
data. It uses a formulaic approach to determine the position of the target
element within the array.
It is particularly effective when the elements are uniformly distributed.
STEP 1: Initialize low and high indices to the start and end of the array,
respectively.
STEP 4 : Repeat steps 2-3 until the target element is found or the low
index exceeds the high index.
PSEUDOCODE
A → Array list
N → Size of A
X → Target Value
Procedure Interpolation_Search()
Set Lo → 0
Set Mid → -1
Set Hi → N-1
if A[Mid] = X
EXIT: Success, Target found at Mid
else
if A[Mid] < X
Set Lo to Mid+1
else if A[Mid] > X
Set Hi to Mid-1
end if
end if
End While
End Procedure
6. NAIVE-STRING-MATCHER (T, P)
The naive pattern searching algorithm does not require any pre-
processing phases. We can find substring by checking once for the string.
It also does not occupy extra space to perform the operation. If a match is
found, the end result of the pattern matching operation will be the index of
specified pattern, otherwise -1. Furthermore, this operation can return all
the indices if the desired pattern appears multiple times within the main
string.
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = T [s + 1....s + m]
5. then print "Pattern occurs with shift" s
7. RABIN-KARP ALGORITHM
pattern.
Step-by-step approach:
RABIN-KARP-MATCHER (T, P, d, q)
1. n ← length [T]
2. m ← length [P]
3. h ← dm-1 mod q
4. p ← 0
5. t0 ← 0
6. for i ← 1 to m
7. do p ← (dp + P[i]) mod q
8. t0 ← (dt0+T [i]) mod q
9. for s ← 0 to n-m
10. do if p = ts
11. then if P [1.....m] = T [s+1.....s + m]
12. then "Pattern occurs with shift" s
13. If s < n-m
14. then ts+1 ← (d (ts-T [s+1]h)+T [s+m+1])mod q
Knuth-Morris and Pratt introduce a linear time algorithm for the string
matching problem. A matching time of O (n) is achieved by avoiding
comparison with an element of 'S' that have previously been involved in
comparison with some element of the pattern 'p' to be matched. i.e.,
backtracking on the string 'S' never occurs
1. The Prefix Function (Π): The Prefix Function, Π for a pattern encapsulates
knowledge about how the pattern matches against the shift of itself. This
information can be used to avoid a useless shift of the pattern 'p.' In other
words, this enables avoiding backtracking of the string 'S.'
2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π' as
inputs, find the occurrence of 'p' in 'S' and returns the number of shifts of 'p'
after which occurrences are found.
Step 1 - If the element is the first element, assume that it is already sorted.
Return 1.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current
element, then move to the next element. Else, shift greater elements in the
array towards the right.
PSEUDOCODE
function insertionSort(array)
key = array[index]
j = index - 1
array[j + 1] = array[j]
j=j-1
array[j + 1] = key
STEPS
swap(&arr[0], &arr[i]);
heapsize--;
maxHeapify(arr,0);