Utils¶
Comparisons¶
-
bool randEquivalent(const Graph &g1, const Graph &g2, size_t numSamples = 100, double tol = 1e-4, size_t maxLength = 1000)¶
Compare two graphs by sampling paths and checking they have the same scores in both graphs.
- Parameters
g1 – A graph to be compared.
g2 – A graph to be compared.
numSamples – The number of samples to use. The more samples the more likely the result is accurate.
tol – The largest allowed absolute difference between the path score from each graph.
maxLength – The maximum length of sampled paths.
Input and Output¶
-
using SymbolMap = std::unordered_map<int, std::string>¶
User defined map of integer to arc label strings for use with e.g.
-
void saveTxt(const std::string &fileName, const Graph &g)¶
Save a graph to a file in txt format.
The first line will be a space separated list of the start nodes (
Graph::start
) and the second line will be a space separated list of accepting nodes (Graph::accept
).The following lines contain the arcs in the format:srcNode dstNode ilabel olabel weight
-
Graph loadTxt(const std::string &fileName)¶
Load a graph from a file in txt format.
The expected format is the first two lines contain a list of space separated start and accept nodes respectively. The following lines should contain the arcs in the format:
wheresrcNode dstNode ilabel [olabel=ilabel] [weight=0.0]
[x=y]
indicate optional values forx
with a default value ofy
.For example:
is a two node graph with an arc from start node 0 to accept node 1 with input and output label of 1,0 1 0 1 1
is a two node graph with an arc from node 0 to node 1 with input label 1 and output label 2, and0 1 0 1 1 2
is a two node graph with an arc from node 0 to node 1 with input label 1, output label 2, and a weight of 3.0.0 1 0 1 1 2 3.0
The returned Graph will contain “maxNodeIdx” nodes where “maxNodeIdx” is the largest index of a node in the file regardless of if there are arcs for every node.
-
std::ostream &operator<<(std::ostream &out, const Graph &g)¶
overload
<<
operator for working withstd::cout
onGraph
-
void draw(const Graph &g, const std::string &filename, const SymbolMap &isymbols = SymbolMap(), const SymbolMap &osymbols = SymbolMap())¶
Write a graph in the Graphviz DOT format to a file.
Arc labels are of the format
ilabel/olabel:weight
. If the output symbols are not specified then theolabel
is omitted and arc labels are of the formatilabel:weight
. If the input symbols are not specified then integer ids are used as the label.Compile to pdf with:
dot -Tpdf graph.dot -o graph.pdf
- Parameters
g – The graph to draw
filename – The name of the file to write to (e.g. graph.dot).
isymbols – A map of integer ids to strings used for arc input labels
osymbols – A map of integer ids to strings used for arc output labels