Friday, October 21, 2022
HomeWordPress DevelopmentMinimal time to go to all nodes of given Graph at the...

Minimal time to go to all nodes of given Graph at the very least as soon as


  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int minimizeTime(vector<vector<int> >& graph)

{

    lengthy lengthy n = graph.dimension();

  

    

    vector<vector<lengthy lengthy> > adj(n);

  

    for (int i = 0; i < n; i++) {

        for (auto j : graph[i]) {

  

            adj[i].push_back(j);

        }

    }

  

    

    lengthy lengthy finalMask = (lengthy lengthy)(1 << n) - 1;

  

    

    

    queue<pair<lengthy lengthy, lengthy lengthy> > q;

  

    

    

    vector<vector<bool> > visited(

        n, vector<bool>(finalMask + 1));

  

    

    

    for (int i = 0; i < n; i++) {

        q.push({ i, (lengthy lengthy)(1 << i) });

    }

  

    

    

    lengthy lengthy timeCount = 0;

  

    

    whereas (q.dimension() > 0) {

        int dimension = q.dimension();

  

        

        for (int i = 0; i < dimension; i++) {

  

            

            auto curr = q.entrance();

            q.pop();

  

            

            

            if (curr.second == finalMask)

                return timeCount;

  

            

            for (auto little one : adj[curr.first]) {

  

                

                lengthy lengthy newVisitedBit

                    = curr.second | (1 << little one);

  

                

                

                

                

                

                if (visited[child][newVisitedBit]

                    == false) {

  

                    q.push({ little one, newVisitedBit });

                    visited[child][newVisitedBit] = true;

                }

            }

        }

  

        

        timeCount++;

    }

  

    

    return -1;

}

  

int predominant()

{

    vector<vector<int> > graph = {

        { 1, 2, 3 }, { 2, 0 }, { 0, 1 }, { 0, 4 }, { 3 }

    };

  

    

    int minTime = minimizeTime(graph);

    cout << minTime << endl;

  

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments