Coverage details for edu.uci.ics.jung.statistics.DegreeDistributions

LineHitsSource
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  */
320public 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     {
420        DoubleArrayList degreeValues = new DoubleArrayList();
43  
440        for (Iterator i = vertices.iterator(); i.hasNext(); )
450            degreeValues.add(((ArchetypeVertex)i.next()).degree());
46  
470        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) {
573        DoubleArrayList outDegreeValues = new DoubleArrayList();
58  
593        Vertex currentVertex = null;
603        for (Iterator i = vertices.iterator(); i.hasNext(); ) {
611006            currentVertex = (Vertex) i.next();
621006            outDegreeValues.add(currentVertex.outDegree());
63         }
64  
653        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) {
741        DoubleArrayList list = new DoubleArrayList();
75  
761        Vertex currentVertex = null;
771        for (Iterator i = vertices.iterator(); i.hasNext(); ) {
786            currentVertex = (Vertex) i.next();
796            list.add(currentVertex.inDegree());
80         }
81  
821        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) {
941        Histogram histogram = new Histogram(min,max,numBins);
95  
961        for (Iterator i = vertices.iterator(); i.hasNext(); ) {
976            Vertex currentVertex = (Vertex) i.next();
986            int currentOutdegree = currentVertex.outDegree();
996            histogram.fill(currentOutdegree);
100         }
1011        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) {
1131        Histogram histogram = new Histogram(min,max,numBins);
114  
1151        for (Iterator i = vertices.iterator(); i.hasNext(); ) {
1166            Vertex currentVertex = (Vertex) i.next();
1176            int currentIndegree = currentVertex.inDegree();
1186            histogram.fill(currentIndegree);
119         }
1201        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 {
1330            BufferedWriter degreeWriter = new BufferedWriter(new FileWriter(file));
1340            for (int i = 0; i < histogram.size(); i++) {
1350                int currentDegree = (int) (i + histogram.getMinimum());
1360                degreeWriter.write(currentDegree + " " + histogram.yValueAt(i) + "\n");
137             }
1380            degreeWriter.close();
139  
1400        } catch (Exception e) {
1410            throw new FatalException("Error saving binned data to " + file,e);
1420        }
1430    }
144 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.