NAME

Graph::Convert - Convert between graph formats: Graph and Graph::Easy

SYNOPSIS

        use Graph::Convert;
        
        my $graph_easy = Graph::Easy->new();
        $graph_easy->add_edge ('Bonn', 'Berlin');
        $graph_easy->add_edge ('Berlin', 'Berlin');

        # from "Graph::Easy" to "Graph"
        my $graph = Graph::Convert->as_graph ( $graph_easy );
        
        # and back to "Graph::Easy"
        my $ge = Graph::Convert->as_graph_easy ( $graph );

        print $ge->as_ascii( );

        # Outputs something like:

        #                +----+
        #                v    |
        # +------+     +--------+
        # | Bonn | --> | Berlin |
        # +------+     +--------+

DESCRIPTION

Graph::Convert lets you convert graphs between the graph formats from Graph and Graph::Easy.

It takes a graph object in either format, and converts it to the desired output format. It handles simple graphs (directed or undirected) as well as multi-edged graphs, and also carries the attributes over.

This enables you to use all the layout and formatting capabilities of Graph::Easy on Graph objects, as well as using the extensive graph algorithms and manipulations of Graph on Graph::Easy objects.

Graph vs. Graph::Easy

Both Graph and Graph::Easy represent graphs, e.g. vertices (or nodes) connected by edges. These graphs can have (arbitrary) attributes attached to the graph, nodes or edges.

Both formats can serialize the graph by creating a text-representation, but unlike Graph::Easy, Graph is not able to create the graph back from the string form.

There are, however, some slight differences between these two packages:

Graph

Graph has different representations for multi-edges and simple graphs, making it somewhat complicated to switch between these two.

It does have an extensive set of algorithms to manipulate the graph, but no layout capabilities.

Graph does not support the notion of subgraphs (or "groups" as they are called in Graph::Easy). While you could assign some sort of group attribute to nodes, this would have no special meaning to the Graph module.

Graph::Easy

Graph::Easy uses the same representation for multi-edged and simple graphs, but has only basic operations to manipulate the graph and its contents.

It has, however, a build-in layouter which can lay out the graph on a grid, as well the ability to output Graphviz and VCG/GDL code. This enables output of ASCII, HTML, SVG and all the formats that graphviz supports, like PDF or PNG.

Graph::Easy supports subgraphs (aka groups).

In addition, Graph::Easy supports class attributes. By setting the attribute for a class and putting objects (nodes/edges etc) into the proper class, it is easier to manipulate attributes for many objects at once.

METHODS

Graph::Convert supports the following methods:

as_graph()

        use Graph::Convert;

        my $graph_easy = Graph::Easy->new( );
        $graph_easy->add_edge('A','B');
        my $graph = Graph::Convert->as_graph( $graph_easy );

        my $undirected_graph = 
           Graph::Convert->as_graph( $graph_easy, { undirected => 1 } );

Converts the given Graph::Easy object into a Graph object.

This routine creates either a simple or a multiedged graph, depending on whether the input Graph::Easy object is a simple graph or not.

If you want to force the output to be a multiedged graph object, use as_multiedged_graph.

Forcing the output to be a simple graph when the input is multi-edged is not supported, as that would require to drop arbitrary edges from the input.

The optional parameter is an hash ref with options that is passed to Graph->new().

Directed and undirected input graphs result automatically in the appropritate type of Graph object being created, but you can force the creation of an undirected graph by either passing { undirected => 1 } as option or use as_undirected_graph().

as_undirected_graph()

        use Graph::Convert;

        my $graph_easy = Graph::Easy->new( );
        $graph_easy->add_edge('A','B');
        my $graph = Graph::Convert->as_undirected_graph( $graph_easy );

Converts the given Graph::Easy object into an undirected Graph object, regardless whether the input graph is a directed graph or not.

as_multiedged_graph()

        use Graph::Convert;

        my $graph_easy = Graph::Easy->new( );
        $graph_easy->add_edge('A','B');
        my $graph = Graph::Convert->as_multiedged_graph( $graph_easy );

Converts the given Graph::Easy object into a multi-edged Graph object, even if the input graph is a simple graph (meaning there is only one edge going from node A to node B).

To create a multi-edged undirected graph, pass in { undirected => 1 } as option:

        use Graph::Convert;

        my $graph_easy = Graph::Easy->new( );
        $graph_easy->add_edge('A','B');
        my $graph = Graph::Convert->as_multiedged_graph( $graph_easy, 
                { undirected => 1 } );

as_graph_easy()

        use Graph::Convert;

        my $graph = Graph->new( );
        $graph_easy->add_edge('A','B');
        my $graph_easy = Graph::Convert->as_graph_easy( $graph_easy );

Converts the given Graph object into a Graph::Easy object.

This routine handles simple (directed or undirected) as well as multi-edged graphs automatically.

Multi-vertexed graphs are not supported e.g. each node must exist only once in the input graph.

SEE ALSO

Graph, Graph::Easy and Graph::Easy::Manual.

LICENSE

This library is free software; you can redistribute it and/or modify it under the terms of the GPL version 2 or later.

See the LICENSE file for a copy of the GPL 2.

AUTHOR

Copyright (C) 2006 - 2007 by Tels http://bloodgate.com