Skip to content

Commit d990ccb

Browse files
committed
Directed graph implementations
1 parent ad0debd commit d990ccb

File tree

9 files changed

+227
-36
lines changed

9 files changed

+227
-36
lines changed

src/main/java/graph/undirected/Graph.java renamed to src/main/java/graph/common/Graph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graph.undirected;
1+
package graph.common;
22

33
/**
44
* Created by nbaruah on 11/12/2016.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package graph.directed;
2+
3+
import graph.common.Graph;
4+
5+
/**
6+
* Created by nbaruah on 11/16/2016.
7+
*/
8+
public interface DiGraph extends Graph {
9+
DiGraph reverse();
10+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package graph.directed;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* Created by nbaruah on 11/18/2016.
7+
*/
8+
public class DirectedGraph implements DiGraph {
9+
LinkedList<Integer>[] adjList;
10+
int V;
11+
int E;
12+
13+
/**
14+
* Create an empty digraph with V vertices
15+
* @param v the number of vertices
16+
*/
17+
public DirectedGraph(int v){
18+
this.V = v;
19+
this.E = 0;
20+
adjList = new LinkedList[v];
21+
for (int i = 0; i < v; i++) {
22+
adjList[i] = new LinkedList<Integer>();
23+
}
24+
}
25+
26+
/**
27+
* Reverse of this digraph
28+
* @return reverse directed graph of this graph,
29+
* where every edge V-->W will be W-->V
30+
*/
31+
public DiGraph reverse() {
32+
DiGraph reverseG = new DirectedGraph(this.V);
33+
for (int v = 0; v < this.V; v++) {
34+
for (int adjV : this.adjList[v]){
35+
reverseG.addEdge(adjV, v);
36+
}
37+
}
38+
return reverseG;
39+
}
40+
41+
public int V() {
42+
return V;
43+
}
44+
45+
public int E() {
46+
return E;
47+
}
48+
49+
public void addEdge(int v, int w) {
50+
adjList[v].add(w);
51+
E++;
52+
}
53+
54+
public Iterable<Integer> getAdjVertices(int v) {
55+
return adjList[v];
56+
}
57+
58+
public int getDegree(int v) {
59+
return adjList[v].size();
60+
}
61+
62+
public String toString(){
63+
StringBuilder builder = new StringBuilder();
64+
builder.append("V = " + this.V + "\t E = " + this.E + "\n");
65+
for (int v = 0; v < this.V; v++) {
66+
builder.append(v + " -> ");
67+
for (int adjV : adjList[v]){
68+
builder.append(adjV + ", ");
69+
}
70+
builder.append("\n");
71+
}
72+
return builder.toString();
73+
}
74+
75+
public static void main(String[] args){
76+
DiGraph G = new DirectedGraph(6);
77+
G.addEdge(0, 1);
78+
G.addEdge(0, 5);
79+
G.addEdge(2, 0);
80+
G.addEdge(2, 3);
81+
G.addEdge(3, 2);
82+
G.addEdge(3, 5);
83+
G.addEdge(4, 2);
84+
G.addEdge(4, 3);
85+
G.addEdge(5, 4);
86+
87+
System.out.println(G);
88+
System.out.println(G.reverse());
89+
System.out.println(G.getDegree(4));
90+
System.out.println(G.getAdjVertices(4));
91+
}
92+
}

src/main/java/graph/undirected/AdjacencyList.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package graph.undirected;
22

3+
import graph.common.Graph;
4+
35
import java.util.LinkedList;
46

57
/**

src/main/java/graph/undirected/BFSPaths.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graph.undirected;
22

33
import common.Queue;
4+
import graph.common.Graph;
45

56
import java.util.Stack;
67

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package graph.undirected;
2+
3+
import graph.common.Graph;
4+
5+
import java.util.Stack;
6+
7+
/**
8+
* Created by nbaruah on 11/16/2016.
9+
*/
10+
public class ConnComponents {
11+
private boolean[] visited; // visited[v] indicates that vertex v is visited
12+
private int[] id; //
13+
private int[] edgeTo;
14+
private int count;
15+
16+
public ConnComponents(Graph G){
17+
visited = new boolean[G.V()];
18+
id = new int[G.V()];
19+
edgeTo = new int[G.V()];
20+
for (int v = 0; v < G.V(); v++) {
21+
if (!visited[v]){
22+
dfs(G, v);
23+
count++;
24+
edgeTo[v] = -1;
25+
}
26+
}
27+
}
28+
29+
public boolean isConnected(int v, int w){
30+
return id[v] == id[w];
31+
}
32+
33+
public int count(){
34+
return count;
35+
}
36+
37+
public int id(int v){
38+
return id[v];
39+
}
40+
41+
private void dfs(Graph G, int v){
42+
visited[v] = true;
43+
id[v] = count;
44+
for (int adjVertex : G.getAdjVertices(v)){
45+
if (!visited[adjVertex]){
46+
dfs(G, adjVertex);
47+
edgeTo[adjVertex] = v;
48+
}
49+
}
50+
}
51+
52+
public Iterable<Integer> getPathTo(int v){
53+
Stack<Integer> path = new Stack<Integer>();
54+
for (int currentV = v; currentV != -1 ; currentV = edgeTo[currentV]) {
55+
path.push(currentV);
56+
}
57+
return path;
58+
}
59+
60+
public static void main(String[] args){
61+
Graph g = new AdjacencyList(6);
62+
g.addEdge(0,1);
63+
g.addEdge(0,2);
64+
g.addEdge(0,3);
65+
g.addEdge(2,3);
66+
g.addEdge(4,5);
67+
68+
ConnComponents cc = new ConnComponents(g);
69+
System.out.println("Number of connected components: " + cc.count());
70+
System.out.println("Is 4 and 3 connected: " + cc.isConnected(4, 3));
71+
System.out.println("Is 0 and 3 connected: " + cc.isConnected(0, 3));
72+
System.out.println("Path to 3: " + cc.getPathTo(3));
73+
System.out.println("Path to 5: " + cc.getPathTo(6));
74+
}
75+
}

src/main/java/graph/undirected/DFSPaths.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package graph.undirected;
22

3+
import graph.common.Graph;
4+
35
import java.util.Stack;
46

57
/**
@@ -10,9 +12,9 @@
1012
*/
1113
public class DFSPaths implements Paths{
1214
private boolean[] visited; // vertex indexed array to mark when a vertex is visited
13-
private int[] visitedFrom; // vertex indexed array to track back from which vertex we reached a particular vertex for the first time
14-
private int source; // Stores the source vertex
15-
private int sizeOfG; // Stores the number of vertices in the graph to be processed
15+
private int[] edgeTo; // vertex indexed array to track back from which vertex we reached a particular vertex for the first time
16+
private int source; // source vertex
17+
private int sizeOfG; // number of vertices in the graph
1618

1719
/**
1820
* Find paths in G from source vertex
@@ -24,7 +26,7 @@ public DFSPaths(Graph G, int source){
2426
validateVertex(source);
2527
this.source = source;
2628
visited = new boolean[G.V()];
27-
visitedFrom = new int[G.V()];
29+
edgeTo = new int[G.V()];
2830
dfs(G, source);
2931
}
3032

@@ -52,23 +54,23 @@ public Iterable<Integer> pathTo(int v){
5254
int currentVertex = v;
5355
while (currentVertex != this.source){
5456
path.push(currentVertex);
55-
currentVertex = visitedFrom[currentVertex];
57+
currentVertex = edgeTo[currentVertex];
5658
}
5759
path.push(this.source);
5860
return path;
5961
}
6062

6163
/**
62-
* Performs a DFS from source, to visit each vertex that has a path with source (undirected graph)
63-
* @param G
64-
* @param source
64+
* Performs a DFS from v, to visit each vertex that has a path with v (undirected graph)
65+
* @param G the graph on which DFS is to perform
66+
* @param v the vertex from which dfs is performed
6567
*/
66-
private void dfs(Graph G, int source){
67-
visited[source] = true;
68-
for (int adjV : G.getAdjVertices(source)){
68+
private void dfs(Graph G, int v){
69+
visited[v] = true;
70+
for (int adjV : G.getAdjVertices(v)){
6971
if (!visited[adjV]){
7072
dfs(G, adjV);
71-
visitedFrom[adjV] = source;
73+
edgeTo[adjV] = v;
7274
}
7375
}
7476
}
@@ -87,11 +89,6 @@ public static void main(String[] args){
8789
G.addEdge(3, 4);
8890

8991
DFSPaths DFSPathsFrom_0 = new DFSPaths(G, 0);
90-
for (int v : DFSPathsFrom_0.pathTo(2)){
91-
System.out.print(" => " +v);
92-
}
93-
System.out.println();
94-
95-
System.out.println(DFSPathsFrom_0.hasPathTo(2));
92+
System.out.println(DFSPathsFrom_0.pathTo(3));
9693
}
9794
}

src/main/java/graph/undirected/GraphClient.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package graph.undirected;
2+
3+
import graph.common.Graph;
4+
5+
/**
6+
* Created by nbaruah on 11/15/2016.
7+
*/
8+
public class GraphUtility {
9+
public static int degree(Graph G, int v){
10+
if (v < 0 || v >= G.V()){
11+
throw new ArrayIndexOutOfBoundsException("Vertex: " + v + " is not in between 0 and " + (G.V() - 1));
12+
}
13+
int degree = 0;
14+
for (int vertex : G.getAdjVertices(v)) {
15+
degree ++;
16+
}
17+
return degree;
18+
}
19+
20+
public static int maxDegree(Graph G){
21+
return 0;
22+
}
23+
24+
public static int avgDegree(Graph G){
25+
return 0;
26+
}
27+
28+
public static int numOfSelfLoops(Graph G){
29+
return 0;
30+
}
31+
}

0 commit comments

Comments
 (0)