-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypto.java
More file actions
207 lines (200 loc) · 8.2 KB
/
Crypto.java
File metadata and controls
207 lines (200 loc) · 8.2 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.util.*;
public class Crypto{
static Scanner scan= new Scanner(System.in);
static String input=scan.nextLine();
static int length=input.length();
public static void main(String[] args) {
int [] test=new int[length];
ArrayList<Set<Integer>> output;
for (int j=0; j<length/2+1; j++) {
output=split(test, j);
}
//printing out the subsets;
System.out.println(collect(7, 3));
}
//finding the best way to construct the space generated by the code
//num specifies the number of subsets;
/*
in the output array, every row is the subset of index+0's+subset
size at the end; the number of columns is the size of the largest
subset plus one
*/
//Seeing if the collection of subsets is elligible;
public static boolean test (ArrayList<Set<Integer>> input) {
for (int r=0; r<input.size(); r++) {
if (single(input.get(r), input)==false)
return false;
}
return true;
}
//detecting the common elements of two strings;
public static Set<Integer> commonEle (Set<Integer> a, Set<Integer> b) {
Set <Integer> output=new HashSet<Integer>();
for (Integer i:a) {
if (b.contains(i)) output.add(i);
}
return output;
}
//Seeing if an element is the only intersection of sets;
public static boolean isOnly (Integer a, ArrayList<Set<Integer>>b) {
Set<Integer> intersection=new HashSet<Integer>();
for (int p=0; p<b.size(); p++) {
intersection=commonEle(intersection, b.get(p));
}
return (intersection.contains(a)&&intersection.size()==1);
}
//Seeing if a single subset is eligible given the array of subsets;
//Here, note that /a/ must be one of the sets in /b/;;
public static boolean single (Set<Integer>a, ArrayList<Set<Integer>>b) {
//num: recording the elements that belong only to subset a;;
int num=0;
/*sum: recording the number of elements that is the only
intersection of sets;; */
int sum=0;
List<Integer> list=new ArrayList<Integer>(a);
for (int q=0; q<list.size(); q++) {
for (int j=0; j<b.size(); j++) {
if (b.get(j).contains(list.get(q))) {
num++;
Integer target=list.get(q);
//index indicates which subsets in /b/ contain target;
List<Integer> index=new ArrayList<Integer>();
index.add(Integer(j));
for (int i=j+1; i<list.size(); i++) {
if (b.get(i).contains(target))
index.add(Integer(i));
}
ArrayList<Set<Integer>> c=new ArrayList<Set<Integer>>();
c.add(a);
for (int s=1; s<index.size()+1; s++) {
c.add(b.get(index.get(s-1)));
}
if (isOnly(target, c)==true) sum++;
break;
}
}
}
if (num>1) return false;
else{
if (num==0) {
if (sum>a.size()-2) return true;
else return false;
}
else {
if (sum>a.size()-3) return true;
else return false;
}
}
//calculating the efficiency of a method;
}
/*
Conditions that each subset must satisfy:
1. At least one element belongs to another set;;
2. At most one element that belongs only to this subset;;
3. Every element except for the one in (2), except for at most
one, has to be the only intersection of other subsets;;
*/
public static Integer[][] split(int[] input, int num) {
Integer[][] result=new Integer[input.length][num];
//exploring different partitions of the original set;
ArrayList<Integer[][]> collection=collect(input.length, num);
/*
remember to calculate the efficiency / specify which part is the
error-correcting code; number of operations and data points and percentages
*/
for (int v=0; v<collection.size()-1; v++) {
Integer[][] a=collection.get(v);
Integer[][] b=collection.get(v+1);
}
return result;
}
//generating the colletion(s) of subsets
public static ArrayList<Integer[][]> collect (int count, int num) {
Set<Integer> whole = new HashSet<Integer>();
for (int u=1; u<count+1; u++)
whole.add(Integer(u));
//Count is the number of elements;
//num is the number of subsets;
ArrayList<Integer[][]> output=new ArrayList<Integer[][]>();
//elements is the collection of all subsets of different lengths;
ArrayList<Set<Integer>> elements=new ArrayList<Set<Integer>>();
for (int j=2; j<count+1; j++) {
ArrayList<Set<Integer>> addon = generate(count, j);
for (Set<Integer> q:addon)
elements.add(q);
}
//now we select a certain number of subsets from Elements to cover all elements
//It is in the form of eg. {(1, 2, 3), (1, 2, 4)...}
ArrayList<Set<Integer>> select=generate(elements.size(), num);
for (int i=0; i<select.size(); i++) {
Set<Integer> target=select.get(i);
ArrayList<Set<Integer>> first=new ArrayList<Set<Integer>>();
for (Integer j:target)
first.add(elements.get(j));
if (covers(first, whole)) {
Integer [][] component = new Integer[count][num];
for (int s=0; s<num; s++) {
for (int t=0; t<first.get(s).size(); t++) {
ArrayList<Integer> e = new ArrayList<Integer>(first.get(s));
component[t][s]=e.get(t);
}
}
output.add(component);
}
}
return output;
//In this function we need to reduce the redundant part as much as possible.
}
//Generating all the subsets of a certain size;
public static ArrayList<Set<Integer>> generate (int count, int size) {
Set<Integer> total=new HashSet<Integer>();
for (int i=1; i<count+1; i++)
total.add(Integer(i));
ArrayList<Set<Integer>> result=new ArrayList<Set<Integer>>();
ArrayList<Set<Integer>> firstStep=generate(count, num-1);
for (int i=0; i<firstStep.size(); i++) {
//adding one element to the subset whose size is one less;
ArrayList<Set<Integer>> addin=new ArrayList<Set<Integer>>();
List<Integer> exception;
exception=new ArrayList<Integer>(supplement(firstStep.get(i), total));
for (int p=0; p<exception.size(); p++) {
Set<Integer> inter=firstStep.get(i);
inter.add(exception.get(p));
addin.add(inter);
}
Set<Set<Integer>> relay1=new HashSet<Set<Integer>>(addin);
Set<Set<Integer>> relay2=new HashSet<Set<Integer>>(result);
result=new ArrayList<Set<Integer>>(Set.union(relay1, relay2));
}
return result;
}
//this function is to calculate C_ab;
public static Set<Integer> supplement (Set<Integer> a, Set<Integer> b) {
Set <Integer> output= new HashSet<Integer>();
List <Integer> bb = new ArrayList<Integer>(b);
for (int c=0; c<bb.size(); c++) {
if (a.contains(bb.get(c))&&(!b.contains(bb.get(c))))
output.add(bb.get(c));
}
return output;
}
//seeing if the union of sets cover another set;
public static boolean covers (ArrayList<Set<Integer>>a, Set<Integer>b) {
Set<Integer> sum = new HashSet<Integer>();
for (Set<Integer> c:a)
sum=Set.union(sum, c);
for (Integer d:b)
if (!sum.contains(d)) return false;
return true;
}
//The following function assigns error-correcting codes to the network;
public static void assign(Integer[][] a) {
}
public static double efficiency (ArrayList<Set<Integer>>d) {
return 0.0;
}
/*
Constructing the Network of Error-Correcting Bits;
Then defining the success of a method;
*/
}