Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tool-up-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
elis
tool-up-backend
Commits
78d4de58
Commit
78d4de58
authored
9 years ago
by
Thiemo Belmega
Browse files
Options
Downloads
Patches
Plain Diff
Implement util to get all subsets of a given size out of a set
parent
f12340e7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/de/unipotsdam/cs/toolup/util/SubsetUtil.java
+57
-0
57 additions, 0 deletions
src/main/java/de/unipotsdam/cs/toolup/util/SubsetUtil.java
src/test/java/de/unipotsdam/cs/toolup/util/SubsetUtilTest.java
+81
-0
81 additions, 0 deletions
...est/java/de/unipotsdam/cs/toolup/util/SubsetUtilTest.java
with
138 additions
and
0 deletions
src/main/java/de/unipotsdam/cs/toolup/util/SubsetUtil.java
0 → 100644
+
57
−
0
View file @
78d4de58
package
de.unipotsdam.cs.toolup.util
;
import
java.util.*
;
/**
* Created by Thiemo on 20.12.2015.
*/
public
class
SubsetUtil
{
public
static
<
T
>
Set
<
Set
<
T
>>
getAllSubsetsOfSize
(
Set
<
T
>
superSet
,
int
size
)
{
List
<
T
>
superList
=
new
ArrayList
<>(
superSet
);
Set
<
Set
<
T
>>
setOfSubSets
=
new
HashSet
<>();
for
(
int
i
=
0
;
i
<
superList
.
size
();
i
++)
{
T
currentElement
=
superList
.
get
(
i
);
if
(
size
==
1
)
{
List
<
T
>
newSubList
=
Arrays
.
asList
(
currentElement
);
setOfSubSets
.
add
(
new
HashSet
<>(
newSubList
));
}
else
{
setOfSubSets
.
addAll
(
createAllSubSetsWithCurrentElementRecursively
(
size
,
superList
,
currentElement
));
}
}
return
setOfSubSets
;
}
/**
* Recursive method.
* <p>
* Example:
* If current element is "1" and superList is "1","2","3","4" and we are looking for sub sets of size 2;
* <p>
* - The method gets all sub sets of size 1 of the list "2","3","4", which are the sub sets {"2"}, {"3"} and {"4"}
* - Then the method adds the current element to each of these sub sets, which results
* in the sets {"1","2"},{"1","3"} and {"1","4"}
* - The method returns a set of all these created sub sets
*/
private
static
<
T
>
Set
<
Set
<
T
>>
createAllSubSetsWithCurrentElementRecursively
(
int
size
,
List
<
T
>
superList
,
T
currentElement
)
{
Set
<
Set
<
T
>>
setOfSubSetsWithoutCurrentElement
=
getAllSmallerSubsetsWithoutCurrentElement
(
size
,
superList
,
currentElement
);
Set
<
Set
<
T
>>
resultSetOfSubSets
=
new
HashSet
<>();
for
(
Set
<
T
>
subSetWithoutCurrentElement
:
setOfSubSetsWithoutCurrentElement
)
{
subSetWithoutCurrentElement
.
add
(
currentElement
);
resultSetOfSubSets
.
add
(
subSetWithoutCurrentElement
);
}
return
resultSetOfSubSets
;
}
private
static
<
T
>
Set
<
Set
<
T
>>
getAllSmallerSubsetsWithoutCurrentElement
(
int
size
,
List
<
T
>
superList
,
T
currentElement
)
{
Set
<
T
>
setWithoutCurrentElement
=
new
HashSet
(
superList
);
setWithoutCurrentElement
.
remove
(
currentElement
);
return
getAllSubsetsOfSize
(
setWithoutCurrentElement
,
size
-
1
);
}
}
This diff is collapsed.
Click to expand it.
src/test/java/de/unipotsdam/cs/toolup/util/SubsetUtilTest.java
0 → 100644
+
81
−
0
View file @
78d4de58
package
de.unipotsdam.cs.toolup.util
;
import
org.testng.annotations.Test
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
static
junit
.
framework
.
Assert
.
assertEquals
;
import
static
org
.
testng
.
AssertJUnit
.
assertTrue
;
@SuppressWarnings
(
"ALL"
)
public
class
SubsetUtilTest
{
List
<
String
>
superList
=
Arrays
.
asList
(
"1"
,
"2"
,
"4"
,
"3"
);
Set
<
String
>
superSet
=
new
HashSet
<>(
superList
);
@Test
public
void
testThatSubsetUtilReturnsAllSubSetsOfSize4
()
{
//arrange
//act
Set
<
Set
<
String
>>
subSets
=
SubsetUtil
.
getAllSubsetsOfSize
(
superSet
,
4
);
//assert
assertTrue
(
subSets
.
contains
(
superSet
));
}
@Test
public
void
testThatSubsetUtilReturnsAllSubSetsOfSize3
()
{
//arrange
List
<
String
>
subList1
=
Arrays
.
asList
(
"1"
,
"2"
,
"4"
);
List
<
String
>
subList2
=
Arrays
.
asList
(
"1"
,
"2"
,
"3"
);
List
<
String
>
subList3
=
Arrays
.
asList
(
"1"
,
"4"
,
"3"
);
List
<
String
>
subList4
=
Arrays
.
asList
(
"3"
,
"2"
,
"4"
);
//act
Set
<
Set
<
String
>>
subSets
=
SubsetUtil
.
getAllSubsetsOfSize
(
superSet
,
3
);
//assert
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList1
)));
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList2
)));
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList3
)));
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList4
)));
}
@Test
public
void
testThatSubsetUtilReturnsAllSubSetsOfSize2
()
{
//arrange
//act
Set
<
Set
<
String
>>
subSets
=
SubsetUtil
.
getAllSubsetsOfSize
(
superSet
,
2
);
//assert
assertEquals
(
6
,
subSets
.
size
());
}
@Test
public
void
testThatSubsetUtilReturnsAllSubSetsOfSize1
()
{
//arrange
List
<
String
>
subList1
=
Arrays
.
asList
(
"1"
);
List
<
String
>
subList2
=
Arrays
.
asList
(
"2"
);
List
<
String
>
subList3
=
Arrays
.
asList
(
"3"
);
List
<
String
>
subList4
=
Arrays
.
asList
(
"4"
);
//act
Set
<
Set
<
String
>>
subSets
=
SubsetUtil
.
getAllSubsetsOfSize
(
superSet
,
1
);
//assert
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList1
)));
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList2
)));
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList3
)));
assertTrue
(
subSets
.
contains
(
new
HashSet
(
subList4
)));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment