You are on page 1of 43

Q-1

(a) Solve The following Crypt arithmetic problem :

C R O S S
+ R O A D S
D E N G E R

Ans :

D has to be 1 because when you do your addition by hand:


CROSS
+ROADS
-----------
DANGER

You see that the D in DANGER must have been carried over from the addition of C and R in CROSS
and ROADS, respectively. Thus, we have:

CROSS
+ROA1S
------------
1ANGER

This, by some fairly weird algebra and modular arithmetic, leads to the following relationships:

R = 2S mod 10
E = (3S + 1 - (2S mod 10)) mod 10
A = (C + 2R - (R + O mod 10)) mod 10
A = G - O mod 10
N = (R + 2O - (O + A mod 10)) mod 10
C = A - R mod 10
O = N - R mod 10
O = G - A mod 10
G = (O + A + S + 1 - (S + 1 mod 10)) mod 10

Obviously, this is of no help whatsoever to anyone, so we do trial and error.

So now we try things.

12344
+25614
------
3NO! D has to be 1

92344
+25614
------
11NO! A cannot be 1 because D is 1
96233
+62513
------
158746

(b) Give the problem space for the following :


(i) Monkey Banana problem
(ii) Water Jug problem

Ans:

(i) Monkey Banana problem

A monkey is in a cage and bananas are suspended from the ceiling, the monkey
wants to eat a banana but cannot reach them
in the room are a chair and a stick
if the monkey stands on the chair and waves the stick, he can knock a banana
down to eat it

what are the actions the monkey should take?

Initial state:

monkey on ground with empty hand bananas suspended

Goal state:

monkey eating bananas.

Actions:

climb chair/get off

grab X

wave X

eat X
(ii) Water Jug problem

The state space for this problem can be described as the set of ordered pairs of
integers (x, y) such that x = 0, 1,2, 3 or 4 and y = 0,1,2 or 3; x represents the number of
gallons of water in the 4-gallon jug and y represents the quantity of water in 3-gallon jug

The start state is (0,0)

The goal state is (2,n)

Production rules for Water Jug Problem

The operators to be used to solve the problem can be described as follows:


Sr. No Current state Next State Description

1 (x, y) if x < 4 (4,y) Fill the 4 gallon jug

2 (x, y) if y <3 (x,3) Fill the 3 gallon jug

3 (x, y) if x > 0 (x-d, y) Pour some water out of the 4 gallon jug

4 (x, y) if y > 0 (x, y-d) Pour some water out of the 3-gallon jug

5 (x, y) if x>0 (0, y) Empty the 4 gallon jug

6 (x, y) if y >0 (x,0) Empty the 3 gallon jug on the ground

7 (x, y) if x + y (4, y-(4-x)) Pour water from the 3 gallon jug into the 4
>= 4 and y >0 gallon jug until the 4-gallon jug is full

8 (x, y) if x + y (x-(3-y), 3) Pour water from the 4-gallon jug into the 3-
>= 3 and x>0 gallon jug until the 3-gallon jug is full

9 (x, y) if x + y (x + y, 0) Pour all the water from the 3-gallon jug into the
<=4 and y>0 4-gallon jug

10 (x, y) if x + y (0, x + y) Pour all the water from the 4-gallon jug into the
<= 3 and x>0 3-gallon jug

11 (0,2) (2,0) Pour the 2 gallons from 3-gallon jug into the 4-
gallon jug

12 (2,y) (0,y) Empty the 2 gallons in the 4-gallon jug on the


ground

To solve the water jug problem

Required a control structure that loops through a simple cycle in which some
rule whose left side matches the current state is chosen, the appropriate change
to the state is made as described in the corresponding right side, and the
resulting state is checked to see if it corresponds to goal state.
One solution to the water jug problem

Shortest such sequence will have a impact on the choice of appropriate


mechanism to guide the search for solution.

Gallons in the Gallons in the 3- Rule applied


4-gallon jug gallon jug

0 0 2

0 3 9

3 0 2

3 3 7

4 2 5 or 12

0 2 9 0r 11

2 0

(c) Solve the following 8-puzzle using breadth first search

1 2 3
8 4
7 6 5
2 8 3
1 6 4
7 5

Ans :

2 8 3
1 6 4
2 8 3
7 5 Right 3
Left 1 1 6 4
Up 2
2 8 3
7 5 7 5
1 6 4 2 8 3
1 4
7 6 5
2 8 3
Left 1 1 6 4
7 5

Up 1.1
2 8 3
6 4
1 7 5
UP 2
2 8 3
1 4
7 6 5
2 3
2 8 3
1 8 4
Left 2.1 1 4 Up 2.2 Right 2.3 2 8 3
7 6 5
7 6 5 1 4
7 6 5

Right 3
2 8 3
1 6 4
7 5

Up 3.1 2 8 3
1 6
7 5 4

Up 1.1
2 8 3
6 4
1 7 5

Right 2 8 3
8 3 1.1.2
6 4
2 6 4 1 7 5
2 8 3
1 4
Left 2.1
7 6 5

2 8 3
8 3 Down 7 1 4
Up 2.1.2
2.1.1 2 1 4 6 5
7 6 5

2 3
Up 2.2
1 8 4
7 6 5

Left 2 3 Right 2 3
2.2.1 1 8 4 2.2.2 1 8 4
7 6 5 7 6 5

Right 2.3 2 8 3
1 4
7 6 5

Down 2.3.2
Up 2.3.1

2 8 3
2 8
1 4 5
1 4 3
7 6
7 6 5

2 8 3
Up 3.1 1 6
7 5 4
2 8 3
Up 2 8
Left 1 6 3.1.2 1 6 3
3.1.1
7 5 4
7 5 4
Up 8 3
1.1.1 2 6 4
1 7 5
Right
1.1.1.1

8 3
2 6 4
1 7 5

2 8 3
Right
6 4
1.1.2
1 7 5

Right Down
1.1.2.2 2 8 3 1.1.2.3 2 8 3
2 3
6 4 6 7 4
6 8 4
1 7 5 1 5

2 3
Left 1 8 4
2.2.1
7 6 5

1 2 3
Down
2.2.1.1 8 4
7 6 5

Right
2.2.1.1.1 Down
1 2 3 1 2 3
2.2.1.1.2
8 4 7 8 4
7 6 5 6 5

Final goal state find and ans is Block Right 2.2.1.1.1

Q-2

(a) Explain Problem Decomposability with example.


Suppose we want to solve the problem of computing the expression

(x2 + 3x + sin2;c cos2*) dx


/

We can solve this problem by breaking it down into three smaller problems;
each of which we can then solve by using a small collection of specific rules.
Figure-2;9 shows the problem tree that will be generated by the process of
problem decomposition as it can be exploited by a simple recursive integration
program that worksras follows: At each step, it checks tojsee whether the
problem if is working on is immediately solvable.' If-so, then the answer is
returned directly. If the problem is not easily solvable, the integrator checks to
see whether it can decompose the problem into smaller problems. If it can, it
creates those problems and calls itself recursively on them. Using: thistechnique
of problem decomposition, we can often solvevery large problemseasily; v .

Now consider the problem illustrated in Figure 2.10. This problem is drawn
firom.the domain often referred to in AMiterature as the blocks world. Assume
that the following operators are available:

Figure 2.10: A Simple Blocks World Problem

2 8 3 1 2 3
1 6 48 4
7 57 6 5

Figure 2.11 : A Proposed Solution for the Blocks Problem

1. CLEARQc) [block x has nothing on it] -+ ON(JC, Table) [pick up x and put it
on the table]
2. CLEAR(x)andCLEAR(y)->ON(jt,;y) [put* on y]

Applying the technique of problem decomposition to this simple blocks world


example would lead to a solution tree such as that shown in Figure 2.11. In the
figure, goals are underlined. States that have been achieved are not underlined.
The idea of this solution is to reduce the problem of getting B on C and A on B to
two separate problems. The first of these new problems, getting B on C, is
simple, given the start state. Simply put B on C. The second subgoal is not quite
so simple. Since the only operators we have allow us to pick up single blocks at
a time, we have to clear off A by removing C before we can pick up A and put it
on B. This can easily be done. However, if we now try to combine the two
subsolutions into one solution, we will fail. Regardless of which one we do first,
we will not be able to do the second as we had planned. In this problem, the two
subproblems are not independent. They interact and those interactions must be
considered in order to arrive at a solution for the entire problem.

These two examples, symbolic integration and the blocks world, illustrate the
difference between decomposable and nondecomposable problems. In Chapter.
3^ we present a specific algorithm for problem decomjposition, and in Chapter
13, we look at what happens when decomposition'is'impossible.

(b) Write and explain Means-End Analysis algorithm.

So far, we have presented a collection of search strategies that can reason


either forward or backward, but for a given problem, one direction or the other
must be chosen. Often, however, a mixture of the two directions is appropriate.
Such a mixed strategy would make it possible to solve the major parts of a
problem first and then go back and solve the small problems that arise in
"gluing" the big pieces together. A technique known as means-ends analysis
allows us to do that.

The means-ends analysis process centers around the'detection of differences


between the current state and the goal state. Once such a difference is isolated,
an operator that can reduce the difference must be found. But perhaps that
operator cannot be applied to the current state. So we set up a subproblem of
getting to a state-in which it can be applied. The kind of backward chaining in
which operators are selected and then subgoals are set up to establish the
preconditions of the'operators is called operator subgoaling. But maybe the
operator does not produce exactly the goal state we want. Then we have a
second subproblem of getting from the state it does produce to the goal. But if
the difference was chosen correctly and if the operator is really effective at
reducing the difference,, then the two subproblems should be easier to solve
than the

Algorithm: Means-Ends Analysis (CURRENT, GOAL)

1. Compare CURRENT to GOAL. If there are no differences between them then


return.

2. Otherwise, select the most important difference and reduce it by doing the
following until success or failure is signaled:

(a) Select an as yet untried operator O that is applicable to the current


difference. If there are no such operators, then signal failure.

(b) Attempt to apply O to CURRENT. Generate descriptions of two states: O-


START, a state in which O's preconditions are satisfied and O-RESULT,
the state that would result if O were applied in O-START.

(c) If
(FIRST-PART <- MEAtCURRENT, O-START))

and

(LAST-PART - MEA(0-RESULT, GOAL))

are successful, then signal success and return the result of concatenating

FIRST-PART, O, and LAST-PART.

Many of the details of this process have been omitted in this discussion. In
particular, the order in which differences are considered can be critical. It is
important that significant differences be reduced before less critical ones. If this
is not done, a great deal of effort may be wasted on situations that take care of
themselves once the main parts of the problem are solved.

The simple process we have described is usually not adequate for solving
complex problems. The number of permutations of differences may get too
large. Working on one difference may interfere with the plan for reducing
another. And in complex worlds, the required difference tables would be
immense. In Chapter 13 we look at some ways in which the basic means-ends
analysis approach can be extended to tackle some of these problems.

(c) Write and explain Generate-and-Test algorithm.

The generate-and-test strategy is the simplest of all the approaches we discuss.


It consists of the following steps:

Algorithm: Generate-and-Test

1. Generate a possible solution. For some problems, this means generating a


particular point in the problem space. For others, it means generating a
path from a start state.

2. Test to see if this is actually a solution by comparing the chosen point or


the endpoint of the chosen path to the set of acceptable goal states.

3. If a solution has been found, quit. Otherwise, return to step 1.

If the generation of possible solutions is done systematically, then this


(
procedure will find a solution eventually, if one exists. Unfortunately, if the
problem space is very \arge, "eventually" may be a very long time.

The generate-and-test algorithm is a depth-first search procedure since


complete solutions must be generated before they can be tested. In its most
systematic form, it is simply an exhaustive search of the problem space.
Generate-and-test can, of course, also operate by generating solutions
randomly, but then there is no guarantee that a solution will ever be found. In
this form, it is also known as the British Museum algorithm, a reference to a
method for finding an object in the British Museum by wandering randomly.1
Between these two extremes lies a practical middle ground in which the search
process proceeds systematically, but some paths are not considered because
they seem Unlikely to lead to a solution. This evaluation is performed by a
heuristic function, as described in Section 2.2.2.

The most straightforward way to implement systematic generate-and-test is


as a depth-first search tree with backtracking. If some intermediate states are
likely to appear often in the tree/however, it may be better to modify that
procedure, as described above; to traverse a graph rather than a tree.

For simple problems; exhaustive generate-and-test is often a reasonable


technique. For example, consider the puzzle that consists of four six-sided
cubes, with each side of each cube painted one of four colors. A
sojutjpjuothe'puzzle consists of an arrangement of the cubes in a row such that
on all four sides of the row one block face of each color is showing. This
probJejrjLcan_be_^olved by a person (who is a much slower processor for this
sort of thin^ffhan even a very cheap computer) in several minutes by
systematically and exhaustively trying all possibilities. It can be solved even
.more quickly using a heuristic generate-and-test procedure.. A quick glance at
the four blocks reveals.that there are moret say, red faces than there are of
other colors. Thus when placing a block with several fed faces, it would be a
good idea to use as few of them as possible as outside faces. As many of them
as possible should be placed to abut the next block. \Using this heuristic, many
configurations need never be explored and a solution can be found quite quickly.

'Or, as another story goes, if a sufficient number of monkeys were


placed in front of a set of typewriters and left alone long enough, then
they would eventually produce all of the works of Shakespeare.

Unfortunately, for problems much harder than this, even heuristic generate-
and-test, all by itself, is not a very effective technique. Bat when combined
with other techniques to restrict the space in which to search even further, the
technique can be very effective.

For example, one early example of a successful AI program is DENDRAL


[Lindsay et ai, 1980], which infers the structure of organic compounds using
mass spectrogram and nuclear magnetic resonance (NMR) data. It uses a
strategy called plan-generate-test in which a planning process that uses
constraint-satisfaction techniques (see Section 3.5)if creates lists of
recommended and contraindicated substructures. The generate-and-test
procedure then uses those lists so that it can explore only a fairly limited set of
structures. Constrained in this way the generate-and-test procedure has
proved highly effective..

This combination of planning, using one problem-solving method (in this


case, constraint satisfaction) with the use of the plan by another problem-
solving method, generate-and-test, is an excellent example of the way
techniques can be combined to overcome the limitations that each possesses
A
individually. "^n>r""*akness ofplanning is that it often produces somewhat
inaccurate solutions since there is no feedback from the world. But by using it
only to produce pieces of solutions-thai: will then be exploited in the generate-
and-test process, the lack of detailed accuracy becomes unimportant. And, at
the same time, the combinatorial problems that arise in simple generate-and-
test are avoided by judicious reference to the plans.

(c) Write and explain Hill Climbing algorithm.

Hill climbing is a variant of generate-and-test in which feedback from the test


procedure is used to help the generator decide which direction to move in the
search space. In a pure generate-and-test procedure, the test function
responds with only a yes or no. But if the test function is augmented with a
heuristic function2 that provides an estimate of how close a given state is to a
goal state, the generate procedure can exploit it .as shown in the procedure
below. This is particularly nice because often the computation of the heuristic
function can be done at almost no cost at the same time that the test for a
solution is being performed. Hill climbing is often used when a good heuristic
function is available for evaluating stateTEut when nrJother useful
knowledgelFavailable. For example, suppose you are in an unfamiliar city
without a map and you want to get downtown. You simply aim for the tall
buildings. The heuristic function is just distance between the current location
and the location of the tall buildings and the desirablestates are those in which
this distance is minimized.
Recall from Section 2.3.4 that one way to characterize problems is according
to their answer to the question, "Is a good solution absolute or relative?"
Absolute solutions exist whenever it is possible to recognize a goal state just
by examining it. Getting downtown is an example of such a problem. For these
problems, hill climbing can terminate whenever a goal state is reached. Only
relative solutions exist, however, for maximization (or minimization) problems,
suchjs the traveling salesman problem. In these problems, there is no a priori
goal state. For problems of this sort, it makes -sense to terminate hill climbing
when there is no reasonable alternative state to move to.

What we are calling the heuristic function is sometimes also called


the objective function, particularly in the literature of mathematical
optimization.

Simple Hill Climbing

The simplest way to implement hill climbing is as follows.

Algorithm: Simple Hill Climbing

1. Evaluate the initial state. If it is also a goal state, then return it and quit.
Otherwise, continue with the initial state as the current state.

2. Loop until a solution is found or until there are no new operators left to be
applied in the current state:

Select an operator that has riot yet been applied to the current state and apply
it to produce a new state.

Evaluate the new state, i. If it is a goal state, then return it and quit, ii. If it is
not a goal state but it is better than the current state, then make it

the current state, iii. If it is not better than the current state, then continue
in the loop.

The key difference between this algorithm and the one we gave for generate-
and test is the use of an evaluation function as a way to inject task-specific
knowledge into the control process. It is the use of such knowledge that makes
this and the other methods discussed in the rest of this chapter heuristic search
methods, and it is that same knowledge that gives these methods their power to
solve some otherwise intractable problems.

Notice that in this algorithm, we have asked the felativelvjrapue question, "Is
one state better than another?" For the algorithm to work, a precise definition
of better must be provided. In some cases, .it meiuis a higher value of the
heuristic function. In others, it means a lower value. It does not matter'which,
as long as a particular hill-climbing program is consistent in its interpretatidh."

To see how hill climbing works, let's return to the puzzle of the four colored
blocks. To solve the problem, we first need to define a heuristic function that
describes how close a particular configuration is to being a solution. One,such
function 1$ Simply the sum of the number of different colors on each of the four
sides. A solution to the puzzle will have a value of 16. Next we need to define a
set of rules that describe ways of transforming one configuration into another.
Actually, one rule will suffice. It says simply pick a block and rotate it 90
degrees in any direction. Having provided these definitions, the next step is to
generate a starting configuration. This can either be done at random or with the
aid of the heuristic function described in the last section. Now hill climbing can
begin. We generate a new state by selecting a block and rotating it. If the
resulting state is better, then we keep it. If not, we return to the previous state
and try a different perturbation.

(a) Can Solution Steps Be Ignored or Undone? Explain with example,

Suppose we are trying to prove a mathematical theorem. We proceed by first


proving a lemma that we think will be useful. Eventually, we realize that the
lemma is no help at all. Are we in trouble?

No. Everything we need to know to prove the theorem is still, true and in
memory, if it ever was. Any rules that could have been applied at the outse?
can still be applied. We can just proceed as we should have in the first place.
All we have lost is the effort that was spent exploring the blind alley.

Now consider a different problem.


jf The 8-Puzzle: The 8-puzzle is a square tray in which are placed, eight \s
square tiles. The remaining ninth square is uncovered. Each tile has a
Nurnberg on it. A tile that is adjacent to the blank space can be slid into
that space. A game consists of a starting position and a specified goal
position. The goal is to transform the starting position into the goal position
by sliding the tiles around.

A sample game using the 8-puzzle is shown in Figure 2.12. In attempting to


solve the 8-puzzle, we might make a stupid move. For example, in the game
shown above, we might start by sliding tile 5 into the empty space. Having
done that, we cannot change our mind and immediately slide tile 6 into the
empty space since the empty space will essentially have moved. But we can
backtrack and undo the first move, sliding tile.5 back to where it was. Then we
can move tile, 6. Mistakes can still be recovered from but not quite as easily as
in the theorem-proving problem. An additional step must
be performed to undo each incorrect step, whereas no action was required to
"undo" a useless lemma. In addition, the control mechanism for an .8puzzle
solver must keep track of the order in which operations are performed so that
the operations can be undone one at a time if necessary. The control structure
for a theorem prover does, not need to record all that information.

Now consider again the problem of playing chess. Suppose a chess-playing


program makes a stupid move and realizes it a couple of moves later. It
cannot fcimply play as though it had never made the stupid move. Nor can it
simply back up and start the game over from that point. All it can do is to try
to make the best of the,current situation and go on from there.

(b)Write and explain agenda driven algorithm. 1

Algorithm: Agenda-Driven Search

1. Do until a goal state is reached or the agenda is empty:

(a) Choose the most promising task from the agenda. Notice that this task
can be represented in any desired form. It can be thought of as an
explicit statement of what to do next or simply as an indication of the
next node to be expanded.

(b) Execute the task by devoting to it the number of resources determined


by its importance. The important resources to consider are time and
space. Executing the task will probably generate additional tasks
(successor nodes). For each of them, do the following:

i. See if it is already on the agenda. If so, then see if this same reason
for doing it is already on its list of justifications. If so, ignore this
current evidence. If this justification was not already present, add it
to the list. If the task was not on the agenda, insert it.

ii. Compute the new task's rating, combining the evidence from all its
justifications. Not all justifications need have equal weight. It is often
useful to'associate with each justification a measure of how strong a

reason it is. These measures are then combined at this step to


produce an overall rating for the task.

One important question that arises in agenda-driven systems is how to find


the most promising task on each cycle. One way to do this is simple. Maintain
the agenda sorted by rating. When a new task is created, insert it into the
agenda in-its proper place. When a task has its justifications changed,
recompute its rating and move it to the correct place in the list. But this method
causes a great deal of time to be spent keeping the agenda in perfect order
Much of this time is wasted since we do not need perfect order. We only need to
know the proper first element. The following modified strategy may occasionally
cause a task other than the best to be executed, but it is significantly cheaper
than the perfect method. When a task is proposed, or a new justification is
added to an existing task, compute the new rating and compare it against the
top few Xe.g., five or ten) elements on the agenda. If it is better, insert the
node into its proper position at the top of the list. Otherwise, leave it where it is
or simply insert it at the end of the agenda. At the beginning of each cycle,
choose the first task on the agenda. In addition, once in a while, go through the
agenda and reorder it properly.
An agenda-driven control structure is also useful if some tasks (or nodes)
provide negative evidence about the merits of other tasks (or nodes). This can
be represented by justifications with negative weightings. If these negative
weightings are used, it may be important to check not only for the possibility of
moving a task to the head of the agenda but also of moving a top task to the
bottom if new, negative justifications appear. But this is easy tq.do.

As you can see, the agenda mechanism provides a good way of focusing the
attention of a complex system in the areas suggested by the greatest number of
positive indicators. But the overhead for each task executed may be fairly high.
This raises the question of (he proper grain size for the division of the entire
problem-solving process into individual tasks. Suppose each task is very small.
Then we will never do even a very small thing unless it realjy is the best thing to
do. But we will spend a large percentage of our total effort on figuring out what
to do next. If, on the other hand, the size of an individual task is very large,
then some effort may be spent finishing one task when there are more
promising ones that could be done. But a smaller percentage of the total time
will be" spent on the overhead of figuring out what to do. The exact choice of
task size for a particular system depends on the extent to which doing one small
thing really means that a set of other small things is likely to be very good to do
too. It often requires some experimentation to get right.

There are some problem domains for which an agenda mechanism is


inappropriate. The agenda"mechanism assumes that if there is good reason to
do something now, then there will also be the same good reason to do
something later unless something better comes along in the interim. But this is
not always the case, particularly for systems that are interacting with people.
The following dialogue would not be acceptable to most people:

Person: I don't want to read any more about China. Give me something ejse.

Computer: OK. What else are you interested in?

Person: How about Italy? I think I'd find Italy fascinating.

Computer: What things about Italy are you interested in reading about?
Q-3(a) Explain heuristic Search.

Ans:

Definition:
A Heuristic is an operationally-effective nugget of information on how to direct
search in a problem space. Heuristics are only approximately correct. Their purpose is to
minimize search on average.

Common Types of Heuristics


 "If-then" rules for state-transition selection

 Macro-operator formation [discussed later]

 Problem decomposition [e.g. hypothesizing islands on the search path]

 Estimation of distance between Scurr and SG. (e.g. Manhattan, Euclidian,


topological distance)

 Value function on each Succ(Scurr)

 cost(path(S0, Scurr)) + E[cost(path(Scurr,SG))]

 Utility: value(S) cost(S)

Heuristic Search

Value function: E(o-Path(S0, Scurr), Scurr, SG)

Since S0 and SG are constant, we abbreviate E(Scurr)

General Form:

1. Quit if done (with success or failure), else:

2. s-Queue:= F(Succ(Scurr),s-Queue)

3. Snext:= Argmax[E(s-Queue)]

4. Go to 1, with Scurr:= Snext


Steepest-Ascent Hill-Climbing

 F(Succ(Scurr), s-Queue) = Succ(Scurr)

 Snext:= Argmax[E(Succ(Scurr))]

 If Succ(Snext) is null, then stop

 Quintessential greedy search

Max-Gradient Search

 "Informed" depth-first search

 Snext:= Argmax[E(Succ(Scurr))]

 But if Succ(Snext) is null, then backtrack

 Alternative: backtrack if E(Snext)<E(Scurr)

Q-3(b) what is Intelligence and Artificial Intelligence?

Ans:
Intelligence:

Intelligence is the computational part of the ability to achieve goals in the world.
Varying kinds and degrees of intelligence occur in people, many animals and some machines.

Artificial Intelligence:

Artificial Intelligence (AI) is usually defined as the science of making computers do


things that require intelligence when done by humans.
Q-3(c) List out merits & demerits of DFS & BFS.

Ans:

Depth first search is another way of traversing graphs, which is closely related to preorder
traversal of a tree. Recall that preorder traversal simply visits each node before its children. It
is most easy to program as a recursive routine
BFS: This can be throught of as being like Dijkstra's algorithm for shortest paths, but with
every edge having the same length. However it is a lot simpler and doesn't need any data
structures. We just keep a tree (the breadth first search tree), a list of nodes to be added to the
tree, and markings (Boolean variables) on the vertices to tell whether they are in the tree or
list.

Q-3(d)(i)Construct The Semantic Net For The All Indian Likes Football

Q-3(e)

Ans:
A Tree structure is a way of representing the hierarchical nature of a structure in a graphical form. In
computer science, a tree is a widely-used data structure that emulates a hierarchical tree structure with
a set of linked nodes.

A node is a structure which may contain a value, a condition, or represent a separate data
structure (which could be a tree of its own). Each node in a tree has zero or more child nodes,
which are below it in the tree (by convention, trees are drawn growing downwards). A node
that has a child is called the child's parent node (or ancestor node, or superior). A node has at
most one parent.

Nodes that do not have any children are called leaf nodes. They are also referred to as
terminal nodes.

A free tree is a tree that is not rooted.


The height of a node is the length of the longest downward path to a leaf from that node. The
height of the root is the height of the tree. The depth of a node is the length of the path to its
root (i.e., its root path). This is commonly needed in the manipulation of the various self
balancing trees, AVL Trees in particular. Conventionally, the value -1 corresponds to a
subtree with no nodes, whereas zero corresponds to a subtree with one node.

The topmost node in a tree is called the root node. Being the topmost node, the root node will
not have parents. It is the node at which operations on the tree commonly begin (although
some algorithms begin with the leaf nodes and work up ending at the root). All other nodes
can be reached from it by following edges or links. (In the formal definition, each such path
is also unique). In diagrams, it is typically drawn at the top. In some trees, such as heaps, the
root node has special properties. Every node in a tree can be seen as the root node of the
subtree rooted at that node.

An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf
node. A sub tree of a tree T is a tree consisting of a node in T and all of its descendants in T.

Tree representations
There are many different ways to represent trees; common representations represent the
nodes as records allocated on the heap (not to be confused with the heap data structure) with
pointers to their children, their parents, or both, or as items in an array, with relationships
between them determined by their positions in the array

Trees and graphs


The tree data structure can be generalized to represent directed graphs by removing the
constraints that a node may have at most one parent, and that no cycles are allowed. Edges
are still abstractly considered as pairs of nodes, however, the terms parent and child are
usually replaced by different terminology.

Relationship with trees in graph theory


In graph theory, a tree is a connected acyclic graph; unless stated otherwise, trees and graphs
are undirected. There is no one-to-one correspondence between such trees and trees as data
structure. We can take an arbitrary undirected tree, arbitrarily pick one of its vertices as the
root, make all its edges directed by making them point away from the root node - producing
an arborescence - and assign an order to all the nodes. The result corresponds to a tree data
structure. Picking a different root or different ordering produces a different one.

Q-4(A) Explain Expert System Architecture In Artificial Intelligence.


An expert system is, typically, composed of two major components, the Knowledge-base
and the Expert System Shell. The Knowledge-base is a collection of rules encoded as
metadata in a file system, or more often in a relational database. The Expert System Shell is
a problem-independent component housing facilities for creating, editing, and executing
rules. A software architecture for an expert system is illustrated in Figure 2.

The shell portion includes software modules whose purpose it is to,

Process requests for service from system users and application layer modules;
Support the creation and modification of business rules by subject matter experts;
Translate business rules, created by a subject matter experts, into machine-readable
forms;
Execute business rules; and
Provide low-level support to expert system components (e.g., retrieve metadata from
and save metadata to knowledge base, build Abstract Syntax Trees during rule
translation of business rules, etc.).

4.1 Client Interface

The Client Interface processes requests for service from system-users and from application
layer components. Client Interface logic routes these requests to an appropriate shell
program unit. For example, when a subject matter expert wishes to create or edit a rule, they
use the Client Interface to dispatch the Knowledge-base Editor. Other service requests
might schedule a rule, or a group of rules, for execution by the Rule Engine.

4.2 Knowledge Base Editor

The Knowledge-base Editor is a simple text editor, a graphical editor, or some hybrid of
these two types. It provides facilities that enable a subject matter expert to compose and add
rules to the Knowledge-base.

4.3 Rule Translator


Rules, as they are composed by subject matter experts, are not directly executable. They
must first be converted from their human-readable form into a form that can be interpreted
by the Rule Engine. Converting rules from one form to another is a function performed by the
Rule Translator.

The translation of rules in their original form to a machine-readable form requires parsing the
textual representation to obtain a data structure referred to as an Abstract Syntax Tree
(AST). The AST representation of rules is the memory-resident data structure that guides
execution of the inference engine when it comes time to apply a rule. The AST is an abstract
data type designed to make its interpretation by the Rule Engine simple and efficient. This
abstract data type is very expressive and permits the construction of very complex and
powerful rules. There is a third form in which rules may be expressed. A rule AST is
converted into an equivalent form suitable for storage in the Knowledge-base. The way in
which this information appears in the Knowledge-base depends on the storage technology.
Relational databases provide an especially convenient and efficient means for storing
metadata. The metadata corresponding to an AST in this case would reside in a collection of
tables. The specific scheme chosen for saving the metadata must permit the ASTs to be
rapidly reconstructed using database queries.

The diagram in Figure 3 summarizes the several conversion operations the Rule Translator
must perform as it adds rules to the Knowledge-base and as it retrieves rules from the
Knowledge-base for execution.

To make these ideas concrete, consider the arithmetic espression,

(a-b) / [x*(c-d)] ,

that might form some part of a rule. In the AST representation, this portion of the rule would
be expressed as a binary tree, like the one in Figure 4, whose nodes are either arithmetic
operators or operands.
Once created, AST representations are converted into rule metadata and stored in the
Knowledge-base. Rule metadata is simply a compact representation of ASTs. The role of the
Rule Translator in the rule editing process is illustrated in the data flow diagram of Figure 5.

In translating rules from one form to another, the structure of the original rule is never lost. It
is always possible to recreate a human-readable rule, exactly, from its Knowledge-base
representation or from its AST representation.

4.4 Rule Engine

The Rule Engine (often referred to as an inference engine in AI literature) is responsible for
executing Knowledge-base rules. It retrieves rules from the Knowledge-base, converts them
to ASTs, and then provides them to its rule interpreter for execution. The Rule Engine
interpreter traverses the AST, executing actions specified in the rule along the way. This
process is depicted in Figure 6.
4.5 Rule Object Classes

The shell component, Rule Object Classes, is a container for object classes supporting,

Rule editing;
AST construction;
Conversion of ASTs to rule metadata;
Conversion of rule metadata to ASTs; and
Knowledge-base operations (query, update, insert, delete).
Q-4(b)Explain NLP

A Closure View:

Evaluation Of natural-language-understanding Systems:


Q-4(c)Explain AO* Algoritms.
Data Structure

Graph

Marked Connectors (down; unlike A*)

Costs q() maintained on nodes

SOLVED markings

Note: Well discuss on acyclic graphs.

Basic Idea Of AO*


First top-down graph growing picks out best available partial solution sub-
graph from explicit graph.
One leaf node of this graph is expanded

Second, bottom-up cost-revising, connector-marking, SOLVE-labeling

AO* Algorithm:

1. Create G = <s> ; q(s) = h (s) If s e TERM mark s


SOLVED

2. Until s labeled SOLVED do:

1. Compute G partial solution subgraph of G by tracing down marked


connectors in G from s.

2. Select n in G, n not in TERM, n a leaf.

3. Expand n , place successors in G, for each successor not already in G let


q(successor)=h (successor). Label SOLVED all
successors in TERM. (If no successors, reset q(n) := infinity ).

4. Let S := {n}.

5. Until S = f do :

1. Remove a node, m, from S which has no descendent in G also in


S (minimal node).

2. Revise cost for m, (check each connector from m)


q(m)=min [c +q(n1)++q(nk )]. Mark chosen
connector. If all successors their
connectors are SOLVED then mark m SOLVED.

3. If m SOLVED or changed q(m) then add to S


all preferred parents of m.

4. End.

6. End.

Tracing the Algorithm:


Que.6

A. Explain nearest neighbour algorithm with example.


Ans.
The nearest neighbor algorithm and the closely related repetitive nearest neighbor algorithm is a
greedy algorithm for finding candidate solutions to the traveling salesman problem. The nearest
neighbor algorithm begins at the first city in your list of cities to visit. It then selects the closest city to
visit next. From the remaining unvisited cities it selects the city closest to city two and so on.

The nearest neighbour algorithm was one of the first algorithms used to determine a solution to the travelling
salesman problem. It quickly yields a short tour, but usually not the optimal one.

Below is the application of nearest neighbour algorithm on TSP

These are the steps of the algorithm:

1. stand on an arbitrary vertex as current vertex.


2. find out the lightest edge connecting current vertex and an unvisited vertex V.
3. set current vertex to V.
4. mark V as visited.
5. if all the vertices in domain are visited, then terminate.
6. Go to step 2.

The sequence of the visited vertices is the output of the algorithm.

The nearest neighbour algorithm is easy to implement and executes quickly, but it can sometimes miss shorter
routes which are easily noticed with human insight, due to its "greedy" nature. As a general guide, if the last few
stages of the tour are comparable in length to the first stages, then the tour is reasonable; if they are much
greater, then it is likely that there are much better tours. Another check is to use an algorithm such as the lower
bound algorithm to estimate if this tour is good enough.

In the worst case, the algorithm results in a tour that is much longer than the optimal tour. To be precise, for
every constant r there is an instance of the traveling salesman problem such that the length of the tour length
computed by the nearest neighbour algorithm is greater than r times the length of the optimal tour. Moreover,
for each number of cities there is an assignment of distances between the cities for which the nearest neighbor
heuristic produces the unique worst possible tour

B. Explain architecture of ANN.


Ans.
1.1 Feed-forward networks
Feed-forward ANNs (figure 1) allow signals to travel one way only; from input to output. There is
no feedback (loops) i.e. the output of any layer does not affect that same layer. Feed-forward
ANNs tend to be straight forward networks that associate inputs with outputs. They are extensively
used in pattern recognition. This type of organisation is also referred to as bottom-up or top-down.
1.2 Feedback networks
Feedback networks (figure 1) can have signals travelling in both directions by introducing loops in
the network. Feedback networks are very powerful and can get extremely complicated. Feedback
networks are dynamic; their 'state' is changing continuously until they reach an equilibrium point.
They remain at the equilibrium point until the input changes and a new equilibrium needs to be
found. Feedback architectures are also referred to as interactive or recurrent, although the latter
term is often used to denote feedback connections in single-layer organisations.
Figure 1.1 An example of a simple feedforward
network Figure 1.2 An example of a complicated network

1.3 Network layers


The commonest type of artificial neural network consists of three groups, or layers, of units: a
layer of "input" units is connected to a layer of "hidden" units, which is connected to a layer of
"output" units. (see Figure 1.1)
The activity of the input units represents the raw information that is fed into the network.
The activity of each hidden unit is determined by the activities of the input units and the weights on the
connections between the input and the hidden units.
The behaviour of the output units depends on the activity of the hidden units and the weights between
the hidden and output units.

This simple type of network is interesting because the hidden units are free to construct their own
representations of the input. The weights between the input and hidden units determine when each hidden
unit is active, and so by modifying these weights, a hidden unit can choose what it represents

We also distinguish single-layer and multi-layer architectures. The single-layer organisation, in which all
units are connected to one another, constitutes the most general case and is of more potential computational
power than hierarchically structured multi-layer organisations. In multi-layer networks, units are often
numbered by layer, instead of following a global numbering.

1.4 Perceptrons
The most influential work on neural nets in the 60's went under the heading of 'perceptrons' a term
coined by Frank Rosenblatt. The perceptron (figure 4.4) turns out to be an MCP model ( neuron
with weighted inputs ) with some additional, fixed, pre--processing. Units labelled A1, A2, Aj ,Ap
are called association units and their task is to extract specific, localised featured from the input
images. Perceptrons mimic the basic idea behind the mammalian visual system. They were mainly
used in pattern recognition even though their capabilities extended a lot more.

Figure 1.4
C. Explain fuzzy set and crisp set. Mention application of fuzzy logic.
Ans.
Fuzzy set:
A fuzzy set is a pair (A,m) where A is a set and .
For each , m(x) is called the grade of membership of x in (A,m). For a finite set A =
{x1,...,xn}, the fuzzy set (A,m) is often denoted by {m(x1) / x1,...,m(xn) / xn}.
Let . Then x is called not included in the fuzzy set (A,m) if m(x) = 0, x is called fully
included if m(x) = 1, and x is called fuzzy member if 0 <m(x) < 1.[4] The set
is called the support of (A,m) and the set
is called its kernel.
Sometimes, more general variants of the notion of fuzzy set are used, with membership
functions taking values in a (fixed or variable) algebra or structureL of a given kind; usually it is
required that L be at least a poset or lattice
Crisp set:
Application of fuzzy logic: There are countless applications for fuzzy logic. In fact, some claim that
fuzzy logic is the encompassing theory over all types of logic. The items in this list are more common
applications that one may encounter in everyday life.
Bus Time Tables
How accurately do the schedules predict the actual travel time on the bus?
Bus schedules are formulated on information that does not remain constant. They use fuzzy logic
because it is impossible to give an exact answer to when the bus will be at a certain stop. Many
unforseen incidents can occur. There can be accidents, abnormal traffic backups, or the bus could break
down. An observant scheduler would take all these possibilities into account, and include them in a
formula for figuring out the approximate schedule. It is that formula which imposes the fuzziness
Temperature control (heating/cooling)
The trick in temperature control is to keep the room at the same temperature consistently. Well, that
seems pretty easy, right? But how much does a room have to cool off before the heat kicks in again?
There must be some standard, so the heat (or air conditioning) isn't in a constant state of turning on and
off. Therein lies the fuzzy logic. The set is determined by what the temperature is actually set to.
Membership in that set weakens as the room temperature varies from the set temperature. Once
membership weakens to a certain point, temperature control kicks in to get the room back to the
temperature it should be.
Medical diagnoses
How many of what kinds of symptoms will yield a diagnosis? How often are doctors in error?
Surely everyone has seen those lists of symptoms for a horrible disease that say "if you have at least 5
of these symptoms, you are at risk". It is a hypochondriac's haven. The question is, how do doctors go
from that list of symptoms to a diagnosis? Fuzzy logic. There is no guaranteed system to reach a
diagnosis. If there were, we wouldn't hear about cases of medical misdiagnosis. The diagnosis can only
be some degree within the fuzzy set.
Predicting travel time
This is especially difficult for driving, since there are plenty of traffic situations that can occur to slow
down travel.
As with bus timetabling, predicting ETA's is a great exercise in fuzzy logic. That's why it is called an
estimated time of arrival. A major player in predicting travel time is previous experience. It took me six
hours to drive to Philadelphia last time, so it should take me about that amount of time when I make the
trip again. Unfortunately, other factors are not typically considered. Weather, traffic, construction,
accidents should all be added into the fuzzy equation to deliver a true estimate.
Antilock Braking System
It's probably something you hardly think about when you're slamming on the brakes in your car
The point of an ABS is to monitor the braking system on the vehicle and release the brakes just before
the wheels lock. A computer is involved in determining when the best time to do this is. Two main
factors that go into determining this are the speed of the car when the brakes are applied, and how fast
the brakes are depressed. Usually, the times you want the ABS to really work are when you're driving
fast and slam on the brakes. There is, of course, a margin for error. It is the job of the ABS to be
"smart" enough to never allow the error go past the point when the wheels will lock
D. Explain cut predicate in detail.
Ans.
Prolog provides a predicate that performs this function. It is called the cut/1, represented by an
exclamation point (!).

The cut/1 effectively tells Prolog to freeze all the decisions made so far in this predicate. That is, if
required to backtrack, it will automatically fail without trying other alternatives. We will first examine
the effects of the cut/1 and then look at some practical reasons to use it . When the cut/1 is encountered,
it re-routes backtracking. It short-circuits backtracking in the goals to its left on its level, and in the
level above, which contained the cut/1. That is, both the parent goal and the goals of the particular rule
being execut/1ed are affected by the cut/1. The effect is undone if a new route is taken into the parent
goal.
We will write some simple predicates that illustrate the behavior of the cut/1, first adding some data to
backtrack over.
data(pentiumIII)
data(athlon).
Here is the first test case. It has no cut/1 and will be used for comparison purposes.
compare_cut_1(X) :-
data(X).
compare_cut_1('last chip').
This is the control case, which exhibits the normal behavior.
?- compare_cut_1(X), write(X), nl, fail.
pentiumIII
Athlon
last chip
no
Next, we put a cut/1 at the end of the first clause.
compare_cut_2(X) :-
data(X),
!.
compare_cut_2('last chip').
Note that it stops backtracking through both the data subgoal (left), and the compare_cut_2/1 parent
(above).
?- compare_cut_2(X), write(X), nl, fail.
pentiumIII
no
Next we put a cut/1 in the middle of two subgoals.
compare_cut_3(X,Y) :-
data(X),
!,
data(Y).
compare_cut_3('last chip').
Note that the cut inhibits backtracking in the parent compare_cut_3/2 and in the goals to the left of
before) the cut (first data). The second data/1 to the right of (after) the cut is still free to backtrack.
?- compare_cut_3(X,Y), write(X-Y), nl, fail.
pentiumIIIpentiumIII
pentiumIIIAthlon
no
Performance is the main reason to use the cut/1. This separates the logical purists from the pragmatists.
Various arguments can also be made as to its effect on code readability and maintainability. It is often
called the 'goto' of logic programming. You will most often use the cut/1 when you know that at a
certain point in a given predicate, Prolog has either found the only answer, or if it hasn't, there is no
answer. In this case you insert a cut/1 in the predicate at that point. Similarly, you will use it when you
want to force a predicate to fail in a certain situation, and you don't want it to look any further.
E. Explain properties of good knowledge representation system.
Ans.
The following properties should be possessed by a knowledge representation system.
Representational Adequacy

->the ability to represent the required knowledge;


inferential Adequacy
- the ability to manipulate the knowledge represented to produce new knowledge corresponding to that
inferred from the original;
Inferential Efficiency
- the ability to direct the inferential mechanisms into the most productive directions by storing
appropriate guides;
Acquisitional Efficiency
- the ability to acquire new knowledge using automatic methods wherever possible rather than reliance
on human intervention.
Que.5

A. Explain how file operation can be done in prolog?

Ans.
When one writes a Prolog program, usually the facts and rules of the program are stored in a (text) file,
and then loaded into the Prolog interpreter. Files have other uses in Prolog, too.

For example, we may wish to write out a table of results that have been computed for us by our Prolog
program. One can use built-in predicates like write, nl, putc, tab and others to write out the table, but by
default it will appear on the computer screen. To direct this output to a file, we use the tell built-in
predicate. Suppose that we wish to write the table to a file called "mytable.data". By executing the
(pseudo-)goal tell("mytable.data"), we tell Prolog that the new current output stream is to be the file
"mytable.data". Subsequent writes will go to this file. When one wishes to stop writing to the file and
resume writing on the screen, one uses the built-in predicate told (with no arguments). Also, the query
?- telling(X).bindsX to the name of the current output file. If the current output stream is not a file, then
X will be bound to something that indicates that the current output stream is the screen - for example,
in Unix, X may be bound to the atom stdout (standard output, which is normally the screen). Example:

?- tell('mytable.data'), write('***** Table of results *****'), nl, told.


% the file mytable.data should now contain a single line of text as above

The built-in Prolog predicate append/1 is like tell, except that it arranges for subsequent write
operations to add data to the end of the specified file, rather than overwriting the file with the first
subsequent write operation. If myothertable.data initially contains, say, a single line, This is the first
line, then append/1 works as follows:

?- append('myotherfile.dat'), write('Here is another line'), nl.


true.
?- halt.
% cat myotherfile.dat # - # is Unix comment char, cat lists file contents
This is the first line
Here is another line
%

The situation for reading from a file is analogous to writing (except that there is no analogue for
append/1). One can use built-in predicates like read, getc and others to read, by default from the
keyboard. By executing the (pseudo-)goal see('mydata.text'), we tell Prolog that the new current input
stream is to be the file mydata.text. Subsequent reads will come from this file. When one wishes to stop
reading from the file and resume reading from the keyboard, one uses the built-in predicate seen (with
no arguments). Also, the query ?- seeing(X).bindsX to the name of the current input file. If the current
input stream is not a file, then X will be bound to something that indicates that the current output
stream is the screen - for example, in Unix, X may be bound to the atom stdin (standard input, which is
normally the keyboard). Example:

?- see('mydata.text'), read(X), seen, write(X), nl.


% the first Prolog term in the file mydata.text should now appear
% on the screen, having been read from the file with read(X), and then
% written to the screen with write(X) and nl.

What happens if you try to read from a file and there is nothing (left) to read, either because the file is
empty, or you have previously read everything there was to read in this file? In this case, Prolog binds
the variable that was the argument to read to the special atom end_of_file. Knowing this means that
you can test after a read to make sure that you did not hit end of file. Example:

?- see('empty.dat'), read(Term).
Term = end_of_file

B. Explain back tracing with example.


Ans.

Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that
incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as
it determines that c cannot possibly be completed to a valid solution.

Example:Constraint satisfaction

The general constraint satisfaction problem consists in finding a list of integers x = (x[1],x[2], , x[n]), each in
some range {1, 2, , m}, that satisfies some arbitrary constraint (boolean function) F.

For this class of problems, the instance data P would be the integersm and n, and the predicate F. In a typical
backtracking solution to this problem, one could define a partial candidate as a list of integers c = (c[1],c[2],
c[k]), for any k between 0 and n, that are to be assigned to the first k variables x[1],x[2], , x[k]). The root
candidate would then be the empty list (). The first and next procedures would then be

functionfirst(P,c)
k length(c)
ifk = n
thenreturn
elsereturn (c[1], c[2], , c[k], 1)

functionnext(P,s)
k length(s)
ifs[k] = m
thenreturn
elsereturn (s[1], s[2], , s[k-1], 1 + s[k])

Here "length(c)" is the number of elements in the list c.


The call reject(P,c) should return true if the constraint F cannot be satisfied by any list of n integers that begins
with the k elements of c. For backtracking to be effective, there must be a way to detect this situation, at least for
some candidatesc, without enumerating all those mn-kn-tuples.

For example, if F is the conjunction of several boolean predicates, F = F[1] F[2] F[p], and each F[i]
depends only on a small subset of the variables x[1], , x[n], then the reject procedure could simply check the
terms F[i] that depend only on variables x[1], , x[k], and return trueif any of those terms returns false. In fact,
reject needs only check those terms that do depend on x[k], since the terms that depend only on x[1], , x[k-1]
will have been tested further up in the search tree.

Assuming that reject is implemented as above, then accept(P,c) needs only check whether c is complete, that is,
whether it has n elements.

It is generally better to order the list of variables so that it begins with the most critical ones (i.e. the ones with
fewest value options, or which have a greater impact on subsequent choices).

One could also allow the next function to choose which variable should be assigned when extending a partial
candidate, based on the values of the variables already assigned by it. Further improvements can be obtained by
the technique of constraint propagation.

In addition to retaining minimal recovery values used in backing up, backtracking implementations commonly
keep a variable trail, to record value change history. An efficient implementation will avoid creating a variable
trail entry between two successive changes when there is no choice point, as the backtracking will erase all of
the changes as a single operation.

An alternative to the variable trail is to keep a timestamp of when the last change was made to the variable. The
timestamp is compared to the timestamp of a choice point. If the choice point has an associated time later than
that of the variable, it is unnecessary to revert the variable when the choice point is backtracked, as it was
changed before the choice point occurred.

Or
Que.5

A. WAP to form the union of two list.


Ans.

domains

integerlist=integer*

predicates

inter_union(integerlist,integerlist)

intersect(integerlist,integerlist,integerlist)

union(integerlist,integerlist,integerlist)

delete(integer,integerlist,integerlist)

member(integer,integerlist)

clauses

inter_union(L1,L2):-
write("Intersection\n"),

intersect(L1,L2,L4),

write(L4),

write("\n Union\n"),

union(L1,L2,L4),

write(L4),

nl.

intersect([H|Tail1],L2,[H|Tail3]):-

member(H,L2),

intersect(Tail1,L2,Tail3).

intersect([H|Tail1],L2,Tail3):-

not(member(H,L2)),

intersect(Tail1,L2,Tail3).

intersect([],_,[]).

member(X,[X|_]).

member(X,[Head|Tail]):-

not(X=Head),

member(X,Tail).

union([],L,L).

union([H|Tail1],L2,[H|Tail3]):-

member(H,L2),

delete(H,L2,L3),

union(Tail1,L3,Tail3).

union([H|Tail1],L2,[H|Tail3]):-

not(member(H,L2)),

union(Tail1,L2,Tail3).

delete(X,[X|Tail],Tail):-

!.

delete(X,[Y|Tail],[Y|Z]):-
delete(X,Tail,Z).

B. WAP to reverse the given list.


Ans.

domains

list=integer*
predicates
seln_sort(list,list)
minimum(list,integer,integer)
efface(list,integer,list)
clauses
seln_sort([X|Xs],[Y|Ys]):-minimum(Xs,X,Y),efface([X|Xs],Y,Zs),seln_sort(Zs,Ys).
seln_sort([],[]).
minimum([Y|Ys],X,Z):-Y<=X,!,minimum(Ys,Y,Z).
minimum([_|Ys],X,Z):-minimum(Ys,X,Z).
minimum([],X,X).
efface([Y|Xs],Y,Xs):-!.
efface([X|Xs],Y,[X|Zs]):-efface(Xs,Y,Zs).

Goal :seln_sort([1,3,45,5,14,3]X).

You might also like