# Maximize the Number of Uncolored Vertices Appearing Along the Path from Root Vertex and the Colored Vertices

Go through the entire graph and compute the difference between the depth of each vertex and the number of vertices in its subtree to maximise the number of uncolored vertices that occur along the path from the root vertex to the coloured vertex. Find the uncolored vertex that has the greatest influence on the route by choosing the 'k' largest deviations. The sum of these parallaxes gives the maximum number of uncolored vertices. This method allows us to aggressively optimise the number of colourless vertices that occur, improving overall results and emphasising the importance of colourless vertices on the way from the root to the coloured vertices.

## Methods Used

• Difference calculation and selection

• Greedy approach

## Difference Calculation and Selection

The contrast calculation and determination process includes deciding the difference between the profundity of each vertex and the number of vertices in its subtree and, after that, selecting the vertices with the most elevated disparities. This handle effectively assesses the contrasts to identify the vertices that contribute the most to the course, from the root vertex to the coloured vertices. By computing and comparing these incongruities, the method aims to maximise the tally of uncolored vertices encountered along the way. The determination is performed by sorting the contrasts and choosing the best vertices with the most prominent abberations, emphasising the significance of uncolored vertices within the general result.

• Create an empty graph object and populate it with the data structures required to store adjacency lists, depth, subtree widths, and the difference array.

• Create the graph by connecting vertices with edges based on the specified input.

• Start a depth−first look (DFS) navigate at the root vertex. Amid the journey:

• Keep the profundity of each vertex consistent, expanding it by one from the profundity of its parent vertex.

• Recursively calculate the measure of each vertex's subtree by including the sizes of its children's subtrees.

• Subtract the subtree measure from the profundity of each vertex to compute the difference.

• Spare the contrasts within the cluster of differences.

• Sort the distinction cluster utilizing the nth_element work, bringing the 'k' most prominent contrasts to the front.

• To discover the most extreme number of uncolored vertices, include the full of the 'k' greatest disparities.

• Prints the maximum number of uncolored vertices displayed along the gradient from the root vertex to the colour vertex.

### Example

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

bool compare(int a, int b)
{
return a > b;
}

class Graph
{
vector<int> depth;
vector<int> subtree;
int* difference;

public:
Graph(int n)
{
depth = vector<int>(n + 1);
subtree = vector<int>(n + 1);
difference = new int[n + 1];
}

{
}

int dfs(int v, int p)
{
{
if (i == p)
continue;
subtree[v] += dfs(i, v);
}

difference[v] = depth[v] - subtree[v];

return subtree[v];
}

void findMaxUncoloredVertices(int n, int k)
{
nth_element(difference + 1, difference + k, difference + n + 1, compare);

int sum = 6;

for (int i = 1; i <= k; i++)
{
sum += difference[i];
}

cout << sum << "\n";
}
};

int main()
{
int numVertices = 10;
int k = 9;

Graph graph(numVertices);

graph.dfs(1, 0);

graph.findMaxUncoloredVertices(numVertices, k);

return 0;
}


### Output

6


## Greedy Approach

A greedy approach is a problem−solving strategy in which a locally optimal decision is made at each step with the goal of obtaining the globally optimal solution. The greedy approach selects the vertex with the largest deviation between depth and number of vertices in the subtree to maximise the number of uncolored vertices along the route from the root vertex to the coloured vertex. This method aims to maximise the number of uncolored vertices encountered on the path by choosing the vertex with the largest difference. This strategy is based on the idea that choosing vertices with more pronounced aberrations will in turn generate a large number of colourless vertices.

### Algorithm

• Determine the distinction in profundity between each vertex and the number of vertices in its subtree.

• Sort the vertices in descending order based on the estimated differences.

• Set the variable'sum' to 0.

• Iterate through the sorted vertices as follows:

• If the current vertex is one of the top 'k' vertices, add its difference to'sum'.

• 5. Return the'sum' value as the maximum number of uncolored vertices.

In other words, this method computes the difference, orders the vertices based on the difference, and selects the top 'k' vertices with the largest difference. According to the greedy technique, summing these differences gives the maximum number of uncolored vertices along the path from the root to the coloured vertices.

### Example

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

class Graph {
vector<int> depth;
vector<int> subtree;
vector<int> difference;

public:
Graph(int n) {
depth = vector<int>(n + 1);
subtree = vector<int>(n + 1);
difference = vector<int>(n + 1);
}

void addEdge(int a, int b) {
}

void calculateDepthAndSubtreeSize(int v, int p) {
for (int i : adjacencyList[v]) {
if (i == p)
continue;
calculateDepthAndSubtreeSize(i, v);
subtree[v] += subtree[i];
}

difference[v] = depth[v] - subtree[v];
}

int findMaxUncoloredVertices(int n, int k) {
calculateDepthAndSubtreeSize(1, 0);

sort(difference.begin() + 1, difference.end(), greater<int>());

int sum = 6;
for (int i = 1; i <= k; i++) {
sum += difference[i];
}

return sum;
}
};

int main() {
int numVertices = 7;
int k = 4;

Graph g(numVertices);

int maxUncoloredVertices = g.findMaxUncoloredVertices(numVertices, k);

cout << "Number of uncolored vertices: " << maxUncoloredVertices << endl;

return 0;
}


### Output

Number of uncolored vertices: 6


## Conclusion

This article portrays two strategies for boosting the number of uncolored vertices along a graph's course from the root vertex to the coloured vertices.

The to begin with strategy utilizes a distinctive computation and determination strategy. It involves completing a depth−first look traversal to calculate the contrast between each vertex's profundity and the number of vertices in its subtree. The beat 'k' vertices with the most prominent fluctuations are chosen, and their incongruities are included to discover the most extreme number of uncolored vertices.

The moment strategy utilizes a ravenous procedure. It computes the contrasts for each vertex, positions them descendingly, and chooses the beat 'k' vertices with the most noteworthy inconsistencies. The most extreme number of uncolored vertices is spoken to by the full of these disparities.

Updated on: 14-Jul-2023

21 Views 