Skip to content

Commit 22a0241

Browse files
committed
2 parents 11e72dc + 0c5726c commit 22a0241

26 files changed

+1244
-31
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ TropicalNumbers = "b3a74e9c-7526-4576-a4eb-79c0d4c32334"
2828
Viznet = "52a3aca4-6234-47fd-b74a-806bdf78ede9"
2929

3030
[compat]
31-
AbstractTrees = "0.3"
31+
AbstractTrees = "0.3, 0.4"
3232
CUDA = "3.5"
3333
Cairo = "1.0"
3434
Compose = "0.9"

docs/make.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for each in readdir(pkgdir(GenericTensorNetworks, "examples"))
1010
input_file = pkgdir(GenericTensorNetworks, "examples", each)
1111
endswith(input_file, ".jl") || continue
1212
@info "building" input_file
13-
output_dir = pkgdir(GenericTensorNetworks, "docs", "src", "tutorials")
13+
output_dir = pkgdir(GenericTensorNetworks, "docs", "src", "generated")
1414
@info "executing" input_file
1515
Literate.markdown(input_file, output_dir; name=each[1:end-3], execute=false)
1616
end
@@ -47,24 +47,24 @@ makedocs(;
4747
pages=[
4848
"Home" => "index.md",
4949
"Problems" => [
50-
"Independent set problem" => "tutorials/IndependentSet.md",
51-
"Maximal independent set problem" => "tutorials/MaximalIS.md",
52-
"Cutting problem" => "tutorials/MaxCut.md",
53-
"Vertex Matching problem" => "tutorials/Matching.md",
54-
"Binary paint shop problem" => "tutorials/PaintShop.md",
55-
"Coloring problem" => "tutorials/Coloring.md",
56-
"Dominating set problem" => "tutorials/DominatingSet.md",
57-
"Satisfiability problem" => "tutorials/Satisfiability.md",
58-
"Set covering problem" => "tutorials/SetCovering.md",
59-
"Set packing problem" => "tutorials/SetPacking.md",
60-
#"Other problems" => "tutorials/Others.md",
50+
"Independent set problem" => "generated/IndependentSet.md",
51+
"Maximal independent set problem" => "generated/MaximalIS.md",
52+
"Cutting problem" => "generated/MaxCut.md",
53+
"Vertex Matching problem" => "generated/Matching.md",
54+
"Binary paint shop problem" => "generated/PaintShop.md",
55+
"Coloring problem" => "generated/Coloring.md",
56+
"Dominating set problem" => "generated/DominatingSet.md",
57+
"Satisfiability problem" => "generated/Satisfiability.md",
58+
"Set covering problem" => "generated/SetCovering.md",
59+
"Set packing problem" => "generated/SetPacking.md",
60+
#"Other problems" => "generated/Others.md",
6161
],
6262
"Topics" => [
63-
"Make extensions" => "extension.md",
64-
"Save and load solutions" => "tutorials/saveload.md",
63+
"Gist" => "gist.md",
64+
"Save and load solutions" => "generated/saveload.md",
6565
"Sum product tree representation" => "sumproduct.md",
66-
"Weighted problems" => "tutorials/weighted.md",
67-
"Open degree of freedoms" => "tutorials/open.md"
66+
"Weighted problems" => "generated/weighted.md",
67+
"Open and fixed degrees of freedom" => "generated/open.md"
6868
],
6969
"Performance Tips" => "performancetips.md",
7070
"References" => "ref.md",

docs/serve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function serve(;host::String="0.0.0.0", port::Int=8000)
1010
doc_env=true,
1111
skip_dirs=[
1212
joinpath("docs", "src", "assets"),
13-
joinpath("docs", "src", "tutorials"),
13+
joinpath("docs", "src", "generated"),
1414
],
1515
literate="examples",
1616
host=\"$host\",

docs/src/generated/Coloring.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
```@meta
2+
EditURL = "<unknown>/examples/Coloring.jl"
3+
```
4+
5+
# Coloring problem
6+
7+
!!! note
8+
It is highly recommended to read the [Independent set problem](@ref) chapter before reading this one.
9+
10+
## Problem definition
11+
A [vertex coloring](https://en.wikipedia.org/wiki/Graph_coloring) is an assignment of labels or colors to each vertex of a graph such that no edge connects two identically colored vertices.
12+
In the following, we are going to defined a 3-coloring problem for the Petersen graph.
13+
14+
````@example Coloring
15+
using GenericTensorNetworks, Graphs
16+
17+
graph = Graphs.smallgraph(:petersen)
18+
````
19+
20+
We can visualize this graph using the following function
21+
22+
````@example Coloring
23+
rot15(a, b, i::Int) = cos(2i*π/5)*a + sin(2i*π/5)*b, cos(2i*π/5)*b - sin(2i*π/5)*a
24+
25+
locations = [[rot15(0.0, 1.0, i) for i=0:4]..., [rot15(0.0, 0.6, i) for i=0:4]...]
26+
27+
show_graph(graph; locs=locations)
28+
````
29+
30+
## Generic tensor network representation
31+
32+
We construct the tensor network for the 3-coloring problem as
33+
34+
````@example Coloring
35+
problem = Coloring{3}(graph);
36+
nothing #hide
37+
````
38+
39+
### Theory (can skip)
40+
Type [`Coloring`](@ref) can be used for constructing the tensor network with optimized contraction order for a coloring problem.
41+
Let us use 3-colouring problem defined on vertices as an example.
42+
For a vertex ``v``, we define the degrees of freedom ``c_v\in\{1,2,3\}`` and a vertex tensor labelled by it as
43+
```math
44+
W(v) = \left(\begin{matrix}
45+
1\\
46+
1\\
47+
1
48+
\end{matrix}\right).
49+
```
50+
For an edge ``(u, v)``, we define an edge tensor as a matrix labelled by ``(c_u, c_v)`` to specify the constraint
51+
```math
52+
B = \left(\begin{matrix}
53+
1 & x & x\\
54+
x & 1 & x\\
55+
x & x & 1
56+
\end{matrix}\right).
57+
```
58+
The number of possible colouring can be obtained by contracting this tensor network by setting vertex tensor elements ``r_v, g_v`` and ``b_v`` to 1.
59+
60+
## Solving properties
61+
##### counting all possible coloring
62+
63+
````@example Coloring
64+
num_of_coloring = solve(problem, CountingMax())[]
65+
````
66+
67+
##### finding one best coloring
68+
69+
````@example Coloring
70+
single_solution = solve(problem, SingleConfigMax())[]
71+
72+
is_vertex_coloring(graph, single_solution.c.data)
73+
74+
vertex_color_map = Dict(0=>"red", 1=>"green", 2=>"blue")
75+
76+
show_graph(graph; locs=locations, vertex_colors=[vertex_color_map[Int(c)]
77+
for c in single_solution.c.data])
78+
````
79+
80+
Let us try to solve the same issue on its line graph, a graph that generated by mapping an edge to a vertex and two edges sharing a common vertex will be connected.
81+
82+
````@example Coloring
83+
linegraph = line_graph(graph)
84+
85+
show_graph(linegraph; locs=[0.5 .* (locations[e.src] .+ locations[e.dst])
86+
for e in edges(graph)])
87+
````
88+
89+
Let us construct the tensor network and see if there are solutions.
90+
91+
````@example Coloring
92+
lineproblem = Coloring{3}(linegraph);
93+
94+
num_of_coloring = solve(lineproblem, CountingMax())[]
95+
````
96+
97+
You will see the maximum size 28 is smaller than the number of edges in the `linegraph`,
98+
meaning no solution for the 3-coloring on edges of a Petersen graph.
99+
100+
---
101+
102+
*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*
103+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```@meta
2+
EditURL = "<unknown>/examples/DominatingSet.jl"
3+
```
4+
5+
# Dominating set problem
6+
7+
!!! note
8+
It is highly recommended to read the [Independent set problem](@ref) chapter before reading this one.
9+
10+
## Problem definition
11+
12+
In graph theory, a [dominating set](https://en.wikipedia.org/wiki/Dominating_set) for a graph ``G = (V, E)`` is a subset ``D`` of ``V`` such that every vertex not in ``D`` is adjacent to at least one member of ``D``.
13+
The domination number ``\gamma(G)`` is the number of vertices in a smallest dominating set for ``G``.
14+
The decision version of finding the minimum dominating set is an NP-complete.
15+
In the following, we are going to solve the dominating set problem on the Petersen graph.
16+
17+
````@example DominatingSet
18+
using GenericTensorNetworks, Graphs
19+
20+
graph = Graphs.smallgraph(:petersen)
21+
````
22+
23+
We can visualize this graph using the following function
24+
25+
````@example DominatingSet
26+
rot15(a, b, i::Int) = cos(2i*π/5)*a + sin(2i*π/5)*b, cos(2i*π/5)*b - sin(2i*π/5)*a
27+
28+
locations = [[rot15(0.0, 1.0, i) for i=0:4]..., [rot15(0.0, 0.6, i) for i=0:4]...]
29+
30+
show_graph(graph; locs=locations)
31+
````
32+
33+
## Generic tensor network representation
34+
We can use [`DominatingSet`](@ref) to construct the tensor network for solving the dominating set problem as
35+
36+
````@example DominatingSet
37+
problem = DominatingSet(graph; optimizer=TreeSA());
38+
nothing #hide
39+
````
40+
41+
### Theory (can skip)
42+
Let ``G=(V,E)`` be the target graph that we want to solve.
43+
The tensor network representation map a vertex ``v\in V`` to a boolean degree of freedom ``s_v\in\{0, 1\}``.
44+
We defined the restriction on a vertex and its neighbouring vertices ``N(v)``:
45+
```math
46+
T(x_v)_{s_1,s_2,\ldots,s_{|N(v)|},s_v} = \begin{cases}
47+
0 & s_1=s_2=\ldots=s_{|N(v)|}=s_v=0,\\
48+
1 & s_v=0,\\
49+
x_v^{w_v} & \text{otherwise},
50+
\end{cases}
51+
```
52+
where ``w_v`` is the weight of vertex ``v``.
53+
This tensor means if both ``v`` and its neighbouring vertices are not in ``D``, i.e., ``s_1=s_2=\ldots=s_{|N(v)|}=s_v=0``,
54+
this configuration is forbidden because ``v`` is not adjacent to any member in the set.
55+
otherwise, if ``v`` is in ``D``, it has a contribution ``x_v^{w_v}`` to the final result.
56+
One can check the contraction time space complexity of a [`DominatingSet`](@ref) instance by typing:
57+
58+
````@example DominatingSet
59+
timespacereadwrite_complexity(problem)
60+
````
61+
62+
## Solving properties
63+
64+
### Counting properties
65+
##### Domination polynomial
66+
The graph polynomial for the dominating set problem is known as the domination polynomial (see [arXiv:0905.2251](https://arxiv.org/abs/0905.2251)).
67+
It is defined as
68+
```math
69+
D(G, x) = \sum_{k=0}^{\gamma(G)} d_k x^k,
70+
```
71+
where ``d_k`` is the number of dominating sets of size ``k`` in graph ``G=(V, E)``.
72+
73+
````@example DominatingSet
74+
domination_polynomial = solve(problem, GraphPolynomial())[]
75+
````
76+
77+
The domination number ``\gamma(G)`` can be computed with the [`SizeMin`](@ref) property:
78+
79+
````@example DominatingSet
80+
domination_number = solve(problem, SizeMin())[]
81+
````
82+
83+
Similarly, we have its counting [`CountingMin`](@ref):
84+
85+
````@example DominatingSet
86+
counting_min_dominating_set = solve(problem, CountingMin())[]
87+
````
88+
89+
### Configuration properties
90+
##### finding minimum dominating set
91+
One can enumerate all minimum dominating sets with the [`ConfigsMin`](@ref) property in the program.
92+
93+
````@example DominatingSet
94+
min_configs = solve(problem, ConfigsMin())[].c
95+
96+
all(c->is_dominating_set(graph, c), min_configs)
97+
````
98+
99+
````@example DominatingSet
100+
show_gallery(graph, (2, 5); locs=locations, vertex_configs=min_configs)
101+
````
102+
103+
Similarly, if one is only interested in computing one of the minimum dominating sets,
104+
one can use the graph property [`SingleConfigMin`](@ref).
105+
106+
---
107+
108+
*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*
109+

0 commit comments

Comments
 (0)