Skip to content

Commit

Permalink
Merge branch 'master' of github.com:graphstream/gs-algo
Browse files Browse the repository at this point in the history
  • Loading branch information
gsavin committed May 5, 2011
2 parents 6c0e60a + 44d30c3 commit 953b40a
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 76 deletions.
53 changes: 25 additions & 28 deletions src/org/graphstream/algorithm/APSP.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
* one edge, one "pass-by" node. As all shortest path that is made of more than
* one edge is necessarily made of two other shortest paths, it is easy to
* reconstruct a shortest path between two arbitrary nodes knowing only a
* pass-by node.
* pass-by node. This approach still stores a lot of data on the graph, however
* far less than if we stored complete paths.
* </p>
*/
public class APSP extends SinkAdapter implements Algorithm {
Expand Down Expand Up @@ -251,8 +252,8 @@ public void compute() {
// The Floyd-Warshall algorithm. You can easily see it is in O(n^3)..

int z = 0;
float prog = 0;
float max = nodeList.size();
double prog = 0;
double max = nodeList.size();
max *= max;

for (Node k : nodeList) {
Expand All @@ -265,14 +266,14 @@ public void compute() {
APSPInfo K = (APSPInfo) k.getAttribute(
APSPInfo.ATTRIBUTE_NAME, APSPInfo.class);

float Dij = I.getLengthTo(J.source.getId());
float Dik = I.getLengthTo(K.source.getId());
float Dkj = K.getLengthTo(J.source.getId());
double Dij = I.getLengthTo(J.source.getId());
double Dik = I.getLengthTo(K.source.getId());
double Dkj = K.getLengthTo(J.source.getId());

// Take into account non-existing paths.

if (Dik >= 0 && Dkj >= 0) {
float sum = Dik + Dkj;
double sum = Dik + Dkj;

if (Dij >= 0) {
if (sum < Dij) {
Expand All @@ -291,7 +292,7 @@ public void compute() {
}

z++;
// System.err.printf( "%3.2f%%%n", (z/((float)n))*100 );
// System.err.printf( "%3.2f%%%n", (z/((double)n))*100 );
}
}

Expand All @@ -313,14 +314,16 @@ public static class APSPInfo {
/**
* Maximum number of hops to attain another node in the graph from the
* "from" node.
* XXX this is the maximum value seen during compute not the maximum shortest path XXX
*/
public float maxLength;
public double maxLength = Double.MIN_VALUE;

/**
* Minimum number of hops to attain another node in the graph from the
* "from" node.
* XXX this is the minimum value seen during compute not the minimum shortest path XXX
*/
public float minLength;
public double minLength = Double.MAX_VALUE;

/**
* Shortest paths toward all other accessible nodes.
Expand All @@ -340,7 +343,7 @@ public static class APSPInfo {
* If false, the edge orientation is not taken into account.
*/
public APSPInfo(Node node, String weightAttributeName, boolean directed) {
float weight = 1;
double weight = 1;
Iterable<? extends Edge> edges = node.getLeavingEdgeSet();

source = node;
Expand All @@ -352,7 +355,7 @@ public APSPInfo(Node node, String weightAttributeName, boolean directed) {
Node other = edge.getOpposite(node);

if (edge.hasAttribute(weightAttributeName))
weight = (float) edge.getNumber(weightAttributeName);
weight = edge.getNumber(weightAttributeName);

targets.put(other.getId(), new TargetPath(other, weight, null));
}
Expand All @@ -376,7 +379,7 @@ public String getNodeId() {
* @return The distance or -1 if no path is stored yet between the two
* nodes.
*/
public float getLengthTo(String other) {
public double getLengthTo(String other) {
if (targets.containsKey(other))
return targets.get(other).distance;

Expand All @@ -388,7 +391,7 @@ public float getLengthTo(String other) {
*
* @return A distance.
*/
public float getMinimumLength() {
public double getMinimumLength() {
return minLength;
}

Expand All @@ -397,7 +400,7 @@ public float getMinimumLength() {
*
* @return A distance.
*/
public float getMaximumLength() {
public double getMaximumLength() {
return maxLength;
}

Expand All @@ -410,7 +413,7 @@ public float getMaximumLength() {
* @param length
* The new minimum path lengths between these nodes.
*/
public void setLengthTo(APSPInfo other, float length, APSPInfo passBy) {
public void setLengthTo(APSPInfo other, double length, APSPInfo passBy) {
targets.put(other.source.getId(), new TargetPath(other.source,
length, passBy));

Expand Down Expand Up @@ -473,12 +476,9 @@ protected int expandPath(int pos, APSPInfo source, TargetPath path,
// We build paths between A and X and between X and B.

TargetPath path1 = source.targets.get(path.passBy.source
.getId()); // path from A -> X
TargetPath path2 = path.passBy.targets.get(path.target.getId()); // path
// from
// X
// ->
// B
.getId()); // path from A -> X
TargetPath path2 = path.passBy.targets.get(path.target.getId());
// path from X -> B

// Now we recurse the path expansion.

Expand Down Expand Up @@ -516,9 +516,6 @@ protected int expandPath(int pos, APSPInfo source, TargetPath path,
* always made of the sum of two shortest paths, and knowing only one
* "pass-by" node in the shortest path, it is possible to rebuild it).
* </p>
*
* @author Antoine Dutot
* @since 2007
*/
public static class TargetPath {
/**
Expand All @@ -529,15 +526,15 @@ public static class TargetPath {
/**
* The distance to this other node.
*/
public float distance;
public double distance;

/**
* An intermediary other node on the minimum path to the other node.
* Used to reconstruct the path between two nodes.
*/
public APSPInfo passBy;

public TargetPath(Node other, float distance, APSPInfo passBy) {
public TargetPath(Node other, double distance, APSPInfo passBy) {
this.target = other;
this.distance = distance;
this.passBy = passBy;
Expand Down Expand Up @@ -598,6 +595,6 @@ public interface Progress {
* @param percent
* a value between 0 and 1, 0 meaning 0% and 1 meaning 100%.
*/
void progress(float percent);
void progress(double percent);
}
}
2 changes: 1 addition & 1 deletion src/org/graphstream/algorithm/Centroid.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void compute() {

for (Node other : graph.getEachNode()) {
if (node != other) {
float d = info.getLengthTo(other.getId());
double d = info.getLengthTo(other.getId());

if (d < 0)
System.err
Expand Down
8 changes: 4 additions & 4 deletions src/org/graphstream/algorithm/ConnectedComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public ConnectedComponents(Graph graph) {
if (graph != null)
init(graph);

connectedComponentsMap = new HashMap<Node, Integer>();

}

/**
Expand All @@ -208,7 +208,6 @@ public List<Node> getGiantComponent() {
maxIndex = c;
}
}

// Get the list of nodes within this component
if (maxIndex != -1) {
ArrayList<Node> giant = new ArrayList<Node>();
Expand Down Expand Up @@ -253,7 +252,7 @@ public int getConnectedComponentsCount(int sizeThreshold) {
* Maximum size for the connected component to be considered (use
* 0 or lower values to ignore the ceiling)
*/
protected int getConnectedComponentsCount(int sizeThreshold, int sizeCeiling) {
public int getConnectedComponentsCount(int sizeThreshold, int sizeCeiling) {
if (!started) {
compute();
}
Expand Down Expand Up @@ -392,6 +391,8 @@ public void compute() {
ids.add(""); // The dummy first identifier (since zero is a special
// value).

connectedComponentsMap = new HashMap<Node, Integer>();

// Initialize the size count structure
connectedComponentsSize = new HashMap<Integer, Integer>();

Expand Down Expand Up @@ -478,7 +479,6 @@ private int computeConnectedComponent(Node v, int id, Edge exception) {
if (connectedComponentsMap.get(n2) != id) {
open.add(n2);
connectedComponentsMap.put(n2, id);
size++;
markNode(n2, id); /* useless */
}
// Also work with (but slower):
Expand Down
6 changes: 3 additions & 3 deletions src/org/graphstream/algorithm/Eccentricity.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public void init(Graph graph) {
* @see org.graphstream.algorithm.Algorithm#compute()
*/
public void compute() {
float min = Float.MAX_VALUE;
double min = Double.MAX_VALUE;
HashSet<Node> eccentricity = new HashSet<Node>();

for (Node node : graph.getEachNode()) {
float m = Float.MIN_VALUE;
double m = Double.MIN_VALUE;
APSP.APSPInfo info = node.getAttribute(apspInfoAttribute);

if (info == null)
Expand All @@ -102,7 +102,7 @@ public void compute() {

for (Node other : graph.getEachNode()) {
if (node != other) {
float d = info.getLengthTo(other.getId());
double d = info.getLengthTo(other.getId());

if (d < 0)
System.err
Expand Down
Loading

0 comments on commit 953b40a

Please sign in to comment.