Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views25 pages

AOA Notes

The document provides an overview of various algorithms used in graph theory and optimization, including the Floyd-Warshall Algorithm for finding all-pairs shortest paths, the Travelling Salesman Problem (TSP) using dynamic programming, the Bellman-Ford Algorithm for single-source shortest paths, the 0/1 Knapsack Problem, and the Assembly Line Scheduling Problem. Each section includes definitions, algorithms, stepwise executions, and final solutions with time complexities. The document serves as a comprehensive guide to these fundamental algorithms in computer science.

Uploaded by

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

AOA Notes

The document provides an overview of various algorithms used in graph theory and optimization, including the Floyd-Warshall Algorithm for finding all-pairs shortest paths, the Travelling Salesman Problem (TSP) using dynamic programming, the Bellman-Ford Algorithm for single-source shortest paths, the 0/1 Knapsack Problem, and the Assembly Line Scheduling Problem. Each section includes definitions, algorithms, stepwise executions, and final solutions with time complexities. The document serves as a comprehensive guide to these fundamental algorithms in computer science.

Uploaded by

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

AOA IA2 SOLUTION

MODULE 4

1.Explain Floyd-Warshall Algorithm for All-Pairs Shortest Path

Definition (1 Mark)

The Floyd-Warshall algorithm is a dynamic programming-based algorithm used to find


the shortest paths between all pairs of vertices in a weighted graph.

 It works on directed or undirected graphs with positive or negative edge


weights (but no negative cycles).

 The algorithm iteratively improves the shortest paths by considering each vertex
as an intermediate.

 It has a time complexity of O(V³), where V is the number of vertices.

Algorithm (3 Marks)

1. Initialize a distance matrix dist[][], where:

o dist[i][j] is the direct edge weight between i and j.

o If no edge exists, set dist[i][j] = ∞.

o dist[i][i] = 0 for all i.

2. Iterate over all vertices (k) as an intermediate:

o For each pair of vertices (i, j), update:

dist[i][j]=min⁡(dist[i][j],dist[i][k]+dist[k][j])dist[i][j] = \min(dist[i][j], dist[i][k] + dist[k]


[j])dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j])

o This checks if the path through vertex k is shorter than the direct path.

3. Repeat for all vertices (V times) to compute the shortest distances between all
pairs.

4. Final distance matrix gives the shortest paths between all nodes.
Stepwise Execution (3 Marks)

Example Graph

Given 4 vertices with the following adjacency matrix:

AB C D

A 0 3 ∞7

B80 2 ∞

C 5 ∞0 1

D2 ∞∞0

Step 1: Initialize Distance Matrix

AB C D

A 0 3 ∞7

B80 2 ∞

C 5 ∞0 1

D2 ∞∞0

Step 2: Consider Each Vertex as an Intermediate (k = 1 to 4)

 After considering vertex A as intermediate:


ABC D

A 0 3 ∞7

B802 ∞

C 580 1

D2 5∞0

 After considering all vertices, the final shortest path matrix is:


ABCD

A 0356

ABCD

B 5023

C 3601

D2570

Final Solution (1 Mark)

The shortest paths between all pairs of nodes are computed using the Floyd-Warshall
algorithm, and the final distance matrix gives the shortest distances. ✅

Question 7: Floyd-Warshall Algorithm for All-Pairs Shortest Path

Definition (1 Mark)

The Floyd-Warshall Algorithm is a dynamic programming technique used to find the


shortest path between all pairs of vertices in a weighted graph.

 It works with both directed and undirected graphs.

 It allows negative weights but not negative cycles.

 The algorithm runs in O(V³) time complexity, where V is the number of vertices.

Algorithm (3 Marks)

1. Initialize a distance matrix dist[][]:

o dist[i][j] stores the weight of the direct edge between i and j.

o If no direct edge exists, set dist[i][j] = ∞.

o dist[i][i] = 0 for all i.

2. Iterate over all vertices (k) as an intermediate:

o For each pair of vertices (i, j), update:


dist[i][j]=min⁡(dist[i][j],dist[i][k]+dist[k][j])dist[i][j] = \min(dist[i][j], dist[i][k] + dist[k]
[j])dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j])

o This checks if the path through vertex k is shorter than the direct path.

3. Repeat for all vertices (V times).

4. Final distance matrix dist[][] contains the shortest path distances between all
pairs.

Stepwise Execution (3 Marks)

Example Graph

Given a graph with 4 vertices and the following adjacency matrix:

AB C D

A 0 3 ∞7

B80 2 ∞

C 5 ∞0 1

D2 ∞∞0

Step 1: Initialize Distance Matrix

AB C D

A 0 3 ∞7

B80 2 ∞

C 5 ∞0 1

D2 ∞∞0

Step 2: Consider Each Vertex as an Intermediate (k = 1 to 4)

 After considering vertex A as intermediate:


ABC D

A 0 3 ∞7

B802 ∞

ABC D

C 580 1

D2 5∞0

 After considering all vertices, the final shortest path matrix is:


ABCD

A 0356

B 5023

C 3601

D2570

Final Solution (1 Mark)

The shortest paths between all pairs of nodes are computed using the Floyd-Warshall
algorithm, and the final distance matrix provides the shortest path values. ✅

Question 2: Travelling Salesman Problem (TSP) using Dynamic


Programming
Graph Representation (1 Mark)

The Travelling Salesman Problem (TSP) is a classic combinatorial optimization problem


where:

 A salesman must visit N cities exactly once and return to the starting city.

 The goal is to find the shortest possible route covering all cities.

 It is solved efficiently using Dynamic Programming (Held-Karp Algorithm).

Algorithm & Proper Stepwise Execution (5 Marks)


Algorithm (Held-Karp using Dynamic Programming)

1. Define dp[mask][i]:

o mask represents the set of visited cities in binary format.

o i is the last visited city.

o dp[mask][i] stores the minimum cost to reach city i after visiting cities in
mask.

2. Base Case:

o dp[1][0] = 0 (starting from city 0).

3. Recurrence Relation:

o Transition formula:

dp[mask][i]=min⁡(dp[mask∖{i}][j]+cost(j,i))∀j∈maskdp[mask][i] = \min(dp[mask \
setminus \{i\}][j] + cost(j, i)) \quad \forall j \in maskdp[mask][i]=min(dp[mask∖{i}][j]
+cost(j,i))∀j∈mask

o This checks all previous cities j and finds the best transition to i.

4. Final Step:

o Compute the minimum cost by considering all paths that return to the
starting city.

Example Execution

Consider 4 cities with the given cost matrix:

A B C D

A 0 10 15 20

B 10 0 35 25

C 15 35 0 30

D 20 25 30 0

Step 1: Compute DP States

We start at A (City 0) and compute dp[mask][i] for all possible city subsets (mask).
 Subset {A, B}:

o dp[011][B] = dp[001][A] + cost(A → B) = 0 + 10 = 10

 Subset {A, C}:

o dp[101][C] = dp[001][A] + cost(A → C) = 0 + 15 = 15

 Subset {A, D}:

o dp[1001][D] = dp[0001][A] + cost(A → D) = 0 + 20 = 20

 Compute Further States:

o dp[1111][D] = min(dp[0111][B] + cost(B → D), dp[1011][C] + cost(C → D))

o Continue for all subsets.

Final Step

 The minimum cost of visiting all cities and returning to A is 80.

Final Solution (1 Mark)

The optimal tour for TSP with cities {A, B, C, D} using dynamic programming is:

 A→B→D→C→A

 Minimum cost = 80 ✅

Time & Space Complexity

 Time Complexity: O(2ⁿ × n²) (Exponential)

 Space Complexity: O(2ⁿ × n) (for storing dp table)

Question 3: Single Source Shortest Path Using Bellman-Ford


Algorithm
Definition (1 Mark)

The Bellman-Ford Algorithm is used to find the shortest path from a single source
vertex to all other vertices in a weighted, directed graph, including graphs with
negative weight edges.
 It works by relaxing all edges V-1 times, where V is the number of vertices.

 If any distance gets updated on the Vth iteration, it means a negative weight cycle
exists.

 The time complexity is O(VE) (V = vertices, E = edges).

Algorithm (3 Marks)

Step 1: Initialize distances

1. Set the source vertex’s distance = 0.

2. Set all other vertex distances = ∞.

Step 2: Relax all edges V-1 times

For each edge (u, v, w):

if dist[u]+w<dist[v], then update dist[v]=dist[u]+w\text{if } dist[u] + w < dist[v], \text{ then


update } dist[v] = dist[u] + wif dist[u]+w<dist[v], then update dist[v]=dist[u]+w

Step 3: Check for negative weight cycles

If any distance still updates after V-1 iterations, there is a negative cycle.

Stepwise Execution (3 Marks)

Example Graph

Consider a weighted directed graph with 4 vertices:

Edge Start End Weight

1 A→B 1

2 B→C 3

3 A→C 4

4 C→D 2

5 B→D 5

6 D → B -6

Step 1: Initialize distances


 dist[A] = 0, dist[B] = ∞, dist[C] = ∞, dist[D] = ∞.

Step 2: Relax edges (3 times for V-1 = 4-1 = 3)

 Iteration 1:

o dist[B] = min(∞, 0 + 1) = 1

o dist[C] = min(∞, 0 + 4) = 4

o dist[D] = min(∞, 4 + 2) = 6

 Iteration 2 & 3:

o Continue updating values based on edges.

Step 3: Check for negative cycles

 Edge D → B with weight -6 creates a negative cycle.

Final Solution (1 Mark)

 If no negative cycle: The shortest paths from A are calculated.

 If negative cycle exists: "Negative cycle detected!" ✅

Time Complexity

 O(VE) (Exponential in large graphs)

Question 4: 0/1 Knapsack Using Dynamic Programming


Formula (1 Mark)

The 0/1 Knapsack Problem is a combinatorial optimization problem where:

 Given n objects with weights w[i] and profits p[i],

 Choose a subset of items such that total weight ≤ Knapsack capacity (M),

 Maximize total profit.

Dynamic Programming Formula:


K[i][w]={0if i=0 or w=0K[i−1][w]if wi>wmax⁡(K[i−1][w],pi+K[i−1][w−wi])otherwiseK[i][w]
= \begin{cases} 0 & \text{if } i = 0 \text{ or } w = 0 \\ K[i-1][w] & \text{if } w_i > w \\ \
max(K[i-1][w], p_i + K[i-1][w - w_i]) & \text{otherwise} \end{cases}K[i][w]=⎩⎨⎧0K[i−1]
[w]max(K[i−1][w],pi+K[i−1][w−wi])if i=0 or w=0if wi>wotherwise

Table Construction (1 Mark)

Given:

 n = 4 items

 Weights: (2, 3, 4, 5)

 Profits: (3, 4, 5, 6)

 Knapsack Capacity = M = 5

Items → 012345

0 000000

1 (w=2, p=3) 0 0 3 3 3 3

2 (w=3, p=4) 0 0 3 4 4 7

3 (w=4, p=5) 0 0 3 4 5 7

4 (w=5, p=6) 0 0 3 4 5 7

Stepwise Execution (4 Marks)

Step 1: Initialize Table

 Create a table K[n+1][M+1] and initialize first row & column to 0 (no items or no
capacity).

Step 2: Fill the Table Using Dynamic Programming

 For each item i, check if it fits in the current capacity w.

 If w[i] > w, exclude item → K[i][w] = K[i-1][w].

 If w[i] ≤ w, choose max of:

o Excluding the item (K[i-1][w])


o Including the item (p[i] + K[i-1][w-w[i]])

Step 3: Compute Final Solution

 Optimal Profit stored at K[4][5] = 7.

Final Solution (1 Mark)

The maximum profit that can be obtained for a knapsack of capacity 5 is 7. ✅

Time Complexity

 O(nM) (polynomial time).

✅ Now moving to Question 5: Assembly Line Scheduling Problem Using Dynamic


Programming 🚀

Question 5: Assembly Line Scheduling Using Dynamic


Programming
Algorithm (3 Marks)

The Assembly Line Scheduling Problem is used to minimize the total time required to
assemble a product using two assembly lines with multiple stations. Each station has a
processing time, and workers can switch lines at a given cost.

Algorithm Steps:

1. Define two tables F1[i] and F2[i]:

o F1[i] = Minimum time to reach station i in Line 1.

o F2[i] = Minimum time to reach station i in Line 2.

2. Recurrence Relations:

F1[i]=min⁡(F1[i−1]+S1[i],F2[i−1]+T2[i−1]+S1[i])F1[i] = \min(F1[i-1] + S1[i], F2[i-1] + T2[i-


1] + S1[i])F1[i]=min(F1[i−1]+S1[i],F2[i−1]+T2[i−1]+S1[i])
F2[i]=min⁡(F2[i−1]+S2[i],F1[i−1]+T1[i−1]+S2[i])F2[i] = \min(F2[i-1] + S2[i], F1[i-1] + T1[i-
1] + S2[i])F2[i]=min(F2[i−1]+S2[i],F1[i−1]+T1[i−1]+S2[i])
o S1[i], S2[i]: Processing time at station i of Line 1 and Line 2.

o T1[i], T2[i]: Time to switch from one line to another.

3. Compute final answer:

o Exit Time Consideration:

Fmin=min⁡(F1[n]+X1,F2[n]+X2)F_{\text{min}} = \min(F1[n] + X1, F2[n] + X2)Fmin


=min(F1[n]+X1,F2[n]+X2)

 X1, X2: Exit times from Line 1 and Line 2.

Constructing F-Table and L-Table (2 Marks)

Given Data Example

Station S1 (Line 1) S2 (Line 2) T1 (Switch 1→2) T2 (Switch 2→1)

1 4 2 — —

2 5 10 7 4

3 3 2 2 3

4 6 5 3 2

Exit 3 2 — —

F-Table (Minimum Time to Reach Each Station)

Station F1 (Line 1) F2 (Line 2)

1 4 2

2 9 12

3 12 14

4 18 19

Exit 21 21

L-Table (Best Previous Station Choice)

Station L1 (Prev from Line 1) L2 (Prev from Line 2)

1 — —
Station L1 (Prev from Line 1) L2 (Prev from Line 2)

2 1 1

3 2 2

4 3 3

Execution & Final Solution (1 Mark)

 Final Optimal Cost = 21

 Optimal Path: Line 1 → Line 1 → Line 1 → Line 1 → Exit

✅ Minimum total cost = 21 time units.

Time Complexity:

 O(n) (Linear time, as we compute values once per station).

MODULE 5

Question 1: Graph Coloring Using Backtracking


Explanation (1 Mark)

The Graph Coloring Problem is used to assign colors to vertices such that no two
adjacent vertices share the same color.

 The goal is to use the minimum number of colors.

 This problem is NP-hard, and backtracking is used to find a valid coloring.

Example:

Consider a graph with 4 vertices and the following edges:

A−B,A−C,B−C,B−D,C−DA - B, A - C, B - C, B - D, C - DA−B,A−C,B−C,B−D,C−D

We need to color it using 3 colors (Red, Blue, Green).


Algorithm (3 Marks)

1. Assign a color to vertex v[i].

2. Check if it is safe (no adjacent vertex has the same color).

3. If safe, proceed to the next vertex (v[i+1]).

4. If no valid color exists, backtrack and try another color.

5. Repeat until all vertices are colored.

Stepwise Execution (3 Marks)

Step 1: Assign colors to vertices

 Start with vertex A, assign Red.

 Move to vertex B, assign Blue (safe).

 Move to vertex C, assign Green (safe).

 Move to vertex D, assign Red (safe).

Step 2: Verify if solution is correct

Vertex Color

A Red

B Blue

C Green

D Red

✅ Valid solution with 3 colors!

Final Solution (1 Mark)

 Minimum colors used = 3

 Final Coloring: {A=Red, B=Blue, C=Green, D=Red}

Time Complexity:
 O(m^V) (Exponential time complexity for V vertices and m colors).

Question 2: Subset Sum Problem Using Backtracking


Definition (1 Mark)

The Subset Sum Problem determines whether a subset of a given set of numbers adds up
to a given sum (D).

 Given a set A = {3, 5, 6, 7} and target D = 15,

 We need to find subsets that sum to 15 using backtracking.

Algorithm (3 Marks)

1. Sort the set in ascending order.

2. Start from the first element and add it to a subset.

3. If the sum exceeds D, backtrack.

4. If the sum matches D, print the subset.

5. Continue until all possible subsets are checked.

Stepwise Execution (2 Marks)

Step 1: Start with Empty Subset

Current Subset={}\text{Current Subset} = \{\}Current Subset={}

Step 2: Try Adding Numbers One by One

 Add 3 → {3} (sum = 3) ✅

 Add 5 → {3, 5} (sum = 8) ✅

 Add 6 → {3, 5, 6} (sum = 14) ✅

 Add 7 → {3, 5, 6, 7} (sum = 21) ❌ (Backtrack)

Step 3: Find a Valid Subset

 Remove 6, Add 7 → {3, 5, 7} (sum = 15) ✅ Solution Found!


 Another solution: {6, 3, 6} (sum = 15) ✅

✅ Final subsets: {3, 5, 7} and {6, 3, 6}

Final Solution (1 Mark)

 Valid Subsets: {3, 5, 7} and {6, 3, 6}

 Subset exists? ✅ Yes!

Time Complexity:

 O(2^n) (Exponential due to backtracking).

Question 3: Illustrate Divide and Conquer, Dynamic Programming,


and Backtracking Approaches Used for Algorithm Design
Introduction

Algorithm design techniques are strategies used to develop efficient solutions for
computational problems. The three most common approaches are:

1. Divide and Conquer

2. Dynamic Programming

3. Backtracking

1. Divide and Conquer

Definition

Divide and Conquer is an algorithmic paradigm where a problem is broken into smaller
subproblems, solved independently, and then combined to get the final result.

Steps in Divide and Conquer:

1. Divide: Break the problem into smaller subproblems.

2. Conquer: Solve each subproblem recursively.

3. Combine: Merge the solutions of the subproblems to get the final answer.
Example: Merge Sort

 Divide: Split the array into two halves.

 Conquer: Recursively sort both halves.

 Combine: Merge the sorted halves.

Time Complexity: O(n log n)

2. Dynamic Programming

Definition

Dynamic Programming (DP) is used for problems that have overlapping subproblems and
optimal substructure. Instead of solving the same problem multiple times, DP stores
results of subproblems to avoid redundant computations.

Steps in Dynamic Programming:

1. Identify subproblems

2. Define recurrence relation

3. Store subproblem results (Memoization or Tabulation)

4. Build solution using stored results

Example: Fibonacci Series using DP

 Recursive Formula:

F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2)F(n)=F(n−1)+F(n−2)

 Stored Results:

o F(5) = F(4) + F(3), but instead of recomputing F(4), we use stored values.

Time Complexity: O(n)

3. Backtracking

Definition

Backtracking is a technique used to solve problems by exploring all possible solutions


and undoing steps when needed (backtracking). It is commonly used for constraint
satisfaction problems.
Steps in Backtracking:

1. Choose a possible solution

2. Check if it satisfies constraints

3. If not, backtrack and try another choice

4. Repeat until solution is found or all possibilities are exhausted

Example: N-Queens Problem

 Place a queen in a row

 Check if it’s safe

 If not safe, backtrack and try another position

 Continue until all queens are placed safely

Time Complexity: O(n!)

Comparison of Techniques

Approach When to Use Example Problem Complexity

Divide & Conquer Independent subproblems Merge Sort O(n log n)

Dynamic Programming Overlapping subproblems Fibonacci, Knapsack O(n) to O(n²)

Backtracking Constraint satisfaction N-Queens, Sudoku O(n!)

Question 4: N-Queens Problem Using Backtracking


Explanation of Backtracking (2 Marks)

The N-Queens Problem is a constraint satisfaction problem where we need to place N


queens on an N × N chessboard such that no two queens attack each other.

Rules for Valid Placement:

 No two queens should be in the same row.

 No two queens should be in the same column.


 No two queens should be on the same diagonal.

Algorithm (2 Marks)

1. Start from the first row and try to place a queen in each column.

2. Check if it is safe (no queens in the same column or diagonal).

3. If safe, place the queen and move to the next row.

4. If not safe, backtrack and try a different column.

5. Repeat until all N queens are placed.

Stepwise Execution with Example (3 Marks)

Example: Solving 4-Queens (N=4)

Row Column Attempted Safe? Action

1 1 ✅ Place Queen

2 1 ❌ Try next column

2 2 ❌ Try next column

2 3 ✅ Place Queen

3 1 ❌ Try next column

3 2 ❌ Try next column

3 4 ✅ Place Queen

4 2 ✅ Place Queen → ✅ Solution Found!

✅ Final Board Configuration:

css

CopyEdit

.Q..

...Q

Q...
..Q.

Final Solution (1 Mark)

✅ One valid solution for N = 4 is:

css

CopyEdit

.Q..

...Q

Q...

..Q.

There are multiple solutions possible for N=4.

Time Complexity: O(N!) (Factorial complexity).

MODULE 6

Question 1: Naïve String Matching Algorithm


Time Complexity Analysis (1 Mark)

The Naïve String Matching Algorithm checks for the presence of a pattern P of length m
inside a text T of length n by sliding P one position at a time and checking character-
by-character.

 Worst-case Time Complexity: O(n × m) (Occurs when all characters of T and P


match except the last one).

 Best-case Time Complexity: O(n) (If mismatches happen at the first comparison).

Algorithm (3 Marks)

1. Start with the first position of T (index i=0).

2. Compare P with T[i:i+m] (substring of length m).

3. If all characters match, record i as a match.

4. Move i one step ahead and repeat until the end of T.


Stepwise Execution (3 Marks)

Example:

 Text (T): "He like Mangoes" (n = 15)

 Pattern (P): "Mangoes" (m = 7)

Step Substring Checked Match?

T[0:7] → "He like" ❌ No

T[1:8] → "e like " ❌ No

T[2:9] → " like M" ❌ No

T[3:10] → "like Ma" ❌ No

T[4:11] → "ike Man" ❌ No

T[5:12] → "ke Mang" ❌ No

T[6:13] → "e Mango" ❌ No

T[7:14] → " Mangoe" ❌ No

T[8:15] → "Mangoes" ✅ Match found at index 8

Final Solution (1 Mark)

 Pattern "Mangoes" found at index 8 in "He like Mangoes"

 Total comparisons: 9

Question 2: Knuth-Morris-Pratt (KMP) Algorithm


Time Complexity Analysis (1 Mark)

The KMP algorithm improves over the Naïve approach by avoiding unnecessary
rechecking of characters.

 It preprocesses the pattern to create a Longest Prefix Suffix (LPS) table, which
helps in skipping unnecessary comparisons.
 Time Complexity: O(n + m)

o O(m) for preprocessing the LPS table.

o O(n) for searching in the text.

Algorithm (3 Marks)

1. Preprocess the pattern to create an LPS (Longest Prefix Suffix) table.

2. Start matching the pattern with the text. If a mismatch occurs:

o Use the LPS table to determine how much to shift the pattern.

o Avoid redundant comparisons.

3. Continue until the pattern is found or the text ends.

Stepwise Execution (3 Marks)

Example:

 Text (T): "ababcababcd"

 Pattern (P): "ababc"

Step 1: Construct LPS Table

Pattern LPS Value Explanation

"a" 0 No prefix & suffix match

"ab" 0 No proper prefix = suffix

"aba" 1 "a" is both prefix & suffix

"abab" 2 "ab" is both prefix & suffix

"ababc" 0 No proper prefix = suffix

Final LPS Table: [0, 0, 1, 2, 0]

Step 2: Search Pattern in Text


Step Text Window Match? Action

T[0:5] → "ababc" ✅ Yes Pattern found at index 0

T[1:6] → "babca" ❌ No Use LPS Table, Shift P

T[2:7] → "abcab" ❌ No Use LPS Table, Shift P

T[5:10] → "ababc" ✅ Yes Pattern found at index 5

✅ Pattern "ababc" found at indices 0 and 5 in "ababcababcd"

Final Solution (1 Mark)

 Pattern "ababc" found at indices [0, 5] in "ababcababcd"

 Time Complexity: O(n + m) = O(11 + 5) = O(16)

Question 3: Naïve String Matching Algorithm


Time Complexity Analysis (1 Mark)

The Naïve String Matching Algorithm compares the pattern P with all possible substrings
of the text T by checking character by character.

 Worst-case Time Complexity: O(n × m)

 Best-case Time Complexity: O(n) (if mismatches occur early).

 Average-case Complexity: O(n × m)

Here, n = 19 (length of "Nobody_Noticed_Him") and m = 3 (length of "Not").

Algorithm (3 Marks)

1. Start from i = 0 and slide the pattern over the text.

2. Compare P with T[i:i+m].

3. If all characters match, store i as a valid index.

4. If a mismatch occurs, shift i by 1 and repeat until i ≤ n - m.


Stepwise Execution (3 Marks)

Example:

 Text (T): "Nobody_Noticed_Him" (n = 19)

 Pattern (P): "Not" (m = 3)

Step Substring Checked Match?

T[0:3] → "Nob" ❌ No

T[1:4] → "obo" ❌ No

T[2:5] → "bod" ❌ No

T[3:6] → "ody" ❌ No

T[4:7] → "dy_" ❌ No

T[5:8] → "_No" ❌ No

T[6:9] → "Not" ✅ Match found at index 6

Final Solution (1 Mark)

 Pattern "Not" found at index 6 in "Nobody_Noticed_Him"

 Total comparisons: 7

 Time Complexity: O(n × m) = O(19 × 3) = O(57)

Question 4(a): Rabin-Karp String Matching Algorithm


Time Complexity Analysis (1 Mark)

The Rabin-Karp Algorithm uses hashing to improve search efficiency:

 Best Case: O(n + m) (if there are no hash collisions).

 Worst Case: O(n × m) (if many hash collisions occur).

 Average Case: O(n + m) (with a good hash function).


Algorithm (3 Marks)

1. Compute the hash value of the pattern P.

2. Compute the hash values of substrings of T (of length m).

3. If a substring has the same hash as P, verify character by character.

4. If a match is found, record the index. Otherwise, continue.

5. Use a rolling hash function to update the hash in constant time.

Stepwise Execution (3 Marks)

Example:

 Text (T): [36, 41, 59, 26, 35]

 Pattern (P): [26]

1. Compute hash of P: H(P) = hash(26).

2. Compute initial hash of T[0:1], T[1:2], ... T[4:5]:

Step Substring Checked Hash Matches? Verification

T[0] → 36 ❌ No -

T[1] → 41 ❌ No -

T[2] → 59 ❌ No -

T[3] → 26 ✅ Yes Match at index 3

T[4] → 35 ❌ No -

Final Solution (1 Mark)

 Pattern [26] found at index 3 in [36, 41, 59, 26, 35]

 Total comparisons: 5 (1 per element)

 Time Complexity: O(n + m) = O(5 + 1) = O(6)

You might also like