/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package medusa.graph;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* @author Sean
*/
public class GraphMatrix {
double[][] distanceMatrix;
public double[][] getDistanceMatrix() {
return distanceMatrix;
}
Map nodeIndexMap;
Map indexNodeMap;
BasicGraph g;
public GraphMatrix(BasicGraph g) {
this.g = g;
createLabelIndex();
createDistanceMatrix();
}
private void createLabelIndex() {
nodeIndexMap = new HashMap();
for (Iterator nodes = g.nodesIterator(); nodes.hasNext();) {
nodeIndexMap.put(nodes.next().getLabel(), new Integer(nodeIndexMap.size()));
}
// now create the reverse map
indexNodeMap = new HashMap();
for (String label : nodeIndexMap.keySet()) {
indexNodeMap.put(nodeIndexMap.get(label), label);
}
}
private void createDistanceMatrix() {
distanceMatrix = new double[g.getNodeSize()][g.getNodeSize()];
for (Iterator edges = g.edgesIterator(); edges.hasNext();) {
// get indexes
Edge e = edges.next();
String n1 = e.getFromName();
String n2 = e.getToName();
int i1 = nodeIndexMap.get(n1);
int i2 = nodeIndexMap.get(n2);
distanceMatrix[i1][i2] = 1.0 - e.getConf();
distanceMatrix[i2][i1] = 1.0 - e.getConf();
}
}
public String getLabelForIndex(int i) {
return indexNodeMap.get(i);
}
public double[] getRowForIndex(int i) {
return distanceMatrix[i];
}
}
Ads help maintain this website.