Skip to content

Commit 54fe4a3

Browse files
authored
Max cut and document update (#11)
* update tests, polish maxcut * update docs
1 parent 7cd2006 commit 54fe4a3

File tree

28 files changed

+459
-87
lines changed

28 files changed

+459
-87
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# GraphTensorNetworks
22

33
[![Build Status](https://github.com/Happy-Diode/GraphTensorNetworks.jl/workflows/CI/badge.svg)](https://github.com/Happy-Diode/GraphTensorNetworks.jl/actions)
4-
[![Coverage Status](https://coveralls.io/repos/github/Happy-Diode/GraphTensorNetworks.jl/badge.svg?branch=fix-test-coverage&t=rIJIK2)](https://coveralls.io/github/Happy-Diode/GraphTensorNetworks.jl?branch=fix-test-coverage)
4+
[![Coverage Status](https://coveralls.io/repos/github/Happy-Diode/GraphTensorNetworks.jl/badge.svg?branch=master&t=rIJIK2)](https://coveralls.io/github/Happy-Diode/GraphTensorNetworks.jl?branch=master)
55
[![](https://img.shields.io/badge/docs-dev-blue.svg)](https://psychic-meme-f4d866f8.pages.github.io/dev/)
66

77
## Installation

docs/src/index.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ CurrentModule = GraphTensorNetworks
44

55
# GraphTensorNetworks
66

7+
This package uses generic tensor network to compute properties of combinatorial problems defined on graph.
8+
The properties includes the size of the maximum set size, the number of sets of a given size and the enumeration of configurations of a given set size.
9+
710
## Background knowledge
811

912
Please check our paper ["Computing properties of independent sets by generic programming tensor networks"]().
10-
If you find our paper or software useful in your work, please cite us.
13+
If you find our paper or software useful in your work, please cite us:
14+
15+
```bibtex
16+
(bibtex to be added)
17+
```
1118

1219
## Quick start
1320

14-
You can find a good installation guide and a quick start in our [README](https://github.com/Happy-Diode/GraphTensorNetworks.jl).
21+
You can find a good installation guide and a quick start in our [README](https://github.com/Happy-Diode/GraphTensorNetworks.jl).
22+
23+
A good example to start with is the [Independent set problem](@ref).

docs/src/ref.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ set_packing
1717

1818
#### Graph Problem Interfaces
1919
```@docs
20-
generate_tensors
20+
GraphTensorNetworks.generate_tensors
2121
symbols
2222
flavors
2323
get_weights
2424
nflavor
2525
```
2626

2727
To subtype [`GraphProblem`](@ref), the new type must contain a `code` field to represent the (optimized) tensor network.
28-
Interfaces [`generate_tensors`](@ref), [`symbols`](@ref), [`flavors`](@ref) and [`get_weights`] are required.
28+
Interfaces [`GraphTensorNetworks.generate_tensors`](@ref), [`symbols`](@ref), [`flavors`](@ref) and [`get_weights`] are required.
2929
[`nflavor`] is optimal.
3030

31+
#### Graph Problem Utilities
32+
```@docs
33+
is_independent_set
34+
mis_compactify!
35+
36+
cut_size
37+
cut_assign
38+
```
3139

3240
## Properties
3341
```@docs
@@ -81,13 +89,14 @@ MergeGreedy
8189
## Others
8290
#### Graph
8391
```@docs
84-
is_independent_set
85-
mis_compactify!
8692
show_graph
8793
8894
diagonal_coupled_graph
8995
square_lattice_graph
90-
unitdisk_graph
96+
unit_disk_graph
97+
98+
random_diagonal_coupled_graph
99+
random_square_lattice_graph
91100
```
92101

93102
One can also use `random_regular_graph` and `smallgraph` in [Graphs](https://github.com/JuliaGraphs/Graphs.jl) to build special graphs.

examples/Coloring/main.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# # Coloring problem
2+
3+
# !!! note
4+
# This tutorial only covers the coloring problem specific features,
5+
# It is recommended to read the [Independent set problem](@ref) tutorial too to know more about
6+
# * how to optimize the tensor network contraction order,
7+
# * what are the other graph properties computable,
8+
# * how to select correct method to compute graph properties,
9+
# * how to compute weighted graphs and handle open vertices.
10+
11+
# ## Introduction
12+
using GraphTensorNetworks, Graphs
13+
14+
# Please check the docstring of [`Coloring`](@ref) for the definition of the vertex cutting problem.
15+
@doc Coloring
16+
17+
# In the following, we are going to defined a 3-coloring problem for the Petersen graph.
18+
19+
graph = Graphs.smallgraph(:petersen)
20+
21+
# We can visualize this graph using the following function
22+
rot15(a, b, i::Int) = cos(2i*π/5)*a + sin(2i*π/5)*b, cos(2i*π/5)*b - sin(2i*π/5)*a
23+
24+
locations = [[rot15(0.0, 1.0, i) for i=0:4]..., [rot15(0.0, 0.6, i) for i=0:4]...]
25+
26+
show_graph(graph; locs=locations)
27+
28+
# Then we define the cutting problem as
29+
problem = Coloring{3}(graph);
30+
31+
# ## Solving properties

examples/IndependentSet/main.jl

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,36 @@ problem = Independence(graph; optimizer=TreeSA(sc_weight=1.0, ntrials=10,
2222
βs=0.01:0.1:15.0, niters=20, rw_weight=0.2),
2323
simplifier=MergeGreedy());
2424

25+
# The `optimizer` is for optimizing the contraction orders.
26+
# Here we use the local search based optimizer in [arXiv:2108.05665](https://arxiv.org/abs/2108.05665).
27+
# If no optimizer is specified, the default fast (in terms of the speed of searching contraction order)
28+
# but worst (in term of contraction complexity) [`GreedyMethod`](@ref) will be used.
29+
# `simplifier` is a preprocessing routine to speed up the `optimizer`.
30+
# Please check section [Tensor Network](@ref) for more details.
31+
# One can check the time, space and read-write complexity with the following function.
32+
33+
timespacereadwrite_complexity(problem)
34+
35+
# The return values are `log2` of the the number of iterations, the number elements in the max tensor and the number of read-write operations to tensor elements.
36+
2537

2638
# ## Solving properties
2739

2840
# ### Maximum independent set size ``\alpha(G)``
29-
maximum_independent_set_size = solve(problem, SizeMax())
41+
maximum_independent_set_size = solve(problem, SizeMax())[]
3042

3143
# ### Counting properties
3244
# ##### counting all independent sets
33-
count_all_independent_sets = solve(problem, CountingAll())
45+
count_all_independent_sets = solve(problem, CountingAll())[]
3446

3547
# ##### counting independent sets with sizes ``\alpha(G)`` and ``\alpha(G)-1``
36-
count_max2_independent_sets = solve(problem, CountingMax(2))
48+
count_max2_independent_sets = solve(problem, CountingMax(2))[]
3749

3850
# ##### independence polynomial
3951
# For the definition of independence polynomial, please check the docstring of [`Independence`](@ref) or this [wiki page](https://mathworld.wolfram.com/IndependencePolynomial.html).
4052
# There are 3 methods to compute a graph polynomial, `:finitefield`, `:fft` and `:polynomial`.
4153
# These methods are introduced in the docstring of [`GraphPolynomial`](@ref).
42-
independence_polynomial = solve(problem, GraphPolynomial(; method=:finitefield))
54+
independence_polynomial = solve(problem, GraphPolynomial(; method=:finitefield))[]
4355

4456
# ### Configuration properties
4557
# ##### finding one maximum independent set (MIS)
@@ -72,4 +84,38 @@ imgs = ntuple(k->(context((k-1)/m, 0.0, 1.2/m, 1.0), show_graph(graph;
7284
Compose.set_default_graphic_size(18cm, 4cm); Compose.compose(context(), imgs...)
7385

7486
# ##### enumeration of all IS configurations
75-
all_independent_sets = solve(problem, ConfigsAll())[]
87+
all_independent_sets = solve(problem, ConfigsAll())[]
88+
89+
# To save/read a set of configuration to disk, one can type the following
90+
filename = tempname()
91+
92+
save_configs(filename, all_independent_sets; format=:binary)
93+
94+
loaded_sets = load_configs(filename; format=:binary, bitlength=10)
95+
96+
# !!! note
97+
# When loading data, one needs to provide the `bitlength` if the data is saved in binary format.
98+
# Because the bitstring length is not stored.
99+
100+
# ## Weights and open vertices
101+
# [`Independence`] accepts weights as a key word argument.
102+
# The following code computes the weighted MIS problem.
103+
problem = Independence(graph; weights=collect(1:10))
104+
105+
max_config_weighted = solve(problem, SingleConfigMax())[]
106+
107+
show_graph(graph; locs=locations, colors=
108+
[iszero(max_config_weighted.c.data[i]) ? "white" : "red" for i=1:nv(graph)])
109+
110+
# The following code computes the MIS tropical tensor (reference to be added) with open vertices 1 and 2.
111+
problem = Independence(graph; openvertices=[1,2,3])
112+
113+
mis_tropical_tensor = solve(problem, SizeMax())
114+
115+
# The MIS tropical tensor shows the MIS size under different configuration of open vertices.
116+
# It is useful in MIS tropical tensor analysis.
117+
# One can compatify this MIS-Tropical tensor by typing
118+
119+
mis_compactify!(mis_tropical_tensor)
120+
121+
# It will eliminate some entries having no contribution to the MIS size when embeding this local graph into a larger one.

examples/Matching/main.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# # Vertex matching problem
2+
3+
# !!! note
4+
# This tutorial only covers the vertex matching problem specific features,
5+
# It is recommended to read the [Independent set problem](@ref) tutorial too to know more about
6+
# * how to optimize the tensor network contraction order,
7+
# * what are the other graph properties computable,
8+
# * how to select correct method to compute graph properties,
9+
# * how to compute weighted graphs and handle open vertices.
10+
11+
# ## Introduction
12+
using GraphTensorNetworks, Graphs
13+
14+
# Please check the docstring of [`Matching`](@ref) for the definition of the vertex matching problem.
15+
@doc Matching
16+
17+
# In the following, we are going to defined a matching problem for the Petersen graph.
18+
19+
graph = Graphs.smallgraph(:petersen)
20+
21+
# We can visualize this graph using the following function
22+
rot15(a, b, i::Int) = cos(2i*π/5)*a + sin(2i*π/5)*b, cos(2i*π/5)*b - sin(2i*π/5)*a
23+
24+
locations = [[rot15(0.0, 1.0, i) for i=0:4]..., [rot15(0.0, 0.6, i) for i=0:4]...]
25+
26+
show_graph(graph; locs=locations)
27+
28+
# Then we define the matching problem as
29+
problem = Matching(graph);
30+
31+
# ## Solving properties

examples/MaxCut/main.jl

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
1-
# # Max-Cut problem
1+
# # Cutting problem (Spin-glass problem)
22

3-
# ## Problem definition
3+
# !!! note
4+
# This tutorial only covers the cutting problem specific features,
5+
# It is recommended to read the [Independent set problem](@ref) tutorial too to know more about
6+
# * how to optimize the tensor network contraction order,
7+
# * what are the other graph properties computable,
8+
# * how to select correct method to compute graph properties,
9+
# * how to compute weighted graphs and handle open vertices.
410

11+
# ## Introduction
12+
using GraphTensorNetworks, Graphs
13+
14+
# Please check the docstring of [`MaxCut`](@ref) for the definition of the vertex cutting problem.
15+
@doc MaxCut
16+
17+
# In the following, we are going to defined an cutting problem for the Petersen graph.
18+
19+
graph = Graphs.smallgraph(:petersen)
20+
21+
# We can visualize this graph using the following function
22+
rot15(a, b, i::Int) = cos(2i*π/5)*a + sin(2i*π/5)*b, cos(2i*π/5)*b - sin(2i*π/5)*a
23+
24+
locations = [[rot15(0.0, 1.0, i) for i=0:4]..., [rot15(0.0, 0.6, i) for i=0:4]...]
25+
26+
show_graph(graph; locs=locations)
27+
28+
# Then we define the cutting problem as
29+
problem = MaxCut(graph);
530

631
# ## Solving properties
7-
using GraphTensorNetworks, Graphs
32+
33+
# ### Maximum cut size ``\gamma(G)``
34+
max_cut_size = solve(problem, SizeMax())[]
35+
36+
# ### Counting properties
37+
# ##### graph polynomial
38+
# Since the variable ``x`` is defined on edges,
39+
# hence the coefficients of the polynomial is the number of configurations having different number of anti-parallel edges.
40+
max_config = solve(problem, GraphPolynomial())[]
41+
42+
# ### Configuration properties
43+
# ##### finding one max cut solution
44+
max_edge_config = solve(problem, SingleConfigMax())[]
45+
46+
# These configurations are defined on edges, we need to find a valid assignment on vertices
47+
max_vertex_config = cut_assign(graph, max_edge_config.c.data)
48+
49+
max_cut_size_verify = cut_size(graph, max_vertex_config)
50+
51+
# You should see a consistent result as above `max_cut_size`.
52+
53+
show_graph(graph; locs=locations, colors=[
54+
iszero(max_vertex_config[i]) ? "white" : "red" for i=1:nv(graph)])
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# # Maximal independent set problem
2+
3+
# !!! note
4+
# This tutorial only covers the maximal independent set problem specific features,
5+
# It is recommended to read the [Independent set problem](@ref) tutorial too to know more about
6+
# * how to optimize the tensor network contraction order,
7+
# * what are the other graph properties computable,
8+
# * how to select correct method to compute graph properties,
9+
# * how to compute weighted graphs and handle open vertices.
10+
11+
# ## Introduction
12+
using GraphTensorNetworks, Graphs
13+
14+
# Please check the docstring of [`MaximalIndependence`](@ref) for the definition of the maximal independence problem.
15+
@doc MaximalIndependence
16+
17+
# In the following, we are going to defined an cutting problem for the Petersen graph.
18+
19+
graph = Graphs.smallgraph(:petersen)
20+
21+
# We can visualize this graph using the following function
22+
rot15(a, b, i::Int) = cos(2i*π/5)*a + sin(2i*π/5)*b, cos(2i*π/5)*b - sin(2i*π/5)*a
23+
24+
locations = [[rot15(0.0, 1.0, i) for i=0:4]..., [rot15(0.0, 0.6, i) for i=0:4]...]
25+
26+
show_graph(graph; locs=locations)
27+
28+
# Then we define the maximal independent set problem as
29+
problem = MaximalIndependence(graph);
30+
31+
# The tensor network structure is different from that of [`Independence`](@ref),
32+
# Its tensor is defined on a vertex and its neighbourhood,
33+
# and it makes the contraction of [`MaximalIndependence`](@ref) much harder.
34+
35+
# ## Solving properties
36+
37+
# ### Counting properties
38+
# ##### maximal independence polynomial
39+
max_config = solve(problem, GraphPolynomial())[]
40+
41+
# Since it only counts the maximal independent sets, the first several coefficients are 0.
42+
43+
# ### Configuration properties
44+
# ##### finding all maximal independent set
45+
max_edge_config = solve(problem, ConfigsAll())[]
46+
47+
# This result should be consistent with that given by the [Bron Kerbosch algorithm](https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm) on the complement of Petersen graph.
48+
maximal_cliques = maximal_cliques(complement(graph))
49+
50+
# For sparse graphs, the generic tensor network approach is usually much faster and memory efficient.

examples/Others/main.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@
77
# A vertex cover is a complement of an independent set.
88

99
# ### Maximal clique problem
10-
# The maximal clique of graph ``G`` is a maximal clique of ``G``'s complement graph.
11-
12-
# ### Spin-glass problem
13-
# It is another way of saying the Max-Cut problem.
10+
# The maximal clique of graph ``G`` is a maximal clique of ``G``'s complement graph.

examples/PaintShop/main.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# # Binary paint shop problem
2+
3+
# !!! note
4+
# This tutorial only covers the binary paint shop problem specific features,
5+
# It is recommended to read the [Independent set problem](@ref) tutorial too to know more about
6+
# * how to optimize the tensor network contraction order,
7+
# * what are the other graph properties computable,
8+
# * how to select correct method to compute graph properties,
9+
# * how to compute weighted graphs and handle open vertices.
10+
11+
# ## Introduction
12+
using GraphTensorNetworks, Graphs
13+
14+
# Please check the docstring of [`PaintShop`](@ref) for the definition of the binary paint shop problem.
15+
@doc PaintShop
16+
17+
# In the following, we are going to defined a binary paint shop problem for the following string
18+
19+
sequence = "abaccb"

0 commit comments

Comments
 (0)