Check if Equal Sum Components can be Obtained from given Graph by Removing Edges from a Cycle


Finding out if it is possible to extract two equal sum components from a graph by eliminating edges from a cycle is the main question in graph theory. To determine which edges should be removed from the graph, it is essential to locate the cycle inside the graph. The main goals are to analyse the graph's structure, show that this transformation is possible, and explain how the graph's cycles, edges, and component sums interact. We may assess whether the graph has the capacity to produce two unique components with equal sums through edge removal from a cycle by carefully evaluating these components.

Methods Used

  • Brute Force Approach

  • Greedy Approach

  • Edge List

Brute Force Approach

In the brute force approach, we carefully examine each potential cycle in the graph and make an effort to eliminate its edges. Next, we check to see if this procedure produces two distinct components with equal sums. This exhaustive search ensures that we discover a solution, but due to its exponential time complexity, it can be computationally taxing for bigger graphs. The quantity of cycles and subsets to be examined grows exponentially as the size of the graph expands, necessitating a large increase in the amount of computer power needed. As a result, while this method works well for short graphs, larger and more complex graphs render it useless and impracticable.

Algorithm

  • Use Depth-First Search (DFS) or Breadth-First Search (BFS) algorithms to find every cycle in the given graph.

  • Iterate over the cycle edges for each cycle that was discovered in step 1.

  • Divide the graph into two parts by taking away the current cycle's edges one at a time.

  • Determine the nodes' sums for each component.

  • Verify that the sums of the two parts are equivalent. If they match, the condition is met, and the procedure is complete. Otherwise, move on to the subsequent cycle and repeat steps 3 through 5.

  • The conclusion that equal sum components cannot be obtained by eliminating edges from any cycle in the graph should be drawn if, after trying all conceivable combinations, no cycle is discovered that satisfies the criterion.

Example

#include <iostream>
#include <vector>

using namespace std;

bool checkEqualSum(const vector<int>& component1, const vector<int>& component2) {
   int sum1 = 0, sum2 = 0;
   for (int num : component1) {
      sum1 += num;
   }
   for (int num : component2) {
      sum2 += num;
   }
   return sum1 == sum2;
}

void findCycles(vector<vector<int>>& graph, vector<int>& 
path, vector<bool>& visited, int node, int start, vector<vector<
int>>& cycles) {
   visited[node] = true;
   path.push_back(node);

   for (int neighbor : graph[node]) {
      if (neighbor == start) {
         cycles.push_back(path);
      } else if (!visited[neighbor]) {
         findCycles(graph, path, visited, neighbor, start, cycles);
      }
   }

   path.pop_back();
   visited[node] = false;
}

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

   vector<bool> visited(n, false);
   vector<int> path;

   for (int i = 0; i < n; ++i) {
      if (!visited[i]) {
         findCycles(graph, path, visited, i, i, cycles);
      }
   }

   for (const vector<int>& cycle : cycles) {
      for (int i = 0; i < cycle.size(); ++i) {
         vector<int> component1, component2;

         for (int j = 0; j < cycle.size(); ++j) {
            if (j == i) {
               component2.push_back(cycle[j]);
            } else {
               component1.push_back(cycle[j]);
            }
         }

         if (checkEqualSum(component1, component2)) {
            return true;
         }
      }
   }

   return false;
}

int main() {
   vector<vector<int>> graph = {
      {1, 2},
      {0, 2, 3},
      {0, 1, 3},
      {1, 2}
   };

   if (canGetEqualSumComponents(graph)) {
      cout << "Equal sum components can be obtained by removing edges from a cycle." << endl;
   } else {
      cout << "Equal sum components cannot be obtained by removing edges from any cycle." << endl;
   }

   return 0;
}

Output

Equal sum components can be obtained by removing edges from a cycle.

Greedy Approach

In the Greedy Approach, we repeatedly eliminate the edges from cycles with the smallest weight to determine if equal sum components can be achieved from a given graph by doing so. This procedure is repeated until we have two components with equal amounts. Although this approach might not always result in the best solution, it does give a useful and quick substitute for larger graphics. We try to achieve a balance between the sums of the components by prioritising the elimination of smaller-weighted edges. Even though it might not always produce the greatest results, this strategy is helpful when computational efficiency is important.

Algorithm

  • To locate cycles in the graph, use depth-first search (DFS) or breadth-first search (BFS). A cycle is a closed path with the same nodes at the beginning and conclusion.

  • The weights of each cycle's edges should be added up for each cycle that was discovered in step 1. Save the sums and the corresponding cycles.

  • Cycles are sorted by their sums: Based on their aggregate values, arrange the cycles discovered in step 2 in ascending order. As a result, we will be able to give smaller-weighted cycles higher priority during the elimination process.

  • Create two empty sets to symbolise the two components that we want to form with equal amounts as the initialization of the components.

  • Remove edges one by one, starting from the cycle with the least sum (sorted in step 3), until the sum of each component is roughly equal to half the overall sum of the edges in the graph. Remove an edge from a cycle if it joins nodes that are already in separate components.

  • After each edge removal, make sure the two components have equal sums by checking for them. If they do, then the setup was successfully done, hence the answer is yes.

  • If equal sum components cannot be attained, keep deleting edges from the remaining cycles while adhering to the greedy approach until the solution is discovered or all cycles have been processed.

  • The answer is no because it is impossible to achieve equal sum components by eliminating edges from cycles in the given graph if the process ends without discovering them.

Example

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

struct Edge {
   int src, dest, weight;
};

vector<Edge> edges = { {0, 1, 5}, {1, 2, 8}, {2, 3, 7}, {3, 4, 3} };

bool removeGreedyEdges(vector<int>& cycleSums) {
}

int main() {
   vector<int> cycleSums; 
   bool equalSums = removeGreedyEdges(cycleSums);

   if (equalSums) {
      cout << "Equal sum components can be obtained by removing edges from cycles.\n";
   } else {
      cout << "Equal sum components cannot be obtained by removing edges from cycles.\n";
   }

   return 0;
}

Output

Equal sum components can be obtained by removing edges from cycles.

Conclusion

In conclusion, a crucial question in graph theory is whether two equal sum components can be extracted from a given graph by eliminating edges from a cycle. The problem is addressed using both the Greedy and Brute Force Approaches. In order to identify a solution, the Brute Force Approach thoroughly investigates every combination of cycles and edge removal, although it can be computationally expensive for big networks. The Greedy Approach, on the other hand, provides a more effective solution by repeatedly eliminating edges from cycles with the lightest weights. The optimum outcome might not always be achieved, though. To determine whether the desired transformation is feasible, it is critical to thoroughly assess the graph's structure.

Updated on: 04-Aug-2023

30 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements