Graph API program | Directed graph | Dgraph


import java.util.List;
import java.util.ArrayList;

class Digraph {
    private final int V; // number of vertices
    private final List[] adj; // adjacency lists... 0 to V-1
    
    /** create an empty digraph with V vertices */
    public Digraph(int V) {
        this.V = V;
        this.adj = new ArrayList[V]; // create empty graph with V vertices
        for(int i = 0; i < V; i++) {
            adj[i] = new ArrayList<>();
        }
    }
    
    /** add a directed edge v→w */
    public void addEdge(int v, int w) {
        // add edge v-w 
        adj[v].add(w);
    }
    
    /** Iterator for vertices pointing from v */
    public Iterable adj(int v) {
        return adj[v];
    }
    
    /** number of vertices */
    public int V() {
        return V;
    }
    
    /** number of edges */
    public int E() {
        int countEdges = 0;
        for(int v = 0; v < V; v++) {
            countEdges += adj[v].size();
        }
        return countEdges;
    }
    
    /** reverse of this digraph */
    Digraph reverse() {
        return null;// TODO
    }
    
    /** string representation */
    public String toString() {
        return "TODO";
    }
    
    private static Digraph preparingGraph() {
        int[][] edges = new int[][] {
		    {0, 5}, {0, 1},
		    {2, 0}, {2, 3},
		    {3, 5}, {3, 2},
		    {4, 3}, {4, 2},
		    {5, 4},
		    {6, 9}, {6, 4}, {6, 8}, {6, 0},
		    {7, 6}, {7, 9},
		    {8, 6},
		    {9, 11}, {9, 10},
		    {10, 12},
		    {11, 4}, {11, 12},
		    {12, 9}
		};
		Digraph G = new Digraph(13);
		// add all edges 
		for(int i = 0; i < edges.length; i++) {
		    int v = edges[i][0];
		    int w = edges[i][1];
		    G.addEdge(v, w);
		}
		return G;
    }
    
	public static void main (String[] args) {
	    System.out.println("Test directed graph ");
	    Digraph G = preparingGraph();
		System.out.println("V: " + G.V());
		System.out.println("E: " + G.E());
		System.out.println("Max degree: " + Digraph.GraphProcessing.maxDegree(G));
		System.out.println("Avg degree: " + Digraph.GraphProcessing.averageDegree(G));
		System.out.println("No of self loops: " + Digraph.GraphProcessing.numberOfSelfLoops(G));
	}
	
	public static class GraphProcessing {
        /** compute degree of v */
        public static int degree(Digraph G, int v) {
            int degree = 0;
            for(int w : G.adj(v)) {
                degree++;
            }
            return degree; // (or) return G.adj(v).size()
        }
        
        /** Compute max degree of graph */
        public static int maxDegree(Digraph G) {
            int maxDegree = 0;
            for(int v = 0; v < G.V(); v++) {
            int degreeOfV = degree(G, v);
                maxDegree = degreeOfV > maxDegree ? degreeOfV : maxDegree;
            }
            return maxDegree;
        }
        
        /** Compute average degree */
        public static double averageDegree(Digraph G) {
            return 2.0 * G.E()/G.V();
        }
        
        /** Compute self loops */
        public static int numberOfSelfLoops(Digraph G) {
            int numberOfLoops = 0;
            for(int v = 0; v < G.V(); v++) {
                for(int w : G.adj(v)) {
                    numberOfLoops = (v == w) ? ++numberOfLoops : numberOfLoops;
                }
            }
            return numberOfLoops/2; // each edge counted twice
        }
    }

    public static class Path {
        /* is there a path from s to v? */
        public boolean hasPathTo(Digraph G, int s, int v)  {
            return ((List)G.adj(s)).contains(v);
        }
        
        /* path from s to v; null if no such path */
        public Iterable pathTo(Digraph G, int s, int v) {
            return null;
        }
        
        /* Print all vertices connected to s */
        public void printAllConnected(Digraph G, int s) {
            //for()
        }
    }
}

Output
Test directed graph 
V: 13
E: 22
Max degree: 4
Avg degree: 3.3846153846153846
No of self loops: 0

Comments