0% found this document useful (0 votes)
7 views18 pages

Design and Analysis of Algorithms Lab - 4

Uploaded by

paul.r212003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views18 pages

Design and Analysis of Algorithms Lab - 4

Uploaded by

paul.r212003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Design and Analysis of Algorithms Lab

(BCSE204P)
Faculty: - Dr Malini S

Digital Assignment IV:


Rabin Karp, Bellman Ford and Floyd W.

Date: 27th March, 2024

Rishabh Paul (21BCE2019)


(School of Computer Science and Engineering)
rishabh.paul2021@vitstudent.ac.in
Q1.) Rabin-Karp

AIM:

The Rabin-Karp algorithm is used for pattern searching in strings. It aims to


efficiently find the occurrences of a given pattern within a text by using
hashing techniques. It's particularly useful when the pattern needs to be
searched in multiple texts.

ALGORITHM:

Step 1: Preprocessing

• Compute the hash value of the pattern.


• Compute the hash value of the initial window of the text with the
same length as the pattern.

Step 2: Main Algorithm

• Slide the window one position at a time through the text.


• Recompute the hash value of the new window.
• Compare the hash value of the window with the hash value of the
pattern.
• If the hash values match, do a character-by-character comparison to
confirm the match.

Step 3: Repeat Step 2

• Repeat Step 2 until the end of the text is reached.


PSEUDOCODE:

function RabinKarp(text, pattern):


n = length of text
m = length of pattern
patternHash = hash(pattern)
textHash = hash(text[0:m])

for i from 0 to n - m:
if textHash == patternHash:
if text[i:i+m] == pattern:
return i
if i < n - m:
textHash = recomputeHash(text, i, i+m, textHash)

return -1 // Pattern not found

function hash(string):
// Compute hash value of string using a suitable hashing function

function recomputeHash(text, oldIndex, newIndex, oldHash):


// Recompute hash value of text when sliding the window
// Use a rolling hash technique for efficiency
C/C++ CODE:
It asks to enter the string, so we’ll provide:

• Typing

• onetwoTyping
OUTPUT/RESULT:
Example2:
VERIFICATION STATUS
Q2.) Bellman-Ford

AIM

The Bellman-Ford algorithm is used to find the shortest paths from a single
source vertex to all other vertices in a weighted graph, even in the presence
of negative weight edges, provided that there are no negative cycles. Its
primary aim is to handle graphs with negative edge weights, which Dijkstra's
algorithm cannot handle efficiently.

ALGORITHM
• Initialization:

1.) Initialize an array dist to store the shortest distances from the source
vertex to all other vertices. Set the distance to the source vertex as 0 and
all other distances as infinity.
2.) Initialize an array prev to keep track of the predecessor vertices on the
shortest paths.

• Relaxation:

1.) Repeat the relaxation process for V-1 iterations, where V is the number
of vertices in the graph.
2.) For each edge (u, v) in the graph, relax the edge by updating the shortest
distance to vertex v if the distance to vertex u plus the weight of edge (u,
v) is smaller than the current shortest distance to v.
3.) Update the prev array to record the predecessor vertex for each vertex
with a relaxed edge.

• Negative Cycle Detection:

1.) After V-1 iterations, if there are still vertices whose distances can be
reduced, then a negative weight cycle exists in the graph.
2.) Iterate through all edges and relax each edge once more.
3.) If any distance is updated during this iteration, it indicates the presence
of a negative cycle.
PSEUDOCODE:

function BellmanFord(Graph G, Vertex source):


// Step 1: Initialization
dist[source] = 0
for each vertex v in G:
if v != source, dist[v] = infinity
prev[v] = NULL

// Step 2: Relaxation
for i from 1 to |V(G)| - 1:
for each edge (u, v) in G:
if dist[u] + weight(u, v) < dist[v]:
dist[v] = dist[u] + weight(u, v)
prev[v] = u

// Step 3: Negative Cycle Detection


for each edge (u, v) in G:
if dist[u] + weight(u, v) < dist[v]:
return "Negative cycle detected"

return dist, prev


C/C++ CODE:
OUTPUT:
VERIFICATION STATUS
Q3.) Floyd-Warshall

AIM

Floyd-Warshall algorithm aims to find the shortest paths between all pairs of
vertices in a weighted graph, including negative weight edges (but with no
negative cycles). It achieves this by iteratively updating shortest path
estimates between pairs of vertices until reaching the optimal solution.

ALGORITHM

Step 1: Initialization
• Create a matrix dist to store the shortest distances between all pairs
of vertices.
• Initialize the matrix such that dist[i][j] represents the shortest
distance from vertex i to vertex j.
• If there's an edge from vertex i to vertex j, set dist[i][j] to the
weight of that edge. If there's no edge, set dist[i][j] to infinity.
• Initialize the diagonal elements of the matrix to 0.

Step 2: Main Algorithm


• Iterate through each vertex k.
• For each pair of vertices i and j, check if the path from i to k to j is
shorter than the current shortest path from i to j.
• If the path through vertex k is shorter, update the shortest distance
accordingly.

Step 3: Repeat Step 2


• Repeat Step 2 for all vertices k, considering each vertex as a
potential intermediate vertex for shortest paths.

Step 4: Output
• After completing the main algorithm, the dist matrix will contain
the shortest distances between all pairs of vertices.
PSEUDOCODE:

function FloydWarshall(Graph G):


let dist[][] be a matrix of size VxV // V is the number of vertices in the
graph
// Step 1: Initialization
for each vertex v in G:
for each vertex u in G:
if u is equal to v, then dist[u][v] = 0
else if there is an edge from u to v, dist[u][v] = weight of the edge
else dist[u][v] = infinity

// Step 2: Main Algorithm


for each vertex k in G:
for each vertex i in G:
for each vertex j in G:
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]

return dist
C/C++ CODE:
OUTPUT:
VERIFICATION STATUS

You might also like