Check if Every Vertex Triplet in Graph Contains two Vertices Connected to Third Vertex


Every triplet of vertices in a graph is checked to see if two of them are directly connected to the third. This property is important because it shows that the vertices are strongly interconnected, promoting a network with many connections. Applications requiring effective and direct connections between entities, such as social networks, transportation networks, and communication networks, all depend on this connectivity. The overall structure of the graph can be evaluated for its connectivity and potential impact on the system it represents by confirming this condition for each vertex triplet. This helps to analyse and optimise the network's performance and functionality.

Methods Used

  • Brute-force Approach

  • Depth-First Search (DFS) Approach

Brute-force Approach

In the brute-force method, each triplet of vertices in the graph is thoroughly examined to see if two of them are linked to the third vertex. This method iterates through all possible triplet combinations, checking each triplet to see if the necessary connection exists. This method has a time complexity of O(V3), where V is the number of vertices, but it might become ineffective for large graphs. Despite being straightforward, it might not be useful for very large graphs. Therefore, for faster and more effective checks on larger graphs, more optimised algorithms like DFS, BFS, or using adjacency matrix/edge list representations are preferred.

Algorithm

  • Pick a vertex from the graph, let's call it "v," to start.

  • Perform the following for each pair of the graph's "u" and "w" (different from "v") vertices:

    • Confirm whether "u" and "w" have a border.

    • In the event that there's an edge between the letters "u" and "w," then see on the off chance that there's also an edge between the letters "v" and both "u" and "w."

    • Move on to the taking after match of vertices in the event that "v" has edges with both "u" and "w," at that point "w," separately.

    • If there isn't a border between "v" and either "u" or "w," return "False" because the triplet's condition isn't met.

  • Return "True" if the condition is met by all triplets (triadic combinations of "u," "v," and "w") in the graph.

  • Return "False" on the off chance that any triplet does not fulfil the prerequisite.

Example

#include <iostream>
#include <vector>
using namespace std;

bool checkTripletCondition(const vector<vector<int>>& graph) {
   int n = graph.size();
   for (int v = 0; v < n; v++) {
      for (int u : graph[v]) {
         for (int w : graph[v]) {
            if (u != v && w != v) {
               bool hasEdgeUV = false;
               bool hasEdgeVU = false;
               bool hasEdgeVW = false;
               bool hasEdgeWV = false;

               for (int x : graph[u]) {
                  if (x == v) hasEdgeUV = true;
               }

               for (int y : graph[v]) {
                  if (y == u) hasEdgeVU = true;
                  if (y == w) hasEdgeVW = true;
               }

               for (int z : graph[w]) {
                  if (z == v) hasEdgeWV = true;
               }

               if (hasEdgeUV && hasEdgeVW && hasEdgeWV) continue;
               else return false;
            }
         }
      }
   }
   return true;
}

int main() {
   vector<vector<int>> graph = {
      {1, 2},
      {0, 2},
      {0, 1, 3},
      {2}
   };

   cout << boolalpha << checkTripletCondition(graph) << endl;
   return 0;
}

Output

true

Depth-First Search (DFS) Approach

The Depth-First Search (DFS) approach entails traversing the graph methodically, starting from a chosen vertex, in order to determine whether every vertex triplet in a graph contains two vertices connected to the third vertex. For each vertex triplet it encounters, the algorithm explores all potential paths and determines whether the necessary connections exist. DFS effectively determines whether two of the triplet's vertex are connected to one another by looking at the vertex's neighbours. The algorithm validates the property for the entire graph if all triplets meet the requirement. DFS is a potent and popular method for exploring graphs, offering a useful way to validate the connectivity requirements in the context.

Algorithm

  • Create a flag variable and set it to true to monitor whether the requirement is met for every triplet.

  • DFS traversal should begin at each vertex in the graph.

  • For each vertex "v" encountered during the DFS traversal, investigate its neighbours (adjacent vertices).

  • Check in case there's an edge between each combination of neighbours "u" and "w" of "v."

  • Check to see on the off chance that "v" has edges with both "u" and "w," in case there's an edge between "u" and "w."

  • Set the flag variable to false and end the DFS for that traversal if the condition is not met for any triplet.

  • Up until the flag variable is changed to false for any triplet, or until all vertices have been visited during the DFS traversal.

  • The flag variable indicates that every vertex triplet in the graph contains two vertices connected to the third vertex if it remains true after the DFS traversal. If not, the prerequisite isn't met for even the slightest triplet.

Example

#include <iostream>
#include <vector>
using namespace std;

class Graph {
private:
   int V;
   vector<vector<int>> adj;

public:
   Graph(int V) : V(V) {
      adj.resize(V);
   }

   void addEdge(int u, int v) {
      adj[u].push_back(v);
      adj[v].push_back(u);
   }

   bool checkTriplets() {
      bool flag = true;

      for (int i = 0; i < V && flag; ++i) {
         vector<bool> visited(V, false);
         flag = flag && dfs(i, -1, visited);
      }

      return flag;
   }

   bool dfs(int curr, int parent, vector<bool>& visited) {
      visited[curr] = true;
      int connectedCount = 0;

      for (int neighbor : adj[curr]) {
         if (neighbor != parent) {
            if (visited[neighbor]) {
               connectedCount++;
            } else {
               if (!dfs(neighbor, curr, visited))
                  return false;
            }
         }
      }

      return connectedCount >= 2;
   }
};

int main() {
   Graph graph(6);

   graph.addEdge(0, 1);
   graph.addEdge(1, 2);
   graph.addEdge(1, 3);
   graph.addEdge(2, 3);
   graph.addEdge(3, 4);
   graph.addEdge(4, 5);

   bool result = graph.checkTriplets();
   cout << "Requirement Met: " << boolalpha << result << endl;

   return 0;
}

Output

Requirement Met: false

Conclusion

In conclusion, two different methods—the brute-force method and the Depth-First Search (DFS) method—have been investigated for the task of determining whether every vertex triplet in a graph contains two vertices connected to the third. The brute-force approach is efficient for small graphs but ineffective for larger ones due to its O(V3) time complexity because it iterates through all feasible triplets and checks for the necessary connections.

The DFS method, on the other hand, provides a more effective means of validating the connectivity requirements in graphs. The graph is efficiently explored, and the necessary connections between the triplets are looked for. The DFS algorithm makes sure that the graph satisfies the required condition and is suitable for larger networks because it has a more manageable time complexity than the brute-force approach.

Updated on: 04-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements