Professional Documents
Culture Documents
19CSE030 Lamia
19CSE030 Lamia
Assignment
Assistant Professor
Dept. of Computer Science Engineering,
University of Barishal
Submitted By:
Name: Lamia Begum
Roll: 19CSE030
Year: 3rd
Semester: 2nd
Session: 2019-20
Submission Date: 10/05/2024
Routes:
class Graph:
self.V = vertices
min_dist = sys.maxsize
min_index = 0
for v in range(self.V):
min_dist = dist[v]
min_index = v
return min_index
dist[src] = 0
u = self.min_distance(dist, spt_set)
spt_set[u] = True
for v in range(self.V):
if self.graph[u][v] > 0 and spt_set[v] == False and dist[v] > dist[u] + self.graph[u][v]:
return dist[dest]
adj_matrix = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 40],
[0, 0, 0, 0, 50, 0, 0]
g = Graph(len(adj_matrix))
for i in range(len(adj_matrix)):
for j in range(len(adj_matrix[0])):
g.graph[i][j] = adj_matrix[i][j]
Backtracking Algorith
backtraking isa problem-solving algorithmic technique that involves finding a solution
incrementally by trying different types and undoging them if they lead to a dead end.
It is commonly used in situations where you need to explore multiple possibilities to solve a
problem, like searching for a path in a maze or solving puzzles like .soduko When a dead end is
reached, the algorithm backtracks to the previous decision point and explores a different path
until a solution is found or all possibilities have been exhausted.
Solving puzzles
Scheduling problems
Code in python
class Graph:
def __init__(self):
self.graph = {}
self.graph[node] = []
self.graph[node].append((adjacent, weight))
def get_neighbors(self, node):
if start == end:
if new_path:
return shortest
def main():
g = Graph()
start = "Barishal"
end = "Patuakhali"
if shortest:
else:
if __name__ == "__main__":
main()
The forward checking algorithm for solving constraint satisfaction problems is a popular and successful
alternative to backtracking. However, its success has largely been determined empirically, and there has
been limited work towards a real understanding of why and when forward checking is the superior
approach.
The "forward checking" algorithm is a technique used in constraint satisfaction problems (CSPs)
to reduce the search space and improve the efficiency of constraint propagation. Here's an
explanation of the algorithm and its applications:
1. Algorithm Overview:
o Forward checking is performed during the search for a solution to a CSP.
o It is applied after assigning a value to a variable.
o The algorithm checks the remaining variables to see if any of their domains are
reduced to empty due to the assignment.
o If a domain becomes empty, it indicates that the current assignment cannot be
extended to a solution, so the algorithm backtracks
Applications:
class Graph:
def __init__(self):
self.graph = {}
self.graph[node] = []
self.graph[node].append((adjacent, weight))
explored = set()
while frontier:
explored.add(node)
if node == end:
if neighbor == end:
else:
def main():
g = Graph()
start = "Barishal"
end = "Patuakhali"
if path:
else:
if __name__ == "__main__":
main()
Graph colouring code
class Graph:
def __init__(self):
self.graph = {}
self.graph[node] = []
self.graph[node].append((adjacent, weight))
color_index = 0
colors[node] = color_index
color_index += 1
colors[neighbor] = color_index
color_index += 1
# If the neighbor has the same color as the node, assign a new color
colors[neighbor] = color_index
color_index += 1
return colors
def main():
g = Graph()
colors = graph_coloring(g)
print("Node\tColor")
if __name__ == "__main__":
main()