souffle  2.0.2-371-g6315b36
TopologicallySortedSCCGraph.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file TopologicallySortedSCCGraph.h
12  *
13  * Defines the class to build the precedence graph,
14  * compute strongly connected components of the precedence graph, and
15  * build the strongly connected component graph.
16  *
17  ***********************************************************************/
18 
19 #pragma once
20 
21 #include "ast/analysis/Analysis.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstddef>
25 #include <iterator>
26 #include <set>
27 #include <string>
28 #include <vector>
29 
30 namespace souffle::ast {
31 
32 class TranslationUnit;
33 
34 namespace analysis {
35 
36 class SCCGraphAnalysis;
37 
38 /**
39  * Analysis pass computing a topologically sorted strongly connected component (SCC) graph.
40  */
41 class TopologicallySortedSCCGraphAnalysis : public Analysis {
42 public:
43  static constexpr const char* name = "topological-scc-graph";
44 
46 
47  void run(const TranslationUnit& translationUnit) override;
48 
49  const std::vector<size_t>& order() const {
50  return sccOrder;
51  }
52 
53  size_t sccOfIndex(const size_t index) const {
54  return sccOrder.at(index);
55  }
56 
57  size_t indexOfScc(const size_t scc) const {
58  auto it = std::find(sccOrder.begin(), sccOrder.end(), scc);
59  assert(it != sccOrder.end());
60  return (size_t)std::distance(sccOrder.begin(), it);
61  }
62 
63  std::set<size_t> indexOfScc(const std::set<size_t>& sccs) const {
64  std::set<size_t> indices;
65  for (const auto scc : sccs) {
66  indices.insert(indexOfScc(scc));
67  }
68  return indices;
69  }
70 
71  /** Output topologically sorted strongly connected component graph in text format */
72  void print(std::ostream& os) const override;
73 
74 private:
75  /** The strongly connected component (SCC) graph. */
76  SCCGraphAnalysis* sccGraph = nullptr;
77 
78  /** The final topological ordering of the SCCs. */
79  std::vector<size_t> sccOrder;
80 
81  /** Calculate the topological ordering cost of a permutation of as of yet unordered SCCs
82  using the ordered SCCs. Returns -1 if the given vector is not a valid topological ordering. */
83  int topologicalOrderingCost(const std::vector<size_t>& permutationOfSCCs) const;
84 
85  /** Recursive component for the forwards algorithm computing the topological ordering of the SCCs. */
86  void computeTopologicalOrdering(size_t scc, std::vector<bool>& visited);
87 };
88 
89 } // namespace analysis
90 } // namespace souffle::ast
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::name
static constexpr const char * name
Definition: TopologicallySortedSCCGraph.h:51
souffle::ast::analysis::Analysis::Analysis
Analysis(std::string identifier)
Definition: Analysis.h:40
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::order
const std::vector< size_t > & order() const
Definition: TopologicallySortedSCCGraph.h:57
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::sccGraph
SCCGraphAnalysis * sccGraph
The strongly connected component (SCC) graph.
Definition: TopologicallySortedSCCGraph.h:84
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::print
void print(std::ostream &os) const override
Output topologically sorted strongly connected component graph in text format.
Definition: TopologicallySortedSCCGraph.cpp:159
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::TopologicallySortedSCCGraphAnalysis
TopologicallySortedSCCGraphAnalysis()
Definition: TopologicallySortedSCCGraph.h:53
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::sccOfIndex
size_t sccOfIndex(const size_t index) const
Definition: TopologicallySortedSCCGraph.h:61
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::sccOrder
std::vector< size_t > sccOrder
The final topological ordering of the SCCs.
Definition: TopologicallySortedSCCGraph.h:87
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::topologicalOrderingCost
int topologicalOrderingCost(const std::vector< size_t > &permutationOfSCCs) const
Calculate the topological ordering cost of a permutation of as of yet unordered SCCs using the ordere...
Definition: TopologicallySortedSCCGraph.cpp:39
souffle::ast::analysis::SCCGraphAnalysis
Analysis pass computing the strongly connected component (SCC) graph for the datalog program.
Definition: SCCGraph.h:50
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::indexOfScc
size_t indexOfScc(const size_t scc) const
Definition: TopologicallySortedSCCGraph.h:65
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::computeTopologicalOrdering
void computeTopologicalOrdering(size_t scc, std::vector< bool > &visited)
Recursive component for the forwards algorithm computing the topological ordering of the SCCs.
Definition: TopologicallySortedSCCGraph.cpp:77
souffle::ast
Definition: Aggregator.h:35
Analysis.h
souffle::ast::analysis::TopologicallySortedSCCGraphAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: TopologicallySortedSCCGraph.cpp:135