Foundation of Algorithms Assignment 4 Chinmay Kulkarni (ck1166)
Foundation of Algorithms Assignment 4 Chinmay Kulkarni (ck1166)
2.b. The expected time to lookup that an edge is O(1). The disadvantage of having Hashtable is it
will require more space i.e. memory than linked list. This problem can be solved by using Binary
search tree instead of Hashtable. But the disadvantage with Binary search tree has lookup time
for an edge as O(log n).
3.a.
If we take u vertex as source node and perform BFS, the values are as follows:
Vertex
d
u
TT
0
3.b. As in the code it can be seen that the initial color of the node is WHITE. When the node is
enqueued in the visited queue, the color of node is changed to GRAY just to show that it is in the
queue and once its all adjacent vertices are visited, the node is dequeued from queue and finally
the color of node is changed to BLACK.
But in the code we can clearly see that there is no comparison between and GRAY and BLACK
color node, and hence it doesnt matter if we remove line 5 and 14 in the code, and we can have a
single color to suffice visited node. It is redundant to have 2 colors notifying that a node is
visited, and moreover the code only checks if a node is WHITE or not.
3.c. The value u.d assigned for u is independent of adjacency list because u.d is a function of
u.d = (s,u), and the BFS algorithm doesnt assume that adjacency list is in some order.
In fig 22.3 lets assume a scenario where, in the adjacency list of w, x is ahead of t and in
adjacency list of w, u is ahead of y, then the BFS algorithm will choose the path through the (x-u)
edge instead of (t-u) edge and hence BFS path is changed. Therefore, we can say that BFS
depends on ordering in adjacency list.
4.
DFS_STACK (G, s)
for each vertex u belongs to G other than s
{
u.color = WHITE
u.pred = NIL
}
time = 0
// time
S = EMPTY
// empty stack
time = time + 1
s.discovery_time = time
s.color = GRAY PUSH(S, s)
{
t = POP(S)
time = time + 1
v.finish_time= time
v.color = BLACK
}
}
A1
A1
A2
A3
A4
A5
A6
A2
A3
A4
A5
x
A6
x
x
x
x
x
x
m[1,2] = p0xp1xp2
= 5x10x3
= 150
m[2,3] = p1xp2xp3
= 10x3x12
= 360
m[3,4] = p2xp3xp4
= 3x12x5
= 180
m[4,5] = p3xp4xp5
= 12x5x50
= 3000
m[5,6] = p4xp5xp6
= 5x50x6
= 1500
A1
A2
A3
A4
A5
A6
A1
150
A2
360
A3
180
A4
A5
x
A6
x
x
x
x
x
x
3000
0
1500
A1
A2
A3
A4
A5
A6
A1
150 (1)
330(2)
405(2)
1655 (4)
A2
360(2)
330(2)
2430(4)
A3
180(3)
930(2)
A4
2010(2)
1950(2)
1770(4)
0
3000(4)
1860(4)
A5
1500(5)
A6
Base case:
For n=1,
Induction hypothesis:
(A1,A2,A3Aj)(Aj+1,Aj+2.Ak+1)
S1
s2
6.b.
6.c. The recurrence for maximum scalar multiplication can be given as:
m[i,j] = [ 0
if i=j ]
[ max [m(i,k) + m(k+1,j) + {rows(si)}* rows(sk)}* rows(sj)
if i<j ]