From 6a7db2aebce81bc007350d2443677959d3524398 Mon Sep 17 00:00:00 2001 From: Taranpreet Singh <47025523+taran-1407@users.noreply.github.com> Date: Fri, 2 Oct 2020 01:39:12 +0530 Subject: [PATCH 1/5] Created GraphBuilder.java This file add utilities for building graph in form of jagged arrays, as opposed to ArrayLists, offering comparatively better performance. --- GraphBuilder/GraphBuilder.java | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 GraphBuilder/GraphBuilder.java diff --git a/GraphBuilder/GraphBuilder.java b/GraphBuilder/GraphBuilder.java new file mode 100644 index 0000000..f27f3de --- /dev/null +++ b/GraphBuilder/GraphBuilder.java @@ -0,0 +1,59 @@ +class GraphBuilder{ + public int[][] makeGraph(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ + int[][] graph = new int[NumberOfNoes][]; + int[] outdegree = new int[NumberOfNoes]; + for(int i = 0; i< NumberOfEdges; i++){ + outdegree[from[i]]++; + if(undirected)outdegree[to[i]]++; + } + for(int i = 0; i< NumberOfNoes; i++)graph[i] = new int[outdegree[i]]; + for(int i = 0; i< NumberOfEdges; i++){ + graph[from[i]][--outdegree[from[i]]] = to[i]; + if(undirected)graph[to[i]][--outdegree[to[i]]] = from[i]; + } + return graph; + } + + public int[][][] makeGraphWithEdgeInfo(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ + int[][][] graph = new int[NumberOfNoes][][]; + int[] outdegree = new int[NumberOfNoes]; + for(int i = 0; i< NumberOfEdges; i++){ + outdegree[from[i]]++; + if(undirected)outdegree[to[i]]++; + } + for(int i = 0; i< NumberOfNoes; i++)graph[i] = new int[outdegree[i]][]; + for(int i = 0; i< NumberOfEdges; i++){ + graph[from[i]][--outdegree[from[i]]] = new int[]{to[i], i, 0}; + if(undirected)graph[to[i]][--outdegree[to[i]]] = new int[]{from[i], i, 1}; + } + return graph; + } + + //Usage Exmaple + public void makeGraphUsage(){ + int N = 6, E = 8; + int[] from = new int[]{0,0,0,1,1,2,2,4}; + int[] to = new int[]{2,4,5,4,5,3,4,5}; + + int[][] graph = makeGraph(N, E, from, to, true); + for(int i = 0; i< N; i++){ + System.out.print(i+": "); + for(int j:graph[i])System.out.print(j+" "); + System.out.println(); + } + } + + //Usage Example + public void makeGraphWithEdgeInfoUsage(){ + int N = 6, E = 8; + int[] from = new int[]{0,0,0,1,1,2,2,4}; + int[] to = new int[]{2,4,5,4,5,3,4,5}; + + int[][][] graph = makeGraphWithEdgeInfo(N, E, from, to, true); + for(int i = 0; i< N; i++){ + System.out.print(i+": "); + for(int[] j:graph[i])System.out.print("["+j[0]+", "+j[1]+", "+j[2]+"],"); + System.out.println(); + } + } +} From 36308c5c996402128e2a14e58bac112a2780d00f Mon Sep 17 00:00:00 2001 From: Taranpreet Singh <47025523+taran-1407@users.noreply.github.com> Date: Fri, 2 Oct 2020 01:50:41 +0530 Subject: [PATCH 2/5] Updated GraphBuilder.java Moved example usage to separate file, modified access specifies and made methods static. --- GraphBuilder/GraphBuilder.java | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/GraphBuilder/GraphBuilder.java b/GraphBuilder/GraphBuilder.java index f27f3de..a8d90e6 100644 --- a/GraphBuilder/GraphBuilder.java +++ b/GraphBuilder/GraphBuilder.java @@ -1,5 +1,5 @@ class GraphBuilder{ - public int[][] makeGraph(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ + public static int[][] makeGraph(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ int[][] graph = new int[NumberOfNoes][]; int[] outdegree = new int[NumberOfNoes]; for(int i = 0; i< NumberOfEdges; i++){ @@ -14,7 +14,7 @@ public int[][] makeGraph(int NumberOfNoes, int NumberOfEdges, int[] from, int[] return graph; } - public int[][][] makeGraphWithEdgeInfo(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ + public static int[][][] makeGraphWithEdgeInfo(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ int[][][] graph = new int[NumberOfNoes][][]; int[] outdegree = new int[NumberOfNoes]; for(int i = 0; i< NumberOfEdges; i++){ @@ -28,32 +28,4 @@ public int[][][] makeGraphWithEdgeInfo(int NumberOfNoes, int NumberOfEdges, int[ } return graph; } - - //Usage Exmaple - public void makeGraphUsage(){ - int N = 6, E = 8; - int[] from = new int[]{0,0,0,1,1,2,2,4}; - int[] to = new int[]{2,4,5,4,5,3,4,5}; - - int[][] graph = makeGraph(N, E, from, to, true); - for(int i = 0; i< N; i++){ - System.out.print(i+": "); - for(int j:graph[i])System.out.print(j+" "); - System.out.println(); - } - } - - //Usage Example - public void makeGraphWithEdgeInfoUsage(){ - int N = 6, E = 8; - int[] from = new int[]{0,0,0,1,1,2,2,4}; - int[] to = new int[]{2,4,5,4,5,3,4,5}; - - int[][][] graph = makeGraphWithEdgeInfo(N, E, from, to, true); - for(int i = 0; i< N; i++){ - System.out.print(i+": "); - for(int[] j:graph[i])System.out.print("["+j[0]+", "+j[1]+", "+j[2]+"],"); - System.out.println(); - } - } } From 7cf55d7907ba66bb23cdd47de954151378391655 Mon Sep 17 00:00:00 2001 From: Taranpreet Singh <47025523+taran-1407@users.noreply.github.com> Date: Fri, 2 Oct 2020 01:54:18 +0530 Subject: [PATCH 3/5] Created GraphBuilderUsage.java This file illustrates the sample usage of GraphBuilder.java --- GraphBuilder/GraphBuilderUsage.java | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 GraphBuilder/GraphBuilderUsage.java diff --git a/GraphBuilder/GraphBuilderUsage.java b/GraphBuilder/GraphBuilderUsage.java new file mode 100644 index 0000000..cc38c57 --- /dev/null +++ b/GraphBuilder/GraphBuilderUsage.java @@ -0,0 +1,34 @@ +class GraphBuilderUsage{ + //Usage Exmaple + public static void makeGraphUsage(){ + int N = 6, E = 8; + int[] from = new int[]{0,0,0,1,1,2,2,4}; + int[] to = new int[]{2,4,5,4,5,3,4,5}; + + int[][] graph = GraphBuilder.makeGraph(N, E, from, to, true); + for(int i = 0; i< N; i++){ + System.out.print(i+": "); + for(int j:graph[i])System.out.print(j+" "); + System.out.println(); + } + } + + //Usage Example + public static void makeGraphWithEdgeInfoUsage(){ + int N = 6, E = 8; + int[] from = new int[]{0,0,0,1,1,2,2,4}; + int[] to = new int[]{2,4,5,4,5,3,4,5}; + + int[][][] graph = GraphBuilder.makeGraphWithEdgeInfo(N, E, from, to, true); + for(int i = 0; i< N; i++){ + System.out.print(i+": "); + for(int[] j:graph[i])System.out.print("["+j[0]+", "+j[1]+", "+j[2]+"],"); + System.out.println(); + } + } + + public static void main(String[] args){ + makeGraphUsage(); + makeGraphWithEdgeInfoUsage(); + } +} From 5d5042d96e9176372ed566c29acc6266c7a844ad Mon Sep 17 00:00:00 2001 From: Taranpreet Singh <47025523+taran-1407@users.noreply.github.com> Date: Fri, 2 Oct 2020 02:10:13 +0530 Subject: [PATCH 4/5] Corrected Typo in variable name --- GraphBuilder/GraphBuilder.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/GraphBuilder/GraphBuilder.java b/GraphBuilder/GraphBuilder.java index a8d90e6..95a4be8 100644 --- a/GraphBuilder/GraphBuilder.java +++ b/GraphBuilder/GraphBuilder.java @@ -1,12 +1,12 @@ class GraphBuilder{ - public static int[][] makeGraph(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ - int[][] graph = new int[NumberOfNoes][]; - int[] outdegree = new int[NumberOfNoes]; + public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ + int[][] graph = new int[NumberOfNodes][]; + int[] outdegree = new int[NumberOfNodes]; for(int i = 0; i< NumberOfEdges; i++){ outdegree[from[i]]++; if(undirected)outdegree[to[i]]++; } - for(int i = 0; i< NumberOfNoes; i++)graph[i] = new int[outdegree[i]]; + for(int i = 0; i< NumberOfNodes; i++)graph[i] = new int[outdegree[i]]; for(int i = 0; i< NumberOfEdges; i++){ graph[from[i]][--outdegree[from[i]]] = to[i]; if(undirected)graph[to[i]][--outdegree[to[i]]] = from[i]; @@ -14,14 +14,14 @@ public static int[][] makeGraph(int NumberOfNoes, int NumberOfEdges, int[] from, return graph; } - public static int[][][] makeGraphWithEdgeInfo(int NumberOfNoes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ - int[][][] graph = new int[NumberOfNoes][][]; - int[] outdegree = new int[NumberOfNoes]; + public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected){ + int[][][] graph = new int[NumberOfNodes][][]; + int[] outdegree = new int[NumberOfNodes]; for(int i = 0; i< NumberOfEdges; i++){ outdegree[from[i]]++; if(undirected)outdegree[to[i]]++; } - for(int i = 0; i< NumberOfNoes; i++)graph[i] = new int[outdegree[i]][]; + for(int i = 0; i< NumberOfNodes; i++)graph[i] = new int[outdegree[i]][]; for(int i = 0; i< NumberOfEdges; i++){ graph[from[i]][--outdegree[from[i]]] = new int[]{to[i], i, 0}; if(undirected)graph[to[i]][--outdegree[to[i]]] = new int[]{from[i], i, 1}; From 4e41ed60c7a0eb6acfa71dc0f5f3440334889f1a Mon Sep 17 00:00:00 2001 From: Taranpreet Singh <47025523+taran-1407@users.noreply.github.com> Date: Fri, 2 Oct 2020 02:28:47 +0530 Subject: [PATCH 5/5] Added README.md --- GraphBuilder/README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 GraphBuilder/README.md diff --git a/GraphBuilder/README.md b/GraphBuilder/README.md new file mode 100644 index 0000000..22fa59c --- /dev/null +++ b/GraphBuilder/README.md @@ -0,0 +1,40 @@ +# Class GraphBuilder + +Creates graph in form of jagged arrays (as compared to Arraylist) for directed as well as undirected graphs from given set of edges in 0 indexing. + +All members of this class are static, so no constructor defined. + +## Methods + +### makeGraph +``` +public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected) +``` +Returns the adjacency list representation of graph with specified Number of Nodes and Edges and the edges specified in from and to arrays in 0-based indexing. If undirected is set to true, the edges are deemed to be undirected, otherwise directed. + +```graph[u]``` is the array of vertices having an edge outgoing from vertex u. + +** Constraints ** +* 0 \leq from[i], to[i] < NumberOfNodes +* from.length = to.length = NumberOfEdges + +** Computational complexity ** +* $ O (NumberOfNodes + NumberOfEdges) $ + +### makeGraphWithEdgeInfo +``` +public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected) +``` + +Returns the adjacency list representation of graph with specified Number of Nodes and Edges and the edges specified in from and to arrays in 0-based indexing. If undirected is set to true, the edges are deemed to be undirected, otherwise directed. This method also returns useful information about edge index and edge direction. + +```graph[u]``` is the array of tuples ```[v, idx, direction]``` where edge indexed ```idx``` is between node ```u``` and node ```v``` and direction takes value either $0$ or $1$. +- If direction is $0$, edge indexed ```idx``` is from ```u``` to ```v``` +- If direction is $1$, edge indexed ```idx``` is from ```v``` to ```u``` + +** Constraints ** +* 0 \leq from[i], to[i] < NumberOfNodes +* from.length = to.length = NumberOfEdges + +** Computational complexity ** +* $ O (NumberOfNodes + NumberOfEdges) $