Skip to content

Commit cbc860e

Browse files
committed
print configurations
1 parent 3b87883 commit cbc860e

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

README.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ For current master
3333
pkg> add UnitDiskMapping#master
3434
```
3535

36-
## Example
36+
## Example 1: Unweighted mapping
37+
#### Step 1: map a Petersen graph to a grid graph
3738
```julia
3839
julia> using Graphs, UnitDiskMapping
3940

4041
julia> g = smallgraph(:petersen)
4142
{10, 15} undirected simple Int64 graph
4243

44+
# `Branching()` is an exact algorithm for finding an optimal vertex ordering
4345
julia> res = map_graph(g, vertex_order=Branching());
4446

45-
julia> res.grid_graph
47+
julia> res.mis_overhead
48+
89
49+
50+
julia> println(res.grid_graph)
4651
4752
4853
@@ -72,7 +77,74 @@ julia> res.grid_graph
7277
7378
7479
80+
```
81+
82+
#### Step 2: solve the MIS problem on grid graph with any method you like
83+
In the following, we show how to solve this maximal independent set problem on grid graph with the open source package [`GraphTensorNetworks`](https://github.com/Happy-Diode/GraphTensorNetworks.jl) and map the configurations back.
84+
The generic tensor network approach for solving MIS works best for graphs with small tree width, it can solve this grid graph `G(V,E)` in sub-exponential time `t~2^{sqrt(|V|)}`.
85+
86+
```julia
87+
julia> using GraphTensorNetworks
88+
89+
julia> gp = Independence(SimpleGraph(res.grid_graph); optimizer=TreeSA(ntrials=1, niters=10), simplifier=MergeGreedy());
7590

91+
julia> misconfig = solve(gp, "config max")[].c;
92+
93+
# create a grid mask as the solution, where occupied locations are marked as value 1.
94+
julia> c = zeros(Int, size(res.grid_graph.content));
95+
96+
julia> for (i, loc) in enumerate(findall(!isempty, res.grid_graph.content))
97+
c[loc] = misconfig.data[i]
98+
end
99+
100+
julia> print_config(res, c)
101+
102+
103+
104+
◯ ● ◯ ● ◯ ● ◯ ● ◯ ● ◯ ◯ ● ◯ ◯ ● ◯ ● ◯ ● ● ◯ ●
105+
106+
107+
108+
● ◯ ◯ ● ◯ ● ◯ ● ◯ ◯ ◯ ● ◯ ◯ ◯ ● ◯ ◯ ● ◯ ● ◯ ●
109+
◯ ● ◯ ◯ ● ◯ ◯ ◯ ◯
110+
111+
112+
◯ ● ● ◯ ◯ ◯ ◯ ◯ ◯ ◯ ◯ ◯ ● ◯ ● ◯ ●
113+
● ◯ ● ● ◯ ●
114+
115+
116+
● ◯ ◯ ◯ ● ◯ ◯ ◯ ◯ ● ◯ ● ◯ ● ◯ ● ◯ ●
117+
◯ ◯ ● ◯ ◯ ◯
118+
119+
120+
◯ ◯ ◯ ◯ ◯ ● ◯ ● ◯ ● ◯ ● ◯ ● ◯ ● ◯ ●
121+
● ◯ ● ◯ ◯ ◯ ◯ ◯ ◯
122+
123+
124+
◯ ● ◯ ● ◯ ● ● ◯ ●
125+
126+
127+
128+
129+
130+
```
131+
132+
#### Step 3: solve the MIS solution back an MIS of the source graph
133+
```julia
134+
julia> original_configs = map_configs_back(res, [c])
135+
1-element Vector{Vector{Int64}}:
136+
[1, 0, 0, 1, 0, 0, 1, 1, 0, 0]
137+
138+
julia> UnitDiskMapping.is_independent_set(g, original_configs[1])
139+
true
140+
```
141+
142+
One can check this configuration with MIS size 4 is indead a maximal independent set of Petersen graph.
143+
Alternatively, one can use the following weighted mapping.
144+
145+
## Example 2: Weighted mapping
146+
One just add a new argument `Weighted()` for the `map_graph` function in the previous example:
147+
```julia
76148
julia> w_res = map_graph(Weighted(), g, vertex_order=Branching());
77149

78150
julia> w_res.grid_graph

src/mapping.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ function print_ugrid(io::IO, content::AbstractMatrix)
9090
end
9191
end
9292
end
93+
export print_config
94+
print_config(mr::MappingResult, config::AbstractMatrix) = print_config(stdout, mr, config)
95+
function print_config(io::IO, mr::MappingResult, config::AbstractMatrix)
96+
content = mr.grid_graph.content
97+
@assert size(content) == size(config)
98+
for i=1:size(content, 1)
99+
for j=1:size(content, 2)
100+
cell = content[i, j]
101+
@assert !(cell.connected || cell.doubled)
102+
if !isempty(cell)
103+
if !iszero(config[i,j])
104+
print(io, "")
105+
else
106+
print(io, "")
107+
end
108+
else
109+
if !iszero(config[i,j])
110+
error("configuration not valid, there is not vertex at location $((i,j)).")
111+
end
112+
print(io, "")
113+
end
114+
print(io, " ")
115+
end
116+
if i!=size(content, 1)
117+
println(io)
118+
end
119+
end
120+
end
121+
93122
Base.copy(ug::UGrid) = UGrid(ug.lines, ug.padding, copy(ug.content))
94123

95124
# TODO:

test/mapping.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,6 @@ end
110110
original_configs = map_configs_back(res, [c])
111111
@test count(isone, original_configs[1]) == missize
112112
@test is_independent_set(g, original_configs[1])
113+
@test println(res.grid_graph) === nothing
114+
@test print_config(res, c) === nothing
113115
end

0 commit comments

Comments
 (0)