graph - Runtime error in java code -
i'm creating program assignment takes input system.in in 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 first line persons, numbers friendships.
the program checks whether or not people listed in last line in "close friendship", they're friends.
i represent network incident matrix, , passes every test on test site use, except one, fails "runtime error". know it's not exception, because catching exceptions nothing error, while catching errors does.
here's code:
public class closefriends { // contains edges represented incident matrix private static boolean edges[][]; // adds directed edge between v1 , v2 // method sorts edges reduce space used public static void addedge(int v1, int v2) { if (v1 > v2) { edges[v1][v2] = true; } else { edges[v2][v1] = true; } } // creates graph v vertices public static void createvertices(int v) { edges = new boolean[v][v]; } // checks if edge exists between v1 , v2 public static boolean isfriends(int v1, int v2) { if (v1 > v2) { return edges[v1][v2]; } else { return edges[v2][v1]; } } // checks if arraylist of vertices close friends public static void closefriends(arraylist<integer> vertices) { int count = 0; int size = vertices.size(); (int = 0; < size; i++) { (int j = + 1; j < size; j++) { if (isfriends(vertices.get(i), vertices.get(j))) { count++; } } } // clique should contain n*(n-1)/2 edges 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, , create amount int v = 0; while (st.hasmoretokens()) { st.nexttoken(); v++; } createvertices(v); arraylist<integer> friends = new arraylist<integer>(); // while system.in has read while (in.ready()) { // read line , add edges based on input, or run 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!
Comments
Post a Comment