我目前难以从哪里开始做家庭作业。基本上,我们已经获得了一个函数和一些广度优先搜索代码。目标是计算上面列出的东西,他强烈建议使用BFS代码来实现目标。任何帮助都表示赞赏:
package algs41;
import stdlib.*;
// This is problem 4.1.16 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)
public class MyGraphProperties {
int[] eccentricity;
int diameter;
int radius;
int center;
// Constructor can be linear in G.V() * G.E()
public MyGraphProperties(Graph G) {
this.eccentricity = new int[G.V()];
int diameter = Integer.MIN_VALUE;
int radius = Integer.MAX_VALUE;
int center = -1;
// If G.V()==0, 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 0)
// 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 eccentricity(int v) { return eccentricity[v]; }
public int diameter() { return diameter; }
public int radius() { return radius; }
public int center() { return center; }
public boolean isCenter(int v) { return eccentricity[v] == radius; }
以下是相关的BFS代码:
private void bfs(Graph G, int s) {
Queue<Integer> q = new Queue<>();
for (int v = 0; v < G.V(); v++) distTo[v] = INFINITY;
distTo[s] = 0;
marked[s] = true;
q.enqueue(s);
while (!q.isEmpty()) {
int v = q.dequeue();
for (int w : G.adj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
}
目前不存在某些事物(例如distTo)。这是因为不允许使用我们给出的BreadthFirstSearch类,但我们可以复制并通过相关代码供我们自己使用。 请尽可能少地编辑bfs void。根据教授的说法,我所提供的bfs就是我们所需要的如上所述,我不确定从哪里开始。任何帮助表示赞赏。
编辑:添加了图表代码
public class Graph {
private final int V;
private int E;
private final Bag<Integer>[] adj;
/**
* Create an empty graph with V vertices.
*/
@SuppressWarnings("unchecked")
public Graph(int V) {
if (V < 0) throw new Error("Number of vertices must be nonnegative");
this.V = V;
this.E = 0;
this.adj = new Bag[V];
for (int v = 0; v < V; v++) {
adj[v] = new Bag<>();
}
}
/**
* Create a random graph with V vertices and E edges with no parallel edges or self loops.
* Expected running time is proportional to V + E.
*/
public Graph(int V, int E) { this (V, E, false); }
/**
* Create a random graph with V vertices and E edges.
* Expected running time is proportional to V + E.
*/
public Graph(int V, int E, boolean allowParallelEdgesAndSelfLoops) {
this(V);
if (E < 0) throw new Error("Number of edges must be nonnegative");
if (allowParallelEdgesAndSelfLoops) {
for (int i = 0; i < E; i++) {
int v = (int) (Math.random() * V);
int w = (int) (Math.random() * V);
addEdge(v, w);
}
} else {
if (E > V*(V-1)/2) throw new Error("Number of edges must be less than V*(V-1)/2");
newEdge: while (E>0) {
int v = (int) (Math.random() * V);
int w = (int) (Math.random() * V);
if (v == w) continue;
for (int w2 : adj[v])
if (w == w2)
continue newEdge;
addEdge(v, w);
E--;
}
}
}
/**
* Create a digraph from input stream.
*/
public Graph(In in) {
this(in.readInt());
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
addEdge(v, w);
}
}
/**
* Copy constructor.
*/
public Graph(Graph G) {
this(G.V());
this.E = G.E();
for (int v = 0; v < G.V(); v++) {
// reverse so that adjacency list is in same order as original
Stack<Integer> reverse = new Stack<>();
for (int w : G.adj[v]) {
reverse.push(w);
}
for (int w : reverse) {
adj[v].add(w);
}
}
}
/**
* Return the number of vertices in the graph.
*/
public int V() { return V; }
/**
* Return the number of edges in the graph.
*/
public int E() { return E; }
/**
* Add the undirected edge v-w to graph.
* @throws java.lang.IndexOutOfBoundsException unless both 0 <= v < V and 0 <= w < V
*/
public void addEdge(int v, int w) {
if (v < 0 || v >= V) throw new IndexOutOfBoundsException();
if (w < 0 || w >= V) throw new IndexOutOfBoundsException();
E++;
adj[v].add(w);
adj[w].add(v);
}
/**
* Return the list of neighbors of vertex v as in Iterable.
* @throws java.lang.IndexOutOfBoundsException unless 0 <= v < V
*/
public Iterable<Integer> adj(int v) {
if (v < 0 || v >= V) throw new IndexOutOfBoundsException();
return adj[v];
}