Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition...

27
Graph Isomorphism Algorithms and networks

Transcript of Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition...

Page 1: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism

Algorithms and networks

Page 2: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 2

Today

• Graph isomorphism: definition

• Complexity: isomorphism completeness

• The refinement heuristic

• Isomorphism for trees– Rooted trees– Unrooted trees

Page 3: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 3

Graph Isomorphism

• Two graphs G=(V,E) and H=(W,F) are isomorphic if there is a bijective function f: V W such that for all v, w V:– {v,w} E {f(v),f(w)} F

Page 4: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Applications

• Chemistry: databases of molecules (etc.)– Actually needed: canonical form of molecule

structure / graph

• Other, e.g., speeding up algorithms for highly symmetric graphs

Graph Isomorphism 4

Page 5: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 5

Variant for labeled graphs

• Let G = (V,E), H=(W,F) be graphs with vertex labelings l: V L, l’ L.

• G and H are isomorphic labeled graphs, if there is a bijective function f: V W such that – For all v, w V: {v,w} E {f(v),f(w)} F

– For all v V: l(v) = l’(f(v)).

• Application: organic chemistry:– determining if two molecules are identical.

Page 6: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 6

Complexity of graph isomorphism

• Problem is in NP, but– No NP-completeness proof is known– No polynomial time algorithm is known

• If GI is NP-complete, then “strange things happen” – “Polynomial time hierarchy collapses to a finite level”

NP

NP-complete

P

Graphisomorphism

If P NP?

Page 7: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 7

Isomorphism-complete

• Problems are isomorphism-complete, if they are `equally hard’ as graph isomorphism– Isomorphism of bipartite graphs– Isomorphism of labeled graphs– Automorphism of graphs

• Given: a graph G=(V,E)• Question: is there a non-trivial

automorphism, i.e., a bijective function f: V V, not the identity, with for all v,wV:

– {v,w} E, if and only if {f(v),f(w)} E.

Page 8: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 8

More isomorphism complete problems

• Finding a graph isomorphism f• Isomorphism of semi-groups• Isomorphism of finite automata• Isomorphism of finite algebra’s• Isomorphism of

– Connected graphs– Directed graphs– Regular graphs– Perfect graphs– Chordal graphs– Graphs that are isomorphic with their complement

Page 9: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 9

Special cases are easier

• Polynomial time algorithms for– Graphs of bounded degree– Planar graphs– Trees– Bounded treewidth

• Expected polynomial time for random graphs

This course

Page 10: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 10

An equivalence relation on vertices

• Say v ~ w, if and only if there is an automorphism mapping v to w.

• ~ is an equivalence relation

• Partitions the vertices in automorphism classes

• Tells on structure of graph

Page 11: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 11

Iterative vertex partition heuristicthe idea

• Partition the vertices of G and H in classes• Each class for G has a corresponding class

for H.• Property: vertices in class must be mapped

to vertices in corresponding class• Refine classes as long as possible• When no refinement possible, check all

possible ways that `remain’.

Page 12: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 12

Iterative vertex partition heuristic

• If |V| |W|, or |E| |F|, output: no. Done.• Otherwise, we partition the vertices of G and H

into classes, such that– Each class for G has a corresponding class for H– If f is an isomorphism from G to H, then f(v) belongs to

the class, corresponding to the class of v.

• First try: vertices belong to the same class, when they have the same degree.– If f is an isomorphism, then the degree of f(v) equals

the degree of v for each vertex v.

Page 13: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 13

Scheme

• Start with sequence SG = (A1) of subsets of G with A1=V, and sequence SH = (B1) of subsets of H with B1=W.

• Repeat until …– Replace Ai in SG by Ai1,…,Air and replace Bi in SH by

Bi1,…,Bir.

• Ai1,…,Air is partition of Ai

• Bi1,…,Bir is partition of Bi

• For each isormorphism f: v in Aij if and only if f(v) in Bij

Page 14: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 14

Possible refinement

• Count for each vertex in Ai and Bi how many neighbors they have in Aj and Bj for some i, j.

• Set Ais = {v in Ai | v has s neighbors in Aj}.

• Set Bis = {v in Bi | v has s neighbors in Bj}.

• Invariant: for all v in the ith set in SG, f(v) in the ith set in SH.

• If some |Ai| |Bi|, then stop: no isomorphism.

Page 15: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 15

Other refinements

• Partition upon other characteristics of vertices– Label

– Number of vertices at distance d (in a set Ai).

– …

Page 16: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 16

After refining

• If each Ai contains one vertex: check the only possible isomorphism.

• Otherwise, cleverly enumerate all functions that are still possible, and check these.

• Works well in practice!

Page 17: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 17

Isomorphism on trees

• Linear time algorithm to test if two (labeled) trees are isomorphic. (Aho, Hopcroft, Ullman, 1974)

• Algorithm to test if two rooted trees are isomorphic.

• Used as a subroutine for unrooted trees.

Page 18: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 18

Rooted tree isomorphism

• For a vertex v in T, let T(v) be the subtree of T with v as root.

• Level of vertex: distance to root.

• If T1 and T2 have different number of levels: not isomorphic, and we stop. Otherwise, we continue:

Page 19: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 19

Structure of algorithm

• Tree is processed level by level, from bottom to root

• Processing a level:– A long label for each vertex is computed– This is transformed to a short label

• Vertices in the same layer whose subtrees are isomorphic get the same labels:– If v and w on the same level, then

• L(v)=L(w), if and only if T(v) and T(w) are isomorphic with an isomorphism that maps v to w.

Page 20: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 20

Labeling procedure• For each vertex, get the set of labels assigned to its

children.• Sort these sets.

– Bucketsort the pairs (L(w), v) for T1, w child of v– Bucketsort the pairs (L(w), v) for T2, w child of v

• For each v, we now have a long label LL(v) which is the sorted set of labels of the children.

• Use bucketsort to sort the vertices in T1 and T2 such that vertices with same long label are consecutive in ordering.

Page 21: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 21

On sorting w.r.t. the long lists (1)

• Preliminary work:– Sort the nodes is the layer on the number of

children they have.• Linear time. (Counting sort / Radix sort.)

– Make a set of pairs (j,i) with (j,i) in the set when the jth number in a long list is i.

– Radix sort this set of pairs.

Page 22: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 22

On sorting w.r.t. the long lists (2)

• Let q be the maximum length of a long list• Repeat

– Distribute among buckets the nodes with at least q children, with respect to the qth label in their long list

• Nodes distributed in buckets in earlier round are taken here in the order as they appear in these buckets.

• The sorted list of pairs (j,i) is used to skip empty buckets in this step.

– q --;

– Until q=0.

Page 23: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 23

After vertices are sorted with respect to long label

• Give vertices with same long label same short label (start counting at 0), and repeat at next level.

• If we see that the set of labels for a level of T1 is not equal to the set for the same level of T2, stop: not isomorphic.

Page 24: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 24

Time

• One layer with n1 nodes with n2 nodes in next layer costs O(n1 + n2) time.

• Total time: O(n).

Page 25: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 25

Unrooted trees

• Center of a tree– A vertex v with the property that the maximum distance

to any other vertex in T is as small as possible.– Each tree has a center of one or two vertices.

• Finding the center:– Repeat until we have a vertex or a single edge:

• Remove all leaves from T.

– O(n) time: each vertex maintains current degree in variable. Variables are updated when vertices are removed, and vertices put in set of leaves when their degree becomes 1.

Page 26: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 26

Isomorphism of unrooted trees

• Note: the center must be mapped to the center• If T1 and T2 both have a center of size 1:

– Use those vertices as root.

• If T1 and T2 both have a center of size 2:– Try the two different ways of mapping the centers– Or: subdivide the edge between the two centers and

take the new vertices as root

• Otherwise: not isomorphic.• 1 or 2 calls to isomorphism of rooted trees: O(n).

Page 27: Graph Isomorphism Algorithms and networks. Graph Isomorphism 2 Today Graph isomorphism: definition Complexity: isomorphism completeness The refinement.

Graph Isomorphism 27

Conclusions

• Similar methods work for finding automorphisms

• We saw: heuristic for arbitrary graphs, algorithm for trees

• There are algorithms for several special graph classes (e.g., planar graphs, graphs of bounded degree,…)