Check Which Player Visits More Number of Nodes


To decide which player visits a more noteworthy number of hubs in a chart, we will use a chart traversal calculation such as depth−first look (DFS) or breadth−first look (BFS). Beginning from a given hub, we navigate the chart, keeping track of the number of hubs gone by by each player. By comparing the tallies at the conclusion of the traversal, we can decide which player has gone by more hubs. DFS investigates the chart by going as deep as conceivable, recently backtracking, while BFS investigates the graph level by level. The player with the next number of gone to hubs features a more broad reach within the chart.

Methods Used

  • BFS

  • Modified BSF

BFS

BFS investigates a chart by going to the vertices in a level−wise way. Beginning from a given vertex, it visits all its neighbours at some point, recently moving on to their neighbours. In this case, we initialise the BFS calculation for each vertex, and for each vertex, we visit its neighbours to check if there is an edge between any two neighbours. In the event that such an edge exists, we look to see if the given condition is fulfilled. On the off chance that the condition is met, we have found a cycle of length 3. On the off chance that no such cycle is found after investigating all vertices, we conclude that there's no cycle of length 3 fulfilling the given condition within the chart.

Algorithm

  • Initialise a line and purge the set.

  • For each vertex v within the graph:

  • Enqueue V into the queue.

  • Add v to the visited set.

  • While the line isn't purgeable, do the following:

  • Dequeue a vertex U from the queue.

  • For each neighbour w of u:

  • If w isn't within the gone−to set:

  • Add w to the gone−to set.

  • Enqueue W into the queue.

  • Check if there is an edge between u and w.

  • If an edge exists and the given condition is fulfilled, return true.

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

  • The BFS calculation investigates the chart by going to vertices in a level−wise way, permitting us to check for cycles of length 3 and confirm the given condition.

Example

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

using namespace std;

bool hasCycleOfLengthThree(vector<vector<int>>& graph, vector<bool>& visited, int start) {
    queue<int> q;
    unordered_set<int> visitedSet;

    q.push(start);
    visited[start] = true;

    while (!q.empty()) {
        int u = q.front();
        q.pop();

        for (int w : graph[u]) {
            if (!visited[w]) {
                visited[w] = true;
                q.push(w);

                // Check if there exists an edge between u and w
                for (int x : graph[w]) {
                    if (x != u && visited[x]) {
                        // Condition for cycle of length 3 is satisfied
                        return true;
                    }
                }
            }
        }
    }

    return false;
}

bool hasCycleOfLengthThree(vector<vector<int>>& graph) {
    int n = graph.size();
    vector<bool> visited(n, false);

    for (int i = 0; i < n; i++) {
        if (!visited[i] && hasCycleOfLengthThree(graph, visited, i)) {
            return true;
        }
    }

    return false;
}

int main() {
    int n = 12; // Number of vertices
    int m = 34; // Number of edges

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

    // Hardcoded edges for testing
    vector<pair<int, int>> edges = {
        {0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {2, 6}, {3, 7}, {3, 8}, {4, 9},
        {5, 9}, {6, 10}, {7, 11}, {8, 11}, {9, 11}, {10, 11}
    };

    for (const auto& edge : edges) {
        int u = edge.first;
        int v = edge.second;

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

    int start = 0; // Starting number
    int end = 11; // Ending number

    if (start >= 0 && start < n && end >= 0 && end < n) {
        if (hasCycleOfLengthThree(graph)) {
            cout << "The graph contains a cycle of length 3 satisfying the given condition.\n";
        } else {
            cout << "The graph does not contain a cycle of length 3 satisfying the given condition.\n";
        }
    } else {
        cout << "Invalid starting or ending number.\n";
    }

    return 0;
}

Output

The graph contains a cycle of length 3 satisfying the given condition.

Modified BSF

BFS starts by selecting a starting centre and exploring all its neighbours a few times before moving on to their neighbours. In this balanced approach, we start with each centre as the starting centre and perform BFS from that centre. In the midst of the BFS traversal, we keep track of the centres at a distance of 2 from the starting centre. In the event that any of these centre points have an edge back to the starting centre, at that point a cycle of length 3 exists that fulfils the given condition. On the off chance that no such cycle is found after endeavouring all conceivable starting centres, at that point there's no cycle of length 3 fulfilling the condition inside the chart.

Algorithm

  • Select a beginning vertex and stamp it as visited.

  • Initialise a line with the beginning vertex.

  • While the line isn't purge, do the following steps:

    • Dequeue a vertex from the front of the queue.

    • Visit all the unvisited neighbours of the dequeued vertex.

  • Check if the condition is fulfilled for the current vertex and its neighbours.

  • If the condition is fulfilled and any of the neighbours has the beginning vertex as a neighbour, a cycle of length 3 exists.

  • c. Check the gone−to neighbours and enqueue them.

  • If no cycle of length 3 fulfilling the condition is found after investigating all vertices, at that point no such cycle exists within the chart.

Example

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

bool hasCycle(vector<vector<int>>& graph, int start) {
    int n = graph.size();
    vector<int> distance(n, -1); // Distance of each node from the start
    queue<int> q;

    distance[start] = 0;
    q.push(start);

    while (!q.empty()) {
        int current = q.front();
        q.pop();

        for (int neighbor : graph[current]) {
            if (distance[neighbor] == -1) {
                distance[neighbor] = distance[current] + 1;
                q.push(neighbor);
            }
            else if (distance[neighbor] == 2 && neighbor != start) {
                return true; // Cycle of length 3 found
            }
        }
    }

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

bool hasCycleOfLength3(vector<vector<int>>& graph) {
    int n = graph.size();

    for (int i = 0; i < n; i++) {
        if (hasCycle(graph, i)) {
            return true; // Cycle of length 3 found from node i
        }
    }

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

int main() {
    int n = 5;
    int m = 6;

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

    // Define the edges
    vector<pair<int, int>> edges = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 0}, {1, 4}};

    // Add edges to the graph
    for (auto edge : edges) {
        int u = edge.first;
        int v = edge.second;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    if (hasCycleOfLength3(graph)) {
        cout << "Cycle of length 3 exists satisfying the given condition." << endl;
    }
    else {
        cout << "No cycle of length 3 exists satisfying the given condition." << endl;
    }

    return 0;
}

Output

Cycle of length 3 exists satisfying the given condition.

Conclusion

This article examines the issue of deciding which player visits a more prominent number of hubs in a chart. It presents two approaches: a breadth−first look (BFS) and an adjusted BFS calculation. The BFS calculation investigates the chart level by level, whereas the altered BFS centres on recognising cycles of length 3 that fulfil a given condition. The article gives point−by−point clarifications of both calculations and presents a comparison of C usage. These calculations empower the comparison of the number of visits to hubs by diverse players and can offer assistance in deciding which player has a more broad reach inside the chart.

Updated on: 13-Jul-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements