b3036667
b3036667

Reputation: 77

Checking if a node has fully connected neighbours

I have a graph and I want to check whether a node has fully connected neighbours. For a node to have fully connected neighbours, all its neighbours must be also be connected.

So, for a node to have fully connected neighbours, the set of its neighbours must be a subset of the set of neighbours of every neighbour of the given node. I have the following code, however it returns false even if it is given a node which does indeed have fully connected neighbours, and I do not see why this is.

public static boolean isFullyConnectedNeighbours(Node node)
{
    Set<Node> neighbours = node.neighbours();

    for(Node neighbour : neighbours)
    {
        if(! neighbour.neighbours().containsAll(neighbours)) return false;
    }

    return true;
}

For a Node, neighbours() returns the set of that node's neighbours

Upvotes: 0

Views: 573

Answers (1)

BretC
BretC

Reputation: 4199

Is it because each node is not a neighbour of itself?

For example...

for(Node neighbour : neighbours)
{
    for(Node n2 : neighbours) {
        if(neighbour == n2) continue;
        if(! neighbour.neighbours().contains(n2)) return false;
    }
}

...or something

Upvotes: 1

Related Questions