Open In App

Program to Find the Arboricity of a Graph

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Maintain graph vertices and edges.
  • Adjacency lists for each vertex store edges.
  • Get each vertex’s degree by counting its adjacency edges.
  • Determine the graph’s highest degree.
  • Dividing the number of edges by the greatest degree and rounding to the next integer gives arboricity. This is the minimal number of forests needed to cover all graph edges.
  • Return arboricity.

Program to Find the Arboricity of a Graph

C++




#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;
}


Java




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

  • Each vertex’s adjacency list takes O(V), where V is the graph’s vertices.
  • Adding an edge to each vertices’ adjacency list is O(1).
  • When the vertex degree is V-1, finding it requires O(V) time.
  • Identifying the graph’s highest vertex degree takes O(V) time.
  • Identifying arboricity is O(1).

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads