2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
11 * Created on Apr 21, 2004
13 package edu.uci.ics.jung.algorithms.transformation;
15 import org.apache.commons.collections15.Factory;
17 import edu.uci.ics.jung.graph.DirectedGraph;
18 import edu.uci.ics.jung.graph.Graph;
19 import edu.uci.ics.jung.graph.UndirectedGraph;
20 import edu.uci.ics.jung.graph.util.EdgeType;
21 import edu.uci.ics.jung.graph.util.Pair;
24 * <p>Functions for transforming graphs into directed or undirected graphs.</p>
27 * @author Danyel Fisher
28 * @author Joshua O'Madadhain
30 public class DirectionTransformer
34 * Transforms <code>graph</code> (which may be of any directionality)
35 * into an undirected graph. (This may be useful for
36 * visualization tasks).
39 * <li/>Vertices are copied from <code>graph</code>.
40 * <li/>Directed edges are 'converted' into a single new undirected edge in the new graph.
41 * <li/>Each undirected edge (if any) in <code>graph</code> is 'recreated' with a new undirected edge in the new
42 * graph if <code>create_new</code> is true, or copied from <code>graph</code> otherwise.
45 * @param graph the graph to be transformed
46 * @param create_new specifies whether existing undirected edges are to be copied or recreated
47 * @param graph_factory used to create the new graph object
48 * @param edge_factory used to create new edges
49 * @return the transformed <code>Graph</code>
51 public static <V,E> UndirectedGraph<V,E> toUndirected(Graph<V,E> graph,
52 Factory<UndirectedGraph<V,E>> graph_factory,
53 Factory<E> edge_factory, boolean create_new)
55 UndirectedGraph<V,E> out = graph_factory.create();
57 for (V v : graph.getVertices())
60 for (E e : graph.getEdges())
62 Pair<V> endpoints = graph.getEndpoints(e);
63 V v1 = endpoints.getFirst();
64 V v2 = endpoints.getSecond();
66 if (graph.getEdgeType(e) == EdgeType.DIRECTED || create_new)
67 to_add = edge_factory.create();
70 out.addEdge(to_add, v1, v2, EdgeType.UNDIRECTED);
76 * Transforms <code>graph</code> (which may be of any directionality)
77 * into a directed graph.
80 * <li/>Vertices are copied from <code>graph</code>.
81 * <li/>Undirected edges are 'converted' into two new antiparallel directed edges in the new graph.
82 * <li/>Each directed edge (if any) in <code>graph</code> is 'recreated' with a new edge in the new
83 * graph if <code>create_new</code> is true, or copied from <code>graph</code> otherwise.
86 * @param graph the graph to be transformed
87 * @param create_new specifies whether existing directed edges are to be copied or recreated
88 * @param graph_factory used to create the new graph object
89 * @param edge_factory used to create new edges
90 * @return the transformed <code>Graph</code>
92 public static <V,E> Graph<V,E> toDirected(Graph<V,E> graph, Factory<DirectedGraph<V,E>> graph_factory,
93 Factory<E> edge_factory, boolean create_new)
95 DirectedGraph<V,E> out = graph_factory.create();
97 for (V v : graph.getVertices())
100 for (E e : graph.getEdges())
102 Pair<V> endpoints = graph.getEndpoints(e);
103 if (graph.getEdgeType(e) == EdgeType.UNDIRECTED)
105 V v1 = endpoints.getFirst();
106 V v2 = endpoints.getSecond();
107 out.addEdge(edge_factory.create(), v1, v2, EdgeType.DIRECTED);
108 out.addEdge(edge_factory.create(), v2, v1, EdgeType.DIRECTED);
110 else // if the edge is directed, just add it
112 V source = graph.getSource(e);
113 V dest = graph.getDest(e);
114 E to_add = create_new ? edge_factory.create() : e;
115 out.addEdge(to_add, source, dest, EdgeType.DIRECTED);