/** * Compute the connector * * @param graph the graph * @return the connector */ private String computeConnector(Graph<V, E> graph) { StringBuilder connectorBuilder = new StringBuilder(); if (graph.getType().isDirected()) { connectorBuilder.append(" ").append(DOTUtils.DIRECTED_GRAPH_EDGEOP).append(" "); } else { connectorBuilder.append(" ").append(DOTUtils.UNDIRECTED_GRAPH_EDGEOP).append(" "); } return connectorBuilder.toString(); }
private void writeGraphStart(TransformerHandler handler, Graph<V, E> g) throws SAXException { // <graph> AttributesImpl attr = new AttributesImpl(); attr.addAttribute( "", "", "edgedefault", "CDATA", g.getType().isDirected() ? "directed" : "undirected"); handler.startElement("", "", "graph", attr); }
/** * Checks that the specified graph is directed and throws a customized * {@link IllegalArgumentException} if it is not. Also checks that the graph reference is not * {@code null} and throws a {@link NullPointerException} if it is. * * @param graph the graph reference to check for beeing directed and not null * @param message detail message to be used in the event that an exception is thrown * @param <V> the graph vertex type * @param <E> the graph edge type * @return {@code graph} if directed and not {@code null} * @throws NullPointerException if {@code graph} is {@code null} * @throws IllegalArgumentException if {@code graph} is not directed */ public static <V, E> Graph<V, E> requireDirected(Graph<V, E> graph, String message) { if (graph == null) throw new NullPointerException(GRAPH_CANNOT_BE_NULL); if (!graph.getType().isDirected()) { throw new IllegalArgumentException(message); } return graph; }
/** * Constructs a new BiconnectivityInspector * * @param graph the input graph */ public BiconnectivityInspector(Graph<V, E> graph) { this.graph = Objects.requireNonNull(graph); if (graph.getType().isDirected()) this.graph = new AsUndirectedGraph<>(graph); }
@Override public BiFunction<Graph<V, E>, GraphType, Specifics<V, E>> getSpecificsFactory() { return (BiFunction<Graph<V, E>, GraphType, Specifics<V, E>> & Serializable) (graph, type) -> { if (type.isDirected()) { return new FastLookupDirectedSpecifics<>( graph, new LinkedHashMap<>(), new HashMap<>(), getEdgeSetFactory()); } else { return new FastLookupUndirectedSpecifics<>( graph, new LinkedHashMap<>(), new HashMap<>(), getEdgeSetFactory()); } }; }
/** * Construct a new maximum flow * * @param network the network * @param epsilon the tolerance for the comparison of floating point values */ public MaximumFlowAlgorithmBase(Graph<V, E> network, double epsilon) { this.network = network; this.directedGraph = network.getType().isDirected(); this.comparator = new ToleranceDoubleComparator(epsilon); }
@Override public BiFunction<Graph<V, E>, GraphType, Specifics<V, E>> getSpecificsFactory() { return (BiFunction<Graph<V, E>, GraphType, Specifics<V, E>> & Serializable) (graph, type) -> { if (type.isDirected()) { return new DirectedSpecifics<V, E>( graph, new LinkedHashMap<>(), getEdgeSetFactory()); } else { return new UndirectedSpecifics<>( graph, new LinkedHashMap<>(), getEdgeSetFactory()); } }; }
/** * Compute the header * * @param graph the graph * @return the header */ private String computeHeader(Graph<V, E> graph) { StringBuilder headerBuilder = new StringBuilder(); if (!graph.getType().isAllowingMultipleEdges()) { headerBuilder.append(DOTUtils.DONT_ALLOW_MULTIPLE_EDGES_KEYWORD).append(" "); } if (graph.getType().isDirected()) { headerBuilder.append(DOTUtils.DIRECTED_GRAPH_KEYWORD); } else { headerBuilder.append(DOTUtils.UNDIRECTED_GRAPH_KEYWORD); } headerBuilder.append(" ").append(computeGraphId(graph)).append(" {"); return headerBuilder.toString(); }
/** * Creates a connectivity inspector for the specified graph. * * @param g the graph for which a connectivity inspector to be created. */ public ConnectivityInspector(Graph<V, E> g) { init(); this.graph = Objects.requireNonNull(g); if (g.getType().isDirected()) this.graph = new AsUndirectedGraph<>(g); }
/** * Compute the centrality index */ private void compute() { // initialize result container this.scores = new HashMap<>(); this.graph.vertexSet().forEach(v -> this.scores.put(v, 0.0)); // compute for each source this.graph.vertexSet().forEach(this::compute); // For undirected graph, divide scores by two as each shortest path // considered twice. if (!this.graph.getType().isDirected()) { this.scores.forEach((v, score) -> this.scores.put(v, score / 2)); } if (normalize) { int n = this.graph.vertexSet().size(); int normalizationFactor = (n - 1) * (n - 2); if (normalizationFactor != 0) { this.scores.forEach((v, score) -> this.scores.put(v, score / normalizationFactor)); } } }
/** * Checks that the specified graph is directed or undirected and throws a customized * {@link IllegalArgumentException} if it is not. Also checks that the graph reference is not * {@code null} and throws a {@link NullPointerException} if it is. * * @param graph the graph reference to check for beeing directed or undirected and not null * @param message detail message to be used in the event that an exception is thrown * @param <V> the graph vertex type * @param <E> the graph edge type * @return {@code graph} if directed and not {@code null} * @throws NullPointerException if {@code graph} is {@code null} * @throws IllegalArgumentException if {@code graph} is mixed */ public static <V, E> Graph<V, E> requireDirectedOrUndirected(Graph<V, E> graph, String message) { if (graph == null) throw new NullPointerException(GRAPH_CANNOT_BE_NULL); if (!graph.getType().isDirected() && !graph.getType().isUndirected()) { throw new IllegalArgumentException(message); } return graph; }
/** * Creates a weak chordality inspector for the {@code graph} * * @param graph the inspected {@code graph} */ public WeakChordalityInspector(Graph<V, E> graph) { this.graph = Objects.requireNonNull(graph); if (graph.getType().isDirected()) { this.graph = new AsUndirectedGraph<>(graph); } n = graph.vertexSet().size(); m = graph.edgeSet().size(); initMappings(); }
/** * {@inheritDoc} * * <p> * By default this method returns the sum of in-degree and out-degree. The exact value returned * depends on the type of the underlying graph. */ @Override public int degreeOf(V vertex) { if (baseType.isDirected()) { return inDegreeOf(vertex) + outDegreeOf(vertex); } else { int degree = 0; Iterator<E> it = edgesOf(vertex).iterator(); while (it.hasNext()) { E e = it.next(); degree++; if (getEdgeSource(e).equals(getEdgeTarget(e))) { degree++; } } return degree; } }
/** * @see java.lang.Object#toString() */ @Override public String toString() { return toStringFromSets(vertexSet(), edgeSet(), getType().isDirected()); } }
/** * Returns a string of the parenthesized pair (V, E) representing this G=(V,E) graph. 'V' is the * string representation of the vertex set, and 'E' is the string representation of the edge * set. * * @return a string representation of this graph. */ @Override public String toString() { return toStringFromSets(vertexSet(), edgeSet(), this.getType().isDirected()); }
/** * Creates a chordality inspector for {@code graph}, which uses an iterator defined by the * second parameter as an internal iterator. * * @param graph the graph for which a chordality inspector is to be created. * @param iterationOrder the constant, which defines iterator to be used by this * {@code ChordalityInspector}. */ public ChordalityInspector(Graph<V, E> graph, IterationOrder iterationOrder) { Objects.requireNonNull(graph); if (graph.getType().isDirected()) { this.graph = new AsUndirectedGraph<>(graph); } else { this.graph = graph; } this.iterationOrder = iterationOrder; this.hole = null; if (iterationOrder == IterationOrder.MCS) { this.orderIterator = new MaximumCardinalityIterator<>(graph); } else { this.orderIterator = new LexBreadthFirstIterator<>(graph); } }
/** * Returns an undirected view of the specified graph. If the specified graph is directed, * returns an undirected view of it. If the specified graph is already undirected, just returns * it. * * @param g the graph for which an undirected view is to be returned * @param <V> the graph vertex type * @param <E> the graph edge type * * @return an undirected view of the specified graph, if it is directed, or or the specified * graph itself if it is already undirected. * * @throws IllegalArgumentException if the graph is neither directed nor undirected * @see AsUndirectedGraph */ public static <V, E> Graph<V, E> undirectedGraph(Graph<V, E> g) { if (g.getType().isDirected()) { return new AsUndirectedGraph<>(g); } else if (g.getType().isUndirected()) { return g; } else { throw new IllegalArgumentException("graph must be either directed or undirected"); } }
private void exportAdjacencyMatrix(Graph<V, E> g, Writer writer) { for (V from : g.vertexSet()) { // assign ids in vertex set iteration order vertexIDProvider.getName(from); } PrintWriter out = new PrintWriter(writer); if (g.getType().isDirected()) { for (V from : g.vertexSet()) { exportAdjacencyMatrixVertex(out, from, Graphs.successorListOf(g, from)); } } else { for (V from : g.vertexSet()) { exportAdjacencyMatrixVertex(out, from, Graphs.neighborListOf(g, from)); } } out.flush(); }
/** * Create a graph type builder which will create a graph with the same type as the one provided. * * @param type the graph type * @return the graph type builder * @param <V> the graph vertex type * @param <E> the graph edge type */ public static <V, E> GraphTypeBuilder<V, E> forGraphType(GraphType type) { GraphTypeBuilder<V, E> builder = new GraphTypeBuilder<>( type.isDirected() || type.isMixed(), type.isUndirected() || type.isMixed()); builder.weighted = type.isWeighted(); builder.allowingSelfLoops = type.isAllowingSelfLoops(); builder.allowingMultipleEdges = type.isAllowingMultipleEdges(); return builder; }
/** * Construct a new Builder. * * @param type the type to base the builder */ public Builder(GraphType type) { this.directed = type.isDirected() || type.isMixed(); this.undirected = type.isUndirected() || type.isMixed(); this.allowSelfLoops = type.isAllowingSelfLoops(); this.allowMultipleEdges = type.isAllowingMultipleEdges(); this.weighted = type.isWeighted(); this.allowCycles = type.isAllowingCycles(); this.modifiable = type.isModifiable(); }