Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
3 | * of California | |
4 | * All rights reserved. | |
5 | * | |
6 | * This software is open-source under the BSD license; see either | |
7 | * "license.txt" or | |
8 | * http://jung.sourceforge.net/license.txt for a description. | |
9 | */ | |
10 | package edu.uci.ics.jung.statistics; | |
11 | ||
12 | import java.io.BufferedWriter; | |
13 | import java.io.FileWriter; | |
14 | import java.util.Iterator; | |
15 | import java.util.Set; | |
16 | ||
17 | import cern.colt.list.DoubleArrayList; | |
18 | import edu.uci.ics.jung.exceptions.FatalException; | |
19 | import edu.uci.ics.jung.graph.ArchetypeVertex; | |
20 | import edu.uci.ics.jung.graph.Vertex; | |
21 | ||
22 | /** | |
23 | * Set of general-purpose functions for analyzing the degree distribution of a set of vertices (normally | |
24 | * the complete set of vertices associated with a graph). These include: <ul> | |
25 | * <li> statistical summary (set of fundamental point statistics) of outdegree distribution </li> | |
26 | * <li> statistical summary (set of fundamental point statistics) of indegree distribution </li> | |
27 | * <li> histogram of outdegree distribution </li> | |
28 | * <li> histogram of outdegree distribution </li> | |
29 | * <li> function for saving degree information to a file </li></ul> | |
30 | * @author Scott White | |
31 | */ | |
32 | 0 | public class DegreeDistributions |
33 | { | |
34 | ||
35 | /** | |
36 | * Given a set of vertices, this function returns a list of degrees. | |
37 | * @param vertices the vertices whose degrees are to be analyzed | |
38 | * @return a list of degrees | |
39 | */ | |
40 | public static DoubleArrayList getDegreeValues(Set vertices) | |
41 | { | |
42 | 0 | DoubleArrayList degreeValues = new DoubleArrayList(); |
43 | ||
44 | 0 | for (Iterator i = vertices.iterator(); i.hasNext(); ) |
45 | 0 | degreeValues.add(((ArchetypeVertex)i.next()).degree()); |
46 | ||
47 | 0 | return degreeValues; |
48 | } | |
49 | ||
50 | ||
51 | /** | |
52 | * Given a set of vertices, this function returns a list of outdegrees. | |
53 | * @param vertices the vertices whose outdegrees are to be analyzed | |
54 | * @return a list of outdegrees | |
55 | */ | |
56 | public static DoubleArrayList getOutdegreeValues(Set vertices) { | |
57 | 3 | DoubleArrayList outDegreeValues = new DoubleArrayList(); |
58 | ||
59 | 3 | Vertex currentVertex = null; |
60 | 3 | for (Iterator i = vertices.iterator(); i.hasNext(); ) { |
61 | 1006 | currentVertex = (Vertex) i.next(); |
62 | 1006 | outDegreeValues.add(currentVertex.outDegree()); |
63 | } | |
64 | ||
65 | 3 | return outDegreeValues; |
66 | } | |
67 | ||
68 | /** | |
69 | * Given a set of vertices, this function returns a list of indegrees. | |
70 | * @param vertices the vertices whose indegrees are to be analyzed | |
71 | * @return a list of indegrees | |
72 | */ | |
73 | public static DoubleArrayList getIndegreeValues(Set vertices) { | |
74 | 1 | DoubleArrayList list = new DoubleArrayList(); |
75 | ||
76 | 1 | Vertex currentVertex = null; |
77 | 1 | for (Iterator i = vertices.iterator(); i.hasNext(); ) { |
78 | 6 | currentVertex = (Vertex) i.next(); |
79 | 6 | list.add(currentVertex.inDegree()); |
80 | } | |
81 | ||
82 | 1 | return list; |
83 | } | |
84 | ||
85 | /** | |
86 | * Generates a histogram of the outdegree distribution for a set of vertices | |
87 | * @param vertices the set of vertices to be analyzed | |
88 | * @param min the minimum value of the data to be binned | |
89 | * @param max the maximum value of the data to be binned | |
90 | * @param numBins the number of bins to be created | |
91 | * @return the histogram instance | |
92 | */ | |
93 | public static Histogram getOutdegreeHistogram(Set vertices, double min, double max, int numBins) { | |
94 | 1 | Histogram histogram = new Histogram(min,max,numBins); |
95 | ||
96 | 1 | for (Iterator i = vertices.iterator(); i.hasNext(); ) { |
97 | 6 | Vertex currentVertex = (Vertex) i.next(); |
98 | 6 | int currentOutdegree = currentVertex.outDegree(); |
99 | 6 | histogram.fill(currentOutdegree); |
100 | } | |
101 | 1 | return histogram; |
102 | } | |
103 | ||
104 | /** | |
105 | * Generates a histogram of the indegree distribution for a set of vertices | |
106 | * @param vertices the set of vertices to be analyzed | |
107 | * @param min the minimum value of the data to be binned | |
108 | * @param max the maximum value of the data to be binned | |
109 | * @param numBins the number of bins to be created | |
110 | * @return the histogram instance | |
111 | */ | |
112 | public static Histogram getIndegreeHistogram(Set vertices, double min, double max, int numBins) { | |
113 | 1 | Histogram histogram = new Histogram(min,max,numBins); |
114 | ||
115 | 1 | for (Iterator i = vertices.iterator(); i.hasNext(); ) { |
116 | 6 | Vertex currentVertex = (Vertex) i.next(); |
117 | 6 | int currentIndegree = currentVertex.inDegree(); |
118 | 6 | histogram.fill(currentIndegree); |
119 | } | |
120 | 1 | return histogram; |
121 | } | |
122 | ||
123 | /** | |
124 | * Saves the empirical degree distribution to a file in the ascii flat file where each line has the | |
125 | * following format: | |
126 | * <degree> <# of vertices with this degree> | |
127 | * @param histogram a histogram representing a degree distribution | |
128 | * @param file the name of the file where the data is to be saved | |
129 | */ | |
130 | public static void saveDistribution(Histogram histogram, String file) { | |
131 | ||
132 | try { | |
133 | 0 | BufferedWriter degreeWriter = new BufferedWriter(new FileWriter(file)); |
134 | 0 | for (int i = 0; i < histogram.size(); i++) { |
135 | 0 | int currentDegree = (int) (i + histogram.getMinimum()); |
136 | 0 | degreeWriter.write(currentDegree + " " + histogram.yValueAt(i) + "\n"); |
137 | } | |
138 | 0 | degreeWriter.close(); |
139 | ||
140 | 0 | } catch (Exception e) { |
141 | 0 | throw new FatalException("Error saving binned data to " + file,e); |
142 | 0 | } |
143 | 0 | } |
144 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |