contrib/mul/mmn/mmn_graph_rep1.h
Go to the documentation of this file.
00001 #ifndef mmn_graph_rep1_h_
00002 #define mmn_graph_rep1_h_
00003 //:
00004 // \file
00005 // \brief Representation of a graph, stored by links at each node.
00006 // \author Tim Cootes
00007 
00008 #include <mmn/mmn_arc.h>
00009 #include <mmn/mmn_dependancy.h>
00010 #include <vcl_vector.h>
00011 #include <vcl_utility.h>  // For vcl_pair
00012 
00013 //: Representation of a graph, stored by links at each node.
00014 //  Optimised for adding arcs and finding arcs for each node.
00015 //  Assumes that there is an ordered list of arcs (but doesn't
00016 //  actually record it explicitly).
00017 class mmn_graph_rep1
00018 {
00019  private:
00020   //: Indicates arcs connected to each node
00021   //  node_data_[i][j].first == vertex connected to node i
00022   //. *.second == index of arc which does the connection.
00023   vcl_vector<vcl_vector<vcl_pair<unsigned,unsigned> > > node_data_;
00024 
00025 #if 0
00026   //: Number of options for each node
00027   //  Used to select most efficient simplification of the graph
00028   vcl_vector<unsigned> n_;
00029 #endif // 0
00030 
00031   //: Maximum number of arcs used
00032   unsigned max_n_arcs_;
00033 
00034   //: Current number of arcs
00035   unsigned n_arcs_;
00036 
00037   //: Index of node chosen to be root (if >n_nodes,then none chosen)
00038   unsigned root_index_;
00039 
00040   //: Remove record of arc v1-v2 from v1
00041   void remove_arc_from_node(unsigned v1, unsigned v2);
00042  public:
00043   //: Default constructor
00044   mmn_graph_rep1();
00045 
00046   //: Indicates arcs connected to each node
00047   //  node_data_[i][j].first == vertex connected to node i
00048   //. *.second == index of arc which does the connection.
00049   const vcl_vector<vcl_vector<vcl_pair<unsigned,unsigned> > >& node_data() const
00050   { return node_data_; }
00051 
00052   //: Maximum number of distinct arcs used
00053   unsigned max_n_arcs() const { return max_n_arcs_; }
00054 
00055   //: Current number of arcs
00056   unsigned n_arcs() const { return n_arcs_; }
00057 
00058   //: Build from list of arcs
00059   void build(unsigned n_nodes, const vcl_vector<mmn_arc>& arcs);
00060 
00061   //: Return index of arc between v1 and v2, or -1 if none
00062   int arc_index(unsigned v1, unsigned v2) const;
00063 
00064   //: Return index of arc between v1 and v2, creating one if none exists
00065   unsigned get_arc(unsigned v1, unsigned v2);
00066 
00067   //: Remove some of leaves of graph, recording dependencies
00068   //  A leaf node is one with only one arc
00069   //  For each leaf node removed, add one dependency object to
00070   //  the deps list.
00071   //  Returns number of leaves removed.
00072   unsigned remove_leaves(vcl_vector<mmn_dependancy>& deps);
00073 
00074   //: Remove all of leaves of graph, recording dependencies
00075   //  A leaf node is one with only one arc
00076   //  For each leaf node removed, add one dependency object to
00077   //  the deps list.  On exit, this graph has no leaves.
00078   //  Returns number of leaves removed.
00079   unsigned remove_all_leaves(vcl_vector<mmn_dependancy>& deps);
00080 
00081   //: Remove arcs from some of the nodes with two neighbours
00082   //  Record the pairwise dependencies.
00083   //  For each node removed, add one dependency object to
00084   //  the deps list.
00085   //  Returns number of removed.
00086   unsigned remove_pair_deps(vcl_vector<mmn_dependancy>& deps);
00087 
00088   //: Compute list of all single and pairwise dependencies
00089   //  Finds ordered list of dependencies.
00090   //  If returns true, then dep is an ordered list of dependencies
00091   //  allowing us solve a minimisation problem one node at a time.
00092   //  If it returns false, then the graph cannot be decomposed into
00093   //  a sequence of single or pairwise dependencies.
00094   //  If dep[i].n_dep==1 for all i, then the graph is a tree, and
00095   //  reversing the order of dep gives a means of traversing from the
00096   //  root to the leaves.  The original order gives a method of
00097   //  visiting every node only after any child/leaf nodes have been
00098   //  visited first.
00099   //  Destroys current structure in the process.
00100   //
00101   //  root_index indicates which node is to be the root for the
00102   //  resulting tree (ie the one node remaining - defined in the
00103   //  v1 of the last element in deps).
00104   bool compute_dependancies(vcl_vector<mmn_dependancy>& deps,
00105                             unsigned root_index);
00106 
00107   //: Compute list of all single and pairwise dependencies
00108   //  As compute_dependancies(deps,root), but root selected by algorithm
00109   bool compute_dependancies(vcl_vector<mmn_dependancy>& deps);
00110 
00111 };
00112 
00113 #endif // mmn_graph_rep1_h_