0

I'm creating a program for an assignment which takes an input from System.in in the following format:

Inky Pinky Blinky Clyde Luigi Mario Bowser
0 2
1 2
5 6
3 5
2 4
4 5
2 3
1 4
closefriends 1 2 4

where the first line is persons, the numbers are friendships.

The program checks whether or not the people listed in the last line are in a "close friendship", where they're all friends.

I represent the network as an incident matrix, and it passes every test on the test site we use, except one, which fails with a "Runtime Error". I know it's not an exception, because catching all exceptions does nothing to the error, while catching all errors does.

Here's the code:

public class CloseFriends {
    // Contains edges represented as an incident matrix
    private static boolean edges[][];

    // Adds a directed edge between v1 and v2
    // The method sorts the edges to reduce space used 
    public static void addEdge(int v1, int v2) {
        if (v1 > v2) {
            edges[v1][v2] = true;
        }
        else {
            edges[v2][v1] = true;
        }
    }

    // Creates a graph with V vertices
    public static void createVertices(int V) {
        edges = new boolean[V][V];
    }

    // Checks if an edge exists between v1 and v2
    public static boolean isFriends(int v1, int v2) {
        if (v1 > v2) {
            return edges[v1][v2];
        }
        else {
            return edges[v2][v1];
        }
    }

    // Checks if an ArrayList of vertices is close friends
    public static void closeFriends(ArrayList<Integer> vertices) {
        int count = 0;
        int size = vertices.size();

        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (isFriends(vertices.get(i), vertices.get(j))) {
                    count++;
                }
            }
        }
        // The clique should contain n*(n-1)/2 edges for everyone to be connected
        if (count == (size * (size - 1) / 2)) {
            System.out.println("yes");
        }
        else {
            System.out.println("no");
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(in.readLine());

        // Count vertices, and create that amount
        int V = 0;
        while (st.hasMoreTokens()) {
            st.nextToken();
            V++;
        }
        createVertices(V);

        ArrayList<Integer> friends = new ArrayList<Integer>();

        // While System.in has something to be read
        while (in.ready()) {
            // Read the line and add edges based on input, or run the algorithm
            String edge = "";

            edge = in.readLine();

            if (edge != null) {
                st = new StringTokenizer(edge);
                if (!edge.startsWith("closefriends")) {
                    addEdge(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                }
                else {
                    st.nextToken();
                    while (st.hasMoreTokens()) {
                        friends.add(Integer.parseInt(st.nextToken()));
                    }
                }
            }
        }
        closeFriends(friends);
    }
}

Thanks!

0 Answers0