-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGraphTranspose.java
More file actions
75 lines (64 loc) · 1.86 KB
/
GraphTranspose.java
File metadata and controls
75 lines (64 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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<Integer>[] adj = new ArrayList[vertices];
// Store the transpose of graph represented by tr
private static ArrayList<Integer>[] 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<Integer>();
tr[i] = new ArrayList<Integer>();
}
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