You are on page 1of 77

Lesson 3:

Problem Solving by Searching


Problem Solving : Introduction
•Problem Solving in games such as “Tower of Hanoi Problem”
can be an example.
• It can be done by building an artificially intelligent system
to solve that particular problem. To do this, one needs to
define the problem statements first and then generating the
solution by keeping the conditions in mind.
•Some of the most popularly used problem solving with the
help of artificial intelligence are:
1-Chess.
2-Travelling Salesman Problem.
3-Tower of Hanoi Problem.
4-Water-Jug Problem.
5-N-Queen Problem.
Problem searching

•In general, searching refers to as finding information one needs.


•Searching is the most commonly used technique of problem
solving in artificial intelligence.
•The searching algorithm helps us to search for solution of
particular problem.
Problem
•Problems are the issues which comes across any system.
•A solution is needed to solve that particular problem.
Steps of solving a problem in AI
The process of solving a problem consists of
five steps. These are:
Steps of solving a problem in AI
• Defining The Problem: The definition of the problem must be
included precisely. It should contain the possible initial as well
as final situations in a bigger picture which should result in
acceptable solution.
• Analyzing The Problem: Analyzing the problem and its
requirement must be done as few features can have immense
impact on the resulting solution.
• Identification Of Solutions: This phase generates reasonable
amount of solutions to the given problem in a particular range.
• Choosing a Solution: From all the identified solutions, the best
solution is chosen basis on the results produced by respective
solutions.
• Implementation: After choosing the best solution, its
implementation is done.
Real-world problems
• Routine finding
– Routing in computer networks (computer example)
– Automated travel advisory system
– Airline travel planning system
– Goal: the best path between the origin and the
destination
• Travelling Salesperson problem (TSP)
– Is a famous touring problem in which each city
must be visited exactly once.
– Goal: shortest tour from starting to destination
and then returning back.
What is a solution ?
• A sequence of actions (a plan) which leads
from the initial state into a goal state (e.g.,
the sequence of actions that gets the goat,
lion, grass and man safely across the
river).
What is a solution ?
• A man has a lion, a goat and grass...he has to pass
through the river from one bank to other bank with one
boat, but...?
the boat can only carry 2 things at a time so its either the
man and the lion or goat or grass, he has to come back for
the other 2 and carry them one after the other...if he takes
the lion, the goat will eat the grass and that is not good, if
he takes the grass, the lion will eat the goat and that is also
not good...how is the man supposed to take all three of
them ??
What is a solution ?

• First trip to the other side: man + goat.


• Second trip: man + lion (when getting to the other side
man loads goat again and returns back along with the
goat), leaving the lion.
• Third trip: man drops off goat and takes grass man +
grass... then goes back).
• Fourth trip: drops off grass where the lion already is.
• Fifth trip: goes back and picks up goat for the second time,
man + goat.
• Sixth trip and last trip: goes back with the goat to where the
lion and the grass already are... the end. Man + goat
Search Algorithms in AI
Artificial Intelligence is the study of building
agents that act rationally (logically).
Most of the time, these agents perform some kind
of search algorithm in the background in order to
achieve their tasks.
Search Algorithms in AI
• Uninformed Search Algorithms

The search algorithms in this section have no additional


information other than the one provided in the problem
definition. The plans to reach the goal state from the start
state differ only by the order and/or length of actions.
Uninformed search is also called Blind search.
• The following uninformed search algorithms are
discussed in this section.
• Depth First Search
• Breath First Search
Search Algorithms in AI
• Uninformed Search Algorithms

Each of these algorithms will have:


•A problem graph, containing the start node S and the goal
node G .

•A strategy, describing the manner in which the graph will be


traversed to get to G
.

•A fringe, which is a data structure used to store all the


possible states (nodes) that you can go from the current states.
•A tree, that results while traversing to the goal node.
•A solution, which will depict the sequence followed from nodes
S to G
.
Search Algorithms in AI
• Uninformed Search Algorithms

1-Depth-first search (DFS) is an algorithm for


traversing or searching tree or graph data
structures. The algorithm starts at the root node
(selecting some arbitrary node as the root node in
the case of a graph) and explores as far as
possible along each branch.
Search Algorithms in AI
• Uninformed Search Algorithms

1-Depth-first search

Searching falls under (AI). A major goal of AI is to give


computers the ability to think, or in other words, mimic human
behavior. The problem is, unfortunately, computers don't
function in the same way our minds do. They require a series
of well-reasoned out steps before finding a solution. Your
goal, then, is to take a complicated task and convert it into
simpler steps that your computer can handle. That conversion
from something complex to something simple is what this
lecture is primarily about. Learning how to use search
algorithms.
Search Algorithms in AI
• Uninformed Search Algorithms

1-Depth-first search

The following is an example of our search tree. It is a series


of interconnected nodes that we will be searching through,
In our above graph, the path connections are not two-way. All
paths go only from top to bottom. In other words, A has a path
to B and C, but B and C do not have a path to A. It is basically
like a one-way street.
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search
We now have a way of describing
location in our graph. We know
how the various nodes (the
lettered circles) are related to
each other (neighbors).
Depth First Search Depth first
search works by taking a node,
checking its neighbors, expanding
the first node it finds among the
neighbors, checking if that
expanded node is our destination,
and if not, continue exploring
more nodes.
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 0
• Let’s start with our root/goal node:
• I will be using two lists to keep track of what we are doing -
an Open list and a Closed List. An Open list keeps track of
what you need to do, and the Closed List keeps track of
what you have already done. Right now, we only have our
starting point, node A. We haven't done anything to it yet,
so let's add it to our Open list.
• Open List: A
• Closed List: <empty>
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 1
• Node A's neighbors are the B and C nodes. Because we
are now done with our A node, we can remove it from our
Open list and add it to our Closed List. You aren't done with
this step though. You now have two new nodes B and C
that need exploring. Add those two nodes to our Open list.
• Now, let's explore the neighbors of our A node. To put
another way, let's take the first item from our Open list and
explore its neighbors:
• Open List: B,C
• Closed List: A
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 2
Our Open list contains two items. For depth first search and
breadth first search, you always explore the first item from our
Open list. The first item in our Open list is the B node. B is not
our destination, so let's explore its neighbors in the next step.

• Open List: B,C


• Closed List: A
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 3
So after exploring B (its neighbors).
I am going to remove it from the Open list and add it to the
Closed List. Our new nodes are D and E, and we add these
nodes to the beginning of our Open list
• Open List: D,E,C
• Closed List: A,B
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 4
• We should start to see a pattern forming. Because D is at
the beginning of our Open List, we expand it. D isn't our
destination, and it does not contain any neighbors. All you
do in this step is remove D from our Open List and add it to
our Closed List:
• Open List: E,C
• Closed List: A,B,D
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 5
• We now expand the E node from our Open list. E is not our
destination, so we explore its neighbors and find out that it
contains the neighbors F and G. Remember, F is our target
• Open List: F,G,C
• Closed List: A,B,D,E
Search Algorithms in AI
• Uninformed Search Algorithms
1-Depth-first search

Step 6
• We now expand the F node. Since it is our intended
destination, we stop. We remove F from our Open list and
add it to our Closed List. Since we are at our destination,
there is no need to expand F in order to find its neighbors.
Our final Open and Closed Lists contain the following data:
• Open List: G,C
• Closed List: A,B,D,E,F

The final path taken by our depth first search method is what
the final value of our Closed List is: A, B, D,E, F.
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search

Breadth-first search (BFS) is an algorithm for traversing or


searching tree or graph data structures. It starts at the tree
root (or some arbitrary node of a graph, sometimes referred
to as a ‘search key’), and explores all of the neighbor nodes
at the present depth prior to moving on to the nodes at the
next depth level..
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search

Let's try to find a path between nodes A and E.


Step 0
• Let's start with our root/goal node: starting point this
is a current working
vertex
Like before, I will continue to employ the Open and Closed
Lists to keep track of what needs to be done:
Open list: A
Closed list: <empty>
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search
Step 1
• Now, let's explore the neighbors of our A node. So far, we
are following in depth first's footsteps:
• We remove A from our Open list and add A to our Closed
List. A's neighbors, the B and C nodes, are added to our
Open list. They are added to the end of our Open list, but
since our Open list was empty (after removing A), it's hard
to show that in this step.
Open list: B,C
Closed list: A
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search
Step 2
• Here is where things start to diverge from our depth first
search method. We take a look the B node because it
appears first in our Open List. Because B isn't our intended
destination, we explore its neighbors.
• B is now moved to our Closed List, but the neighbors of B,
nodes D and E are added to the end of our Open list ,
where as in DFS, node D and E are added to the beginning
of the open list.
Open list: C,D,E
Closed list: A,B
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search
Step 3
• We can now expand our C node, Since C has no
neighbors, all we do is remove C from our Closed List and
move on.

Open list: D,E


Closed list: A,B,C
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search
Step 4
• Similar to Step 3, we expand node D and it is first in the
open list. Since it isn't our destination, and it too does not
have any neighbors, we simply remove D from close to
Open list, add D to Closed List, and continue on:

Open list: E
Closed list: A,B,C,D
Search Algorithms in AI
• Uninformed Search Algorithms

2-Breath-first search
Step 5
• Because our Open list in step 4 only had one item ‘E’, we
have no choice but to take a look at node E. Since node E
is also our destination ‘assuming’, we can stop here:

Open list: <empty>


Closed list: A,B,C,D,E
Search Algorithms in AI
Informed Search Algorithms

Here, the algorithms have information on the goal


state, which helps in more efficient searching. This
information is obtained by something called a
heuristic.
Search Heuristics: In an informed search, a
heuristic is a function that estimates how close a
state is to the goal state. For examples –
Manhattan distance, Euclidean distance, etc.
-Heuristics are only used in informed search
Search Algorithms in AI
Informed Search
Algorithms

1-A* Tree Search, or


simply known as A*
Search.
To approximate the shortest
path in real-life situations, like-
in maps, games where there
can be many hindrances.
Search Algorithms in AI
Informed Search Algorithms

1-A* Tree Search, or simply known as A* Search.


The key feature of the A* algorithm is that
-It keeps a track of each visited node which helps in
ignoring the nodes that are already visited, saving a
huge amount of time.
-It also has a list that holds all the nodes that are left
to be explored
-It chooses the most optimal node from this list, thus
saving time not exploring unnecessary or less
optimal nodes.
Search Algorithms in AI
Informed Search Algorithms
A* Search.
-We use two lists namely ‘open list’ and ‘closed list’
-The open list contains all the nodes that are being
generated and are not existing in the closed list and
each node explored after it’s neighboring nodes are
discovered.
-After a node Is fully explored the pointer is moved from
it and the node is then placed in the closed, the
neighbors are placed in the open list this is how the
nodes expand.
Search Algorithms in AI
Informed Search Algorithms
A* Search.
-Each node has a pointer to its parent so that at any
given point it can retrace the path to the parent.
-Initially, the open list holds the start(Initial) node.
-The next node chosen from the open list is based on
its f score, the node with the least f score is picked up
and explored.
Search Algorithms in AI
Informed Search Algorithms
A* Search.
F(x) = g(x) + h(x)
- h(x) is an estimate of the distance of the current
node from the goal node (the total number steps to
reach to the goal from starting),
- g(x) (the cost to search the node, the weight of
edge leading to a particular node).
- F(x) is the total of both h(x) and g(x)
Search Algorithms in AI
Informed Search Algorithms
A* Search.
starting
F(x) = g(x) + h(x) H(3)

- Heuristic function ?
H(2)

H(1)

goal H(0)
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
0
s -> =0 +7 =7
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6 (less costly)
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6 (less costly)
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly)
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly)
S->A->C =(1+5=6) +1 =7
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly)
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly)
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly)
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly)
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6 X
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6 X
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
(S->B->C)->D = (4+2+3=9) + 0 =9
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7 X
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6 X
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
(S->B->C)->D = (4+2+3=9) + 0 =9
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7 X
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6 X
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
(S->B->C)->D = (4+2+3=9) + 0 =9
(S->A->C)->D = (1+5+3=9) +0 =9
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7 X
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7 X
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6 X
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
(S->B->C)->D = (4+2+3=9) + 0 =9
(S->A->C)->D = (1+5+3=9) +0 =9
Search Algorithms in AI
Informed Search Algorithms
A* Search. F(x)=g(x)+h(x)
s -> = 0+7 =7
S->A =1+6 = 7 X
S->B = 4+2=6 (less costly) X
S->B->C = (4+2=6) + 1 =7 S->B->C = (4+2=6) + 1 =7 X
S->A->B =(1+2=3) + 2 = 5 (less costly) X
S->A->C =(1+5=6) +1 =7 X
S->A->D =(1+12=13) +0 =13
(S->A->B)->C =(1+2+2=5) +1= 6 X
(S->A->B->C)->D =(1+2+2+3=8) + 0 =8
(S->B->C)->D = (4+2+3=9) + 0 =9
(S->A->C)->D = (1+5+3=9) +0 =9
Search Algorithms in AI
Informed Search Algorithms (A* search)
• This is a classic Toy Problem
• The puzzle starts with 8 sliding-tiles
out of place and one gap into which
the tiles can be slid. The goal is to
have all of the numbers in order.
Search Algorithms in AI
Heuristic for the 8-puzzle
• # tiles out of place (h1)

• Manhattan distance (h2)


• Sum of the distance of each tile
from its goal position
• Tiles can only move up or down
→ city blocks
Search Algorithms in AI
Heuristic for the 8-puzzle

0 1 2
0 1 2 3 4 5 0 2 5
3 4 5 6 7 3 1 7
6 7 Goal State 6 4

h1=1 h1=5
h2=1 h2=1+1+2+1+2=7
Search Algorithms in AI
Informed Search Algorithms
• You are permitted to slide blocks horizontally or
vertically into the blank square. The following shows
a sequence of legal moves from an initial board
position (left) to the goal position (right).
Search Algorithms in AI
Informed Search Algorithms

• 8 puzzle series game heuristic value


• H1 (5)
• H2 (1+1+0+0+1+1+0+0) =4
• H total is 5+4 =9
Search Algorithms in AI
Local Search Algorithms
2- Greedy Algorithm:
Greedy algorithm is also known as Prim’s
algorithm is used to find the Minimum Spanning
Tree MST. In Prim’s Algorithm we grow the
spanning tree from a starting position, we add
vertex to the growing spanning tree.
MST minimum weight spanning tree is a subset
of the edges, edge-weighted undirected graph that
connects all the vertices together, without any
cycles and with the minimum possible total edge
weight .
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

It starts with an empty spanning tree. The idea is to maintain


two sets of vertices. The first set contains the vertices already
included in the MST, the other set contains the vertices not
yet included. At every step, it considers all the edges that
connect the two sets, and picks the minimum weight edge
from these edges. After picking the edge, it moves the other
endpoint of the edge to the set containing MST
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

A group of edges that connects two set of vertices in a


graph is called cut in graph theory. So, at every step of
Prim’s algorithm, we find a cut (of two sets, one contains
the vertices already included in MST and other contains
rest of the vertices), pick the minimum weight edge from
the cut and include this vertex to MST Set.
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:
1) Create a set (mstSet) that
keeps track of vertices already
included in MST. mstSet{}
2) Assign a key value to all
vertices in the input graph.
Initialize all key values as
INFINITE. Assign key value as 0
for the first vertex so that it is
picked first. “Source vertex”
.
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:
3) While mstSet doesn’t include all
vertices
-Pick a vertex u which is not there
in mstSet and has minimum key
value.
-Include u to mstSet.
-Update key value of all adjacent vertices of u. To
update the key values, iterate through all adjacent
vertices. For every adjacent vertex v, if weight of
edge u-v is less than the previous key value of v,
update the key value as weight of u-v
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:
The idea of using key values is
to pick the minimum weight
edge from cut. The key values
are used and included only for
vertices which are not yet
included in MST, the key value
for these vertices indicate the
minimum weight edges
connecting them to the set of
vertices included in MST.
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

vertex 0 1 2 3 4 5 6 7 8

key 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

vertex 0 1 2 3 4 5 6 7 8
key 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

vertex 0 1 2 3 4 5 6 7 8
key 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

The set mstSet is initially empty and keys assigned to vertices are {0,
INF, INF, INF, INF, INF, INF, INF} where INF indicates infinite. Now pick
the vertex with minimum key value. The vertex 0 is picked, include it
inmstSet. After including to mstSet, update key values of adjacent
vertices. Adjacent vertices of 0 are 1 and 7. The key values of 1 and 7
are updated as 4 and 8. Following subgraph shows vertices and their
key values, the vertices with finite key values are also shown.

vertex 0 1 2 3 4 5 6 7 8
key 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
0
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:
Verte 0 1 2 3 4 5 6 7 8
x
Key 0 8 ∞ ∞ ∞ ∞ 8 ∞
4
Over here no key updates on vertex 7 will be made as the keys over
here is more then the keys in the table, only vertex 2 key will be
updated with minimum value ‘8’ as previously it had infinite value.
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

Pick the vertex with minimum key value and not already
included in MST (not in mstSET). We can either pick vertex 7
or vertex 2, let vertex 7 is picked. So mstSet now becomes {0,
1, 7}. Update the key values of adjacent vertices of 7. The key
value of vertex 6 and 8 becomes finite (1 and 7 respectively).
vertex 0 1 2 3 4 5 6 7 8

key 0 4 8 ∞ ∞ ∞ ∞ ∞
8 Previous
values
vertex 0 1 2 3 4 5 6 7 8
New
key 0 4 8 ∞ ∞ ∞ 1 8 7 values
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

Pick the vertex with minimum key value and not already included in MST
(not in mstSET). Vertex 6 is picked. So mstSet now becomes {0, 1, 7, 6}.
Update the key values of adjacent vertices of 6. The key value of vertex 5
and 8 are updated.
At this point check the position where you stand and go to the new vertex
position keeping in mind the lowest key value in the above table.
verte 0 1 2 3 4 5 6 7 8
x Previous
key 0 4 8 ∞ ∞ ∞ 8 7 values
1
verte 0 1 2 3 4 5 6 7 8 New
x
values
key 0 4 8 ∞ ∞ 2 1 8 6
Search Algorithms in AI
Local greedy Search Algorithms
2- Greedy Algorithm:

We repeat the above steps until mstSet includes all vertices of


given graph. Finally, we get the following graph.
Jazak Allah khair

You might also like