You are on page 1of 2

Machine Exercise 7

Depth First Search (DFS) and Breadth First Search (BFS)

Problem Statement:

You are in your dormitory, chilling and resting, when suddenly a friend contacts you. "Hey, I'm in EEE.
I need to finish this ME for EEE13 before 5:30PM. Help ASAP." As any good programmer, you refuse
to stand up for any work you can do sitting down, and you refuse to go to EEE if you can simply help
out from the comfort of your dormitory.

Of course you don't do the ME for him. You do the honest thing and simply guide your friend through
completing his work so far. However, you quickly find out that trying to explain how everything works
over facebook is inefficient, and you thought of the next obvious thing to do: hack into his computer and
give him an interactive tutorial session!

For whatever reason, his computer needs to be connected to your laptop through some series of
connections via ethernet cables. You asked your friend to figure out what are the direct connections
between computers, and he gives you info one at a time. Given the information that your friend has
provided so far, you need to figure out (a) whether your laptop and his computer are connected, and (b)
if it is connected, what is the fewest number of computers that you will need to go through.

For our problem, we can consider two computers A and B to be directly connected if there is an ethernet
cable connecting A and B directly. We also define two computers A and B to be connected when either
A and B are directly connected or when there is a computer C wherein A and C are connected and B and
C are connected (a recursive definition!).

Input Description:

The first line of the input contains two integers, N and Q. N denotes the number of computers that are in
the network we are considering. Each computer is denoted by an integer from 1 to N. Your friend's
computer is computer 1, while your laptop is computer N. Q denotes the number of queries that will
follow. Q lines will follow, each line denoting a query.

There are three types of queries, and they are indicated by the first integer in the line. Type 1 queries are
of the form "1 A B" and describes a direct connection between computers A and B. Type 2 and 3 queries
are described in the Output Description.

Output Description:

For each type 2 query, you must output "CONNECTED" in a single line if computers 1 and N are
connected, otherwise output "NOT CONNECTED". For each type 3 query, output the fewest number of
computers in between a path from 1 to N. Note that the count should not include computers 1 and N. If
there is no such path, output "INFINITY".
Sample Input:


Sample Output:



1 <= N <= 105

Number of type 1 queries will not exceed N*(N-1)/2
Number of type 2 and 3 queries combined will not exceed 103
No type 1 query will denote a connection for two computers that already have a direct connection


Week 1: Connectivity Checking (DFS)

Type 1 and Type 2 queries must be working.
Week 2: Connectivity and Distance Checking (BFS)
Type 3 queries must also be implemented.

Grading Criteria:

10% - Interview
30% - DFS
- 10% for 1 <= N <= 10
- 10% for 1 <= N <= 103
- 10% for 1 <= N <= 105
60% - BFS
- 20% for 1 <= N <= 10
- 20% for 1 <= N <= 103
- 20% for 1 <= N <= 105