0% found this document useful (0 votes)
39 views19 pages

Ai and Soft Computing Lab

Uploaded by

Indraneel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views19 pages

Ai and Soft Computing Lab

Uploaded by

Indraneel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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;

private LinkedList<Integer> adj[];

@SuppressWarnings("unchecked") Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}

void addEdge(int v, int w)


{

adj[v].add(w);
}

void DFSUtil(int v, boolean visited[])


{

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)
{

boolean visited[] = new boolean[V];

DFSUtil(v, visited);
}

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 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.*;

public class Graph {

private int V;

private LinkedList<Integer> adj[];

Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}

void addEdge(int v, int w) { adj[v].add(w); }

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.

public class GFG{


public static int gcd(int a, int b)
{
if (b == 0)
return a;

return gcd(b, a % b);


}

public static int pour(int fromCap, int toCap,


int d)
{

int from = fromCap;


int to=0;
int step = 1;

while (from != d && to != d)


{
int temp = Math.min(from, toCap - to);

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));
}

public static void main(String[] args)


{
int n = 3, m = 5, d = 4;

System.out.println("Minimum number of " +


"steps required is " +
minSteps(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)

public class MagicSquare


{
public void makeSquare(int s)
{
int magicSqr[][] = new int[s][s];

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

Objective : Introduction to MATLAB


MATLAB is a software package for high-performance mathematical computation,
visualization, and programming environment. It provides an interactive
environment with hundreds of built-in functions for technical computing, graphics,
and animations.
MATLAB stands for Matrix Laboratory. MATLAB was written initially to
implement a simple approach to matrix software developed by the LINPACK
(Linear system package) and EISPACK (Eigen system package) projects.
MATLAB is a modern programming language environment, and it has refined data
structures, includes built-in editing and debugging tools, and supports object-
oriented programming.
MATLAB is Multi-paradigm. So, it can work with multiple types of programming
approaches, such as Functional, Object-Oriented, and Visual.

Advantage of MATLAB

Ease of Use:

The program can be used as a scratchpad to evaluate expressions typed at the


command line, or it can be used to execute large prewritten programs. Applications
may be written and changed with the built-in integrated development environment
and debugged with the MATLAB debugger. Because the language is so simple to
use, it is optimal for the fast prototyping of new applications.
Many program development tools are supported to make the program easy to use.
They contain an integrated editor/debugger, on-line documentation and manuals, a
workspace browser, and extensive demos.
Platform Independence:

MATLAB is supported on different computer systems, providing a considerable


measure of platform independence. The language is provided on Windows
2000/XP/Vista, Linux, various versions of UNIX, and the Macintosh. Applications
written on any platform will run on the other entire platform, and information files
written on any platform may be read apparently on any other platform. As a result,
programs written in MATLAB can shift to new platforms when the needs of the
user change.

Predefined Functions:

MATLAB comes complete with a huge library of predefined functions that


provides tested and prepackaged solutions to many primary technical tasks. For
example, suppose that we are writing a program that must evaluate the statistics
associated with an input data set. In most languages, we would need to write our
subroutines or functions to implement calculations such as the arithmetic mean,
standard deviation, median, and so on. These and hundreds of other services are
built right into the MATLAB language, making your job much more comfortable.
In addition to the vast libraries of services built into the basic MATLAB language,
there are many special-purpose toolboxes applicable to help solve complex
problems in particular areas. For example, a user can buy standard toolkits to solve
problems in signal processing, control systems, communications, image
processing, and neural networks, etc. There is also a broad compilation of free
user-contributed MATLAB programs that are shared through the MATLAB Web
site.
Device-Independent Plotting:

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

Pattern Recognition in Machine Learning:


What is Pattern Recognition:
Pattern Recognition is the modernized Acknowledgment of models and textures in
data. It has applications in quantifiable data assessment, signaltaking care, picture
examination, information recovery, bioinformatics, data pressure, PC
representations, and artificial intelligence. Design
Acknowledgment has its beginning stages in estimations and planning; a couple of
present-day ways of managing Example Acknowledgment consolidates the usage
of artificial intelligence due to the vast openness of tremendous data and one more
flood of dealing with power. These activities should be visible as two highlights of
a comparative field of usage, and they have gone through massive improvement
over late numerous years.

Pattern recognition possesses the following features:

• Design acknowledgment framework ought to perceive recognizable


examples rapidly and exact.
• See and orchestrate new things.
• Unequivocally see shapes and things from different places
• Perceive models and things regardless, when to some degree, concealed
• See plans quickly, efficiently, and with automaticity.
• Planning and Learning in Model Affirmation.

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

Introduction to fuzzy logic

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.

There are largely three types of fuzzifiers:

Singleton fuzzifier
Gaussian fuzzifier
Trapezoidal or triangular fuzzifier
What is Fuzzy Control?

It is a technique to embody human-like thinkings into a control system.


It may not be designed to give accurate reasoning but it is designed to give acceptable
reasoning.
It can emulate human deductive thinking, that is, the process people use to infer conclusions
from what they know.
Any uncertainties can be easily dealt with the help of fuzzy logic.

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

Implementation of fuzzy operations


Consider 2 Fuzzy Sets denoted by A and B, then let’s consider Y be the Union of them, then for
every member of A and B, Y will be:

A = dict()
B = dict()
Y = dict()

A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}


B = {"a": 0.9, "b": 0.9, "c": 0.4, "d": 0.5}

print('The First Fuzzy Set is :', A)


print('The Second Fuzzy Set is :', B)

for A_key, B_key in zip(A, B):


A_value = A[A_key]
B_value = B[B_key]

if A_value > B_value:


Y[A_key] = A_value
else:
Y[B_key] = B_value

print('Fuzzy Set Union is :', Y)

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()

A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}


B = {"a": 0.9, "b": 0.9, "c": 0.4, "d": 0.5}

print('The First Fuzzy Set is :', A)


print('The Second Fuzzy Set is :', B)

for A_key, B_key in zip(A, B):


A_value = A[A_key]
B_value = B[B_key]

if A_value < B_value:


Y[A_key] = A_value
else:
Y[B_key] = B_value
print('Fuzzy Set Intersection is :', Y)

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()

A = {"a": 0.2, "b": 0.3, "c": 0.6, "d": 0.6}

print('The Fuzzy Set is :', A)

for A_key in A:
Y[A_key]= 1-A[A_key]

print('Fuzzy Set Complement is :', Y)

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}

You might also like