Title: Generate subsets of size K
1Generate subsets of size K
- public static getKSubsets(int n, int k)
-
- boolean subset new booleann // subseti
indicates if (i1) is in the subset, i 1, 2n - recursiveGenKSubsets( subset, n, k)
-
- // subset indicated what integers are selected
into the subset. - // n indicate the range 1, 2, ..n
- // k size of subset
- public static recursiveGenKSubset(boolean
subset, int n, int k) -
- // Base case 1 n k. So all members in 1,
..., n will be selected into the subset - // Base case 2 k 0. NO number in 1, , n
will be selected into the subset. -
- // Recursive case 1 Select number n, then
create subsets of size (k-1) in 1, , n-1. - // Recursive case 2 Do NOT select number n,
then create subsets of size (k) in 1, , n-1.
2New graph representation
class myNewGraph String Vertices int
Edges // different from before int numVertices,
numEdges
3New graph representation
- Constructor
- pubic myNewGraph(int capacity)
-
- Vertices new Stringcapacity
- Edges new intcapacity1
- //At first, vertices all have 0 neighbors
- for(int i 0 i lt capacity i)
- Edgesi0 0
-
- methods
- public void addVertex(String v)
- // Assume v is does not already exist.
- public void addEdge(String v1, String v2)
- // Assume both v1 and v2 already exist.
- // Assume edge between v1 and v2 does not
already exist. - public void deleteVertex(String v)
4public void addVertex(String v)
- Assume v is does not already exist
- Q What to do if Vertices is already full?
- How to change Vertices and Edges?
class myNewGraph String Vertices int
Edges // different from before int numVertices,
numEdges
5public void addEdge(String v1, String v2)
- Assume both v1 and v2 already exist.
- Assume edge between v1 and v2 does not already
exist. - If v1 has index i, v2 has index j,
- Q What to do if the Edgesi or Edgesj is
full? - How to expand Edgesi or Edgesj?
class myNewGraph String Vertices int
Edges // different from before int numVertices,
numEdges
6public void deleteVertex(String v)
- Assume v already exists
- Suppose v has index i,
- - Change Vertices move Verteicesi1,,
numVertices-1 to the front by 1 slot. - - Change Edges
- - Move row (i1), , row numVertices-1 up by 1
row. - - Decrement indices gt i in Edges
- - Delete i from its neighbors adjacencies.
class myNewGraph String Vertices int
Edges // different from before int numVertices,
numEdges