Print Nodes Which are not Part of any Cycle in a Directed Graph


In a coordinated diagram, recognizing hubs that are not piece of any cycle is critical for different applications. These hubs structure the underpinning of non-cyclic subgraphs and assume a huge part in understanding the general chart structure. By using effective chart crossing calculations, for example, Profundity First Hunt (DFS) or Tarjan's calculation for firmly associated parts, we can without much of a stretch decide and print the hubs that are not engaged with any cycle. These methodologies guarantee that hubs with no cycle cooperation are featured, giving important bits of knowledge into the diagram's non-cyclic parts and supporting different chart related critical thinking situations.

Methods Used

  • Depth-First Search (DFS) with Cycle Detection

  • Tarjan's Algorithm for Strongly Connected Components

Depth-First Search (DFS) with Cycle Detection

In this methodology, we use Profundity First Pursuit (DFS) to navigate the coordinated chart and distinguish cycles en route. We mark the visited hubs and keep a rundown to follow the hubs in the ongoing DFS way. In the event that we experience a back edge (an edge to a hub in the ongoing DFS way), we distinguish a cycle. Toward the finish of the DFS, the hubs in the ongoing DFS way will be important for a cycle. The hubs not in the ongoing DFS way are not piece of any cycle and can be printed.

Algorithm

  • Play out a Profundity First Hunt (DFS) on the diagram, beginning from each unvisited hub.

  • During the DFS, mark the visited hubs and add them to the ongoing DFS way list.

  • On the off chance that we experience a back edge (an edge to a hub in the ongoing DFS way), we distinguish a cycle and imprint all hubs in the ongoing DFS way as a component of a cycle.

  • When the DFS is finished for a hub, eliminate it from the ongoing DFS way list.

  • Subsequent to finishing the DFS for all hubs, the hubs that are not piece of any cycle will be left plain, and we can print them.

Example

#include <iostream>
#include <vector>

class Graph {
public:
   Graph(int numVertices);
   void addEdge(int src, int dest);
   void DFS();
private:
   void DFSUtil(int v, std::vector<bool>& visited, std::vector<int>& dfsPath);
   int numVertices;
   std::vector<std::vector<int>> adjList;
};

Graph::Graph(int numVertices) : numVertices(numVertices) {
   adjList.resize(numVertices);
}

void Graph::addEdge(int src, int dest) {
   adjList[src].push_back(dest);
}

void Graph::DFSUtil(int v, std::vector<bool>& visited, std::vector<int>& dfsPath) {
   visited[v] = true;
   dfsPath.push_back(v);

   for (int neighbor : adjList[v]) {
      if (!visited[neighbor]) {
         DFSUtil(neighbor, visited, dfsPath);
      }
      else {
         std::cout << "Cycle found: ";
         for (size_t i = 0; i < dfsPath.size(); ++i) {
            if (dfsPath[i] == neighbor) {
               while (i < dfsPath.size()) {
                  std::cout << dfsPath[i] << " ";
                  ++i;
               }
               break;
            }
         }
         std::cout << std::endl;
      }
   }

   dfsPath.pop_back();
}

void Graph::DFS() {
   std::vector<bool> visited(numVertices, false);
   std::vector<int> dfsPath;

   for (int i = 0; i < numVertices; ++i) {
      if (!visited[i]) {
         DFSUtil(i, visited, dfsPath);
      }
   }
}

int main() {
   Graph graph(6);
   graph.addEdge(0, 1);
   graph.addEdge(1, 2);
   graph.addEdge(2, 3);
   graph.addEdge(3, 4);
   graph.addEdge(4, 1);
   graph.addEdge(4, 5);
   
   std::cout << "DFS traversal with cycle detection:\n";
   graph.DFS();

   return 0;
}

Output

DFS traversal with cycle detection:
Cycle found: 1 2 3 4 

Tarjan's Algorithm for Strongly Connected Components

Tarjan's calculation is a strong calculation used to track down all emphatically associated parts in a coordinated diagram. An unequivocally associated part is a subset of hubs where there is a coordinated way between any two hubs in the subset. Hubs that are not piece of any firmly associated part are not piece of any cycle. By finding emphatically associated parts, we can recognize hubs that are not piece of any cycle and print them\

Algorithm

  • Apply Tarjan's calculation to the guided diagram to track down all emphatically associated parts.

  • In the wake of tracking down all emphatically associated parts, distinguish hubs that are essential for a firmly associated part.

  • Hubs that are not piece of any unequivocally associated part are not piece of any cycle and can be printed.

  • The two methodologies really distinguish and print hubs that are not piece of any cycle in a coordinated chart. The DFS approach gives a less difficult and more direct execution, while Tarjan's calculation is more intricate yet gives extra data about emphatically associated parts, which can be helpful for specific chart related undertakings. The decision of approach relies upon the particular necessities and setting of the main pressing concern.

Example

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

class Graph {
   int V;
   vector<vector<int>> adj;
   vector<bool> visited;
   vector<int> disc, low;
   stack<int> st;
   vector<vector<int>> SCCs;
   vector<bool> essentialNodes;

public:
   Graph(int V) : V(V) {
      adj.resize(V);
      visited.resize(V, false);
      disc.resize(V, -1);
      low.resize(V, -1);
      essentialNodes.resize(V, true);
   }

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

   void tarjanDFS(int u) {
      static int time = 0;
      disc[u] = low[u] = ++time;
      st.push(u);
      visited[u] = true;

      for (int v : adj[u]) {
         if (disc[v] == -1) {
            tarjanDFS(v);
            low[u] = min(low[u], low[v]);
         } else if (visited[v]) {
            low[u] = min(low[u], disc[v]);
         }
      }

      if (low[u] == disc[u]) {
         vector<int> SCC;
         int v;
         do {
            v = st.top();
            st.pop();
            SCC.push_back(v);
            visited[v] = false;
         } while (v != u);

         SCCs.push_back(SCC);
      }
   }

   void tarjan() {
      for (int i = 0; i < V; ++i) {
         if (disc[i] == -1) {
            tarjanDFS(i);
         }
      }
   }

   void identifyEssentialNodes() {
      for (const vector<int>& SCC : SCCs) {
         for (int v : SCC) {
            for (int u : adj[v]) {
               if (find(SCC.begin(), SCC.end(), u) == SCC.end()) {
                  essentialNodes[u] = false;
               }
            }
         }
      }
   }

   void printEssentialNodes() {
      cout << "Essential Nodes for Each SCC:\n";
      for (int i = 0; i < V; ++i) {
         if (essentialNodes[i]) {
            cout << i << " ";
         }
      }
      cout << endl;
   }
};

int main() {
   Graph g(6);
   g.addEdge(0, 1);
   g.addEdge(1, 2);
   g.addEdge(2, 0);
   g.addEdge(1, 3);
   g.addEdge(3, 4);
   g.addEdge(4, 5);
   g.addEdge(5, 3);

   g.tarjan();
   g.identifyEssentialNodes();
   g.printEssentialNodes();

   return 0;
}

Output

Essential Nodes for Each SCC:
0 1 2 4 5

Conclusion

The two methodologies really take care of the issue of recognizing hubs that are not piece of any cycle in a coordinated chart. The DFS approach is easy to execute and requires insignificant extra information structures. Then again, Tarjan's calculation gives extra data about emphatically associated parts, which can be helpful in specific situations.

The decision between the two methodologies relies upon the particular prerequisites of the issue and the requirement for extra data past distinguishing hubs not associated with cycles. As a general rule, in the event that the sole goal is to find hubs not piece of any cycle, the DFS approach might be liked because of its straightforwardness. Be that as it may, in the event that further examination of emphatically associated parts is wanted, Tarjan's calculation can be an important device. The two methodologies offer proficient arrangements and can be adjusted in view of the attributes of the coordinated chart and the ideal results of the examination

Updated on: 04-Aug-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements