Search in sources :

Example 56 with UndirectedGraph

use of org.gephi.graph.api.UndirectedGraph in project gephi by gephi.

the class PageRankNGTest method testNullGraphPageRank.

@Test
public void testNullGraphPageRank() {
    pc.newProject();
    GraphModel graphModel = GraphGenerator.generateNullUndirectedGraph(5);
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    PageRank pr = new PageRank();
    double[] pageRank;
    HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
    pageRank = pr.calculatePagerank(graph, indicies, false, false, 0.001, 0.85);
    Node n1 = graph.getNode("0");
    Node n4 = graph.getNode("3");
    int index1 = indicies.get(n1);
    int index4 = indicies.get(n4);
    double pr1 = pageRank[index1];
    double pr4 = pageRank[index4];
    double res = 0.2d;
    double diff1 = Math.abs(pr1 - res);
    double diff4 = Math.abs(pr4 - res);
    assertTrue(diff1 < 0.01);
    assertTrue(diff4 < 0.01);
    assertEquals(pr1, pr4);
}
Also used : GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Node(org.gephi.graph.api.Node) Test(org.testng.annotations.Test)

Example 57 with UndirectedGraph

use of org.gephi.graph.api.UndirectedGraph in project gephi by gephi.

the class PageRankNGTest method testCompleteGraphPageRank.

@Test
public void testCompleteGraphPageRank() {
    pc.newProject();
    GraphModel graphModel = GraphGenerator.generateCompleteUndirectedGraph(5);
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    PageRank pr = new PageRank();
    double[] pageRank;
    HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
    pageRank = pr.calculatePagerank(graph, indicies, false, false, 0.001, 0.85);
    Node n2 = graph.getNode("2");
    int index2 = indicies.get(n2);
    double pr2 = pageRank[index2];
    double res = 0.2d;
    double diff2 = Math.abs(pr2 - res);
    assertTrue(diff2 < 0.01);
}
Also used : GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Node(org.gephi.graph.api.Node) Test(org.testng.annotations.Test)

Example 58 with UndirectedGraph

use of org.gephi.graph.api.UndirectedGraph in project gephi by gephi.

the class PageRankNGTest method testStarGraphPageRank.

@Test
public void testStarGraphPageRank() {
    pc.newProject();
    GraphModel graphModel = GraphGenerator.generateStarUndirectedGraph(5);
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    PageRank pr = new PageRank();
    double[] pageRank;
    HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
    pageRank = pr.calculatePagerank(graph, indicies, false, false, 0.001, 0.6);
    Node n1 = graph.getNode("0");
    Node n2 = graph.getNode("1");
    Node n3 = graph.getNode("2");
    Node n4 = graph.getNode("3");
    Node n5 = graph.getNode("4");
    Node n6 = graph.getNode("5");
    int index1 = indicies.get(n1);
    int index2 = indicies.get(n2);
    int index3 = indicies.get(n3);
    int index4 = indicies.get(n4);
    int index5 = indicies.get(n5);
    int index6 = indicies.get(n6);
    double pr1 = pageRank[index1];
    double pr2 = pageRank[index2];
    double pr3 = pageRank[index3];
    double pr4 = pageRank[index4];
    double pr5 = pageRank[index5];
    double pr6 = pageRank[index6];
    boolean oneMoreThree = pr1 > pr3;
    double res = 1.;
    double diff = 0.01;
    double sum = pr1 + pr2 + pr3 + pr4 + pr5 + pr6;
    assertTrue(oneMoreThree);
    assertEquals(pr2, pr4);
    assertTrue(Math.abs(sum - res) < diff);
}
Also used : GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Node(org.gephi.graph.api.Node) Test(org.testng.annotations.Test)

Example 59 with UndirectedGraph

use of org.gephi.graph.api.UndirectedGraph in project gephi by gephi.

the class GraphDistanceNGTest method testSpecial2UndirectedGraphBetweenness.

@Test
public void testSpecial2UndirectedGraphBetweenness() {
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
    UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
    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");
    Node node6 = graphModel.factory().newNode("5");
    Node node7 = graphModel.factory().newNode("6");
    Node node8 = graphModel.factory().newNode("7");
    undirectedGraph.addNode(node1);
    undirectedGraph.addNode(node2);
    undirectedGraph.addNode(node3);
    undirectedGraph.addNode(node4);
    undirectedGraph.addNode(node5);
    undirectedGraph.addNode(node6);
    undirectedGraph.addNode(node7);
    undirectedGraph.addNode(node8);
    Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
    Edge edge13 = graphModel.factory().newEdge(node1, node3, false);
    Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
    Edge edge24 = graphModel.factory().newEdge(node2, node4, false);
    Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
    Edge edge67 = graphModel.factory().newEdge(node6, node7, false);
    Edge edge78 = graphModel.factory().newEdge(node7, node8, false);
    Edge edge85 = graphModel.factory().newEdge(node8, node5, false);
    Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
    undirectedGraph.addEdge(edge12);
    undirectedGraph.addEdge(edge13);
    undirectedGraph.addEdge(edge34);
    undirectedGraph.addEdge(edge24);
    undirectedGraph.addEdge(edge56);
    undirectedGraph.addEdge(edge67);
    undirectedGraph.addEdge(edge78);
    undirectedGraph.addEdge(edge85);
    undirectedGraph.addEdge(edge45);
    GraphDistance d = new GraphDistance();
    d.initializeStartValues();
    UndirectedGraph hierarchicalUndirectedGraph = graphModel.getUndirectedGraph();
    HashMap<Node, Integer> indicies = d.createIndiciesMap(hierarchicalUndirectedGraph);
    HashMap<String, double[]> metricsMap = (HashMap) d.calculateDistanceMetrics(graphModel.getGraph(), indicies, false, false);
    double[] betweenness = metricsMap.get(GraphDistance.BETWEENNESS);
    Node n4 = undirectedGraph.getNode("3");
    Node n6 = undirectedGraph.getNode("5");
    Node n7 = undirectedGraph.getNode("6");
    int index4 = indicies.get(n4);
    int index6 = indicies.get(n6);
    int index7 = indicies.get(n7);
    assertEquals(betweenness[index4], 12.5, TOLERANCE);
    assertEquals(betweenness[index6], 2.5, TOLERANCE);
    assertEquals(betweenness[index7], 0.5, TOLERANCE);
}
Also used : HashMap(java.util.HashMap) Node(org.gephi.graph.api.Node) GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Test(org.testng.annotations.Test)

Example 60 with UndirectedGraph

use of org.gephi.graph.api.UndirectedGraph in project gephi by gephi.

the class GraphDistanceNGTest method testSpecial1DirectedGraphDiameter.

@Test
public void testSpecial1DirectedGraphDiameter() {
    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");
    Node node6 = graphModel.factory().newNode("5");
    Node node7 = graphModel.factory().newNode("6");
    Node node8 = graphModel.factory().newNode("7");
    directedGraph.addNode(node1);
    directedGraph.addNode(node2);
    directedGraph.addNode(node3);
    directedGraph.addNode(node4);
    directedGraph.addNode(node5);
    directedGraph.addNode(node6);
    directedGraph.addNode(node7);
    directedGraph.addNode(node8);
    Edge edge12 = graphModel.factory().newEdge(node1, node2);
    Edge edge23 = graphModel.factory().newEdge(node2, node3);
    Edge edge34 = graphModel.factory().newEdge(node3, node4);
    Edge edge41 = graphModel.factory().newEdge(node4, node1);
    Edge edge56 = graphModel.factory().newEdge(node5, node6);
    Edge edge67 = graphModel.factory().newEdge(node6, node7);
    Edge edge78 = graphModel.factory().newEdge(node7, node8);
    Edge edge85 = graphModel.factory().newEdge(node8, node5);
    Edge edge45 = graphModel.factory().newEdge(node4, node5);
    directedGraph.addEdge(edge12);
    directedGraph.addEdge(edge23);
    directedGraph.addEdge(edge34);
    directedGraph.addEdge(edge41);
    directedGraph.addEdge(edge56);
    directedGraph.addEdge(edge67);
    directedGraph.addEdge(edge78);
    directedGraph.addEdge(edge85);
    directedGraph.addEdge(edge45);
    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 diameter = d.getDiameter();
    assertEquals(diameter, 7.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)

Aggregations

UndirectedGraph (org.gephi.graph.api.UndirectedGraph)100 GraphModel (org.gephi.graph.api.GraphModel)99 Node (org.gephi.graph.api.Node)97 Test (org.testng.annotations.Test)93 Edge (org.gephi.graph.api.Edge)44 GraphController (org.gephi.graph.api.GraphController)39 HashMap (java.util.HashMap)36 DirectedGraph (org.gephi.graph.api.DirectedGraph)13 Graph (org.gephi.graph.api.Graph)7 LinkedList (java.util.LinkedList)6