Nodes have values. Node n's value is denoted n.val. Two or more nodes can have the same value.
- A node
nis acategory 0node or more simply a '0' node iffn.val % 4 == 0. - A node
nis a1node iffn.val % 4 == 1. - A node
nis a2node iffn.val % 4 == 2. - A node
nis a3node iffn.val % 4 == 3.
Nodes can be matched as follows:
- Two nodes which have the same value cannot be matched.
- a
0node can be matched to a1node, a2node, or a3node. We will abbreviate this fact as(0 | 1,2,3 ). - a
1node can be matched to a0node, a2node, or a1node as long as both nodes don't have the same value. Using our abbreviation,( 1 | 0,1,2 ). - a
2node can be matched to a0node, a1node, or a3node. Abbreviated as( 2 | 0,1,3 ). - a
3node can be matched to a0node, a2node, or a3node as long as both nodes don't have the same value. Using the abbreviation,( 3 | 0,2,3 ).
Under these very specific circumstances, can you help me find an efficient maximal matching algorithm?
Whichever maximal matching we achieve, the following statements must be true:
- If any
0nodes are unmatched, then all1,2, and3nodes must be matched. - If any
2nodes are unmatched, then all0,1, and3nodes must be matched. - If any
1nodes are unmatched, then all0and2nodes must be matched. Furthermore, all the unmatched1nodes must have the same value. It is also possible for some3nodes to be unmatched (see next statement). - If any
3nodes are unmatched, then all0and2nodes must be matched. Furthermore, all the unmatched3nodes must have the same value. It is also possible for somecategory 1nodes to be unmatched (see previous statement).
I have tried matching in the following manner, but don't know if it is optimal:
While there are unmatched nodes, try first to match all '0' nodes to '1' nodes, then '2' nodes, then '3' nodes in that order. Once you do this, either some '0' nodes are unmatched, in which case you are done, or all '0' nodes are matched, in which case pick the next biggest category which has unmatched nodes and try matching the nodes in that category to nodes in the same or bigger categories. So for example, if you have unmatched '1' nodes, first try to match those to other '1' nodes, then either you have matched all '1' nodes and move on to the next category with unmatched nodes, or you still have some unmatched '1' nodes, in which case you try to match as many '1' nodes as possible to '2' nodes. After this, either all '1' nodes will be matched or all '2' nodes will be matched. Etc...
I just have not found a way to know whether this approach leads to a maximal matching or not.
If you have a better approach or can shed some light on the optimality of my current approach, I would really appreciate it.