Daa notes
Daa notes
Algorithm:
def is_safe(vertex, color, graph, colors, n):
"""Check if it is safe to assign 'color' to the given 'vertex'."""
for neighbor in range(n):
if graph[vertex][neighbor] == 1 and colors[neighbor] == color:
return False
return True
Time Complexity:
Worst-Case Complexity:
o At every vertex, we have mm choices for coloring.
o For nn vertices, the worst-case number of recursive calls is
mnm^n.
o Time Complexity: O(mn)O(m^n).
Optimizations:
o The use of the is_safe() function prunes invalid color assignments
early, reducing the effective search space.
o Practical performance depends on the graph's structure (e.g.,
density of edges).
Space Complexity:
o The space required is O(n)O(n), primarily for the recursion stack
and the colors array.
3 ) Compare backtracking with branch and bound method with respect to:
search technique, exploration of state space tree and king of problems that
can be solved.
Decision Problems
Optimization Problems
Problem Type (finding any feasible
(finding the best solution)
solution)
Bounding function
Feasibility function
(estimates the best possible
Key Function (checks if a partial
solution achievable from a
solution is feasible)
node)
First Solution:
Subset: [5,10,15][5, 10, 15][5,10,15]
Sum: 303030
This solution stops as soon as the first subset with the required sum is found.
Args:
text: The original text string.
pattern: The pattern string to be searched.
Returns:
A list of indices where the pattern is found in the text.
"""
n = len(text)
m = len(pattern)
d = 256 # Number of characters in the alphabet
q = 101 # A prime number for modulo operation
# Slide the pattern over the text and compare hash values
matches = []
for i in range(n - m + 1):
if p_hash == t_hash:
# Check characters individually to avoid false positives due to hash
collisions
for j in range(m):
if text[i + j] != pattern[j]:
break
else:
matches.append(i)
return matches
Time Complexity:
Best Case: O(n) - When there are no matches and hash collisions are
minimal.
Average Case: O(n + m) - Assuming a good hash function and minimal
collisions.
Worst Case: O(nm) - When there are many false positives due to hash
collisions, and character-by-character comparisons are required for each
potential match.
Space Complexity: O(m) - For storing the pattern and intermediate
calculations.
Key Points:
The choice of the prime number q and the base d can significantly
impact the performance of the algorithm.
The algorithm's efficiency relies on the assumption that hash collisions
are rare.
In the worst case, the algorithm can degenerate to a brute-force search.
By carefully selecting the parameters and implementing the algorithm
efficiently, the Rabin-Karp algorithm can be a powerful tool for string searching.
def merge_sort(arr):
if len(arr) <= 1:
return
mid = len(arr) // 2
left_arr = arr[:mid]
right_arr = arr[mid:]
t1.start()
t2.start()
t1.join()
t2.join()
9 ) Q8) a
Distributed Breadth-First Search on the Graph
The graph is represented as an adjacency matrix:
ABCDE F G
A0 1 00 000
B 1 0 01 100
C 0 0 01 001
D0 1 10 010
E 0 1 00 010
F 0 0 01 100
G0 0 10 000
Conclusion
Distributed BFS processes nodes level by level in a coordinated fashion
using message-passing.
This ensures the shortest path from the source to all other nodes is
found.
def worker_thread():
# Code to be executed by the worker thread
shared_resource = 0
lock = threading.Lock()
def increment_shared_resource():
global shared_resource
with lock:
shared_resource += 1