Newer
Older
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
import org.graphstream.algorithm.Toolkit;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.stream.file.FileSource;
import org.graphstream.stream.file.FileSourceEdge;
public class App {
public static void main(String[] args) {
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Graph g = getNerdsGraph();
int nb_nodes = g.getNodeCount();
int nb_links = g.getEdgeCount();
System.out.format("N: %d\nL: %d\n<k>: %.3f\n<C>: %.3f\nConnected: %b\n",
nb_nodes,
nb_links,
Toolkit.averageDegree(g),
Toolkit.averageClusteringCoefficient(g),
Toolkit.isConnected(g)
);
exportDegreeDistribution(g, "./data/distrib", true);
}
public static void exportDegreeDistribution(Graph g, String destFile, boolean normalized) {
int[] degreesDistribution = Toolkit.degreeDistribution(g);
double[] convertedDistribution = new double[degreesDistribution.length];
for(int i=0; i < degreesDistribution.length; i++)
convertedDistribution[i] = normalized ?
(double)degreesDistribution[i] / g.getNodeCount() :
(double)degreesDistribution[i];
try {
BufferedWriter w = new BufferedWriter(new FileWriter(new File(destFile)));
for(int i=0; i < convertedDistribution.length; i++)
if(convertedDistribution[i] != 0)
w.write(String.format("%d\t%f\n", i, convertedDistribution[i]));
w.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static Graph getNerdsGraph() {
Graph g = new SingleGraph("nerds");
FileSource fs = new FileSourceEdge();
fs.addSink(g);
try {
InputStream stream = App.class.getClassLoader().getResourceAsStream("nerds.txt");
fs.readAll(stream);
} catch (IOException e) {
e.printStackTrace();
}
return g;