• 0

Country: United States

Comment hidden because of low score. Click to expand.
1
of 1 vote

this is the independent set problem. though similar to the matching, it is np-hard.

the relations form the edges between vertices. the problem asks the size of the largest subset of the vertices of a graph with the property that no two are connected by an edge. note that the edges can be of an arbitrary size.

Comment hidden because of low score. Click to expand.
0
of 0 vote

if the relation is transitive(from aRb and bRc follows aRc) and symetric (from aRb follows bRa), then it's finding the connected component's in a undirected graph and just pick one vertex from each component.
I assume the relation is not to be treated as transitive, so, I don't see much else then a brute force, maximizing the recursion where you exclude directly related elements from the set of potential candidates. Every element has two options: it is in the set or it isn't. Since it's only about finding the size, I assume we can use some dp-techniques, similar like knapsack.
where those interviews phd level?

Comment hidden because of low score. Click to expand.
0
of 0 vote

We can try something like this (I assume the relations are not transitive, and are symmetric):
1. Select a node with minimum number of connections, and add it to the output set.
2. Remove the selected node and nodes connected to it from the graph.
3. Go to #1.

Comment hidden because of low score. Click to expand.
0
of 0 vote

@ChrisK, i think your idea works and i just elaborate with a 3-step solution as follows:
Step 1. Construct the undirected graph where each vertex is an element from the given set and each edge is a relation from the list.
Step 2. Compute all the connected components of the graph.
Step 3. Count those components of size 1 (i.e. single-vertex components), and this count is actually the answer to the problem.

Comment hidden because of low score. Click to expand.
0
of 0 vote

How about graph coloring and finding the subset with highest number of colors?

Comment hidden because of low score. Click to expand.
0
of 0 vote

This question can be thought of as finding the largest clique in the complement of the graph formed by the relationships between the objects. This is a well known NP-Hard problem.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we apply DFS to the above problem, just finding connected components and printing one vertex from each set?

``````DFS(int connected[][], boolean v[][], int j, int n){
for(int i=0; i<n; i++){
if(connected[j][i]==1 && !visited[j][i]){
visited[j][i] = true;
DFS(connected, v, i,n);
}
}
}

printSubset(int connected[][]){
int n = connected.length;
boolean visited[][] = new boolean[][];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(connected[i][j]==1 && !visited[i][j]){
System.out.printf("%d ", i);
visited[i][j] = 1;
DFS(connected,visited,j,n);
}
}
}
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

O(n^2) + exp(k) -

``````public static void main(String[] args) {

int n = 6;
int[][] connected = new int[n][n];
connected = 1;
connected = 1;

connected = 1;
connected = 1;

connected = 1;
connected = 1;

connected = 1;
connected = 1;

int m = max(connected);
System.out.println(m);
}

// S = {a,b,c,d,e,f} and relations {a,d}, {b,c}, {a,c}, {a,e}
public static int max(int[][] connected) {
int n = connected.length;
Map<Integer, List<List<Integer>>> map = new HashMap<Integer, List<List<Integer>>>();
int[] dp = new int[n];
dp = 1;

List<List<Integer>> o = new ArrayList<List<Integer>>();
List<Integer> lo = new ArrayList<Integer>();
map.put(0, o);

for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1];
List<List<Integer>> olist = new ArrayList<List<Integer>>();
List<Integer> l = new ArrayList<Integer>();
map.put(i, olist);

for (int j = i - 1; j >= 0; j--) {
List<List<Integer>> connections = map.get(j);
if (null != connections) {
for (List<Integer> nodes : connections) {
boolean canconnect = true;
for (int node : nodes) {
if (connected[i][node] == 1) {
canconnect = false;
break;
}
}
if (canconnect) {
dp[i] = Math.max(dp[i], nodes.size() + 1);
List<List<Integer>> nconn = map.get(i);
List<Integer> list = new ArrayList<Integer>();
for (int k : nodes) {
}
}
}
}
}
}
return dp[n - 1];
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

The answer would be number of connected components in a graph right? Can use DFS or BFS for this?

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.