Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 1 | //===- llvm/Analysis/MaximumSpanningTree.h - Interface ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Nick Lewycky | 06d2b42 | 2011-04-05 20:39:27 +0000 | [diff] [blame] | 10 | // This module provides means for calculating a maximum spanning tree for a |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 11 | // given set of weighted edges. The type parameter T is the type of a node. |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_ANALYSIS_MAXIMUMSPANNINGTREE_H |
| 16 | #define LLVM_ANALYSIS_MAXIMUMSPANNINGTREE_H |
| 17 | |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/EquivalenceClasses.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/BasicBlock.h" |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 21 | #include <vector> |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 24 | |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 25 | /// MaximumSpanningTree - A MST implementation. |
| 26 | /// The type parameter T determines the type of the nodes of the graph. |
| 27 | template <typename T> |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 28 | class MaximumSpanningTree { |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 29 | public: |
| 30 | typedef std::pair<const T*, const T*> Edge; |
| 31 | typedef std::pair<Edge, double> EdgeWeight; |
| 32 | typedef std::vector<EdgeWeight> EdgeWeights; |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 33 | protected: |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 34 | typedef std::vector<Edge> MaxSpanTree; |
| 35 | |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 36 | MaxSpanTree MST; |
| 37 | |
Richard Smith | 75dd7f0 | 2012-08-21 21:03:40 +0000 | [diff] [blame] | 38 | private: |
| 39 | // A comparing class for comparing weighted edges. |
| 40 | struct EdgeWeightCompare { |
| 41 | static bool getBlockSize(const T *X) { |
| 42 | const BasicBlock *BB = dyn_cast_or_null<BasicBlock>(X); |
| 43 | return BB ? BB->size() : 0; |
| 44 | } |
| 45 | |
| 46 | bool operator()(EdgeWeight X, EdgeWeight Y) const { |
| 47 | if (X.second > Y.second) return true; |
| 48 | if (X.second < Y.second) return false; |
| 49 | |
| 50 | // Equal edge weights: break ties by comparing block sizes. |
| 51 | size_t XSizeA = getBlockSize(X.first.first); |
| 52 | size_t YSizeA = getBlockSize(Y.first.first); |
| 53 | if (XSizeA > YSizeA) return true; |
| 54 | if (XSizeA < YSizeA) return false; |
| 55 | |
| 56 | size_t XSizeB = getBlockSize(X.first.second); |
| 57 | size_t YSizeB = getBlockSize(Y.first.second); |
| 58 | if (XSizeB > YSizeB) return true; |
| 59 | if (XSizeB < YSizeB) return false; |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | }; |
| 64 | |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 65 | public: |
| 66 | static char ID; // Class identification, replacement for typeinfo |
| 67 | |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 68 | /// MaximumSpanningTree() - Takes a vector of weighted edges and returns a |
| 69 | /// spanning tree. |
| 70 | MaximumSpanningTree(EdgeWeights &EdgeVector) { |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 71 | |
Richard Smith | 75dd7f0 | 2012-08-21 21:03:40 +0000 | [diff] [blame] | 72 | std::stable_sort(EdgeVector.begin(), EdgeVector.end(), EdgeWeightCompare()); |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 73 | |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 74 | // Create spanning tree, Forest contains a special data structure |
| 75 | // that makes checking if two nodes are already in a common (sub-)tree |
| 76 | // fast and cheap. |
| 77 | EquivalenceClasses<const T*> Forest; |
| 78 | for (typename EdgeWeights::iterator EWi = EdgeVector.begin(), |
| 79 | EWe = EdgeVector.end(); EWi != EWe; ++EWi) { |
| 80 | Edge e = (*EWi).first; |
| 81 | |
| 82 | Forest.insert(e.first); |
| 83 | Forest.insert(e.second); |
| 84 | } |
| 85 | |
| 86 | // Iterate over the sorted edges, biggest first. |
| 87 | for (typename EdgeWeights::iterator EWi = EdgeVector.begin(), |
| 88 | EWe = EdgeVector.end(); EWi != EWe; ++EWi) { |
| 89 | Edge e = (*EWi).first; |
| 90 | |
| 91 | if (Forest.findLeader(e.first) != Forest.findLeader(e.second)) { |
| 92 | Forest.unionSets(e.first, e.second); |
| 93 | // So we know now that the edge is not already in a subtree, so we push |
| 94 | // the edge to the MST. |
| 95 | MST.push_back(e); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | typename MaxSpanTree::iterator begin() { |
| 101 | return MST.begin(); |
| 102 | } |
| 103 | |
| 104 | typename MaxSpanTree::iterator end() { |
| 105 | return MST.end(); |
| 106 | } |
Andreas Neustifter | 6ef116d | 2009-08-28 11:28:24 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // End llvm namespace |
| 110 | |
| 111 | #endif |