The C++ Graph Classes: A Tutorial - Graph(C++) and Graph_alg(C++)

Graph Subgraph Creation

It is often necessary to create new Graphs that are a substantial part of an existing Graph. The following Graph member function is defined for this purpose:

       Graph induced_graph(const Set_of_p<Vertex>& vset);

A new Graph can be created from a given set of Vertices in an existing Graph in the following way, where g is a previously defined Graph, and vpset is a set of some (or all) of the Vertices in g:

       Graph g2 = g.induced_graph(vpset);

g2 will contain the Vertices included in vpset, as well as all Edges whose source and destination are also in vpset.

For instance, in our manufacturing example, suppose we want to isolate the information received from the first question asked (``where is a given module needed?''), which describes a subassembly we'll call a thingamabob. We can use induced_graph to solve this problem, but we need to add one small piece of code first. Since the solution in the section, ``Where is a Given Module Needed?'', is returned in a List_of_p, and induced_graph expects a Set_of_p, we need first to insert the List_of_p elements into a Set_of_p. We'll accomplish this by creating a small local function called listp_to_setp:

     Set_of_p<Module> listp_to_setp(List_of_p<Module> lp) {
           Set_of_p<Module> sm;  // holds the solution
                                 // set created
           List_of_piter<Module> lpi (lp);
           Module* mp;
           while ( sm.insert(mp);
           return sm;

Now, with the List_of_p mlist from the section, ``Where is a Given Module Needed?'', we can use induced_graph in the following way, where widget is the original Product:


Product thingamabob = widget.induced_graph(listp_to_setp(mlist)); . . .

thingamabob will contain modules K and Z, and the directed Transport_Time between them. We could then do further processing on this new Graph, processing that would not affect the original Product widget: we could insert new Module and Transport_Time components into thingamabob, etc. Of course, destruction of Module or Transport_Time objects that are included in both Products will affect both by removing these objects from the Products: see the sections, ``Vertex Constructors and Destructors'', and ``Edge Constructors and Destructors''.

Next topic: Vertex Constructors and Destructors
Previous topic: Graph Information Retrieval

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005