2.2.10. Graph.h

2.2.10.1. Class Graph

class Graph

This class models an undirected graph with labels on vertices and edges, without loops and without parallel edges. Certain labels are regarded as models of chemical atoms and bonds. See Molecule Encoding for more information on this. See also GraphGraphInterface.h for the documentation for the graph interface for this class.

2.2.10.1.1. Synopsis

struct Graph {
    class Vertex;
    class Edge;
    class VertexIterator;
    class VertexRange;
    class EdgeIterator;
    class EdgeRange;
    class IncidentEdgeIterator;
    class IncidentEdgeRange;
private: // The actual class interface
    Graph(std::unique_ptr<lib::Graph::Single> g);
    Graph(const Graph&) = delete;
    Graph &operator=(const Graph&) = delete;
public:
    ~Graph();
    std::size_t getId() const;
    friend std::ostream &operator<<(std::ostream &s, const mod::Graph &g);
    lib::Graph::Single &getGraph() const;
public: // graph interface
    std::size_t numVertices() const;
    VertexRange vertices() const;
    std::size_t numEdges() const;
    EdgeRange edges() const;
public:
    std::pair<std::string, std::string> print() const;
    std::pair<std::string, std::string> print(const GraphPrinter &first, const GraphPrinter &second) const;
    std::string getGMLString(bool withCoords = false) const;
    std::string printGML(bool withCoords = false) const;
    const std::string &getName() const;
    void setName(std::string name) const;
    const std::string &getSmiles() const;
    const std::string &getGraphDFS() const;
    const std::string &getLinearEncoding() const;
    bool getIsMolecule() const;
    double getEnergy() const;
    void cacheEnergy(double value) const;
    double getMolarMass() const;
    unsigned int vLabelCount(const std::string &label) const;
    unsigned int eLabelCount(const std::string &label) const;
    std::size_t isomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches) const;
    std::size_t monomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches) const;
    std::shared_ptr<Graph> makePermutation() const;
    void setImage(std::shared_ptr<Function<std::string()> > image);
    std::shared_ptr<Function<std::string()> > getImage() const;
    void setImageCommand(std::string cmd);
    std::string getImageCommand() const;
    Vertex getVertexFromExternalId(int id) const;
private:
    struct Pimpl;
    std::unique_ptr<Pimpl> p;
public:
    static std::shared_ptr<Graph> graphGMLString(const std::string &data);
    static std::shared_ptr<Graph> graphGML(const std::string &file);
    static std::shared_ptr<Graph> graphDFS(const std::string &graphDFS);
    static std::shared_ptr<Graph> smiles(const std::string &smiles);
    static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g);
    static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g, std::map<int, std::size_t> externalToInternalIds);
};

2.2.10.1.2. Details

std::size_t getId() const
Returns:the unique instance id among Graph objects.
std::size_t numVertices() const
Returns:the number of vertices in the graph.
VertexRange vertices() const
Returns:a range of all vertices in the graph.
std::size_t numEdges() const
Returns:the number of edges in the graph.
EdgeRange edges() const
Returns:a range of all edges in the graph.
std::pair<std::string, std::string> print() const
std::pair<std::string, std::string> print(const GraphPrinter &first, const GraphPrinter &second) const

Print the graph, using either the default options or the options in first and second. If first and second are the same, only one depiction will be made.

Returns:a pair of names for the PDF-files that will be compiled in post-processing. If first and second are the same, the two file prefixes are equal.
std::string getGMLString(bool withCoords = false) const
Returns:the GML representation of the graph, optionally with generated 2D coordinates.
Throws:LogicError when coordinates are requested, but none can be generated.
std::string printGML(bool withCoords = false) const

Print the GML representation of the graph, optionally with generated 2D coordinates.

Returns:the filename of the printed GML file.
Throws:LogicError when coordinates are requested, but none can be generated.
const std::string &getName() const
void setName(std::string name) const

Access the name of the graph.

const std::string &getSmiles() const
Returns:the canonical SMILES string of the graph, if it’s a molecule.
Throws:LogicError if the graph is not a molecule.
const std::string &getGraphDFS() const
Returns:the GraphDFS string of the graph.
const std::string &getLinearEncoding() const
Returns:the SMILES string if the graph is a molecule, otherwise the GraphDFS string.
bool getIsMolecule() const
Returns:whether or not the graph models a molecule. See Molecule Encoding.
double getEnergy() const
Returns:some energy value if the graph is a molecule. The energy is calculated using Open Babel, unless already calculated or cached by Graph::cacheEnergy().
void cacheEnergy(double value) const

If the graph models a molecule, sets the energy to a given value.

Throws:LogicError if the graph is not a molecule.
double getMolarMass() const
Returns:the molar mass of the graph, if it is a molecule. The molar mass is calculated by Open Babel.
unsigned int vLabelCount(const std::string &label) const
Returns:the number of vertices in the graph with the given label.
unsigned int eLabelCount(const std::string &label) const
Returns:the number of edges in the graph with the given label.
std::size_t isomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches) const
Returns:the number of isomorphisms found from this graph to g, but at most maxNumMatches.
std::size_t monomorphism(std::shared_ptr<Graph> g, std::size_t maxNumMatches) const
Returns:the number of monomorphisms from this graph to g, though at most maxNumMatches.
std::shared_ptr<Graph> makePermutation() const
Returns:a graph isomorphic to this, but with the vertex indices randomly permuted.
void setImage(std::shared_ptr<Function<std::string()>> image)

Set a custom depiction for the graph. The depiction file used will be the string returned by the given function, with .pdf appended. The function will only be called once. Give nullptr to use auto-generated depiction.

std::shared_ptr<Function<std::string()>> getImage() const
Returns:the current custom depiction file function.
void setImageCommand(std::string cmd)

Set a command to be run in post-processing if a custom depiction is set. The command is only run once.

std::string getImageCommand() const
Returns:the current post-processing command.
Vertex getVertexFromExternalId(int id) const

If the graph was not loaded from an external data format, then this function always return a null descriptor. If the graph was loaded from a SMILES string, but any class label was not unique, then the function always return a null descriptor.

Note

In general there is no correlation between external and internal ids.

Returns:the vertex descriptor for the given external id. The descriptor is null if the external id was not used.
static std::shared_ptr<Graph> graphGMLString(const std::string &data)
Returns:a graph created from the given GML data.
Throws:InputError on bad input.
static std::shared_ptr<Graph> graphGML(const std::string &file)
Returns:a graph loaded from the given GML file.
Throws:InputError on bad input.
static std::shared_ptr<Graph> graphDFS(const std::string &graphDFS)
Returns:a graph loaded from the given GraphDFS string.
Throws:InputError on bad input.
static std::shared_ptr<Graph> smiles(const std::string &smiles)
Returns:a graph representing a molecule, loaded from the given SMILES string.
Throws:InputError on bad input.
static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g)
static std::shared_ptr<Graph> makeGraph(std::unique_ptr<lib::Graph::Single> g, std::map<int, std::size_t> externalToInternalIds)
Returns:a graph wrapping the given internal graph object. If an id mapping is given, it will be used for the getVertexFromExternalId() function.