Java examples for 2D Graphics:Graphics
Calculate the transpose graph.
//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; } }