Skip to content

Commit

Permalink
Merge pull request #50 from taran-1407/patch-1
Browse files Browse the repository at this point in the history
Adding GraphBuilder Utility
  • Loading branch information
NASU41 authored Oct 2, 2020
2 parents c85d2d8 + 4e41ed6 commit 3c5ee91
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
31 changes: 31 additions & 0 deletions GraphBuilder/GraphBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class GraphBuilder{
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< 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];
}
return graph;
}

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< 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};
}
return graph;
}
}
34 changes: 34 additions & 0 deletions GraphBuilder/GraphBuilderUsage.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
40 changes: 40 additions & 0 deletions GraphBuilder/README.md
Original file line number Diff line number Diff line change
@@ -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) $

0 comments on commit 3c5ee91

Please sign in to comment.