Question: I need help with calculating whether or not a graph is connected or not. As it stands now, I know all the code I have
I need help with calculating whether or not a graph is connected or not. As it stands now, I know all the code I have works, but I don't have the check for connectivity coded yet. It is supposed to throw a new IllegalArgumentException. I also know that the hasPathto code works, but I do not know how to implement it/how to get it to correctly figure out if something is marked. As it stand, a node is marked if it can be reached, and therefore is connected. My code:

// You need only change the constructor // You can also change the main method // But you should not change eccentricity), diameter, radius(), center) or iscenter) // You can (and should!) add more private methods (such as bfs or dfs) public class MyGraphPropertiesf int[] eccentricity; int diameter int radius; int center; boolean[] marked; // Constructor can be linear in G.VO G.E) public MyGraphProperties(Graph G)f this . eccentricity = new int[GV()]; int diameter = Integer.MIN VALUE; int radius = Integer.MAX VALUE; int center -1; marked ne boolean[GM)]; int s e; // If G.VC)-=e, then these are the correct values. I If G is not connected, you should throw a new // IllegalArgumentException) // I suggest that you first get it to work for a connected graph // This will require that you traverse the graph starting from some node // (say e) // You can then adjust your code so that it throws an exception in the // case that all nodes are not visited for (int v = 0; v eccentricity[v]) center = v; diameter eccentricity[v]; = radius eccentricity[v]; = this . diameter this . radius = this . center = = diameter; radius; center; private boolean hasPathTo(int v) f return marked v];) private int bfs (Graph G, int s) t Queue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
