Techno Blender
Digitally Yours.

Java Program to Find the Arboricity of a Graph

0 43


import java.util.*;

  

public class Arboricity {

    static int V, E;

    static LinkedList<Integer> adj[];

  

    Arboricity(int v)

    {

        

        V = v;

  

        

        adj = new LinkedList[v];

        for (int i = 0; i < v; ++i)

            adj[i] = new LinkedList();

    }

  

    void addEdge(int v, int w)

    {

        

        

        adj[v].add(w);

        adj[w].add(v);

    }

  

    int degree(int v)

    {

        

        return adj[v].size();

    }

  

    int arboricity()

    {

        

        

        int maxDegree = 0;

        for (int i = 0; i < V; i++)

            maxDegree = Math.max(maxDegree, degree(i));

  

        

        return (int)Math.ceil((double)E

                              / (double)maxDegree);

    }

  

    public static void main(String args[])

    {

        Arboricity g = new Arboricity(4);

  

        

        g.E = 5;

  

        

        g.addEdge(0, 1);

        g.addEdge(0, 2);

        g.addEdge(1, 2);

        g.addEdge(2, 3);

        g.addEdge(3, 3);

  

        

        System.out.println("Arboricity of the given graph: "

                           + g.arboricity());

    }

}


import java.util.*;

  

public class Arboricity {

    static int V, E;

    static LinkedList<Integer> adj[];

  

    Arboricity(int v)

    {

        

        V = v;

  

        

        adj = new LinkedList[v];

        for (int i = 0; i < v; ++i)

            adj[i] = new LinkedList();

    }

  

    void addEdge(int v, int w)

    {

        

        

        adj[v].add(w);

        adj[w].add(v);

    }

  

    int degree(int v)

    {

        

        return adj[v].size();

    }

  

    int arboricity()

    {

        

        

        int maxDegree = 0;

        for (int i = 0; i < V; i++)

            maxDegree = Math.max(maxDegree, degree(i));

  

        

        return (int)Math.ceil((double)E

                              / (double)maxDegree);

    }

  

    public static void main(String args[])

    {

        Arboricity g = new Arboricity(4);

  

        

        g.E = 5;

  

        

        g.addEdge(0, 1);

        g.addEdge(0, 2);

        g.addEdge(1, 2);

        g.addEdge(2, 3);

        g.addEdge(3, 3);

  

        

        System.out.println("Arboricity of the given graph: "

                           + g.arboricity());

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment