Problem Statement
This problem is about simple undirected graphs.
(That is, each edge is bidirectional, there are no self-loops, and each pair of vertices is connected by at most one edge.)
Suppose that we are given a graph G and a set S that is a non-empty subset of the vertices of G. Let G(S) be a new graph. The vertices of G(S) are the vertices in the set S. Two vertices in G(S) are connected by an edge if and only if they are connected in the original graph G. Each such graph G(S) is called an induced subgraph of the graph G.
A graph with n vertices has 2^n - 1 distinct induced subgraphs. (Note that the set S must be non-empty. One of the 2^n - 1 induced subgraphs is the graph G itself.)
You are given an ints k. Construct a simple undirected graph G with the following properties:
For each k that satisfies the given constraints there is at least one such graph G. If there are multiple solutions, you may choose any of them. Return the description of your chosen graph G: a String[] res with n elements, each containing n characters. For each i and j, res[i][j] should be 'Y' if there is an edge connecting vertices i and j, otherwise res[i][j] should be 'N'. Note that for each i, res[i][i] should be 'N'.
Suppose that we are given a graph G and a set S that is a non-empty subset of the vertices of G. Let G(S) be a new graph. The vertices of G(S) are the vertices in the set S. Two vertices in G(S) are connected by an edge if and only if they are connected in the original graph G. Each such graph G(S) is called an induced subgraph of the graph G.
A graph with n vertices has 2^n - 1 distinct induced subgraphs. (Note that the set S must be non-empty. One of the 2^n - 1 induced subgraphs is the graph G itself.)
You are given an ints k. Construct a simple undirected graph G with the following properties:
- The vertices in G are numbered 0 through n-1, where n is their number.
- The value of n must be between 1 and 20, inclusive.
- Among the induced subgraphs of G there are exactly k connected graphs.
For each k that satisfies the given constraints there is at least one such graph G. If there are multiple solutions, you may choose any of them. Return the description of your chosen graph G: a String[] res with n elements, each containing n characters. For each i and j, res[i][j] should be 'Y' if there is an edge connecting vertices i and j, otherwise res[i][j] should be 'N'. Note that for each i, res[i][i] should be 'N'.
Definition
- Class:
- ConnectedSubgraph
- Method:
- construct
- Parameters:
- int
- Returns:
- String[]
- Method signature:
- String[] construct(int k)
- (be sure your method is public)
Constraints
- k will be between 1 and 65,535, inclusive.
Examples
6
Returns: {"NYN", "YNY", "NYN" }Our output is a graph with 3 vertices and 2 edges: 0-1 and 1-2. This graph has 7 induced subgraphs. Among those, 6 are connected. These are the subgraphs induced by {0}, {1}, {2}, {0,1}, {1,2}, and {0,1,2}. Only the subgraph induced by {0,2} is not connected: it consists of two isolated vertices.5
Returns: {"NNNNN", "NNNNN", "NNNNN", "NNNNN", "NNNNN" }Our output is a graph with 5 vertices and no edges. Each induced subgraph with a single vertex is connected.31
Returns: {"NYYYY", "YNYYY", "YYNYY", "YYYNY", "YYYYN" }Note that the graph we choose may only have at most 20 vertices. We cannot output a graph with 31 nodes and no edges. Out output is a complete graph on 5 nodes. Each of its 31 induced subgraphs is obviously connected.9
Returns: {"NYNNNN", "YNNNNN", "NNNYNN", "NNYNNN", "NNNNNY", "NNNNYN" }1
Returns: {"N" }