Java Program to Find a Good Feedback Vertex Set in a Graph


The Java programme points to a great Input Vertex Set in a chart. An Input Vertex Set may be a set of vertices in a chart such that expelling those vertices and their occurrence edges comes about in a non-cyclic chart. The programme utilises an algorithmic approach to recognise a small Input Vertex Set that maintains the graph's basic properties. By iteratively selecting vertices with high degrees and evacuating them at their occurrence edges, the programme finds an assumed arrangement. This permits proficient distinguishing proof of critical vertices that contribute to cycles within the chart. The coming Criticism Vertex Set can be valuable for different applications, such as organising investigations and optimisation.

Methods Used

  • Approximation algorithm

  • Brute force approach

Approximation Algorithm

Within the setting of the Java programme to discover a great Input Vertex Set in a chart, the Guess Calculation is an approach that ensures an arrangement inside a certain calculation of the ideal arrangement. The calculation begins by iteratively selecting a vertex with the highest degree and expelling it together with its occurrence edges. In any case, not at all like the covetous calculation, this approach also considers the expulsion of vertices with lower degrees if it makes a difference in breaking cycles. By iteratively expelling vertices, the calculation points form a non-cyclic chart, and the chosen vertices frame the Criticism Vertex Set. The guess calculation gives a sensibly great arrangement, considering the productivity of the computation.

Algorithm

  • Initialise a purge set S to store the chosen vertices.

  • While G contains edges,

  • a. Discover a vertex v with the most elevated degree in G.

  • b. Include v to S.

  • c. Evacuate v and its occurrence edges from G.

  • d. Rehash steps a–c until G gets to be edgeless.

  • For each remaining vertex u in G:

  • a. On the off chance that you are adjacent to any vertex in S, evacuate you from G.

  • Return S as the inexact Input Vertex Set.

Example

#include <iostream>
#include <vector>
#include <set>
#include <algorithm> // Include the algorithm header for sort function

using namespace std;

// Function to find a good feedback vertex set in a graph
set<int> findFeedbackVertexSet(vector<vector<int>>& graph) {
   int n = graph.size();
   vector<pair<int, int>> degrees;

   // Calculate the degrees of each vertex
   for (int i = 0; i < n; ++i) {
      degrees.push_back({graph[i].size(), i});
   }

   // Sort the vertices in descending order of degrees
   sort(degrees.rbegin(), degrees.rend());

   set<int> feedbackVertexSet;

   // Greedily select vertices with the highest degree
   for (int i = 0; i < n; ++i) {
      int vertex = degrees[i].second;

      // Check if adding this vertex forms a cycle
      bool formsCycle = false;
      for (int neighbor : graph[vertex]) {
         if (feedbackVertexSet.count(neighbor) > 0) {
            formsCycle = true;
            break;
         }
      }

      // If adding this vertex does not form a cycle, add it to the feedback vertex set
      if (!formsCycle) {
         feedbackVertexSet.insert(vertex);
      }
   }

   return feedbackVertexSet;
}

// Test the function
int main() {
   // Create a sample graph
   vector<vector<int>> graph = {
      {1, 2, 3},
      {0, 3},
      {0, 3},
      {0, 1, 2}
   };

   // Find a good feedback vertex set
   set<int> feedbackSet = findFeedbackVertexSet(graph);

   // Print the vertices in the feedback vertex set
   cout << "Feedback Vertex Set: ";
   for (int vertex : feedbackSet) {
      cout << vertex << " ";
   }
   cout << endl;

   return 0;
}

Output

Feedback Vertex Set: 3 

Brute Force Approach

The brute force approach within the setting of finding a great Criticism vertices Set in a chart includes thoroughly checking all conceivable combinations of vertices to decide the littlest set that breaks all cycles. It efficiently creates all subsets of vertices and checks in the event that evacuating those vertices makes the chart non-cyclic. This approach ensures finding the ideal arrangement but gets illogical for expansive charts due to its exponential time complexity. The brute-force approach includes emphasising through all conceivable vertex combinations, expelling them from the chart, and checking for acyclicity until the littlest Criticism Vertex Set is found.

Algorithm

  • Initialise a variable to store the least measure of the Input Vertex Set.

  • Generate all conceivable subsets of vertices within the graph.

  • For each subset, expand the vertices from the chart and check in the event that the coming about chart is acyclic.

  • If the chart is non-cyclic, calculate the measure of the current subset.

  • If the measure of the current subset is less than the least estimate, upgrade the least size.

  • Repeat steps 3-5 for all created subsets.

  • Return the least estimated value of the Input Vertex Set.

Example

#include <iostream>
#include <vector>
#include <set>

using namespace std;

// Function to check if the given vertices form a feedback vertex set
bool isFeedbackVertexSet(const vector<vector<int>>& graph, const set<int>& vertices) {
   // Check if each vertex in the set breaks a cycle
   for (int vertex : vertices) {
      for (int neighbor : graph[vertex]) {
         if (vertices.find(neighbor) == vertices.end()) {
            return false; // A cycle exists, so not a feedback vertex set
         }
      }
   }
   return true; // No cycles found, so it's a feedback vertex set
}

// Recursive function to find all possible feedback vertex sets
void findAllFeedbackVertexSets(const vector<vector<int>>& graph, set<int>& currentSet, int vertex, vector<set<int>>& allSets) {
   // Base case: reached the end of the graph
   if (vertex == graph.size()) {
      if (isFeedbackVertexSet(graph, currentSet)) {
         allSets.push_back(currentSet);
      }
      return;
   }

   // Recursive case: include current vertex or exclude it
   currentSet.insert(vertex);
   findAllFeedbackVertexSets(graph, currentSet, vertex + 1, allSets);
   currentSet.erase(vertex);
   findAllFeedbackVertexSets(graph, currentSet, vertex + 1, allSets);
}

// Function to find a good feedback vertex set in the graph
set<int> findGoodFeedbackVertexSet(const vector<vector<int>>& graph) {
   vector<set<int>> allSets;
   set<int> currentSet;

   // Find all possible feedback vertex sets
   findAllFeedbackVertexSets(graph, currentSet, 0, allSets);

   // Find the smallest feedback vertex set
   set<int> smallestSet;
   size_t smallestSize = graph.size() + 1;

   for (const set<int>& vertices : allSets) {
      if (vertices.size() < smallestSize) {
         smallestSize = vertices.size();
         smallestSet = vertices;
      }
   }

   return smallestSet;
}

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

   // Find a good feedback vertex set in the graph
   set<int> feedbackVertexSet = findGoodFeedbackVertexSet(graph);

   // Print the result
   cout << "Feedback Vertex Set: ";
   if (!feedbackVertexSet.empty()) {
      for (int vertex : feedbackVertexSet) {
         cout << vertex << " ";
      }
   } else {
         cout << "No feedback vertex set found.";
   }
   cout << endl;

   return 0;
}

Output

Feedback Vertex Set: No feedback vertex set found.

Conclusion

This article clarifies the concepts of limitation fulfilment and limitation engendering within the setting of deciding in the event that it is conceivable to dole out values that fulfil given relations. It diagrams the algorithmic approaches included and gives code cases in C++.This article clarifies the concepts of limitation fulfilment and limitation engendering within the setting of deciding in the event that it is conceivable to dole out values that fulfil given relations. It diagrams the algorithmic approaches included and gives code cases in C++.

Imperative fulfilment includes deciding on the off chance that it is attainable to dole out values to factors that meet the given limitations, whereas limitation proliferation iteratively applies rules to diminish the conceivable values based on the relations. The article illustrates how these procedures can be utilised to illuminate the issue of fulfilling relations between factors, highlighting the significance of effective look space investigation and disposing of clashing assignments.

Updated on: 31-Jul-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements