Professional Documents
Culture Documents
struct node {
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
int* visited;
// DFS algo
void DFS(struct Graph* graph, int vertex) {
struct node* adjList = graph->adjLists[vertex];
struct node* temp = adjList;
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
Page 1
}
}
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
int main() {
Page 2
struct Graph* graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
printGraph(graph);
DFS(graph, 2);
return 0;
}
Page 3
OBJECTIVE: Write a program to perform Breadth First Search (BFS).
while queue:
vertex = queue.popleft()
print(str(vertex) + " ", end="")
if __name__ == '__main__':
graph = {'A' : ['B','C'], 'B' : ['D','E'], 'C' : ['F'], 'D' : [], 'E' : [], 'F' : []}
print("Breadth First Traversal: ")
bfs(graph, 'A')
Page 4
OUTPUT: The output for the BFS program is as follows:
Page 5
OBJECTIVE: Write a program for the Water Jug problem.
AIM: To use the jugs to measure out a specific amount of water by filling
and emptying the jugs in a particular order.
q = deque()
q.append((0, 0))
Page 6
m[(u[0], u[1])] = 1
if (u[0] == target or u[1] == target):
isSolvable = True
if (u[0] == target):
if (u[1] != 0):
path.append([u[0], 0])
else:
if (u[0] != 0):
path.append([0, u[1]])
sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break
c = u[0] - ap
d = u[1] + ap
q.append([a, 0])
q.append([0, b])
if (not isSolvable):
print("Solution not possible")
if __name__ == '__main__':
Page 7
OUTPUT: The output for the Water Jug problem is as follows:
Page 8
OBJECTIVE: To implement Magic Square using any heuristic technique of
AI.
AIM: The sum of all rows, columns and diagonals of the Magic Square must
be 15 in a 3x3 matrix.
new_row = (row - 1) % 3
new_col = (col + 1) % 3
if magic_square[new_row][new_col] == 0:
row, col = new_row, new_col
else:
row = (row + 1) % 3
return magic_square
def print_magic_square(matrix):
for row in matrix:
print(row)
Page 9
# Print the 3x3 magic square
magic_square = generate_3x3_magic_square()
print_magic_square(magic_square)
Page 10
INTRODUCTION TO MATLAB
Engineers and scientists worldwide rely on MATLAB for their analytical and design tasks, which play a
crucial role in advancing various industries. MATLAB's matrix-based language provides an intuitive way to
perform computational mathematics. It also offers built-in graphics that facilitate data visualization and
interpretation. The user-friendly desktop environment encourages experimentation and fosters discovery.
MATLAB offers a comprehensive suite of tools and features that have undergone rigorous testing and are
designed to seamlessly work together. Beyond basic analysis, MATLAB empowers you to handle larger
datasets and extend your work to clusters and cloud computing resources. Furthermore, you can integrate
MATLAB code with other programming languages, allowing you to deploy your algorithms and applications
across web, enterprise, and production systems.
While working in MATLAB, you issue commands that create variables and call functions. For an
introduction:
• ans – Most recent answer
• clc – Clear Command Window
• diary – Log Command Window text to file
• format – Set output display format
• home – Send cursor home
• iskeyword – Determine whether input is MATLAB keyboard
• more – Control paged output in Command Window
• commandwindow – Select the Command Window
• commandhistory – Open Command History window
Page 11