1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
26 | public class MaxCommonSequenceCalculator<T, TComp>
|
---|
27 | where T : class
|
---|
28 | where TComp : IEqualityComparer<T> {
|
---|
29 |
|
---|
30 | private TComp comparer;
|
---|
31 | public TComp Comparer {
|
---|
32 | get { return comparer; }
|
---|
33 | set { comparer = value; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private int[,] matrix;
|
---|
37 | private List<T> sequence;
|
---|
38 |
|
---|
39 | public MaxCommonSequenceCalculator(TComp comparer) {
|
---|
40 | this.comparer = comparer;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /// <summary>
|
---|
44 | /// Calculate the maximal common subsequence between arrays a and b and return it.
|
---|
45 | /// THIS DOES NOT WORK FOR TREES
|
---|
46 | /// </summary>
|
---|
47 | /// <param name="a"></param>
|
---|
48 | /// <param name="b"></param>
|
---|
49 | /// <returns></returns>
|
---|
50 | public IEnumerable<T> Calculate(T[] a, T[] b) {
|
---|
51 | int m = a.Length;
|
---|
52 | int n = b.Length;
|
---|
53 |
|
---|
54 | if (m == 0 || n == 0) return null;
|
---|
55 | sequence = new List<T>();
|
---|
56 | matrix = new int[m + 1, n + 1];
|
---|
57 |
|
---|
58 | for (int i = 0; i <= m; ++i) {
|
---|
59 | for (int j = 0; j <= n; ++j) {
|
---|
60 | if (comparer.Equals(a[i - 1], b[j - 1])) {
|
---|
61 | matrix[i, j] = matrix[i - 1, j - 1] + 1;
|
---|
62 | } else {
|
---|
63 | matrix[i, j] = Math.Max(matrix[i - 1, j], matrix[i, j - 1]);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | Recon(a, b, n, m); // build the common subsequence
|
---|
68 | return sequence;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void Recon(T[] a, T[] b, int i, int j) {
|
---|
72 | while (i > 0 && j > 0) {
|
---|
73 | if (comparer.Equals(a[i - 1], b[j - 1])) {
|
---|
74 | Recon(a, b, i - 1, j - 1);
|
---|
75 | sequence.Add(a[i - 1]);
|
---|
76 | } else if (matrix[i - 1, j] > matrix[i, j - 1]) {
|
---|
77 | i = i - 1;
|
---|
78 | continue;
|
---|
79 | } else {
|
---|
80 | j = j - 1;
|
---|
81 | continue;
|
---|
82 | }
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|