From 07c4a573fb37123709d30ee7a6cc15c9bb15925c Mon Sep 17 00:00:00 2001 From: Tim Dwyer Date: Sun, 16 Jul 2006 13:48:42 +0000 Subject: Layout algorithm is now applied to each connected component in the selection separately. Previously, behaviour of layout on disconnected graphs was... undefined! (bzr r1421) --- src/libcola/connected_components.cpp | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/libcola/connected_components.cpp (limited to 'src/libcola/connected_components.cpp') diff --git a/src/libcola/connected_components.cpp b/src/libcola/connected_components.cpp new file mode 100644 index 000000000..8450e4874 --- /dev/null +++ b/src/libcola/connected_components.cpp @@ -0,0 +1,67 @@ +#include +#include "cola.h" +using namespace std; + +namespace cola { + namespace ccomponents { + struct Node { + unsigned id; + bool visited; + vector neighbours; + Rectangle* r; + }; + // Depth first search traversal of graph to find connected component + void dfs(Node* v, + set& remaining, + Component* component, + map > &cmap) { + v->visited=true; + remaining.erase(v); + cmap[v->id]=make_pair(component,component->node_ids.size()); + component->node_ids.push_back(v->id); + component->rects.push_back(v->r); + for(unsigned i=0;ineighbours.size();i++) { + Node* u=v->neighbours[i]; + if(!u->visited) { + dfs(u,remaining,component,cmap); + } + } + } + } + + using namespace ccomponents; + + // for a graph of n nodes, return connected components + void connectedComponents( + vector &rs, + vector &es, + vector &components) { + unsigned n=rs.size(); + vector vs(n); + set remaining; + for(unsigned i=0;i::iterator e=es.begin();e!=es.end();e++) { + vs[e->first].neighbours.push_back(&vs[e->second]); + vs[e->second].neighbours.push_back(&vs[e->first]); + } + map > cmap; + while(!remaining.empty()) { + Component* component=new Component; + Node* v=*remaining.begin(); + dfs(v,remaining,component,cmap); + components.push_back(component); + } + for(vector::iterator e=es.begin();e!=es.end();e++) { + pair u=cmap[e->first], + v=cmap[e->second]; + assert(u.first==v.first); + u.first->edges.push_back(make_pair(u.second,v.second)); + } + } +} +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 -- cgit v1.2.3