AI LAB (1)
AI LAB (1)
CODE:
from collections import deque
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
public class Solution {
public static void main(String[] args) {
int jug1 = 4, jug2 = 3, target = 2;
System.out.println("Path from initial state to solution state ::");
solve(jug1, jug2, target);}
public static void solve(int a, int b, int target) {
HashMap<String, Integer> m = new HashMap<>();
boolean isSolvable = false;
Deque<int[]> q = new ArrayDeque<>();
int[][] path = new int[1000][2];
int pathIndex = 0;
while (!q.isEmpty()) {
int[] u = q.poll();
if (m.containsKey(u[0] + " " + u[1])) continue;
if (u[0] > a || u[1] > b || u[0] < 0 || u[1] < 0) continue;
path[pathIndex++] = u;
m.put(u[0] + " " + u[1], 1);
if (u[0] == target || u[1] == target) {
isSolvable = true;
if (u[0] == target) {
if (u[1] != 0) path[pathIndex++] = new int[]{u[0], 0};
} else {
if (u[0] != 0) path[pathIndex++] = new int[]{0, u[1]};
}
for (int i = 0; i < pathIndex; i++) {
System.out.println("(" + path[i][0] + ", " + path[i][1] + ")");
}
break;}
q.offer(new int[]{a, u[1]}); // Fill Jug1
q.offer(new int[]{u[0], b}); // Fill Jug2
OUTPUT:
Experiment - 2
2. Write a LISP function to compute the difference of squares. (if x > y return x2 – y2,
Otherwise y2 – x2).
(defun difference-of-squares (x y)
(let ((x-squared (* x x))
(y-squared (* y y)))
(if (> x y)
(- x-squared y-squared)
(- y-squared x-squared))))
3. Write a Recursive LISP function which takes one argument as a list and returns the
last element of the list. (Do not use the last predicate.)
(defun last-element (lst)
(if (null (cdr lst)) ;
(car lst) ;
(last-element (cdr lst)))) ;
4. Write a Recursive LISP function which takes one argument as a list and return list
except the last element of the list. (Do not use butlast.)
(defun remove-last (lst)
"Recursively remove the last element of the list lst."
(if (null (cdr lst)) ; Check if cdr (rest) of the list is empty
'() ; If it is, return an empty list (since there's no element to remove)
(cons (car lst) ; Otherwise, cons the car (first element) of the list
(remove-last (cdr lst))))) ; with the result of recursively removing the last element
(print (remove-last '(1 2 3 4 5)))
(print (remove-last '(a b c d e)))
5. Write a Recursive LISP function which takes one argument as a list and returns the
reverse of the list. (Do not use reverse predicate).
(defun reverse-list (lst)
(if (null lst) ; If the list is empty
'() ; Return an empty list
(append (reverse-list (cdr lst)) ; Recursively reverse the rest of the list
(list (car lst))))) ; Append the first element to the reversed list
(print (reverse-list '(1 2 3 4 5)))
(print (reverse-list '(a b c d e)))
6. Write a Recursive LISP function which takes two arguments first an atom second a
list returns a list after removing the first occurrence of that atom within the list.
(defun remove-first-occurrence (atom lst)
"Recursively remove the first occurrence of atom within the list lst."
(cond ((null lst) '()) ; Base case: if the list is empty, return an empty list
((equal atom (car lst)) (cdr lst)) ; If the first element of the list is equal to the atom, skip it by
returning the rest of the list
(t (cons (car lst) ; Otherwise, cons the first element of the list
(remove-first-occurrence atom (cdr lst)))))) ; with the result of recursively removing the atom
from the rest of the list
;; Example usage
(format t "Factorial of 5 is ~a" (factorial 5))
2. Write a function that evaluates a fully parenthesized infix arithmetic expression. For
examples, (infix (1+ (2*3))) should return 7.
3. Write a function that performs a depth first traversal of a binary tree. The function
should return a list containing the tree nodes in the order they were visited.
(defstruct binary-tree
value
left
right)
(defvar example-tree
(make-binary-tree :value 'A
:left (make-binary-tree :value 'B
:left (make-binary-tree :value 'D)
:right (make-binary-tree :value 'E))
:right (make-binary-tree :value 'C
:left (make-binary-tree :value 'F)
:right (make-binary-tree :value 'G))))
;; Example usage
(format t "Is 17 prime? ~a" (is-prime 17))
(format t "Is 25 prime? ~a" (is-prime 25))
Experiment - 4
Refer following figure as map with distance details, Write a program in your
preferred language to generate path from ARAD to BUCHREST, analyze result
obtained by
a) Depth First Search
b) Breadth First Search
c) Uniform Cost Search
# Define the graph as a dictionary where keys are city names and values are lists of adjacent cities and their
distances
graph = {
'Arad': [('Zerind', 75), ('Timisoara', 118), ('Sibiu', 140)],
'Zerind': [('Oradea', 71), ('Sibiu', 151)],
' Oradea': [],
'Timisoara': [('Lugoj', 70)],
'Lugoj': [('Mehadia', 75)],
'Mehadia': [('Drobeta', 75)],
'Drobeta': [('Craiova', 120)],
'Sibiu': [('Fagaras', 99), ('Rimnicu Vilcea', 80), ('Pitesti', 97)],
'Fagaras': [('Bucharest', 211)],
'Rimnicu Vilcea': [('Pitesti', 90), ('Craiova', 146)],
'Pitesti': [('Bucharest', 101)],
'Craiova': [('Giurgiu', 98)],
'Giurgiu': [('Bucharest', 86)],
}
Output: