/*
 * Copyright (c) 2003, The JUNG Authors 
 *
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see either
 * "license.txt" or
 * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
 */
/*
 * Created on Apr 21, 2004
 */
package edu.uci.ics.jung.algorithms.transformation;

import com.google.common.base.Supplier;

import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedGraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.graph.util.Pair;

/**
 * 

Functions for transforming graphs into directed or undirected graphs. * * * @author Danyel Fisher * @author Joshua O'Madadhain */ public class DirectionTransformer { /** * Transforms graph (which may be of any directionality) * into an undirected graph. (This may be useful for * visualization tasks). * Specifically: *

* * @param graph the graph to be transformed * @param create_new specifies whether existing undirected edges are to be copied or recreated * @param graph_factory used to create the new graph object * @param edge_factory used to create new edges * @param the vertex type * @param the edge type * @return the transformed Graph */ public static UndirectedGraph toUndirected(Graph graph, Supplier> graph_factory, Supplier edge_factory, boolean create_new) { UndirectedGraph out = graph_factory.get(); for (V v : graph.getVertices()) out.addVertex(v); for (E e : graph.getEdges()) { Pair endpoints = graph.getEndpoints(e); V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); E to_add; if (graph.getEdgeType(e) == EdgeType.DIRECTED || create_new) to_add = edge_factory.get(); else to_add = e; out.addEdge(to_add, v1, v2, EdgeType.UNDIRECTED); } return out; } /** * Transforms graph (which may be of any directionality) * into a directed graph. * Specifically: *
    *
  • Vertices are copied from graph. *
  • Undirected edges are 'converted' into two new antiparallel directed edges in the new graph. *
  • Each directed edge (if any) in graph is 'recreated' with a new edge in the new * graph if create_new is true, or copied from graph otherwise. *
* * @param graph the graph to be transformed * @param create_new specifies whether existing directed edges are to be copied or recreated * @param graph_factory used to create the new graph object * @param edge_factory used to create new edges * @param the vertex type * @param the edge type * @return the transformed Graph */ public static Graph toDirected(Graph graph, Supplier> graph_factory, Supplier edge_factory, boolean create_new) { DirectedGraph out = graph_factory.get(); for (V v : graph.getVertices()) out.addVertex(v); for (E e : graph.getEdges()) { Pair endpoints = graph.getEndpoints(e); if (graph.getEdgeType(e) == EdgeType.UNDIRECTED) { V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); out.addEdge(edge_factory.get(), v1, v2, EdgeType.DIRECTED); out.addEdge(edge_factory.get(), v2, v1, EdgeType.DIRECTED); } else // if the edge is directed, just add it { V source = graph.getSource(e); V dest = graph.getDest(e); E to_add = create_new ? edge_factory.get() : e; out.addEdge(to_add, source, dest, EdgeType.DIRECTED); } } return out; } }

Ads help maintain this website.