0% found this document useful (0 votes)
41 views7 pages

CS702: Advanced Algorithm Analysis and Design: Ms120400020 Question No. 1

The document contains the solution to two graph problems using breadth-first search and depth-first search on a sample graph: 1) For breadth-first search, it shows the dist (distance) and pred (predecessor) values at each step, with a distance of 0-3 and predecessors a, b, d. 2) For depth-first search, it again shows the dist and pred values at each step, with distances of 0-4 and predecessors a, b, d, f, g.

Uploaded by

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

CS702: Advanced Algorithm Analysis and Design: Ms120400020 Question No. 1

The document contains the solution to two graph problems using breadth-first search and depth-first search on a sample graph: 1) For breadth-first search, it shows the dist (distance) and pred (predecessor) values at each step, with a distance of 0-3 and predecessors a, b, d. 2) For depth-first search, it again shows the dist and pred values at each step, with distances of 0-4 and predecessors a, b, d, f, g.

Uploaded by

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

CS702: Advanced Algorithm Analysis and Design

Assignment No.3
Ms120400020
QUESTION No. 1

If X= <A, B, C, A, C, B, A, C> and Y= <C, A, B, A, B, C, A, B, A>.

c(1,1)=max(c(0,1),c(1,0))=max(0,0)=0
b[1,1]=
c(1,2)=c(0,1)+1=0+1=1
b[1,2]=
c(1,3)=max(c(0,3),c(1,2))=max(0,1)=1
b[1,3]=
c(1,4)= c(0,3)+1=0+1=1
b[1,4]=
c(1,5)=max(c(0,5),c(1,4))=max(0,1)=1
b[1,5]=
c(1,6)=max(c(0,6),c(1,5))=max(0,1)=1
b[1,6]=
c(1,7)= c(0,6)+1=0+1=1
b[1,7]=
c(1,8)=max(c(0,8),c(1,7))=max(0,1)=1
b[1,8]=
c(1,9)= c(0,8)+1=0+1=1
b[1,9]=
c(2,1)=max(c(1,1),c(2,0))=max(0,0)=0
b[2,1]=
c(2,2)=max(c(1,2),c(2,1))=max(1,0)=1
b[2,2]=
c(2,3)= c(1,2)+1=1+1=2
b[2,3]=
c(2,4)=max(c(1,4),c(2,3))=max(1,2)=2
b[2,4]=

c(2,5)= c(1,4)+1=1+1=2
b[2,5]=
c(2,6)=max(c(1,6),c(2,5))=max(1,2)=2
b[2,6]=
c(2,7)=max(c(1,7),c(2,6))=max(1,2)=2
b[2,7]=
c(2,8)= c(1,7)+1=1+1=2
b[2,8]=
c(2,9)=max(c(1,9),c(2,8))=max(1,2)=2
b[2,7]=
c(3,1)= c(2,0)+1=0+1=1
b[3,1]=
c(3,2)=max(c(2,2),c(3,1))=max(1,1)=1
b[3,2]=
c(3,3)=max(c(2,3),c(3,2))=max(2,1)=2
b[3,3]=
c(3,4)=max(c(2,4),c(3,3))=max(2,2)=2
b[3,4]=
c(3,5)=max(c(2,5),c(3,4))=max(2,2)=2
b[3,5]=
c(3,6)= c(2,5)+1=2+1=3
b[3,6]=
c(3,7)=max(c(2,6),c(3,6))=max(2,3)=3
b[3,7]=
c(3,8)=max(c(2,8),c(3,7))=max(2,3)=3
b[3,8]=
c(3,9)=max(c(2,9),c(3,8))=max(2,3)=3
b[3,9]=
c(4,1)=max(c(3,1),c(4,0))=max(1,0)=1
b[4,1]=
c(4,2)= c(3,1)+1=1+1=2
b[4,2]=

c(4,3)=max(c(3,3),c(4,2))=max(2,2)=2
b[4,3]=
c(4,4)= c(3,3)+1=2+1=3
b[4,4]=
c(4,5)=max(c(3,5),c(4,4))=max(2,3)=3
b[4,5]=
c(4,6)=max(c(3,6),c(4,5))=max(3,3)=3
b[4,6]=
c(4,7)= c(3,6)+1=3+1=4
b[4,7]=
c(4,8)=max(c(3,8),c(4,7))=max(3,4)=4
b[4,8]=
c(4,9)= c(3,8)+1=3+1=4
b[4,9]=
c(5,1)= c(4,0)+1=0+1=1
b[5,1]=
c(5,2)=max(c(4,2),c(5,1))=max(2,1)=2
b[5,2]=
c(5,3)=max(c(4,3),c(5,2))=max(2,2)=2
b[5,3]=
c(5,4)=max(c(4,4),c(5,3))=max(3,2)=3
b[5,4]=
c(5,5)=max(c(4,5),c(5,4))=max(3,3)=3
b[5,5]=
c(5,6)= c(4,5)+1=4+1=4
b[5,6]=
c(5,7)=max(c(4,7),c(5,6))=max(4,4)=4
b[5,7]=
c(5,8)=max(c(4,8),c(5,7))=max(4,4)=4
b[5,8]=
c(5,9)=max(c(4,9),c(5,8))=max(4,4)=4
b[5,9]=

c(6,1)=max(c(5,1),c(6,0))=max(1,0)=1
b[6,1]=
c(6,2)=max(c(5,2),c(6,1))=max(2,1)=1
b[6,2]=
c(6,3)= c(5,2)+1=2+1=3
b[6,3]=
c(6,4)=max(c(5,4),c(6,3))=max(3,3)=3
b[6,4]=
c(6,5)= c(5,4)+1=3+1=4
b[6,5]=
c(6,6)=max(c(5,6),c(6,5))=max(4,4)=4
b[6,6]=
c(6,7)=max(c(5,7),c(6,6))=max(4,4)=4
b[6,7]=
c(6,8)= c(5,7)+1=4+1=5
b[6,8]=
c(6,9)=max(c(5,9),c(6,8))=max(4,5)=5
b[6,9]=
c(7,1)=max(c(6,1),c(7,0))=max(1,0)=1
b[7,1]=
c(7,2)= c(6,1)+1=1+1=2
b[7,2]=
c(7,3)=max(c(6,3),c(7,2))=max(3,2)=3
b[7,3]=
c(7,4)= c(6,3)+1=3+1=4
b[7,4]=
c(7,5)=max(c(6,5),c(7,4))=max(4,4)=4
b[7,5]=
c(7,6)=max(c(6,6),c(7,5))=max(4,4)=4
b[7,6]=
c(7,7)= c(6,6)+1=4+1=5
b[7,7]=

c(7,8)=max(c(6,8),c(7,7))=max(5,5)=5
b[7,8]=
c(7,9)= c(6,8)+1=5+1=6
b[7,9]=
c(8,1)=max(c(7,1),c(8,0))=max(1,0)=1
b[8,1]=
c(8,2)=max(c(7,2),c(8,1))=max(2,1)=1
b[8,2]=
c(8,3)= c(7,2)+1=2+1=3
b[8,3]=
c(8,4)=max(c(7,4),c(8,3))=max(4,3)=4
b[8,4]=
c(8,5)= c(7,4)+1=4+1=5
b[8,5]=
c(8,6)=max(c(7,6),c(8,5))=max(4,5)=5
b[8,6]=
c(8,7)=max(c(7,7),c(8,6))=max(5,5)=5
b[8,7]=
c(8,8)= c(7,7)+1=5+1=6
b[8,8]=
c(8,9)=max(c(7,9),c(8,8))=max(6,6)=6
b[8,9]=

If X= <A, B, C, A, C, B, A, C> and Y= <C, A, B, A, B, C, A, B, A>.

C A B A B C A B A
0 0 0 0 0 0 0 0 0 0
A 0 0 1 1 1 1 1 1 1 1
B 0 0 1 2 2 2 2 2 2 2
C 0 1 1 2 2 2 3 3 3 3
A 0 1 2 2 3 3 3 4 4 3
C 0 1 2 2 3 3 4 4 4 4
B 0 1 2 3 3 4 4 4 5 5
A 0 1 2 3 4 4 4 5 5 6
C 0 1 2 3 4 5 5 5 6 6

If X= <A, B, C, A, C, B, A, C> and Y= <C, A, B, A, B, C, A, B, A>.

C A B A B C A B A

LCS = ABABAB
Question No. 2 (25 Points)

a) Show the dist (distance) and pred (predecessor) values that result from running breadth-
first search on the graph below, using vertex a as the source.

b) Show the dist (distance) and pred (predecessor) values that result from running depth-
first search on the graph below, using vertex a as the source.

b d f

c e g

Sol:

Q
b d f
A prec
nil

c e g a

b prec
a

Q bb
1 d f
A prec
nil

1 e g b c

c prec 1 1
a
b prec d prec
a b
Q bb
1 2 f
A prec
nil

1 e g c d

C prec 1 2
a

f prec
b prec d prec d
a b
Q
1 2 f
A prec
nil

1 e g d

c prec 2
a

b prec a d prec b f prec d


Q
1 2 3
A prec
nil

1 e g f

c prec 3
a

b prec a d prec b f prec d

Q
1 2 3
A prec
nil

1 e 4 g

c prec g prec
4
a f

Node Pred dist


a Nil 0
B A 1
C A 1
D B 2
F D 3
G F 4
E Nil infinity

You might also like