Check if a Cycle of Length 3 Exists or Not in a Graph that Satisfy a given Condition


To check in the event that a cycle of length 3 exists in a chart fulfilling a given condition, ready to repeat through each vertex and look at its neighbouring vertices. On the off chance that a vertex has two neighbours that are too associated, a cycle of length 3 exists. This condition guarantees that there's an edge between the two neighbours, creating a triangle. By filtering all vertices and their adjoining vertices, we will recognise whether such a cycle exists or not. In the event that we discover a vertex with two associated neighbours, we are able to conclude that a cycle of length 3 fulfilling the given condition is displayed within the chart.

Methods Used

  • Adjacency Matrix approach

  • Adjacency List approach

Adjacency Approach

To check if a cycle of length 3 exists in a chart that fulfils a given condition, we are able to utilise the contagiousness approach. In this approach, we repeat through each vertex within the chart and check its adjoining vertices. For each vertex, we check on the off chance that any two of its adjoining vertices are too closely associated. In the event that such a match is found, we check to see if the condition is fulfilled for that match. On the off chance that the condition is met, it demonstrates the nearness of a cycle of length 3 that fulfils the given condition. By looking at all vertices within the chart, we can decide whether such a cycle exists or not.

Algorithm

  • Initialise a boolean variable called "cycleExists" to false.

  • Iterate through each vertex within the graph:

    • For each vertex, repeat through its adjoining vertices.

    • For each adjoining vertex, emphasise its adjoining vertices (barring the current vertex).

    • In the event that any two adjoining vertices are associated, continue to the next step.

  • Check on the off chance that the condition is fulfilled for the combination of associated vertices found in step 2c.

    • In the event that the condition is fulfilled, set "cycleExists" to genuine and break out of the loops.

  • After completing the cycles, check the value of "cycleExists".

    • In the event that "cycleExists" is genuine, a cycle of length 3 fulfilling the given condition exists within the graph.

    • In case "cycleExists" is wrong, no such cycle exists.

  • Output the result.

  • This calculation repeats through the vertices of the chart, analyses their adjoining vertices, and checks in the event that any match of adjoining vertices shapes a cycle of length 3 that fulfils the given condition.

Example

#include <iostream>
#include <vector>

using namespace std;

bool checkCycle(vector<vector<int>>& graph, int v, vector<bool>& visited, int parent, int condition) {
    visited[v] = true;

    for (int u : graph[v]) {
        if (!visited[u]) {
            visited[u] = true;

            for (int w : graph[u]) {
                if (visited[w] && w != parent && condition == graph[v][u] + graph[u][w]) {
                    return true;
                }
            }

            visited[u] = false;
        }
    }

    return false;
}

bool hasCycleOfLength3(vector<vector<int>>& graph, int condition) {
    int numVertices = graph.size();
    vector<bool> visited(numVertices, false);

    for (int v = 0; v < numVertices; v++) {
        visited[v] = true;

        for (int u : graph[v]) {
            if (checkCycle(graph, u, visited, v, condition)) {
                return true;
            }
        }

        visited[v] = false;
    }

    return false;
}

int main() {
    int numVertices, numEdges;
    cout << "Enter the number of vertices and edges: ";
    cin >> numVertices >> numEdges;

    vector<vector<int>> graph(numVertices);

    cout << "Enter the connections between vertices (u, v) and their corresponding weights: " << endl;
    for (int i = 0; i < numEdges; i++) {
        int u, v, weight;
        cin >> u >> v >> weight;

        graph[u].push_back(v);
        graph[v].push_back(u);

        // Store the weight/condition between u and v
        graph[u][v] = weight;
        graph[v][u] = weight;
    }

    int condition;
    cout << "Enter the condition to be satisfied: ";
    cin >> condition;

    if (hasCycleOfLength3(graph, condition)) {
        cout << "Cycle of length 3 satisfying the condition exists." << endl;
    } else {
        cout << "Cycle of length 3 satisfying the condition does not exist." << endl;
    }

    return 0;
}


Output

Enter the number of vertices and edges:  

Adjacency List Approach

The adjacent List approach could be an information structure utilised to speak to a chart. In this approach, each vertex of the chart is related to a list that contains all its adjoining vertices. To check in the event that a cycle of length 3 exists in a chart fulfilling a given condition, we will iterate through each vertex and its adjoining vertices. For each adjoining vertex, we check on the off chance that it includes a common adjoining vertex with the current vertex. In the event that such a common vertex exists, a cycle of length 3 is found. This approach guarantees effective investigation of the chart by putting away, as it were, the fundamental data about almost all the vertices and their associations within the contagiousness list.

Algorithm

  • Make a contagiousness list to speak to the chart, where each vertex includes a list of its adjoining vertices.

  • Iterate through each vertex within the graph.

  • For each vertex, repeat through its adjoining vertices.

  • For each adjacent vertex, emphasise its adjoining vertices (barring the current vertex).

  • Check if there's a common vertex between the current vertex and the adjoining vertex's adjoining vertices.

  • If a common vertex is found, a cycle of length 3 exists. Return true.

  • If no cycle of length 3 is found, return false.

Example

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

bool hasCycleOfLength3(vector<vector<int>>& graph) {
    int n = graph.size();
    
    for (int u = 0; u < n; ++u) {
        unordered_set<int> adjSet(graph[u].begin(), graph[u].end());

        for (int v : graph[u]) {
            for (int w : graph[v]) {
                if (w != u && adjSet.count(w) > 0) {
                    return true; // Cycle of length 3 found
                }
            }
        }
    }

    return false; // No cycle of length 3 found
}

int main() {
    // Create the graph as an adjacency list
    vector<vector<int>> graph = {
        {1, 2},
        {0, 2},
        {0, 1, 3},
        {2, 4},
        {3}
    };

    // Check if a cycle of length 3 exists
    bool cycleExists = hasCycleOfLength3(graph);

    // Print the result
    if (cycleExists) {
        cout << "A cycle of length 3 exists in the graph." << endl;
    } else {
        cout << "No cycle of length 3 exists in the graph." << endl;
    }

    return 0;
}

Output

A cycle of length 3 exists in the graph.

Conclusion

This article examines the method of checking for the presence of a cycle of length 3 in a chart that fulfils a given condition. It clarifies two approaches, specifically the Contagiousness Framework approach and the Contagiousness List approach. The article traces the calculations and gives code bits in C for both approaches. The Contagiousness Network approach includes emphasising each vertex and its neighbouring vertices to recognise a cycle of length 3 that fulfils the condition. The Contagiousness List approach utilises an information structure speaking to the chart and checks for a common vertex among the adjoining vertices to decide the nearness of a cycle.

Updated on: 13-Jul-2023

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements