Can someone help me solve this problem from Algorithhms 4th Edition by Robert Se
ID: 3606645 • Letter: C
Question
Can someone help me solve this problem from Algorithhms 4th Edition by Robert Sedgewick, Kevin Wayne.
4.1.16 The eccentricity of a vertex v is the the length of the shortest path from that ver- tex to the furthest 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. Implement the following API: public class GraphProperties GraphProperties (Graph G) constructor (exception ifG not connected) int eccentricity(int v) int diameterO int radius () int centerO eccentricity of v diameter of G radius of G a center ofGExplanation / Answer
import java.util. *;
public class GraphProperties {
int [] distTo; // the distance to the vertex
int [] e; // eccentricity
int [] c; // cycle
int [] edgeTo; // predecessors
int diameter = 0;
int radius = Integer.MAX_VALUE;
int center;
GraphProperties (Graph G) {
int V = G.V ();
distTo = new int [V];
e = new int [V];
c = new int [V];
edgeTo = new int [V];
// traverse all the vertices and make v trees
for (int v = 0; v <V; v ++) {
bfp (g, v);
}
/ / Find the maximum and minimum, each looking for the mother, the value of each Fu
for (int v = 0; v <V; v ++) {
diameter = e [v]> diameter? e [v]: diameter;
if (e [v] <radius) {
radius = e [v];
center = v;
}
}
}
// the main algorithm
void bfp (Graph G, int s) {
int eccen = 0;
int cycle = Integer.MAX_VALUE;
for (int v = 0; v <G.V (); v ++)
distTo [v] = -1; // initialize to 0 to indicate that it has not been found
distTo [s] = 0;
Queue <Integer> q = new LinkedList <Integer> ();
q.offer (s);
while (! q.isEmpty ()) {
int v = q.poll ();
for (int w: G.adj (v)) {// v is me, w is my ex-girlfriend
if (distTo [w] == -1) {// if not found
distTo [w] = distTo [v] + 1; // record distance
eccen = distTo [w]; // this is the depth-first algorithm, so the last depth of the search is the deepest
} encounter a wive of w, it shows that the hands of his wife now met the former girlfriend, life rounded around the formation of a ring (provided that you encounter the case of wu = edgeTo [v]) / To not your wife)
int d = distTo [w] + distTo [v] + 1; / / count about how much around a circle
cycle = d <cycle? d: cycle; // record the smallest circle, take the smallest routine
}
}
// diameter of the diameter of the graph: the maximum value of the eccentricity of all points in the figure.
public int diameter () {
return diameter
}
// radius of G The radius of the graph: the minimum value of the eccentricity of all points in the graph.
public int radius () {
return radius
}
// center of the center of the graph: the center of the center of the eccentricity of the vertices.
public int center () {
return center
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.