-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSetPartitionConsideringSparseArray.cs
More file actions
137 lines (108 loc) · 4.03 KB
/
SetPartitionConsideringSparseArray.cs
File metadata and controls
137 lines (108 loc) · 4.03 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SetPartitionConsideringSparseArray
{
class SetPartitionConsideringSparseArray
{
public static int SETSIZE = 200; // maximum set we can handle, 200, so that the amount of space O(200x200)
public static int[][] result = new int[SETSIZE + 1][];
public static int[][] pValue = new int[SETSIZE + 1][];
/*
* Sept. 5, 2015
*
* Problem statement:
* Analysis:
*
* How to understand the code?
* Thought process?
* Simple test case to go over?
* Warm up process: how to give user a clear idea what to solve in the function?
* When practising the code, try to provide a list of references, so it is easy to evaluate
* the idea is great, and implementation is also ok.
*
* time complexity, space complexity:
*
* code is too long? Need improvement?
*/
static void Main(string[] args)
{
int[] test = { 1, 1, 5, 2, 3, 10, 9, 8 };
int[] testGood2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] testGood3 = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[] testGood1 = { 1, 1, 5, 2 };
SETSIZE = test.Length;
// partition the set so that two sets are minimally different value;
int sum = 0;
foreach (int i in test)
sum += i;
int target = sum / 2 + 1;
int arraySize_J = target + 1;
for (int i = 0; i < SETSIZE + 1; i++)
{
result[i] = new int[arraySize_J];
pValue[i] = new int[arraySize_J];
}
for (int i = 0; i < SETSIZE + 1; i++)
{
pValue[i][0] = 1;
}
for (int i = 0; i < arraySize_J; i++)
pValue[0][i] = 0;
pValue[0][0] = 1;
for (int i = 1; i < SETSIZE + 1; i++)
for (int j = 1; j < arraySize_J; j++)
{
pValue[i][j] = 0;
if (pValue[i - 1][j] > 0) pValue[i][j] = 1;
if (i >= 1)
if (j - test[i - 1] >= 0) pValue[i][j] = pValue[i - 1][j - test[i - 1]];
}
int k = 0;
int SumBestPartition = 0;
int indexSet = 0;
bool foundOne = false;
// need to tell the value of S, and then, know how to choose the first set
ArrayList firstSet = new ArrayList();
for (int i = 1; i <= SETSIZE; i++)
{
for (int j = 1; j <= arraySize_J; j++)
{
if (pValue[SETSIZE + 1 - i][arraySize_J - j] == 1)
{
SumBestPartition = arraySize_J - j;
indexSet = SETSIZE + 1 - i;
firstSet.Add(test[indexSet - 1]); // first node
foundOne = true;
break;
}
}
if (foundOne)
break;
}
// and then choose the first set
int test2 = SumBestPartition;
bool setIsComplete = false;
int currentPartition = SumBestPartition - test[indexSet - 1];
SumBestPartition -= test[indexSet - 1];
// find next point and then add into the set
do
{
for (int i = 1; i < indexSet; i++)
{
if (pValue[i][currentPartition] == 1)
{
firstSet.Add(test[i - 1]);
currentPartition -= test[i - 1];
indexSet = i;
break;
}
}
} while (currentPartition > 0);
int checkFirstSet = 1;
}
}
}