Calculate the transpose graph. - Java 2D Graphics

Java examples for 2D Graphics:Graphics

Description

Calculate the transpose graph.

Demo Code


//package com.java2s;

public class Main {
    /**//from w  ww . jav a2  s. c o m
     * Calculate the transpose graph.
     * 
     * @param graph a directed graph
     * @return the transpose graph
     */
    public static int[][] transpose(int[][] graph) {
        int n = graph.length;

        int[] degree = new int[n];
        for (int[] edges : graph) {
            for (int endpoint : edges) {
                ++degree[endpoint];
            }
        }

        int[][] res = new int[n][];
        for (int i = 0; i < n; ++i) {
            res[i] = new int[degree[i]];
        }

        int[] next = new int[n];
        for (int i = 0; i < n; ++i) {
            for (int endpoint : graph[i]) {
                res[endpoint][next[endpoint]++] = i;
            }
        }

        return res;
    }
}

Related Tutorials