Ai and Soft Computing Lab
Ai and Soft Computing Lab
CS3131 LAB-A1
Jitendra Choudary
219301150
INDEX
Serial Topic Date Remark Sign
No.
LAB 1
Depth-First Search Algorithm
Aim:- To traverse and search for target element in a binary tree using
Depth-First Search algorithm.
import java.io.*;
import java.util.*;
class Graph {
private int V;
@SuppressWarnings("unchecked") Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
adj[v].add(w);
}
visited[v] = true;
System.out.print(v + " ");
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
void DFS(int v)
{
DFSUtil(v, visited);
}
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println(
"Following is Depth First Traversal "
+ "(starting from vertex 2)");
g.DFS(2);
}
}
OUTPUT
LAB 2
Breadth-First Search(BFS) Algorithm
Aim:- To traverse and search for a target element in a binary tree using
Breadth-First Search algorithm.
import java.util.*;
private int V;
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
void BFS(int s)
{
boolean visited[] = new boolean[V];
LinkedList<Integer> queue
= new LinkedList<Integer>();
visited[s] = true;
queue.add(s);
while (queue.size() != 0) {
s = queue.poll();
System.out.print(s + " ");
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
}
}
// Driver code
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println(
"Following is Breadth First Traversal "
+ "(starting from vertex 2)");
g.BFS(2);
}
}
OUTPUT
LAB 3
Water Jug Problem
Aim:- To write an algorithm that solves the famous Water jug problem.
to += temp;
from -= temp;
step++;
if (from == d || to == d)
break;
if (from == 0)
{
from = fromCap;
step++;
}
if (to == toCap)
{
to = 0;
step++;
}
}
return step;
}
public static int minSteps(int m, int n, int d)
{
if (m > n)
{
int t = m;
m = n;
n = t;
}
if (d > n)
return -1;
if ((d % gcd(n, m)) != 0)
return -1;
return Math.min(pour(n, m, d),
pour(m, n, d));
}
OUTPUT
LAB 4
Magic Square Problem
Aim:- To write an algorithm to output a magic square of a particular
size.
Software:- IntelliJ(Java)
int r = s / 2;
int c = s - 1;
for (int no = 1; no <= s * s;)
{
if (r == -1 && c == s) // 3rd rule
{
c = s - 2;
r = 0;
}
else
{
if (c == s)
{
c = 0;
}
if (r < 0)
{
r = s - 1;
}
}
if (magicSqr[r][c] != 0)
{
c = c - 2;
r = r + 1;
continue;
}
else
{
magicSqr[r][c] = no;
no = no + 1;
}
c = c + 1;
r = r - 1;
}
System.out.println("The Magic Square for " + s + ": \n");
System.out.println("Sum of each column or row " + s * (s * s +
1) / 2 + ": \n");
for (r = 0; r < s; r++)
{
for (c = 0; c < s; c++)
{
System.out.print(magicSqr[r][c] + " ");
}
System.out.println();
}
}
public static void main(String[] argvs)
{
int n = 11;
MagicSquare obj = new MagicSquare();
obj.makeSquare(n);
}
}
OUTPUT
LAB 5
Advantage of MATLAB
Ease of Use:
Predefined Functions:
MATLAB has many basic plotting and imaging commands. The plots and pictures
can be displayed on any graphical output device provided by the computer on
which MATLAB is running. This facility makes MATLAB an outstanding tool for
visualizing technical information.
LAB 6
Examples:
A Sensor: A sensor is a device used to measure a property, like strain, position,
temperature, or speed increment, and reply with input.
A Preprocessing System: Division is used, and it is the strategy engaged with
partitioning data into different segments. It can, moreover, be portrayed as the
system of secluding or allotting data into parts called pieces.
A Component Extraction System: feature extraction starts from a secret game plan
of assessed data and develops decided values (features) wanted to be helpful and
non-overabundance, working with the subsequent learning and hypothesis steps,
and on occasion, provoking better human understandings. It will, in general, be
manual or robotized.
A Portrayal Calculation: Example acknowledgment estimations overall hope to
give a reasonable answer for each possible data and to perform "without a doubt"
matching of the information sources, considering their quantifiable assortment.
A Preparation Set: Preparing data is a certain level of a, by and large, dataset close
by testing set. If all else fails, the more the planning data, the more the computation
or classifier performs.
LAB 7
Fuzzy Logic is based on the idea that in many cases, the concept of true or false is too
restrictive, and that there are many shades of gray in between. It allows for partial truths, where
a statement can be partially true or false, rather than fully true or false.
Fuzzy Logic is used in a wide range of applications, such as control systems, image processing,
natural language processing, medical diagnosis, and artificial intelligence.
The fundamental concept of Fuzzy Logic is the membership function, which defines the degree
of membership of an input value to a certain set or category. The membership function is a
mapping from an input value to a membership degree between 0 and 1, where 0 represents
non-membership and 1 represents full membership.
Fuzzy Logic is implemented using Fuzzy Rules, which are if-then statements that express the
relationship between input variables and output variables in a fuzzy way. The output of a Fuzzy
Logic system is a fuzzy set, which is a set of membership degrees for each possible output value.
Membership function
Definition: A graph that defines how each point in the input space is mapped to membership
value between 0 and 1. Input space is often referred to as the universe of discourse or universal
set (u), which contains all the possible elements of concern in each particular application.
Singleton fuzzifier
Gaussian fuzzifier
Trapezoidal or triangular fuzzifier
What is Fuzzy Control?
Application
It is used in the aerospace field for altitude control of spacecraft and satellites.
It has been used in the automotive system for speed control, traffic control.
It is used for decision-making support systems and personal evaluation in the large company
business.
It has application in the chemical industry for controlling the pH, drying, chemical distillation
process.
Fuzzy logic is used in Natural language processing and various intensive applications in Artificial
Intelligence.
LAB 8
A = dict()
B = dict()
Y = dict()
Output
The First Fuzzy Set is : {'a': 0.2, 'b': 0.3, 'c': 0.6, 'd': 0.6}
The Second Fuzzy Set is : {'a': 0.9, 'b': 0.9, 'c': 0.4, 'd': 0.5}
Fuzzy Set Union is : {'a': 0.9, 'b': 0.9, 'c': 0.6, 'd': 0.6}
LAB 9
Consider 2 Fuzzy Sets denoted by A and B, then let’s consider Y be the Intersection of
them,then for every member of A and B, Y will be:
A = dict()
B = dict()
Y = dict()
Output
The First Fuzzy Set is : {'a': 0.2, 'b': 0.3, 'c': 0.6, 'd': 0.6}
The Second Fuzzy Set is : {'a': 0.9, 'b': 0.9, 'c': 0.4, 'd': 0.5}
Fuzzy Set Intersection is : {'a': 0.2, 'b': 0.3, 'c': 0.4, 'd': 0.5}
LAB 10
Consider a Fuzzy Sets denoted by A , then let’s consider Y be the Complement of it, then for
every member of A , Y will be:
degree_of_membership(Y)= 1 - degree_of_membership(A)
A = dict()
Y = dict()
for A_key in A:
Y[A_key]= 1-A[A_key]
Output
The Fuzzy Set is : {'a': 0.2, 'b': 0.3, 'c': 0.6, 'd': 0.6}
Fuzzy Set Complement is : {'a': 0.8, 'b': 0.7, 'c': 0.4, 'd': 0.4}