how widen between twin many widely distant nodes
i'm operative by before years acm programming enemy problems perplexing improved during clarify graph problems.
the i'm operative i'm given an erratic array undirected graph nodes, neighbors distances edges joining nodes. i need widen between twin farthest nodes eachother (the weight distance, # nodes away).
now, i have dijkstra's algorithm form of:
// dijkstra's single-source algorithm
private int cheapest(double[] distances, boolean[] visited)
{
int best = -1;
(int i = 0; i < size(); i++)
{
(!visited[i] && ((best < 0) || (distances[i] < distances[best])))
{
best = i;
}
}
relapse best;
}
// dijkstra's continued
public double[] distancesfrom(int source)
{
double[] outcome = new double[size()];
java.util.arrays.fill(result, double.positive_infinity);
result[source] = 0; // 0 widen itself
boolean[] visited = new boolean[size()];
(int i = 0; i < size(); i++)
{
int node = cheapest(result, visited);
visited[node] = true;
(int j = 0; j < size(); j++)
{
result[j] = math.min(result[j], result[node] + getcost(node, j));
}
}
relapse result;
}
with doing i give sole node give me list distances node. so, i squeeze largest widen list distances nonetheless i can't certain any sole node twin farthest ones during presumably end.
so wholly fortitude i cruise run dijkstra's algorithm each node, by any returned list distances looking largest distance. after intense any node returning it's list distances i should have value largest widen between any twin nodes (the "road" widen between twin many widely seperated villages). there got an easier proceed since seems unequivocally computationally expensive. problem says there illustration inputs adult 500 nodes i wouldn't wish take prohibitively long. i should it?
here illustration quarrel problem:
total nodes: 5
edges:
nodes 2 - bond - node 4. distance/weight 25
nodes 2 - bond - node 5. distance/weight 26
nodes 3 - bond - node 4. distance/weight 16
nodes 1 - bond - node 4. distance/weight 14
the answer illustration quarrel "67 miles". length highway between twin many widely distant villages.
so should i i described there many easier many reduction computationally costly way?
Comments
Post a Comment