Open
Graph Drawing
Framework

 v.2012.05
 

MultilevelGraph.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  *
00004  * last checkin:
00005  *   $Author: gutwenger $
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $
00007  ***************************************************************/
00008 
00042 #ifdef _MSC_VER
00043 #pragma once
00044 #endif
00045 
00046 #ifndef OGDF_MULTILEVEL_GRAPH_H
00047 #define OGDF_MULTILEVEL_GRAPH_H
00048 
00049 #include <ogdf/basic/Graph.h>
00050 #include <ogdf/basic/GraphAttributes.h>
00051 #include <vector>
00052 #include <map>
00053 
00054 namespace ogdf {
00055 
00056 //Stores info on merging for a refinement level
00057 struct NodeMerge
00058 {
00059     // Node/Edge IDs instead of pointers as the nodes themselves may be nonexistent.
00060     std::vector<int> m_deletedEdges;
00061     std::vector<int> m_changedEdges;
00062     std::map<int, double> m_doubleWeight; // for changed and deleted edges
00063     std::map<int, int> m_source;
00064     std::map<int, int> m_target;
00065 
00066     int m_mergedNode;
00067     std::vector< std::pair<int, double> > m_position; // optional information <target, distance>. mergedNode will be placed at average of relative distances to target.
00068 
00069     std::vector<int> m_changedNodes; // there may be placement strategies that use more than one reference-node.
00070     std::map<int, double> m_radius; // for changed nodes and the merged node
00071     
00072     int m_level;
00073 
00074 
00075     NodeMerge(int level) : m_level(level) { }
00076     ~NodeMerge() { }
00077 };
00078 
00079 
00080 class MultilevelGraph
00081 {
00082 private:
00083     bool m_createdGraph; //used in destructor, TODO: check if it is needed
00084     Graph * m_G;
00085     GraphAttributes * m_GA; //<! Keeps layout info in replacement of information below (todo: remove them)
00086     std::vector<NodeMerge *> m_changes;
00087     NodeArray<double> m_radius;
00088     double m_avgRadius; //stores average node radius for scaling and random layout purposes
00089 
00090     EdgeArray<double> m_weight;
00091 
00092     // Associations to index only as the node/edge may be nonexistent
00093     NodeArray<int> m_nodeAssociations;
00094     EdgeArray<int> m_edgeAssociations;
00095 
00096     std::vector<node> m_reverseNodeIndex;
00097     std::vector<int> m_reverseNodeMergeWeight;//<! Keeps number of vertices represented by vertex with given index  
00098     std::vector<edge> m_reverseEdgeIndex;
00099 
00100     MultilevelGraph * removeOneCC(std::vector<node> &componentSubArray);
00101     void copyFromGraph(const Graph &G, NodeArray<int> &nodeAssociations, EdgeArray<int> &edgeAssociations);
00102     void prepareGraphAttributes(GraphAttributes &GA) const;
00103 
00104     void initReverseIndizes();
00105     void initInternal();
00106 
00107 public:
00108     ~MultilevelGraph();
00109     MultilevelGraph();
00110     MultilevelGraph(Graph &G);
00111     MultilevelGraph(GraphAttributes &GA);
00112     // if the Graph is available without const, no copy needs to be created.
00113     MultilevelGraph(GraphAttributes &GA, Graph &G);
00114 
00115     // creates MultilevelGraph directly from GML file.
00116     MultilevelGraph(istream &is);
00117     MultilevelGraph(const String &filename);
00118 
00119     NodeArray<double> &getRArray() { return m_radius; }
00120     EdgeArray<double> &getWArray() { return m_weight; }
00121 
00122     edge getEdge(unsigned int index);
00123     node getNode(unsigned int index);
00124 
00125     double radius(node v) { return m_radius[v]; }
00126     void radius(node v, double r) { m_radius[v] = r; }
00127     double averageRadius() const { return m_avgRadius;}
00128 
00129     double x(node v) { return m_GA->x(v); }
00130     double y(node v) { return m_GA->y(v); }
00131     void x(node v, double x) { m_GA->x(v) = x;}
00132     void y(node v, double y) { m_GA->y(v) = y;}
00133 
00134     void weight(edge e, double weight) { m_weight[e] = weight; }
00135     double weight(edge e) { return m_weight[e]; }
00136 
00137     //returns the merge weight, i.e. the number of nodes represented by v on the current level
00138     int mergeWeight(node v) {return m_reverseNodeMergeWeight[v->index()];}
00139 
00140     void moveToZero();
00141 
00142     int getLevel();
00143     Graph & getGraph() { return *m_G; }
00145     GraphAttributes & getGraphAttributes() const {return *m_GA;}; 
00146     void exportAttributes(GraphAttributes &GA) const;
00147     void exportAttributesSimple(GraphAttributes &GA) const;
00148     void importAttributes(const GraphAttributes &GA);
00149     void importAttributesSimple(const GraphAttributes &GA);
00150     void reInsertGraph(MultilevelGraph &MLG);
00151     void reInsertAll(std::vector<MultilevelGraph *> components);
00152     void copyNodeTo(node v, MultilevelGraph &MLG, std::map<node, node> &tempNodeAssociations, bool associate, int index = -1);
00153     void copyEdgeTo(edge e, MultilevelGraph &MLG, std::map<node, node> &tempNodeAssociations, bool associate, int index = -1);
00154     void writeGML(ostream &os);
00155     void writeGML(const String &fileName);
00156 
00157     // the original graph will be cleared to save Memory
00158     std::vector<MultilevelGraph *> splitIntoComponents();
00159 
00160     bool postMerge(NodeMerge * NM, node merged);
00161     //\a merged is the node now represented by \a theNode
00162     bool changeNode(NodeMerge * NM, node theNode, double newRadius, node merged);
00163     bool changeEdge(NodeMerge * NM, edge theEdge, double newWeight, node newSource, node newTarget);
00164     bool deleteEdge(NodeMerge * NM, edge theEdge);
00165     std::vector<edge> moveEdgesToParent(NodeMerge * NM, node theNode, node parent, bool deleteDoubleEndges, int adjustEdgeLengths);
00166     NodeMerge * getLastMerge();
00167     node undoLastMerge();
00168 
00169     void updateReverseIndizes();
00170     //sets the merge weights back to initial values
00171     void updateMergeWeights();
00172 };
00173 
00174 } // namespace ogdf
00175 
00176 #endif