Question: / / Exercise 4 . 1 . 1 6 package algs 4 1 ; import stdlib. * ; / / This is problem 4 .
Exercise
package algs;
import stdlib.;
This is problem from the textbook
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
TODO: complete the following, using only Graph and GraphGenerator.
You may copy code from other classes in algs but you should not use the classes themselves.
In particular, you may not use BreadthFirstPaths although you may copy code from there.
The "distance" from vertex v to vertex w is the length of the shortest path
from v to w
The "eccentricity" of a vertex v is distance to the farthest vertex from v
The "diameter" of a graph is the maximum eccentricity of any vertex.
The "radius" of a graph is the smallest eccentricity of any vertex.
A "center" is a vertex whose eccentricity is the radius.
The center is not necessarily unique.
public class MyGraphProperties
int eccentricity;
int diameter;
int radius;
int center;
Constructor can be linear in GV GE
public MyGraphPropertiesGraph G
this.eccentricity new intGV;
int diameter Integer.MINVALUE;
int radius Integer.MAXVALUE;
int center ;
If GV then these are the correct values.
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
You can then adjust your code so that it throws an exception in the case that all nodes are not visited
TODO
compute the eccentricity, diameter, radius and center
The center of the graph is not unique, set center to SOME center it does not matter which one
this.diameter diameter;
this.radius radius;
this.center center;
Do not change the following constant time methods
public int eccentricityint v return eccentricityv;
public int diameter return diameter;
public int radius return radius;
public int center return center;
public boolean isCenterint v return eccentricityv radius;
public static void mainString args
Graph G GraphGenerator.fromIn new IndatatinyGtxt; this is nonconnected should throw an exception
Graph G GraphGenerator.connected ; Random nonconnected graph should throw an exception
Graph G GraphGenerator.fromIn new IndatatinyCGtxt; diameter radius every node is a center
Graph G GraphGenerator.binaryTree ; A complete binary tree
Graph G GraphGenerator.path ; A path diameterV
Graph G GraphGenerator.connected ; Random connected graph
StdOut.printlnG;
GtoGraphviz gpng;
MyGraphProperties gp new MyGraphPropertiesG;
for int v ; v GV; v
StdOut.format eccentricity of d: d
v gpeccentricity v;
StdOut.format diameterd radiusd centerd
gpdiameter gpradius gpcenter ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
