/** * 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()); }
@Override public Function<GraphType, IntrusiveEdgesSpecifics<V, E>> getIntrusiveEdgesSpecificsFactory() { return (Function<GraphType, IntrusiveEdgesSpecifics<V, E>> & Serializable) (type) -> { if (type.isWeighted()) { return new WeightedIntrusiveEdgesSpecifics<V, E>(new LinkedHashMap<>()); } else { return new UniformIntrusiveEdgesSpecifics<>(new LinkedHashMap<>()); } }; }
@Override public Function<GraphType, IntrusiveEdgesSpecifics<V, E>> getIntrusiveEdgesSpecificsFactory() { return (Function<GraphType, IntrusiveEdgesSpecifics<V, E>> & Serializable) (type) -> { if (type.isWeighted()) { return new WeightedIntrusiveEdgesSpecifics<V, E>(new LinkedHashMap<>()); } else { return new UniformIntrusiveEdgesSpecifics<>(new LinkedHashMap<>()); } }; }
/** * Checks that the specified graph is weighted 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 being weighted and not null * @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 weighted */ public static <V, E> Graph<V, E> requireWeighted(Graph<V, E> graph) { if (graph == null) throw new NullPointerException(GRAPH_CANNOT_BE_NULL); if (!graph.getType().isWeighted()) { throw new IllegalArgumentException(GRAPH_MUST_BE_WEIGHTED); } return graph; } }
private void readGraph6(Graph<V, E> g, Map<Integer, V> vertexIndexMap) throws ImportException { // check whether there's enough data int requiredBytes = (int) Math.ceil(vertexIndexMap.size() * (vertexIndexMap.size() - 1) / 12.0) + byteIndex; if (bytes.length < requiredBytes) throw new ImportException( "Graph string seems to be corrupt. Not enough data to read graph6 graph"); // Read the lower triangle of the adjacency matrix of G for (int i = 0; i < vertexIndexMap.size(); i++) { for (int j = 0; j < i; j++) { int bit = getBits(1); if (bit == 1) { V from = vertexIndexMap.get(i); V to = vertexIndexMap.get(j); String label = "e_" + i + "_" + j; E e = edgeProvider.buildEdge(from, to, label, new HashMap<>()); g.addEdge(from, to, e); if (g.getType().isWeighted()) g.setEdgeWeight(e, defaultWeight); } } } }
private void exportEdges(PrintWriter out, Graph<V, E> g) { boolean exportEdgeWeights = parameters.contains(Parameter.EXPORT_EDGE_WEIGHTS); boolean exportEdgeLabels = parameters.contains(Parameter.EXPORT_EDGE_LABELS); for (E edge : g.edgeSet()) { out.println(TAB1 + "edge"); out.println(TAB1 + "["); String id = edgeIDProvider.getName(edge); out.println(TAB2 + "id" + DELIM + id); String s = vertexIDProvider.getName(g.getEdgeSource(edge)); out.println(TAB2 + "source" + DELIM + s); String t = vertexIDProvider.getName(g.getEdgeTarget(edge)); out.println(TAB2 + "target" + DELIM + t); if (exportEdgeLabels) { String label = (edgeLabelProvider == null) ? edge.toString() : edgeLabelProvider.getName(edge); out.println(TAB2 + "label" + DELIM + quoted(label)); } if (exportEdgeWeights && g.getType().isWeighted()) { out.println(TAB2 + "weight" + DELIM + Double.toString(g.getEdgeWeight(edge))); } out.println(TAB1 + "]"); } }
this.graph.getType().isWeighted() ? new WeightedQueue() : new UnweightedQueue(); queue.insert(s, 0.0);
g.addEdge(from, to, e); if (g.getType().isWeighted()) g.setEdgeWeight(e, defaultWeight);
@Override public Spanner<E> getSpanner() { if (graph.getType().isWeighted()) { return new WeightedSpannerAlgorithm().run(); } else { return new UnweightedSpannerAlgorithm().run(); } }
boolean weighted = g.getType().isWeighted(); Map<V, Double> weights; if (weighted) {
graph.addEdge(from, to, e); if (graph.getType().isWeighted()) { double weight = defaultWeight; if (cols.length > 3) {
graph.getVertexSupplier(), graph.getEdgeSupplier(), graph.getType().isWeighted()); Graphs.addGraph(eulerGraph, graph); Map<E, GraphPath<V, E>> shortcutEdges = new HashMap<>();
@Override public GraphType getType() { return new DefaultGraphType.Builder() .directed().weighted(super.getType().isWeighted()).allowMultipleEdges(false) .allowSelfLoops(false).allowCycles(false).build(); }
graph.getVertexSupplier(), graph.getEdgeSupplier(), graph.getType().isWeighted()); Graphs.addGraph(eulerGraph, graph); Map<E, GraphPath<V, E>> shortcutEdges = new HashMap<>();
/** * 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); }
/** * 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(); }
if (g.getType().isSimple()) complementGraph = new SimpleGraph<>( g.getVertexSupplier(), g.getEdgeSupplier(), g.getType().isWeighted()); else complementGraph = new Multigraph<>( g.getVertexSupplier(), g.getEdgeSupplier(), g.getType().isWeighted()); new ComplementGraphGenerator<>(g).generateGraph(complementGraph);