Search in sources :

Example 36 with DirectedGraph

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);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Test(org.testng.annotations.Test)

Example 37 with DirectedGraph

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;
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) HashMap(java.util.HashMap) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge)

Example 38 with DirectedGraph

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;
}
Also used : Node(org.gephi.graph.api.Node) Color(java.awt.Color) DecimalFormat(java.text.DecimalFormat) DijkstraShortestPathAlgorithm(org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm) AbstractShortestPathAlgorithm(org.gephi.algorithms.shortestpath.AbstractShortestPathAlgorithm) BellmanFordShortestPathAlgorithm(org.gephi.algorithms.shortestpath.BellmanFordShortestPathAlgorithm) LinearGradient(org.gephi.ui.utils.GradientUtils.LinearGradient) Entry(java.util.Map.Entry) DirectedGraph(org.gephi.graph.api.DirectedGraph) Graph(org.gephi.graph.api.Graph) DirectedGraph(org.gephi.graph.api.DirectedGraph) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener) GraphController(org.gephi.graph.api.GraphController)

Example 39 with DirectedGraph

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);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) Test(org.testng.annotations.Test)

Example 40 with DirectedGraph

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);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) Test(org.testng.annotations.Test)

Aggregations

DirectedGraph (org.gephi.graph.api.DirectedGraph)64 Node (org.gephi.graph.api.Node)60 GraphModel (org.gephi.graph.api.GraphModel)57 Test (org.testng.annotations.Test)52 Edge (org.gephi.graph.api.Edge)37 GraphController (org.gephi.graph.api.GraphController)36 HashMap (java.util.HashMap)17 LinkedList (java.util.LinkedList)6 UndirectedGraph (org.gephi.graph.api.UndirectedGraph)6 Graph (org.gephi.graph.api.Graph)2 Color (java.awt.Color)1 DecimalFormat (java.text.DecimalFormat)1 Entry (java.util.Map.Entry)1 AbstractShortestPathAlgorithm (org.gephi.algorithms.shortestpath.AbstractShortestPathAlgorithm)1 BellmanFordShortestPathAlgorithm (org.gephi.algorithms.shortestpath.BellmanFordShortestPathAlgorithm)1 DijkstraShortestPathAlgorithm (org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm)1 Interval (org.gephi.graph.api.Interval)1 TimeRepresentation (org.gephi.graph.api.TimeRepresentation)1 NodeClickEventListener (org.gephi.tools.spi.NodeClickEventListener)1 LinearGradient (org.gephi.ui.utils.GradientUtils.LinearGradient)1