How to define hash function for a tree structure which depends only on the structure of the tree, and is same irrespective of node labels?
For example, 2--1--3--4 should have same hash function as 1--4--2--3 or 4--1--3--2.
How to define hash function for a tree structure which depends only on the structure of the tree, and is same irrespective of node labels?
For example, 2--1--3--4 should have same hash function as 1--4--2--3 or 4--1--3--2.
Find the centre of the tree. Then run a recursive algorithm as such from the centre:
recurse(u, p):
hash = INITH
vector childrenhash = {}
for each (u,v) in G:
if v!=p:
childrenhash.insert(recurse(v,u))
childrenhash.sort()
for elem in childrenhash:
hash = (hash * (elem xor PR)) % MOD
return hash
Choose some appropriate values for INITH
, MOD
and PR
.
Two isomorphic trees will have the same hash.
If you throw out node labels, what is left is the number of children for each node. So you could just count the number of children for each node and write them all in one string( array, vector, ...).
Example:
a 2
/ \ / \
b c => 0 2 => 2,0,2,0,0
/ \ / \
d e 0 0
Now, suppose you're saying, the following trees should be considered equal:
a a
/ | \ / | \
b c d b c d
/ \ \ / \ |
d e f d e f
You can add more transformation step to the same idea: sort the children:
a 3 3
/ | \ / | \ / | \
b c d => 0 2 1 => 0 1 2 => 3,0,1,2,0,0,0
/ \ \ / \ \ | / \
d e f 0 0 0 0 0 0
a 3 3
/ | \ / | \ / | \
b c d => 2 0 1 => 0 1 2 => 3,0,1,2,0,0,0
/ \ | / \ | | / \
d e f 0 0 0 0 0 0
I'm probably following idea in the link by @gilleain: https://math.stackexchange.com/questions/1604260/algorithm-for-equality-of-trees-of-restricted-depth