Artificial Intelligence: CIT 246

DAA- Unit II
By
Dr. A.S.Alvi
Review of graphs

A graph represents relationship among items.

A graph consists of a set of vertices and a set of edges that
connect the vertices.
G = (V, E)
Review of graphs (cont.)

V: the set of vertices (or nodes)

E: the set of pairs of edges that connect the vertices
Vertex
V0
V1
Edge
V2
V3
Example:

computing the fastest route through mass transportation

computing the fastest way for routing electronic mail
through a network of computers
Problem: Laying Telephone Wire
Central office
Wiring: Naive Approach
Central office
Expensive!
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers
Minimum-cost spanning trees



Suppose you have a connected undirected graph with a weight (or
cost) associated with each edge
The cost of a spanning tree would be the sum of the costs of its
edges
A minimum-cost spanning tree is a spanning tree that has the
lowest cost
A
19
16
21 11
33
E
F
18
B
5
14
D
A
6
C
10
A connected, undirected graph
16
11
F
E
18
B
5
6
C
D
A minimum-cost spanning tree
Prim’s Algorithm

In Kruskal algorithm the selection function chooses edges in
increasing order of length without worrying too much about
their connection to previously chosen edges, except that we are
careful never to form a cycle.

In Prim’s algorithm, on the other , the minimum spanning tree
grows in a natural way, starting from the arbitrary root. At
each stage we add a new branch to the tree already
constructed. The algorithm stops when all the nodes have
been reached.
Prim’s Algorithm

Let B be a set of nodes and T a set of edges.

Initially B contain a single arbitrary node and T is empty.

At each step Prim’s algorithm looks for the shortest possible
edge {u,v} such that u B and v  N\B.

It then add v to B and {u,v} to T.

In this way the edges in T form at any instant a minimum
spanning tree for the nodes in B.

We continue thus as long as B is not equal to N.
Prim’s algorithm

Start form any arbitrary vertex

Find the edge that has minimum weight form all known
vertices

Stop when the tree covers all vertices
The execution of Prim's algorithm(moderate
part)
8
the root
vertex
7
c
b
4
d
9
2
11
a
7
8
h
i
6
2
8
f
7
c
b
4
e
10
g
1
14
4
d
9
2
11
a
7
8
h
i
1
14
4
6
e
10
g
2
f
8
7
c
b
4
d
9
2
11
a
7
8
h
i
6
2
8
f
7
c
b
4
e
10
g
1
14
4
d
9
2
11
a
7
8
h
i
1
14
4
6
e
10
g
2
f
8
7
c
b
4
d
9
2
11
a
7
8
h
i
6
2
8
f
7
c
b
4
e
10
g
1
14
4
d
9
2
11
a
7
8
h
i
1
14
4
6
e
10
g
2
f
8
7
c
b
4
d
9
2
11
a
7
8
h
i
6
2
8
f
7
c
b
4
e
10
g
1
14
4
d
9
2
11
a
7
8
h
i
1
14
4
6
e
10
g
2
f
8
7
c
b
4
d
9
2
11
a
7
8
h
i
1
14
4
6
e
10
g
2
f
Bottleneck spanning tree: A spanning tree of G whose largest edge
weight is minimum over all spanning trees of G. The value of the bottleneck
spanning tree is the weight of the maximum-weight edge in T.
Theorem:
A minimum spanning tree is also a bottleneck spanning tree.
(Challenge problem)
Example 1
1
1
4
6
4
3
2
2
4
5
3
5
6
8
7
4
3
7
6
Example 2
9
a
2
5
a
6
2
d
4
4
5
c
9
b
5
5
6
d
4
4
e
5
c
b
5
e
Example 3
Prim’s Algorithm
function Prim (G= < N,A >:graph; length: A
R+ ) ;set of edges
{initialiazation}
TØ
B  { an arbitrary member of N}
while B ± N do
find e ={u,v} of minimum length such that
u B and v  N\B
T  T  {e}
B  B  {v}
return T
Minimum Connector Algorithms
Kruskal’s algorithm
1. Select the shortest edge in
a network
2. Select the next shortest
edge which does not create
a cycle
3. Repeat step 2 until all
vertices have been
connected
Prim’s algorithm
1. Select any vertex
2. Select the shortest edge
connected to that vertex
3. Select the shortest edge
connected to any vertex
already connected
4. Repeat step 3 until all
vertices have been connected
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra's algorithm - Pseudocode
dist[s] ←0
for all v ∈ V–{s}
do dist[v] ←∞
S←∅
Q←V
(distance to source vertex is zero)
(set all other distances to infinity)
(S, the set of visited vertices is initially empty)
(Q, the queue initially contains all
vertices)
(while the queue is not empty)
(select the element of Q with the min. distance)
(add u to list of visited vertices)
while Q ≠∅
do u ← mindistance(Q,dist)
S←S∪{u}
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v)
(if new shortest path found)
then d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist
An Example

2
0

4
4
2
2
1
1
2
6 
3
4
2
3

Initialize
Select the node with the
minimum temporary distance
label.
3
5

2
Update Step

2
0

4
4
2
2
1
1
2
6 
3
4
2
3

4
3
5


Choose u such that N_(u) S

2
2
0
4
4
2
2
1
1
2
6 
3
4
2
3
4
3
5

Update Step
6

2
2
0
4
4
2
2
1
1
2
4
2
3
The predecessor of
node 3 is now node 2
6 
3
3
5
4

3
4

Choose u Such That N_(u)
2
2
0
6
4
4
2
2
1
1
2
6 
3
4
2
3
3
3
5
4
S
Update
2
2
0
6
4
4
2
2
1
1
2
4
2
3
3
d(5) is not changed.
6 
3
3
5
4

Choose u s.t . N_(u)
2
2
0
6
4
4
2
2
1
1
S
2
6 
3
4
2
3
3
3
5
4
Update
2
2
0
6
4
4
2
2
1
1
2
4
2
3
3
d(4) is not changed
6
6 
3
3
5
4

Choose u s.t. N_(u)
2
2
0
6
4
4
2
2
1
1
S
2
3
4
6 6
2
3
3
3
5
4
Update
2
2
0
6
4
4
2
2
1
1
2
3
4
2
3
3
d(6) is not updated
6 6
3
5
4

Choose u s.t. N_(u)
2
2
0
6
4
4
2
2
1
1
S
2
3
4
6 6
2
3
3
There is nothing to update
3
5
4
End of Algorithm
2
2
0
6
4
4
2
2
1
1
2
3
4
6 6
2
3
3
3
5
4
All nodes are now permanent
The predecessors form a tree
The shortest path from node 1 to node 6 can be found by
tracing back predecessors
50
Kruskal’s Algorithm

Starts with each vertex in its own component.

Repeatedly merges two components into one by choosing a
light edge that connects them (i.e., a light edge crossing the cut
between them).

Scans the set of edges in monotonically increasing order by
weight.

Uses a disjoint-set data structure to determine whether an edge
connects vertices in different components.
Example 1
1
1
4
6
4
3
2
2
4
5
3
5
6
8
7
4
3
7
6
Example 3
b
5
a
3
7
c
1
-3
11
d
0
e
2
f
b
5
a
1
3
d
0
c
-3
e
f
Example 4
A cable company want to connect five villages to their
network which currently extends to the market town of
Avonford. What is the minimum length of cable needed?
5
Brinleigh
Cornwell
3
4
6
8
8
Avonford
7
Donster
Fingley
5
4
2
Edan
Ans:
B
5
All vertices have
been
connected.
C
3
6
8
The solution is
4
8
A
D
F
7
5
4
2
E
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree:
18
Kruskal’s Algorithm
:
2
8
14
21
19
25
9
17
5
13
1
Kruskal’s Algorithm
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
Sort the edges by increasing edge weight
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv
edge
dv
(D,E)
1
(B,E)
4
(D,G)
2
(B,F)
4
(E,G)
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv
(B,E)
4
2
(B,F)
4
(E,G)
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)

Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4

(B,F)
4
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
2
(E,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Accepting edge (E,G) would create a cycle
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)

Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
F
10
A
3
C
4
3
4
8
5
6
B
4
H
1
2
3
D
4
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5

(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
3
F
C
A
3
4
5
B
H
2
3
G
D
1
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5

(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Done
Total Cost =
 dv = 21
}
not
considere
d
5
A
4
6
2
C
B
2
1
3
E
3
D
2
4
F
minimum- spanning tree
A
B
2
2
C
D
1
3
E
2
F
Example 1
1
1
4
6
4
3
2
2
4
5
3
5
6
8
7
4
3
7
6


function Kruskal(G = (N, A): graph; length: A - R+ ): set of edges
{initialization}
Sort A by increasing length
n  the number of nodes in N
T  Ø {will contain the edges of the minimum spanning tree}
initialize n sets, each containing a different element of N
{greedy loop}
repeat
e  {u, v}  shortest edge not yet considered
ucomp  find(u)
vcomp  find (v)
if ucomp  vcompthen
merge(ucomp, vcomp)
T  T  {e}
until T contains n -1 edges
return T
The activity selection problem

Problem: n activities, S = {1, 2, …, n}, each activity i has
a start time si and a finish time fi, si  fi.

Activity i occupies time interval [si, fi].

i and j are compatible if sj  fi.

The problem is to select a maximum-size set of mutually
compatible activities
Example:
i
si
1
1
2
3
3
3
4
5
5
3
6
5
7
6
8
8
9
8
10
2
11
12
fi
4
5
6
7
8
9
10
11
12
13
14
The solution set = {1, 4, 8, 11}
Solution of the example:
i
1
si
1
fi
4
accept
Yes
2
3
4
5
3
0
5
3
5
6
7
8
No
No
Yes
No
7
8
9
6
8
8
10
11
12
No
Yes
No
10
2
13
No
11
12
14
Yes
Solution = {1, 4, 8, 11}
JOB SEQUENCING WITH DEADLINES
The problem is stated as below.

There are n jobs to be processed on a machine.

Each job i has a deadline di≥ 0 and profit pi≥0 .

pi is earned iff the job is completed by its deadline.

The job is completed if it is processed on a machine for unit
time.

Only one machine is available for processing jobs.

Only one job is processed at a time on the machine.
JOB SEQUENCING WITH DEADLINES (Contd..)

A feasible solution is a subset of jobs J such that each job is
completed by its deadline.

An optimal solution is a feasible solution with maximum
profit value.
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27),
(d1,d2,d3,d4) = (2,1,2,1)
JOB SEQUENCING WITH DEADLINES (Contd..)
Sr.No. Feasible
Solution
(i)
(1,2)
(ii)
(1,3)
(iii) (1,4)
(iv)
(2,3)
(v)
(3,4)
(vi)
(1)
(vii) (2)
(viii) (3)
(ix)
(4)
Processing
Sequence
(2,1)
(1,3) or (3,1)
(4,1)
(2,3)
(4,3)
(1)
(2)
(3)
(4)
Profit value
110
115
127
25
42
100
10
15
27
is the optimal one
JOB SEQUENCING WITH DEADLINES (Contd..)
Example 2 :
With n = 4 and the following values ,
(p1,p2,p3,p4) = (50,10,15,30) and (d1,d2,d3,d4) = (2,1,2,1)
Find the Optimal solution.
Example 2 :
Let n = 4 , profit vector P = (30,35,10,25) , deadline
vector D = (2,1,2,1) .Find the Optimal solution.
GREEDY ALGORITHM TO OBTAIN AN OPTIMAL
SOLUTION

Consider the jobs in the non increasing order of profits subject
to the constraint that the resulting job sequence J is a feasible
solution.

In the example considered before, the non-increasing profit
vector is
(100 27
p1
p4
15
10)
p3
p2
(2
1
2
1)
d1 d4 d3 d2
GREEDY ALGORITHM TO OBTAIN AN OPTIMAL
SOLUTION (Contd..)
J = { 1} is a feasible one
J = { 1, 4} is a feasible one with processing sequence ( 4,1)
J = { 1, 3, 4} is not feasible
J = { 1, 2, 4} is not feasible
J = { 1, 4} is optimal
The Fractional Knapsack Problem

Given: A set S of n items, with each item i having




bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with
weight at most W.
If we are allowed to take fractional amounts, then this is
the fractional knapsack problem.

In this case, we let xi denote the amount we take of item i

Objective: maximize
b (x / w )
iS

Constraint:
x
iS
i
i
W
i
i
Example

Given: A set S of n items, with each item i having



bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with
weight at most W.
“knapsack”
Solution:
• 1 ml of 5
• 2 ml of 3
• 6 ml of 4
• 1 ml of 2
Items:
1
2
3
4
5
Weight:
4 ml
8 ml
2 ml
6 ml
1 ml
Benefit:
$12
$32
$40
$30
$50
3
4
20
5
50
Value:
($ per ml)
10 ml
Greedy solution for Fractional Knapsack

Given a set of item I:
Weight
Cost
w1
c1
w2
c2
w3
c3
w4
c4

Let P be the problem of selecting items from I, with weight K,
such that the resulting cost(value) is maximum.
1.
Calculate vi = ci / wi for i=1,2,3………n.
Sort the items by decreasing vi. Let the sorted item sequence
be 1,2,3…….n, and the corresponding v and weight be vi and
wi respectely.
2.
Greedy solution for Fractional Knapsack
3. Let K be the current weight limit (initially , k = K). In each
iteration, we choose item I from the head of the unselected list.
If k>=Wi , we take item I , and k=k-wi, then consider the next
unselected item.
If k<wi, we take a fraction f of item I, i.e. only take
f=k/wi (<1) of item I, which weights exactely k. Then the
algorithm if finished.


function Kruskal(G = (N, A): graph; length: A - R+ ): set of edges
{initialization}
Sort A by increasing length
n  the number of nodes in N
T  Ø {will contain the edges of the minimum spanning tree}
initialize n sets, each containing a different element of N
{greedy loop}
repeat
e  {u, v}  shortest edge not yet considered
ucomp  find(u)
vcomp  find (v)
if ucomp  vcompthen
merge(ucomp, vcomp)
T  T  {e}
until T contains n -1 edges
return T
Prim’s Algorithm
function Prim (G= < N,A >:graph; length: A
R+ ) ;set of edges
{initialiazation}
TØ
B  { an arbitrary member of N}
while B ± N do
find e ={u,v} of minimum length such that
u B and v  N\B
T  T  {e}
B  B  {v}
return T
Dijkstra's algorithm - Pseudocode
dist[s] ←0
for all v ∈ V–{s}
do dist[v] ←∞
S←∅
Q←V
(distance to source vertex is zero)
(set all other distances to infinity)
(S, the set of visited vertices is initially empty)
(Q, the queue initially contains all
vertices)
(while the queue is not empty)
(select the element of Q with the min. distance)
(add u to list of visited vertices)
while Q ≠∅
do u ← mindistance(Q,dist)
S←S∪{u}
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v)
(if new shortest path found)
then d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist
Tower of Hanoi
There are three towers
The disks, with decreasing sizes, placed on the first tower
You need to move all of the disks from the first tower to the
second tower
Larger disks can not be placed on top of smaller disks
The third tower can be used to temporarily hold disks
a
b
c
Tower of Hanoi
a
b
c
Problem Characteristics

Is the problem decomposable?

Can solution steps be ignored or undone?

Is the universe predictable?

Is a good solution absolute or relative?

Is the solution a state or a path?

What is the role of knowledge?

Does the task require human-interaction?
Is the problem decomposable?


Can the problem be broken down to smaller problems to be
solved independently?
Decomposable problem can be solved easily.
2
(x2 + 3x + sin2x.cos x) dx
2
x dx
x3/3
3x dx
3x dx
2
2
 sin x.cos x dx
2
(1− cos x)cos2xdx
3x2/2

 cos2xdx
−  cos4xdx
Can solution steps be ignored or undone?

Ignorable (e.g. Theorem Proving), in which solution steps
can be ignored.

Recoverable (e.g. 8-puzzle), in which solution steps can be
undone.

Irrecoverable (e.g. Chess), in which solution steps can be
undone.
Is the universe predictable?

The 8-Puzzle: Every time we make a move, we know exactly
what will happen.
Certain outcome!

Playing Bridge: We cannot know exactly where all the cards
are or what the other players will do on their turns.
Uncertain outcome!
Is a good solution absolute or relative?

1. Marcus was a man
2. Marcus was a Pompeian
3. Marcus was born in 40 A.D
4. All men are mortal.
5. All Pompeians died when the volcano erupted in
79 A.D
6. No mortal lives longer than 150 years.
7. It is now 2008 A.D

Question: Is Marcus alive?







What is the role of knowledge?

Playing Chess
Knowledge is important only to constrain the search for a
solution.

Reading Newspaper
Knowledge is required even to be able to recognize a
solution.
Does the task require human-interaction?
Is the solution a state or a path?

The Water Jug Problem
• The path that leads to the goal must be reported.

A path-solution problem can be reformulated as a state-
solution problem by describing a state as a partial path to a
solution.

The question is whether that is natural or not.