Skip to content

Commit 74dc4f4

Browse files
author
Justin Wetherell
committed
Merge branch 'jsroyal-master' - Graph.DFS
2 parents 5d0422e + f0666e9 commit 74dc4f4

File tree

4 files changed

+150
-10
lines changed

4 files changed

+150
-10
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ This is a collection of algorithms and data structures which I've implement over
128128
* A* path finding algorithm
129129
* Maximum flow
130130
- Push-Relabel
131+
* Graph Traversal
132+
- Depth First Traversal
131133

132134
## Search
133135
* Get index of value in array

src/com/jwetherell/algorithms/graph/CycleDetection.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,40 @@ private CycleDetection() { }
2121
* @param graph Graph
2222
* @return true if a cycle exists
2323
*/
24-
public static boolean detect(Graph<Integer> graph) {
24+
public static <T extends Comparable<T>> boolean detect(Graph<T> graph) {
2525
if (graph == null)
2626
throw new IllegalArgumentException("Graph is NULL.");
2727

2828
if (graph.getType() != Graph.TYPE.UNDIRECTED)
2929
throw new IllegalArgumentException("Graph is needs to be Undirected.");
3030

31-
final Set<Graph.Vertex<Integer>> visitedVerticies = new HashSet<Graph.Vertex<Integer>>();
32-
final Set<Graph.Edge<Integer>> visitedEdges = new HashSet<Graph.Edge<Integer>>();
31+
final Set<Graph.Vertex<T>> visitedVerticies = new HashSet<Graph.Vertex<T>>();
32+
final Set<Graph.Edge<T>> visitedEdges = new HashSet<Graph.Edge<T>>();
3333

34-
final List<Graph.Vertex<Integer>> verticies = graph.getVertices();
34+
final List<Graph.Vertex<T>> verticies = graph.getVertices();
3535
if (verticies == null || verticies.size() == 0)
3636
return false;
3737

3838
// Select the zero-ith element as the root
39-
final Graph.Vertex<Integer> root = verticies.get(0);
39+
final Graph.Vertex<T> root = verticies.get(0);
4040
return depthFirstSearch(root, visitedVerticies, visitedEdges);
4141
}
4242

43-
private static final boolean depthFirstSearch(Graph.Vertex<Integer> vertex, Set<Graph.Vertex<Integer>> visitedVerticies, Set<Graph.Edge<Integer>> visitedEdges) {
43+
private static final <T extends Comparable<T>> boolean depthFirstSearch(Graph.Vertex<T> vertex, Set<Graph.Vertex<T>> visitedVerticies, Set<Graph.Edge<T>> visitedEdges) {
4444
if (!visitedVerticies.contains(vertex)) {
4545
// Found an unvisited, add to the set
4646
visitedVerticies.add(vertex);
4747

48-
final List<Graph.Edge<Integer>> edges = vertex.getEdges();
48+
final List<Graph.Edge<T>> edges = vertex.getEdges();
4949
if (edges != null) {
5050
// Follow each unvisited edge, visit the vertex the edge connects to.
51-
for (Graph.Edge<Integer> edge : edges) {
52-
final Graph.Vertex<Integer> to = edge.getToVertex();
51+
for (Graph.Edge<T> edge : edges) {
52+
final Graph.Vertex<T> to = edge.getToVertex();
5353
boolean result = false;
5454
if (to != null && !visitedEdges.contains(edge)) {
5555
visitedEdges.add(edge);
5656

57-
final Graph.Edge<Integer> recip = new Graph.Edge<Integer>(edge.getCost(), edge.getToVertex(), edge.getFromVertex());
57+
final Graph.Edge<T> recip = new Graph.Edge<T>(edge.getCost(), edge.getToVertex(), edge.getFromVertex());
5858
visitedEdges.add(recip);
5959

6060
result = depthFirstSearch(to, visitedVerticies, visitedEdges);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.jwetherell.algorithms.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Stack;
8+
9+
import com.jwetherell.algorithms.data_structures.Graph;
10+
import com.jwetherell.algorithms.data_structures.Graph.Edge;
11+
import com.jwetherell.algorithms.data_structures.Graph.Vertex;
12+
13+
/* Depth First Travesal in given "Directed graph" (Adjacany matrix) */
14+
public class DepthFirstTraversal {
15+
16+
@SuppressWarnings("unchecked")
17+
public static final <T extends Comparable<T>> Graph.Vertex<T>[] depthFirstTraversal(Graph<T> graph, Graph.Vertex<T> source) {
18+
// use for look-up via index
19+
final ArrayList<Vertex<T>> vertices = new ArrayList<Vertex<T>>();
20+
vertices.addAll(graph.getVertices());
21+
22+
// used for look-up via vertex
23+
final int n = vertices.size();
24+
final Map<Vertex<T>,Integer> vertexToIndex = new HashMap<Vertex<T>,Integer>();
25+
for (int i=0; i<n; i++) {
26+
final Vertex<T> v = vertices.get(i);
27+
vertexToIndex.put(v,i);
28+
}
29+
30+
// adjacency matrix
31+
final byte[][] adj = new byte[n][n];
32+
for (int i=0; i<n; i++) {
33+
final Vertex<T> v = vertices.get(i);
34+
final int idx = vertexToIndex.get(v);
35+
final byte[] array = new byte[n];
36+
adj[idx] = array;
37+
final List<Edge<T>> edges = v.getEdges();
38+
for (Edge<T> e : edges)
39+
array[vertexToIndex.get(e.getToVertex())] = 1;
40+
}
41+
42+
// visited array
43+
final byte[] visited = new byte[n];
44+
for (int i = 0; i < visited.length; i++)
45+
visited[i] = -1;
46+
47+
// for holding results
48+
final Graph.Vertex<T>[] arr = new Graph.Vertex[n];
49+
50+
// start at the source
51+
Vertex<T> element = source;
52+
int c = 0;
53+
int i = vertexToIndex.get(element);
54+
visited[i] = 1;
55+
int k = 0;
56+
arr[k] = element;
57+
k++;
58+
59+
final Stack<Vertex<T>> stack = new Stack<Vertex<T>>();
60+
stack.push(source);
61+
while (!stack.isEmpty()) {
62+
element = stack.peek();
63+
c = vertexToIndex.get(element);
64+
i = 0;
65+
while (i < n) {
66+
if (adj[c][i] == 1 && visited[i] == -1) {
67+
stack.push(element);
68+
visited[i] = 1;
69+
element = vertices.get(i);
70+
arr[k] = element;
71+
k++;
72+
c = vertexToIndex.get(element);
73+
i = 0;
74+
continue;
75+
}
76+
i++;
77+
}
78+
stack.pop();
79+
}
80+
return arr;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.jwetherell.algorithms.graph.test;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import junit.framework.Assert;
7+
8+
import org.junit.Test;
9+
10+
import com.jwetherell.algorithms.data_structures.Graph;
11+
import com.jwetherell.algorithms.graph.DepthFirstTraversal;
12+
13+
public class DepthFirstTraversalTest {
14+
15+
private static final List<Graph.Vertex<Integer>> vertices = new ArrayList<Graph.Vertex<Integer>>();
16+
private static final List<Graph.Edge<Integer>> edges = new ArrayList<Graph.Edge<Integer>>();
17+
18+
private static final Graph.Vertex<Integer> v0 = new Graph.Vertex<Integer>(0);
19+
private static final Graph.Vertex<Integer> v1 = new Graph.Vertex<Integer>(1);
20+
private static final Graph.Vertex<Integer> v2 = new Graph.Vertex<Integer>(2);
21+
private static final Graph.Vertex<Integer> v3 = new Graph.Vertex<Integer>(3);
22+
23+
static {
24+
vertices.add(v0);
25+
vertices.add(v1);
26+
vertices.add(v2);
27+
vertices.add(v3);
28+
29+
edges.add(new Graph.Edge<Integer>(0, v0, v1));
30+
edges.add(new Graph.Edge<Integer>(0, v0, v2));
31+
edges.add(new Graph.Edge<Integer>(0, v1, v2));
32+
edges.add(new Graph.Edge<Integer>(0, v2, v0));
33+
edges.add(new Graph.Edge<Integer>(0, v2, v3));
34+
edges.add(new Graph.Edge<Integer>(0, v3, v3));
35+
}
36+
37+
private static final Graph<Integer> graph = new Graph<Integer>(Graph.TYPE.DIRECTED, vertices, edges);
38+
39+
@Test
40+
public void test1() {
41+
final Graph.Vertex<Integer>[] result = DepthFirstTraversal.depthFirstTraversal(graph, v2);
42+
Assert.assertTrue(result[0].getValue()==2);
43+
Assert.assertTrue(result[1].getValue()==0);
44+
Assert.assertTrue(result[2].getValue()==1);
45+
Assert.assertTrue(result[3].getValue()==3);
46+
}
47+
48+
@Test
49+
public void test2() {
50+
final Graph.Vertex<Integer>[] result = DepthFirstTraversal.depthFirstTraversal(graph, v0);
51+
Assert.assertTrue(result[0].getValue()==0);
52+
Assert.assertTrue(result[1].getValue()==1);
53+
Assert.assertTrue(result[2].getValue()==2);
54+
Assert.assertTrue(result[3].getValue()==3);
55+
}
56+
}

0 commit comments

Comments
 (0)