Open In App

Program to Find the Arboricity of a Graph

Graph arboricity measures edge density. It is the fewest trees that can cover all graph edges. It covers all graph edges with the fewest edge-disjoint trees. Trees are acyclic-connected graphs in a forest. In arboricity, a forest covers an edge if a tree includes it. Arboricity is helpful in graph theory because it lowers the edge-chromatic number of a graph, which is the lowest number of colors required to color the edges such that no two adjacent edges have the same color.

By dividing the number of edges by the graph’s highest vertex degree, arboricity may be computed. The smallest number of forests that can cover all graph edges is rounded up to the closest integer.



Algorithm

Program to Find the Arboricity of a Graph




#include <bits/stdc++.h>
using namespace std;
 
int V, E;
vector<int> adj[10001];
 
void addEdge(int v, int w) {
    // Add the edge (v, w) to the adjacency list of both vertices
    adj[v].push_back(w);
    adj[w].push_back(v);
}
 
int degree(int v) {
    // Return the degree of the vertex v
    return adj[v].size();
}
 
int arboricity() {
    // Find the maximum degree of the vertices in the graph
    int maxDegree = 0;
    for (int i = 0; i < V; i++)
        maxDegree = max(maxDegree, degree(i));
 
    // Return the arboricity of the graph
    return ceil((double)E / (double)maxDegree);
}
 
int main() {
    V = 4;
 
    // Set the number of edges in the graph
    E = 5;
 
    // Add edges to the graph
    addEdge(0, 1);
    addEdge(0, 2);
    addEdge(1, 2);
    addEdge(2, 3);
    addEdge(3, 3);
 
    // Print the arboricity of the graph
    cout << "Arboricity of the given graph: " << arboricity() << endl;
 
    return 0;
}




import java.util.*;
 
public class Arboricity {
    static int V, E;
    static LinkedList<Integer> adj[];
 
    Arboricity(int v)
    {
        // Store the number of vertices in the graph
        V = v;
 
        // Create an adjacency list for each vertex
        adj = new LinkedList[v];
        for (int i = 0; i < v; ++i)
            adj[i] = new LinkedList();
    }
 
    void addEdge(int v, int w)
    {
        // Add the edge (v, w) to the adjacency list of both
        // vertices
        adj[v].add(w);
        adj[w].add(v);
    }
 
    int degree(int v)
    {
        // Return the degree of the vertex v
        return adj[v].size();
    }
 
    int arboricity()
    {
        // Find the maximum degree of the vertices in the
        // graph
        int maxDegree = 0;
        for (int i = 0; i < V; i++)
            maxDegree = Math.max(maxDegree, degree(i));
 
        // Return the arboricity of the graph
        return (int)Math.ceil((double)E
                              / (double)maxDegree);
    }
 
    public static void main(String args[])
    {
        Arboricity g = new Arboricity(4);
 
        // Set the number of edges in the graph
        g.E = 5;
 
        // Add edges to the graph
        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(1, 2);
        g.addEdge(2, 3);
        g.addEdge(3, 3);
 
        // Print the arboricity of the graph
        System.out.println("Arboricity of the given graph: "
                           + g.arboricity());
    }
}

This uses an adjacency list to create a graph and estimates its arboricity by calculating the greatest degree of the vertices and dividing the number of edges by that degree. The smallest number of forests that can cover all graph edges is rounded up to the closest integer.

Output:



Arboricity of the given graph: 2

Complexity Analysis

The algorithm’s time complexity is linear in the graph’s vertices, O(V). As we keep edges in each vertex’s adjacency list, the technique requires O(E + V) space.


Article Tags :