Utilities
This section contains the documentation for the utilities. The functions listed below are mostly used dealing with graphs and for generating input graph instances (randomly or specific types of graphs).
Graph Utilities
These utilities are for dealing with graphs in general (reading graphs from a file, plotting graphs, converting graphs, etc.).
PdagExtendability.algo2label — Methodalgo2label(algo::String)::StringMap the function name of the used algorithm to a label for the plot.
Examples
julia> algo2label("pdag2dag_hs()-1")
Dor Tarsi HS - Standard - 1PdagExtendability.dict_to_csv — Methoddict_to_csv(dict::Dict, use_median::Bool = true)::StringConvert a dictionary of time measurements into a csv-formatted string. The parameter use_median indicates whether the median or mean should be used: If set to true (or ommitted), the median will be used, otherwise the mean.
It is possible to set the file parameter in order to save the csv-formatted string in that given file.
Examples
julia> dict = get_times_dict("../logs/log.txt")
Dict{String,Dict{Any,Any}} with 1 entry:
"pdag2dag_lg()-1" => Dict{Any,Any}("example.txt"=>Dict("median"=>2426.5,"mean"=>2594.65))
julia> dict_to_csv(dict)
"Algorithm;Instance;Time
pdag2dag_lg()-1;example.txt;2426.5
"PdagExtendability.get_times_dict — Methodget_times_dict(file::String)::DictConvert a logfile to a dictionary containing the times. The dictionary has the following structure: { "algorithm": { "input1": { "median": 1.124, "mean": 1.312 }, "input2": ... } }
Examples
julia> get_times_dict("../logs/log.txt")
Dict{String,Dict{Any,Any}} with 1 entry:
"pdag2dag_lg()-1" => Dict{Any,Any}("example.txt"=>Dict("median"=>2426.5,"mean"=>2594.65))PdagExtendability.print_dict — Functionprint_dict(dict::Dict, io::Core.IO = stdout)Print the contents of a dictionary as a formatted JSON string with readable indentation.
Examples
julia> dict = get_times_dict("../logs/log.txt")
Dict{String,Dict{Any,Any}} with 1 entry:
"pdag2dag_lg()" => Dict{Any,Any}("example.txt"=>Dict("median"=>2426.5,"mean"=>2594.65))
julia> julia> print_dict(dict)
{
"pdag2dag_lg()": {
"example.txt": {
"median": 2426.5,
"mean": 2594.646
}
}
}PdagExtendability.plotsvg — Methodplotsvg(g, file::String)Draw a graph and save it in a .svg file.
Examples
julia> g = SimpleDiGraph(2)
{2, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> plotsvg(g, "plot.svg")
falsePdagExtendability.readinputgraph — Functionreadinputgraph(file::String, only_undir::Bool = false)::SimpleDiGraphRead a graph from a file and return a SimpleDiGraph. The file must be formatted as follows. The first line contains the number of vertices and the number of edges, separated by a space. One empty line follows. Afterwards, there is one line for each edge. Each line representing an edge has a startvertex and an endvertex, separated by a space. Undirected edges are represented by two directed edges.
The parameter only_undir can be set to true to indicate that the input graph contains only undirected edges. This allows the input file to contain only one edge for each undirected edge.
Examples
julia> g = readinputgraph("../benchmarks/example.txt")
{3, 3} directed simple Int64 graphPdagExtendability.graph2digraph — Methodgraph2digraph(g::SimpleGraph)::SimpleDiGraphConvert an undirected graph (SimpleGraph) into a directed graph (SimpleDiGraph).
Examples
julia> g = SimpleGraph(3)
{3, 0} undirected simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> collect(edges(graph2digraph(g)))
2-element Vector{LightGraphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 2 => 1PdagExtendability.graph2str — Methodgraph2str(g; is_only_undir::Bool = false)::StringConvert a graph g to the corresponding string representation. Set is_only_undir to false if the graph contains directed edges.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> graph2str(g)
"3 0
"PdagExtendability.is_consistent_extension — Methodis_consistent_extension(g1::SimpleDiGraph, g2::SimpleDiGraph)::BoolCheck whether g1 is a consistent extension of g2.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 2, 3)
true
julia> add_edge!(g, 3, 2)
true
julia> e = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(e, 1, 2)
true
julia> add_edge!(e, 2, 3)
true
julia> is_consistent_extension(e, g)
truePdagExtendability.isdag — Methodisdag(g::SimpleDiGraph)::BoolCheck whether g is a directed acyclic graph.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 2, 3)
true
julia> isdag(g)
true
julia> add_edge!(g, 3, 1)
true
julia> isdag(g)
falsePdagExtendability.nanosec2millisec — Methodnanosec2sec(time::Float64)::Float64Convert a number in nanoseconds to milliseconds.
Examples
julia> nanosec2millisec(1000000.0)
1.0PdagExtendability.save2file — Methodsave2file(g, file::String; is_only_undir::Bool = true)Save a graph g to a given file. Set is_only_undir to false if the graph contains directed edges.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> save2file(g, "../benchmarks/dummy/graph.txt")PdagExtendability.skeleton — Methodskeleton(g::SimpleDiGraph)::Vector{Tuple{Int64, Int64}}Compute the skeleton of g. Edges are represented as tuples (u, v) where the smaller number is always first.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 3, 2)
true
julia> skeleton(g)
2-element Vector{Tuple{Int64, Int64}}:
(1, 2)
(2, 3)PdagExtendability.vstructures — Methodvstructures(g::SimpleDiGraph)::Vector{Tuple{Int64, Int64, Int64}}Compute all v-structures of graph g. The v-structures of form u -> v <- w are represented as tuples (x, v, y) where x is always the minimum of u and w and y is the maximum of u and w. The list of v-structures which is returned is sorted first by x, then by v and last by y.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 3, 2)
true
julia> vstructures(g)
1-element Vector{Tuple{Int64, Int64, Int64}}:
(1, 2, 3)Graph Generators
In order to generate even more input graph instances, there are different graph generation approaches available. These graph generation approaches allow for an easy enlargement of the dataset of input graph instances.
PdagExtendability.random_dag — Methodrandom_dag(min_r, max_r, min_v_per_r, max_v_per_r, prob)::SimpleDiGraphCreate a random DAG with the following properties: min_r is the minimum number of ranks for the DAG and max_r the maximum number of ranks. min_v_per_r indicates the minimum number of vertices per rank and max_v_per_r the maximum number of vertices per rank. prob is the probability of having an edge between two vertices.
References
https://stackoverflow.com/questions/12790337/generating-a-random-dag
Examples
julia> random_dag(3, 5, 3, 5, 0.2)
{15, 19} directed simple Int64 graph
julia> random_dag(3, 5, 3, 5, 0.2)
{12, 12} directed simple Int64 graphPdagExtendability.random_dag_v2 — Methodrandom_dag_v2(n::Int64, m::Int64)::SimpleDiGraphCreate a random DAG with n vertices and m edges.
Examples
julia> random_dag_v2(10, 40)
{10, 40} directed simple Int64 graphPdagExtendability.barbellgraph — Methodbarbellgraph(n::Int64; filepath::String = "")::SimpleGraphCreate a barbell graph with n vertices.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> barbellgraph(6)
{6, 7} undirected simple Int64 graphPdagExtendability.bintreegraph — Methodbintreegraph(n::Int64; filepath::String = "")::SimpleGraphCreate a binary tree with n vertices.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> bintreegraph(7)
{7, 6} undirected simple Int64 graphPdagExtendability.centipedegraph — Methodcentipedegraph(n::Int64; filepath::String = "")::SimpleGraphCreate a centipede graph with n vertices. Note that n has to be equal.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> centipedegraph(8)
{8, 7} undirected simple Int64 graphPdagExtendability.cliquegraph — Methodcliquegraph(n::Int64; filepath::String = "")::SimpleGraphCreate a clique graph with n vertices. The graph contains a clique of size n/2, where each vertex of that clique has one additional neighbor which is not connected to any other vertex. Note that n has to be equal.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> cliquegraph(8)
{8, 10} undirected simple Int64 graphPdagExtendability.completegraph — Methodcompletegraph(n::Int64; filepath::String = "")::SimpleGraphCreate a complete graph with n vertices.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> completegraph(4)
{4, 6} undirected simple Int64 graphPdagExtendability.cyclegraph — Methodcyclegraph(n::Int64; filepath::String = "")::SimpleGraphCreate a cycle with n vertices. Note that n has to be greater or equal to 3.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> cyclegraph(8)
{8, 8} undirected simple Int64 graphPdagExtendability.doublestargraph — Methoddoublestargraph(n::Int64; filepath = "")::SimpleGraphCreate a graph with n vertices, consisting of two connected stars.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> doublestargraph(8)
{8, 7} undirected simple Int64 graphPdagExtendability.extbarbellgraph — Methodextbarbellgraph(n::Int64; filepath::String = "")::SimpleGraphCreate an extended barbell graph with n vertices. That is, two cliques connected by a path. The cliques and the path are of size n/3 each.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> extbarbellgraph(8)
{8, 7} undirected simple Int64 graphPdagExtendability.friendshipgraph — Methodfriendshipgraph(n::Int64; filepath::String = "")::SimpleGraphCreate a friendship graph with n vertices. Note that n has to be odd.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> friendshipgraph(7)
{7, 9} undirected simple Int64 graphPdagExtendability.graph2pdag — Methodgraph2pdag(g::SimpleDiGraph, prob::Float64)::SimpleDiGraphConvert an undirected graph (encoded as a SimpleDiGraph) into a partially directed graph.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 2, 1)
true
julia> add_edge!(g, 1, 3)
true
julia> add_edge!(g, 3, 1)
true
julia> collect(edges(graph2pdag(g, 0.5)))
3-element Vector{LightGraphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 1 => 3
Edge 2 => 1PdagExtendability.pathgraph — Methodpathgraph(n::Int64; filepath::String = "")::SimpleGraphCreate a path with n vertices.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> pathgraph(10)
{10, 9} undirected simple Int64 graphPdagExtendability.stargraph — Methodstargraph(n::Int64; filepath::String = "")::SimpleGraphCreate a star graph with n vertices.
If a filepath is provided, the graph will also be written to that file.
Examples
julia> stargraph(11)
{11, 10} undirected simple Int64 graphPdagExtendability.erdos_renyi_pdag — Methoderdos_renyi_pdag(n::Int64, p1::Float64, p2::Float64; seed = 123)::SimpleDiGraphCreate a partially directed graph using the Erdős–Rényi model. The graph has n vertices with a probability p1 for having an edge between two vertices. p2 is the probablity for an edge to be directed.
Examples
julia> erdos_renyi_pdag(10, 0.2, 0.5)
{10, 12} directed simple Int64 graphPdagExtendability.random_pdag — Methodrandom_pdag(g::SimpleDiGraph, p::Float64)::g::SimpleDiGraphGenerates a partially directed graph by directing a given percentage of edges in a given undirected graph. Takes as input a fully undirected graph, encoded as a SimpleDiGraph. The generated PDAG will be extendable if the input is extendable.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 2, 1)
true
julia> add_edge!(g, 1, 3)
true
julia> add_edge!(g, 3, 1)
julia> collect(edges(random_pdag(g, 0.5)))
3-element Vector{LightGraphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 1 => 3
Edge 3 => 1PdagExtendability.random_pdag — Methodrandom_pdag(g::SimpleDiGraph, m::Int64)::SimpleDiGraphGenerates a partially directed graph by keeping m undirected edges in a given undirected graph g; all other edges are being directed. Takes as input a fully undirected graph, encoded as a SimpleDiGraph. The generated PDAG will be extendable if the input is extendable.
Examples
julia> g = SimpleDiGraph(3)
{3, 0} directed simple Int64 graph
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 2, 1)
true
julia> add_edge!(g, 2, 3)
true
julia> add_edge!(g, 3, 2)
true
julia> collect(edges(random_pdag(g, 1)))
3-element Vector{LightGraphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 2 => 1
Edge 2 => 3
julia> collect(edges(random_pdag(g, 1)))
3-element Vector{LightGraphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 2 => 3
Edge 3 => 2