From 0338ef9a4267b25d46f5d2f094a50b5b872220c8 Mon Sep 17 00:00:00 2001 From: Yukta-Jaiswal <64637853+Yukta-Jaiswal@users.noreply.github.com> Date: Fri, 29 Oct 2021 09:43:50 +0530 Subject: [PATCH] Create transpose.java --- .../Data_structure/transpose.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Java_Programs/Data_structure/transpose.java diff --git a/Program's_Contributed_By_Contributors/Java_Programs/Data_structure/transpose.java b/Program's_Contributed_By_Contributors/Java_Programs/Data_structure/transpose.java new file mode 100644 index 0000000000..278ec58314 --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java_Programs/Data_structure/transpose.java @@ -0,0 +1,75 @@ +// Java program to find the transpose of a graph +import java.util.*; +import java.lang.*; +import java.io.*; + +class Graph +{ + // Total number of vertices + private static int vertices = 5; + + // Find transpose of graph represented by adj + private static ArrayList[] adj = new ArrayList[vertices]; + + // Store the transpose of graph represented by tr + private static ArrayList[] tr = new ArrayList[vertices]; + + // Function to add an edge from source vertex u to + // destination vertex v, if choice is false the edge is added + // to adj otherwise the edge is added to tr + public static void addedge(int u, int v, boolean choice) + { + if(!choice) + adj[u].add(v); + else + tr[u].add(v); + } + + // Function to print the graph representation + public static void printGraph() + { + for(int i = 0; i < vertices; i++) + { + System.out.print(i + "--> "); + for(int j = 0; j < tr[i].size(); j++) + System.out.print(tr[i].get(j) + " "); + System.out.println(); + } + } + + // Function to print the transpose of + // the graph represented as adj and store it in tr + public static void getTranspose() + { + + // Traverse the graph and for each edge u, v + // in graph add the edge v, u in transpose + for(int i = 0; i < vertices; i++) + for(int j = 0; j < adj[i].size(); j++) + addedge(adj[i].get(j), i, true); + } + + public static void main (String[] args) throws java.lang.Exception + { + for(int i = 0; i < vertices; i++) + { + adj[i] = new ArrayList(); + tr[i] = new ArrayList(); + } + addedge(0, 1, false); + addedge(0, 4, false); + addedge(0, 3, false); + addedge(2, 0, false); + addedge(3, 2, false); + addedge(4, 1, false); + addedge(4, 3, false); + + // Finding transpose of the graph + getTranspose(); + + // Printing the graph representation + printGraph(); + } +} + +// This code is contributed by code_freak