Skip to content

Commit

Permalink
Refactored for executing speed
Browse files Browse the repository at this point in the history
By sharing a dynamic programming table on similar tasks, it can run
faster than before. I also modified many codes in dp_modules.
I refactored a sorting algorithm, reduced clone and unnecessary
conditions.
  • Loading branch information
europeanplaice committed Mar 31, 2022
1 parent ac11227 commit 28883a8
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 317 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "subset_sum"
version = "0.18.3"
version = "0.19.0"
edition = "2018"
authors = ["Tomohiro Endo <[email protected]>"]
description = "Solves subset sum problem and returns a set of decomposed integers. It also can match corresponding numbers from two vectors and be used for Account reconciliation."
Expand Down
24 changes: 2 additions & 22 deletions docs/pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ There are four ways to use this program.
* [CLI](#CLI)🖥️
* [Rust](#rust)🦀
* [Python](#python)🐍
* [Web](https://europeanplaice.github.io/subset_sum/find_subset.html)🌎 (Easy to use)
* [Web](https://europeanplaice.github.io/subset_sum/find_subset)🌎 (Easy to use)

Here is also an out of the box example of python you can run now in google colab.
https://colab.research.google.com/github/europeanplaice/subset_sum/blob/main/python/python_subset_sum.ipynb

And it has three methods.
And it has two methods.

* `find_subset`
* It finds a subset from an array.
* `find_subset_fast_only_positive`
* It finds a subset from an array. It can't accept negative values but relatively faster.
* `Sequence Matcher`
* It finds subset sum relationships with two arrays. Solving multiple subset sub problem.

Expand Down Expand Up @@ -136,24 +134,6 @@ print(dpss.find_subset([1, -2, 3, 4, 5], 2, 3))
```
>>> [[4, -2], [3, -2, 1]]
```
#### `find_subset_fast_only_positive`
```python
help(dpss.find_subset_fast_only_positive)
```
```
>>> find_subset_fast_only_positive(arr, value, max_length, /)
>>> Finds subsets sum of a target value. It can't accept negative values but relatively faster.
>>> # Arguments
>>> * `arr` - An array.
>>> * `value` - The value to the sum of the subset comes.
>>> * `max_length` - The maximum length of combinations of the answer.
```
```python
print(dpss.find_subset_fast_only_positive([1, 2, 3, 4, 5], 10, 4))
```
```
>>> [[4, 3, 2, 1], [5, 3, 2], [5, 4, 1]]
```

#### `sequence_matcher`
```python
Expand Down
Binary file modified docs/pkg/dpss_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Tomohiro Endo <[email protected]>"
],
"description": "Solves subset sum problem and returns a set of decomposed integers. It also can match corresponding numbers from two vectors and be used for Account reconciliation.",
"version": "0.18.3",
"version": "0.19.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
87 changes: 9 additions & 78 deletions python/python_subset_sum.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,20 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install dpss\n",
"import dpss"
"import dpss\n",
"dpss.__version__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[([1050], [1050]),\n",
" ([1980], [30, 1950]),\n",
" ([2980], [80, 2900]),\n",
" ([3500], [200, 3300]),\n",
" ([4000], [20, 3980])],\n",
" [([1050], [1050]),\n",
" ([1980], [30, 1950]),\n",
" ([2980], [80, 2900]),\n",
" ([3500, 4000], [20, 200, 3300, 3980])],\n",
" [([1050], [1050]),\n",
" ([1980], [30, 1950]),\n",
" ([3500], [200, 3300]),\n",
" ([2980, 4000], [20, 80, 2900, 3980])],\n",
" [([1050], [1050]),\n",
" ([1980], [30, 1950]),\n",
" ([4000], [20, 3980]),\n",
" ([2980, 3500], [80, 200, 2900, 3300])],\n",
" [([1980], [30, 1950]),\n",
" ([2980], [80, 2900]),\n",
" ([1050, 3500, 4000], [20, 200, 1050, 3300, 3980])],\n",
" [([1980], [30, 1950]),\n",
" ([2980], [80, 2900]),\n",
" ([3500], [200, 3300]),\n",
" ([1050, 4000], [20, 1050, 3980])],\n",
" [([1980], [30, 1950]),\n",
" ([2980], [80, 2900]),\n",
" ([4000], [20, 3980]),\n",
" ([1050, 3500], [200, 1050, 3300])],\n",
" [([1980], [30, 1950]),\n",
" ([3500], [200, 3300]),\n",
" ([1050, 2980, 4000], [20, 80, 1050, 2900, 3980])],\n",
" [([1980], [30, 1950]),\n",
" ([3500], [200, 3300]),\n",
" ([4000], [20, 3980]),\n",
" ([1050, 2980], [80, 1050, 2900])],\n",
" [([1980], [30, 1950]),\n",
" ([4000], [20, 3980]),\n",
" ([1050, 2980, 3500], [80, 200, 1050, 2900, 3300])]]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"dpss.sequence_matcher(\n",
" [1980, 2980, 3500, 4000, 1050],\n",
Expand All @@ -83,40 +36,18 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 4, 5], [2, 3, 5], [1, 2, 3, 4]]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"dpss.find_subset_fast_only_positive([1, 2, 3, 4, 5], 10, 4)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 5], [2, 4], [1, 2, 3]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"dpss.find_subset([1, 2, 3, 4, 5], 6, 3)"
]
Expand Down
7 changes: 1 addition & 6 deletions python/test_py_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

def test_find_subset():
assert dpss.find_subset([1, 2, 3, 4, 5], 6, 3) == [
[1, 5], [2, 4], [1, 2, 3]]


def test_find_subset_fast_only_positive():
assert dpss.find_subset_fast_only_positive([1, 2, 3, 4, 5], 10, 4) == [
[1, 4, 5], [2, 3, 5], [1, 2, 3, 4]]
[2, 4], [1, 5], [1, 2, 3]]


def test_sequence_matcher():
Expand Down
22 changes: 1 addition & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ There are four ways to use this program.
Here is also an out of the box example of python you can run now in google colab.
https://colab.research.google.com/github/europeanplaice/subset_sum/blob/main/python/python_subset_sum.ipynb

And it has three methods.
And it has two methods.

* `find_subset`
* It finds a subset from an array.
* `find_subset_fast_only_positive`
* It finds a subset from an array. It can't accept negative values but relatively faster.
* `Sequence Matcher`
* It finds subset sum relationships with two arrays. Solving multiple subset sub problem.

Expand Down Expand Up @@ -136,24 +134,6 @@ print(dpss.find_subset([1, -2, 3, 4, 5], 2, 3))
```
>>> [[4, -2], [3, -2, 1]]
```
#### `find_subset_fast_only_positive`
```python
help(dpss.find_subset_fast_only_positive)
```
```
>>> find_subset_fast_only_positive(arr, value, max_length, /)
>>> Finds subsets sum of a target value. It can't accept negative values but relatively faster.
>>> # Arguments
>>> * `arr` - An array.
>>> * `value` - The value to the sum of the subset comes.
>>> * `max_length` - The maximum length of combinations of the answer.
```
```python
print(dpss.find_subset_fast_only_positive([1, 2, 3, 4, 5], 10, 4))
```
```
>>> [[4, 3, 2, 1], [5, 3, 2], [5, 4, 1]]
```

#### `sequence_matcher`
```python
Expand Down
Loading

0 comments on commit 28883a8

Please sign in to comment.