You are on page 1of 3

Solution

1.1 Source in a directed graph is a node that has no incoming edge. So, the sources in above graph
are A and B. Sink in a directed graph is a node that has no outgoing edge. So, the sinks in above
graph are J, G and I.
1.2 One linearization of this graph could be A B C D E F G H I.
1.3 We can count the number of linearization through the following algorithm
1) We can first initialize all vertices as unvisited.
2) We can now choose vertex which is unvisited and has zero indegree and decrease indegree
of all those vertices by 1 (corresponding to removing edges) , now we will add this vertex to
the result and call the recursive function again and backtrack
3) After returning from function, we can reset the values of visited, result and indegree.

2.1

The dfs order traversal of the above graph as per condition (we have to pick a vertex which is
alphabetically first, when given the choice ) – A B C D E F G H I.

Pre and post number order of each vertex would be

A – [1,16]

B – [2,13]

C – [3,10]

D – [4,5]

E – [6,9]

F – [7,8]

G – [11,12]

H– [14,15]

2.2
The meta-graph of the above graph is as follows:

There are five connected components.

The vertices in the first connected component are: A

The vertices in the second connected component are: B C E F

The vertices in the third connected component are: H

The vertices in the fourth connected component are: G

The vertices in the fifth connected component are: D

a) Let G = (V, E) be our (directed) graph that we will be using in our approach to solution of the
above problem. As given in the problem, we start with bill of 1 dollar which will print two bills 1 and
2, and then we can give these bills to machines to print more bills and we can continue to repeat this
process. But we know that for every dollar d, we give to the machine, we can print two bills with one
being equal to square of d modulo 400 and other being square of d + 1 modulo 400.As we can see
using the above process, we can’t get a bill with value greater than Or equal to 400 as we know that
when we take modulo of number by 400 , the result would be in the range [0,399]. This, the set of
nodes in our graph shall be the integers modulo 400, representing values of bills the machine might
print: V = {0,1,2…… 399}. An edge between two nodes v0 and v1 exists if the machine is willing to
print bills of value v1 when given balls of value v0. Now the question remains whether there is a
path from node 1 to node 20 because then only we can know that if we can print bill of 20 dollars by
repeating this entire process in each step starting from node 1.

b) Given the above analysis, we have reduced our problem to finding a path between node 1 and
node 20 in the graph as we have described above. Now we can easily solve this by using depth first
search algorithm. In depth first search algorithm, we pick a node and start traversing in it’s complete
depth. For each node, we find it’s neighbour which has not been visited and visit it and this recursive
process goes on. To make sure, this recursion does not go on infinitely, we keep a visited array
marking nodes as they are being visited. For a give n node, when all it’s neighbours have been
visited, we return it’s parent. The depth first search ends automatically when all the nodes have
been visited. Now, to see whether node 1 is connected to Node 20 or not, we can apply depth first
search on node 1 and if during the search, we find node 20 we can conclude that bill of dollar 20 can
be printed and thus we can end our search but if after all connected components of 1 are visited
that is the dfs has ended and we still haven’t found 20, we can conclude that bill of dollar 20 can’t be
printed.

You might also like