-
Notifications
You must be signed in to change notification settings - Fork 0
/
Outlier_Based_Occurrence_Window.java
109 lines (93 loc) · 3.82 KB
/
Outlier_Based_Occurrence_Window.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
*
* @author Himel Dev
*/
import java.util.*;
public class Outlier_Based_Occurrence_Window
{
public static void main(String[] args)
{
Outlier_Based_Occurrence_Window instance = new Outlier_Based_Occurrence_Window();
ArrayList<Integer> example_sequence = new ArrayList<>(Arrays.asList(1, 1, 2, 4, 5, 3, 2, 5, 2, 1));
ArrayList<Integer> example_pattern = new ArrayList<>(Arrays.asList(1, 2, 3));
int ret[] = instance.get_minimum_outlier_window(example_sequence, example_pattern);
if(ret[0] == 1)
System.out.println(ret[1]+" "+ret[2]+" "+ret[3]);
}
int[] get_minimum_outlier_window(ArrayList<Integer> sequence, ArrayList<Integer> pattern)
{
int sequence_length = sequence.size();
int pattern_length = pattern.size();
int window_start = 0;
int window_end = sequence_length;
int pattern_element_tally[] = new int [pattern_length];
int member_count = 0;
int outlier_count = 0;
int minimum_outlier_count = sequence_length;
for (int start = 0, end = 0; end < sequence_length; end++)
{
// Check if the last event of current window is a pattern element
int position = -1;
for(int i = 0; i < pattern_length; i++)
{
if(sequence.get(end) - pattern.get(i) == 0)
{
position = i;
break;
}
}
// If the last event of current window is not a pattern element, increment number of outliers and expand the window
if (position == -1)
{
outlier_count++;
continue;
}
// If the last event of current window is a pattern element, check if the window has covered any new pattern element
pattern_element_tally[position]++;
if (pattern_element_tally[position] == 1)
member_count++;
// If current window has covered all elements of pattern, shift the window's starting boundary to make it as compact as possible
// Also, make appropriate adjustments to number of outliers and tally of pattern elements
if (member_count == pattern_length)
{
//System.out.println("Start: "+start+" "+end);
position = -1;
for(int i = 0; i < pattern_length; i++)
{
if(sequence.get(start) - pattern.get(i) == 0)
position = i;
}
while (position == -1 || pattern_element_tally[position] > 1)
{
start++;
if (position != -1)
pattern_element_tally[position]--;
else
outlier_count--;
position = -1;
for(int i = 0; i < pattern_length; i++)
{
if(sequence.get(start) - pattern.get(i) == 0)
position = i;
}
}
//System.out.println("End: "+start+" "+end);
if (outlier_count < minimum_outlier_count)
{
window_start = start;
window_end = end;
minimum_outlier_count = outlier_count;
}
}
}
int ret[] = new int[4];
if(member_count == pattern_length)
ret[0] = 1;
else
ret[0] = 0;
ret[1] = window_start;
ret[2] = window_end;
ret[3] = minimum_outlier_count;
return ret;
}
}