/** * Check if arrays are large enought to support computation. */ protected void checkArrays() { n = flowGraph.getEdgeCount(); if (capacities == null || capacities.length < 2 * n) { capacities = new double[2 * n]; flows = new double[2 * n]; } }
/** * Check if arrays are large enought to support computation. */ protected void checkArrays() { n = flowGraph.getEdgeCount(); if (capacities == null || capacities.length < 2 * n) { capacities = new double[2 * n]; flows = new double[2 * n]; } }
public double getElementCount() { return g.getEdgeCount(); } }
public double getElementCount() { return g.getEdgeCount(); } }
/** * This method computes the mean degree of the graph. */ public double meanDegree() { return 2.0 * internalGraph.getEdgeCount() / (double) internalGraph.getNodeCount(); }
/** * This method computes the mean degree of the graph. */ public double meanDegree() { return 2.0 * internalGraph.getEdgeCount() / (double) internalGraph.getNodeCount(); }
/** * The density is the number of links in the graph divided by the total * number of possible links. * * @return The density of the graph. * @complexity O(1) */ public static double density(Graph graph) { float m = (float) graph.getEdgeCount(); float n = (float) graph.getNodeCount(); if (n > 0) return ((2 * m) / (n * (n - 1))); return 0; }
/** * Returns the value of the average degree of the graph. A node with a loop * edge has degree two. * * @return The average degree of the graph. * @complexity O(1). */ public static double averageDegree(Graph graph) { float m = graph.getEdgeCount() * 2; float n = graph.getNodeCount(); if (n > 0) return m / n; return 0; }
/** * Returns the value of the average degree of the graph. A node with a loop * edge has degree two. * * @return The average degree of the graph. * @complexity O(1). */ public static double averageDegree(Graph graph) { float m = graph.getEdgeCount() * 2; float n = graph.getNodeCount(); if (n > 0) return m / n; return 0; }
/** * The density is the number of links in the graph divided by the total * number of possible links. * * @return The density of the graph. * @complexity O(1) */ public static double density(Graph graph) { float m = (float) graph.getEdgeCount(); float n = (float) graph.getNodeCount(); if (n > 0) return ((2 * m) / (n * (n - 1))); return 0; }
/** * Returns a random subset of edges of fixed size. Each edge has the same * chance to be chosen. * * @param graph * A graph. * @param k * The size of the subset. * @param random * A source of randomness. * @return A random subset of edges of size <code>k</code>. * @throws IllegalArgumentException * If <code>k</code> is negative or greater than the number of * edges. * @complexity O(<code>k</code>) */ public static <T extends Edge> List<T> randomEdgeSet(Graph graph, int k, Random random) { if (k < 0 || k > graph.getEdgeCount()) throw new IllegalArgumentException("k must be between 0 and " + graph.getEdgeCount()); Set<Integer> subset = RandomTools.randomKsubset(graph.getEdgeCount(), k, null, random); List<T> result = new ArrayList<T>(subset.size()); for (int i : subset) result.add(graph.<T> getEdge(i)); return result; }
/** * Choose an edge at random. * * @param random * The random number generator to use. * @return O(1). */ public static Edge randomEdge(Graph graph, Random random) { int n = graph.getEdgeCount(); if (n > 0) { return graph.getEdge(random.nextInt(n)); // int r = random.nextInt(n); // int i = 0; // // for (Edge edge : graph.getEachEdge()) { // if (r == i) // return edge; // i++; // } } return null; }
/** * Choose an edge at random. * * @param random * The random number generator to use. * @return O(1). */ public static Edge randomEdge(Graph graph, Random random) { int n = graph.getEdgeCount(); if (n > 0) { return graph.getEdge(random.nextInt(n)); // int r = random.nextInt(n); // int i = 0; // // for (Edge edge : graph.getEachEdge()) { // if (r == i) // return edge; // i++; // } } return null; }
/** * Sort all edges by their "passes" attribute and return the array of sorted * edges. * * @return An array with all edges of the graph sorted by their number of * entity pass. */ public ArrayList<Edge> findTheMostUsedEdges() { ArrayList<Edge> edges = new ArrayList<Edge>(context.graph.getEdgeCount()); Iterator<? extends Edge> i = context.graph.getEdgeIterator(); while (i.hasNext()) { edges.add(i.next()); } Collections.sort(edges, new Comparator<Edge>() { public int compare(Edge e1, Edge e2) { int n1 = (int) e1.getNumber(context.passesAttribute); int n2 = (int) e2.getNumber(context.passesAttribute); return (n1 - n2); } }); return edges; }
/** * Sort all edges by their "passes" attribute and return the array of sorted * edges. * * @return An array with all edges of the graph sorted by their number of * entity pass. */ public ArrayList<Edge> findTheMostUsedEdges() { ArrayList<Edge> edges = new ArrayList<Edge>(context.graph.getEdgeCount()); Iterator<? extends Edge> i = context.graph.getEdgeIterator(); while (i.hasNext()) { edges.add(i.next()); } Collections.sort(edges, new Comparator<Edge>() { public int compare(Edge e1, Edge e2) { int n1 = (int) e1.getNumber(context.passesAttribute); int n2 = (int) e2.getNumber(context.passesAttribute); return (n1 - n2); } }); return edges; }
/** * Returns a random subset of edges. Each edge is chosen with given * probability. * * @param graph * A graph. * @param p * The probability to choose each edge. * @param random * A source of randomness. * @return A random subset of edges. * @throws IllegalArgumentException * If <code>p</code> is negative or greater than one. * @complexity In average O(<code>m * p<code>), where <code>m</code> is the * number of edges. */ public static <T extends Edge> List<T> randomEdgeSet(Graph graph, double p, Random random) { if (p < 0 || p > 1) throw new IllegalArgumentException("p must be between 0 and 1"); Set<Integer> subset = RandomTools.randomPsubset(graph.getEdgeCount(), p, null, random); List<T> result = new ArrayList<T>(subset.size()); for (int i : subset) result.add(graph.<T> getEdge(i)); return result; }
/** * Returns a random subset of edges. Each edge is chosen with given * probability. * * @param graph * A graph. * @param p * The probability to choose each edge. * @param random * A source of randomness. * @return A random subset of edges. * @throws IllegalArgumentException * If <code>p</code> is negative or greater than one. * @complexity In average O(<code>m * p<code>), where <code>m</code> is the * number of edges. */ public static <T extends Edge> List<T> randomEdgeSet(Graph graph, double p, Random random) { if (p < 0 || p > 1) throw new IllegalArgumentException("p must be between 0 and 1"); Set<Integer> subset = RandomTools.randomPsubset(graph.getEdgeCount(), p, null, random); List<T> result = new ArrayList<T>(subset.size()); for (int i : subset) result.add(graph.<T> getEdge(i)); return result; }
double p = 0; double M = 0; double n = graph.getEdgeCount(); double W; double S = 0; for (int i = 0; i < graph.getEdgeCount(); i++) { Edge e = graph.getEdge(i); Object idx0 = e.getNode0().getAttribute(communityAttributeKey);
/** * Creates copies of all graph arcs and edges. Instantiates and fills * {@link #nodes} and {@link #arcs}. */ protected void cloneGraph() { nodes = new HashMap<String, NSNode>(4 * graph.getNodeCount() / 3 + 2); for (Node node : graph) { NSNode copy = new NSNode(node); nodes.put(copy.id, copy); } int arcCount = graph.getEdgeCount(); for (Edge edge : graph.getEachEdge()) if (!edge.isDirected()) arcCount++; arcs = new HashMap<String, NSArc>(4 * arcCount / 3 + 1); for (Edge edge : graph.getEachEdge()) { NSArc copy = new NSArc(edge, true); arcs.put(copy.id, copy); if (!edge.isDirected()) { copy = new NSArc(edge, false); arcs.put(copy.id, copy); } } }
/** * Creates copies of all graph arcs and edges. Instantiates and fills * {@link #nodes} and {@link #arcs}. */ protected void cloneGraph() { nodes = new HashMap<String, NSNode>(4 * graph.getNodeCount() / 3 + 2); for (Node node : graph) { NSNode copy = new NSNode(node); nodes.put(copy.id, copy); } int arcCount = graph.getEdgeCount(); for (Edge edge : graph.getEachEdge()) if (!edge.isDirected()) arcCount++; arcs = new HashMap<String, NSArc>(4 * arcCount / 3 + 1); for (Edge edge : graph.getEachEdge()) { NSArc copy = new NSArc(edge, true); arcs.put(copy.id, copy); if (!edge.isDirected()) { copy = new NSArc(edge, false); arcs.put(copy.id, copy); } } }