/** * {@inheritDoc} */ @Override public GraphType getType() { return base.getType(); }
/** * {@inheritDoc} */ @Override public GraphType getType() { return delegate.getType(); }
/** * Constructor for AsWeightedGraph where the weights are provided through a map. Invocations of * the @link{setEdgeWeight} method will update the map. Moreover, calls to @link{setEdgeWeight} * are propagated to the underlying graph. * * @param graph the backing graph over which a weighted view is to be created. * @param weights the map containing the edge weights. * @throws NullPointerException if the graph or the weights are null. */ public AsWeightedGraph(Graph<V, E> graph, Map<E, Double> weights) { this(graph, weights, graph.getType().isWeighted()); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * Test whether an undirected graph is a tree. * * @param graph the input graph * @param <V> the graph vertex type * @param <E> the graph edge type * @return true if the graph is tree, false otherwise */ public static <V, E> boolean isTree(Graph<V, E> graph) { if (!graph.getType().isUndirected()) { throw new IllegalArgumentException(GRAPH_MUST_BE_UNDIRECTED); } return (graph.edgeSet().size() == (graph.vertexSet().size() - 1)) && isConnected(graph); }
/** * Creates a new induced subgraph. Running-time = O(1). * * @param base the base (backing) graph on which the subgraph will be based. * @param vertexMask vertices to exclude in the subgraph. If a vertex is masked, it is as if it * is not in the subgraph. Edges incident to the masked vertex are also masked. * @param edgeMask edges to exclude in the subgraph. If an edge is masked, it is as if it is not * in the subgraph. */ public MaskSubgraph(Graph<V, E> base, Predicate<V> vertexMask, Predicate<E> edgeMask) { super(); this.base = Objects.requireNonNull(base, "Invalid graph provided"); this.baseType = base.getType(); this.vertexMask = Objects.requireNonNull(vertexMask, "Invalid vertex mask provided"); this.edgeMask = Objects.requireNonNull(edgeMask, "Invalid edge mask provided"); this.vertices = new MaskVertexSet<>(base.vertexSet(), vertexMask); this.edges = new MaskEdgeSet<>(base, base.edgeSet(), vertexMask, edgeMask); }
/** * 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(); }
/** * Test whether an undirected graph is a forest. A forest is a set of disjoint trees. By * definition, any acyclic graph is a forest. This includes the empty graph and the class of * tree graphs. * * @param graph the input graph * @param <V> the graph vertex type * @param <E> the graph edge type * @return true if the graph is forest, false otherwise */ public static <V, E> boolean isForest(Graph<V, E> graph) { if (!graph.getType().isUndirected()) { throw new IllegalArgumentException(GRAPH_MUST_BE_UNDIRECTED); } if (graph.vertexSet().isEmpty()) // null graph is not a forest return false; int nrConnectedComponents = new ConnectivityInspector<>(graph).connectedSets().size(); return graph.edgeSet().size() + nrConnectedComponents == graph.vertexSet().size(); }
/** * Create a graph type builder which will create the same graph type as the parameter graph. The * new graph will use the same vertex and edge suppliers as the input graph. * * @param graph a graph * @return a type builder * @param <V> the graph vertex type * @param <E> the graph edge type */ public static <V, E> GraphTypeBuilder<V, E> forGraph(Graph<V, E> graph) { GraphTypeBuilder<V, E> builder = forGraphType(graph.getType()); builder.vertexSupplier = graph.getVertexSupplier(); builder.edgeSupplier = graph.getEdgeSupplier(); return builder; }
/** * {@inheritDoc} */ @Override public MinimumCostFlow<E> getMinimumCostFlow( final MinimumCostFlowProblem<V, E> minimumCostFlowProblem) { this.problem = Objects.requireNonNull(minimumCostFlowProblem); if (problem.getGraph().getType().isUndirected()) { throw new IllegalArgumentException( "The algorithm doesn't support undirected flow networks"); } n = problem.getGraph().vertexSet().size(); m = problem.getGraph().edgeSet().size(); calculateMinimumCostFlow(); return minimumCostFlow; }
@Override public Spanner<E> getSpanner() { if (graph.getType().isWeighted()) { return new WeightedSpannerAlgorithm().run(); } else { return new UnweightedSpannerAlgorithm().run(); } }
/** * Returns all anticomponents of a graph and a vertex set. * * @param g A Graph * @param Y A set of vertices * @return List of anticomponents of Y in g */ private List<Set<V>> findAllAnticomponentsOfY(Graph<V, E> g, Set<V> Y) { Graph<V, E> target; if (g.getType().isSimple()) target = new SimpleGraph<>( g.getVertexSupplier(), g.getEdgeSupplier(), g.getType().isWeighted()); else target = new Multigraph<>( g.getVertexSupplier(), g.getEdgeSupplier(), g.getType().isWeighted()); new ComplementGraphGenerator<>(g).generateGraph(target); return findAllComponents(target, Y); }
private void computeGlobalClusteringCoefficient() { NeighborCache<V, E> neighborCache = new NeighborCache<>(graph); computed = true; double numberTriplets = 0; for (V v : graph.vertexSet()) { if (graph.getType().isUndirected()) { numberTriplets += 1.0 * graph.degreeOf(v) * (graph.degreeOf(v) - 1) / 2; } else { numberTriplets += 1.0 * neighborCache.predecessorsOf(v).size() * neighborCache.successorsOf(v).size(); } } globalClusteringCoefficient = 3 * GraphMetrics.getNumberOfTriangles(graph) / numberTriplets; }
/** * Creates a new subgraph. * * @param base the base (backing) graph on which the subgraph will be based. * @param vertexSubset vertices to include in the subgraph. If <code>null</code> then all * vertices are included. * @param edgeSubset edges to in include in the subgraph. If <code>null</code> then all the * edges whose vertices found in the graph are included. */ public AsSubgraph(Graph<V, E> base, Set<? extends V> vertexSubset, Set<? extends E> edgeSubset) { super(); this.base = GraphTests.requireDirectedOrUndirected(base); this.baseType = base.getType(); this.isInduced = edgeSubset == null; if (base instanceof ListenableGraph<?, ?>) { ((ListenableGraph<V, E>) base).addGraphListener(new BaseGraphListener()); } initialize(vertexSubset, edgeSubset); }
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(); }
private Graph<DistinctGraphObject<V, V, E>, DistinctGraphObject<E, V, E>> getDistinctObjectGraph(Graph<V, E> graph) { Graph<DistinctGraphObject<V, V, E>, DistinctGraphObject<E, V, E>> transformedGraph = GraphTypeBuilder .<DistinctGraphObject<V, V, E>, DistinctGraphObject<E, V, E>> forGraphType(graph.getType()) .buildGraph(); for (V vertex : graph.vertexSet()) { transformedGraph.addVertex(new DistinctGraphObject<>(vertex, graph)); } for (E edge : graph.edgeSet()) { transformedGraph.addEdge( new DistinctGraphObject<>(graph.getEdgeSource(edge), graph), new DistinctGraphObject<>(graph.getEdgeTarget(edge), graph), new DistinctGraphObject<>(edge, graph)); } return transformedGraph; }