use of org.gephi.graph.api.DirectedGraph in project gephi by gephi.
the class GraphDistanceNGTest method testSpecial2DirectedGraphRadius.
@Test
public void testSpecial2DirectedGraphRadius() {
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
DirectedGraph directedGraph = graphModel.getDirectedGraph();
Node node1 = graphModel.factory().newNode("0");
Node node2 = graphModel.factory().newNode("1");
Node node3 = graphModel.factory().newNode("2");
Node node4 = graphModel.factory().newNode("3");
Node node5 = graphModel.factory().newNode("4");
directedGraph.addNode(node1);
directedGraph.addNode(node2);
directedGraph.addNode(node3);
directedGraph.addNode(node4);
directedGraph.addNode(node5);
Edge edge12 = graphModel.factory().newEdge(node1, node2);
Edge edge14 = graphModel.factory().newEdge(node1, node4);
Edge edge23 = graphModel.factory().newEdge(node2, node3);
Edge edge25 = graphModel.factory().newEdge(node2, node5);
Edge edge35 = graphModel.factory().newEdge(node3, node5);
Edge edge43 = graphModel.factory().newEdge(node4, node3);
Edge edge51 = graphModel.factory().newEdge(node5, node1);
Edge edge54 = graphModel.factory().newEdge(node5, node4);
directedGraph.addEdge(edge12);
directedGraph.addEdge(edge14);
directedGraph.addEdge(edge23);
directedGraph.addEdge(edge25);
directedGraph.addEdge(edge35);
directedGraph.addEdge(edge43);
directedGraph.addEdge(edge51);
directedGraph.addEdge(edge54);
GraphDistance d = new GraphDistance();
d.initializeStartValues();
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
HashMap<Node, Integer> indicies = d.createIndiciesMap(undirectedGraph);
d.calculateDistanceMetrics(graphModel.getGraph(), indicies, true, false);
double radius = d.getRadius();
assertEquals(radius, 2.0, TOLERANCE);
}
use of org.gephi.graph.api.DirectedGraph in project gephi by gephi.
the class ClusteringCoefficient method createNeighbourTable.
private HashMap<Node, EdgeWrapper> createNeighbourTable(Graph graph, Node node, HashMap<Node, Integer> indicies, ArrayWrapper[] networks, boolean directed) {
HashMap<Node, EdgeWrapper> neighborTable = new HashMap<>();
if (!directed) {
for (Edge edge : graph.getEdges(node)) {
Node neighbor = graph.getOpposite(node, edge);
neighborTable.put(neighbor, new EdgeWrapper(1, networks[indicies.get(neighbor)]));
}
} else {
for (Node neighbor : ((DirectedGraph) graph).getPredecessors(node)) {
neighborTable.put(neighbor, new EdgeWrapper(1, networks[indicies.get(neighbor)]));
}
for (Edge out : ((DirectedGraph) graph).getOutEdges(node)) {
Node neighbor = out.getTarget();
EdgeWrapper ew = neighborTable.get(neighbor);
if (ew == null) {
neighborTable.put(neighbor, new EdgeWrapper(1, network[indicies.get(neighbor)]));
} else {
ew.count++;
}
}
}
return neighborTable;
}
use of org.gephi.graph.api.DirectedGraph in project gephi by gephi.
the class HeatMap method getListeners.
@Override
public ToolEventListener[] getListeners() {
listeners = new ToolEventListener[1];
listeners[0] = new NodeClickEventListener() {
@Override
public void clickNodes(Node[] nodes) {
try {
Node n = nodes[0];
Color[] colors;
float[] positions;
if (heatMapPanel.isUsePalette()) {
colors = heatMapPanel.getSelectedPalette().getColors();
positions = heatMapPanel.getSelectedPalette().getPositions();
dontPaintUnreachable = true;
} else {
gradientColors = colors = heatMapPanel.getGradientColors();
gradientPositions = positions = heatMapPanel.getGradientPositions();
dontPaintUnreachable = heatMapPanel.isDontPaintUnreachable();
}
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
AbstractShortestPathAlgorithm algorithm;
if (gc.getGraphModel().isDirected()) {
DirectedGraph graph = gc.getGraphModel().getDirectedGraphVisible();
algorithm = new BellmanFordShortestPathAlgorithm(graph, n);
algorithm.compute();
} else {
Graph graph = gc.getGraphModel().getGraphVisible();
algorithm = new DijkstraShortestPathAlgorithm(graph, n);
algorithm.compute();
}
//Color
LinearGradient linearGradient = new LinearGradient(colors, positions);
//Algorithm
double maxDistance = algorithm.getMaxDistance();
if (!dontPaintUnreachable) {
//+1 to have the maxdistance nodes a ratio<1
maxDistance++;
}
if (maxDistance > 0) {
for (Entry<Node, Double> entry : algorithm.getDistances().entrySet()) {
Node node = entry.getKey();
if (!Double.isInfinite(entry.getValue())) {
float ratio = (float) (entry.getValue() / maxDistance);
Color c = linearGradient.getValue(ratio);
node.setColor(c);
} else if (!dontPaintUnreachable) {
Color c = colors[colors.length - 1];
node.setColor(c);
}
}
}
Color c = colors[0];
n.setColor(c);
heatMapPanel.setStatus(NbBundle.getMessage(HeatMap.class, "HeatMap.status.maxdistance") + new DecimalFormat("#.##").format(algorithm.getMaxDistance()));
} catch (Exception e) {
Logger.getLogger("").log(Level.SEVERE, "", e);
}
}
};
return listeners;
}
use of org.gephi.graph.api.DirectedGraph in project gephi by gephi.
the class WeightedDegreeNGTest method testDirectedCyclicGraphDegree.
@Test
public void testDirectedCyclicGraphDegree() {
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
DirectedGraph graph = graphModel.getDirectedGraph();
Node n1 = graph.getNode("0");
Node n3 = graph.getNode("2");
Node n5 = graph.getNode("4");
WeightedDegree d = new WeightedDegree();
d.execute(graph);
double inDegree3 = (Double) n3.getAttribute(WeightedDegree.WINDEGREE);
double degree1 = (Double) n1.getAttribute(WeightedDegree.WDEGREE);
double outDegree5 = (Double) n5.getAttribute(WeightedDegree.WOUTDEGREE);
double avDegree = d.getAverageDegree();
assertEquals(inDegree3, 1.0);
assertEquals(degree1, 2.0);
assertEquals(outDegree5, 1.0);
assertEquals(avDegree, 1.0);
}
use of org.gephi.graph.api.DirectedGraph in project gephi by gephi.
the class WeightedDegreeNGTest method testDirectedPathGraphDegree.
@Test
public void testDirectedPathGraphDegree() {
GraphModel graphModel = GraphGenerator.generatePathDirectedGraph(2);
DirectedGraph graph = graphModel.getDirectedGraph();
Node n1 = graph.getNode("0");
Node n2 = graph.getNode("1");
WeightedDegree d = new WeightedDegree();
d.execute(graph);
double inDegree1 = (Double) n1.getAttribute(WeightedDegree.WINDEGREE);
double inDegree2 = (Double) n2.getAttribute(WeightedDegree.WINDEGREE);
double outDegree1 = (Double) n1.getAttribute(WeightedDegree.WOUTDEGREE);
double avDegree = d.getAverageDegree();
assertEquals(inDegree1, 0.0);
assertEquals(inDegree2, 1.0);
assertEquals(outDegree1, 1.0);
assertEquals(avDegree, 0.5);
}
Aggregations