JuliaGraphs / JuliaGraphs/GraphsOptim.jl

Isomorphism

Open
#23 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
22
Forks
8
PR merge metrics
No merged PRs in 30d

Description

A natural addition is a function to find isomorphisms between graphs. I've written some code that does that. (I'm sure it works for simple graphs and simple digraphs. Not sure about weighted.) I hope you find this useful.

using Graphs
using JuMP
using HiGHS
using ChooseOptimizer

set_solver(HiGHS)
set_solver_verbose(false)

_graphs_not_iso() = error("The graphs are not isomorphic")

"""
    graph_iso(g::AbstractGraph, h::AbstractGraph)::Dict{Int,Int}

Return an isomorphism `f` from `g` to `h`, or throw an error if the 
graphs are not isomorphic. Here `f` is a `Dict` with the property that
`(u,v)` is an edge of `g` if and only if `(f[u],f[v])` is an edge of `h`.
"""
function graph_iso(g::AbstractGraph, h::AbstractGraph)::Dict{Int,Int}
    n = nv(g)
    if n != nv(h)
        _graphs_not_iso()
    end

    # other simple tests can go here, for example:
    if ne(g) != ne(h)
        _graphs_not_iso()
    end

    A = adjacency_matrix(g)
    B = adjacency_matrix(h)
    MOD = Model(get_solver())
    @variable(MOD, X[1:n, 1:n], Bin)

    # success when A*X = X*B and X is permutation 

    # X must be doubly stochastic ==> permutation
    for i = 1:n
        @constraint(MOD, sum(X[i, j] for j = 1:n) == 1)
        @constraint(MOD, sum(X[j, i] for j = 1:n) == 1)
    end

    # A*X = X*B 
    for i = 1:n
        for k = 1:n
            @constraint(
                MOD,
                sum(A[i, j] * X[j, k] for j = 1:n) == sum(X[i, j] * B[j, k] for j = 1:n)
            )
        end
    end

    optimize!(MOD)
    status = Int(termination_status(MOD))

    # status == 1 means success
    if status != 1
        _graphs_not_iso()
    end

    # get the matrix
    P = Int.(value.(X))

    # convert to a dictionary
    result = Dict{Int,Int}()
    for v=1:n 
        for w=1:n 
            if P[v,w] > 0
                result[v] = w 
            end
        end
    end

    return result
end

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository files or tests; start by reviewing the proposed graph_iso function and its Graphs, JuMP, and HiGHS usage. Verify the stated behavior for simple graphs and digraphs, and clarify weighted-graph support. Done should include a decided supported scope, tests for isomorphic and non-isomorphic inputs, and an integrated implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.