55import java .util .Arrays ;
66import java .util .HashMap ;
77import java .util .HashSet ;
8+ import java .util .Iterator ;
89import java .util .Map ;
910import java .util .Set ;
1011import java .util .stream .Collectors ;
1112
1213/**
14+ * A rudimentary Graph having all the basic methods.
15+ *
1316 * @author rampatra
1417 * @since 2019-02-10
1518 */
1619public class Graph <E extends Comparable <E >> {
20+ // map for a fast lookup
1721 private Map <E , GraphNode <E >> nodes = new HashMap <>();
1822
19- public GraphNode <E > add (E value , E ... adjacentValues ) {
20- return add (value , Arrays .stream (adjacentValues ).map (GraphNode ::new ).collect (Collectors .toSet ()));
23+ public GraphNode <E > addEdges (E value , E ... adjacentValues ) {
24+ return addOrUpdateNodes (value , Arrays .stream (adjacentValues ).map (GraphNode ::new ).collect (Collectors .toSet ()));
2125 }
2226
23- public GraphNode <E > add (E value , Set <GraphNode <E >> adjacentNodes ) {
24- GraphNode <E > newNode = new GraphNode <>(value , adjacentNodes );
25- nodes .put (value , newNode );
26- return newNode ;
27- }
28-
29- public GraphNode <E > addOrUpdate (E value , Set <GraphNode <E >> adjacentNodes ) {
27+ private GraphNode <E > addOrUpdateNodes (E value , Set <GraphNode <E >> adjacentNodes ) {
3028 GraphNode <E > node = nodes .get (value );
3129 if (node == null ) {
3230 return add (value , adjacentNodes );
@@ -35,25 +33,67 @@ public GraphNode<E> addOrUpdate(E value, Set<GraphNode<E>> adjacentNodes) {
3533 return node ;
3634 }
3735
36+ private GraphNode <E > add (E value , Set <GraphNode <E >> adjacentNodes ) {
37+ GraphNode <E > newNode = new GraphNode <>(value , adjacentNodes );
38+ nodes .put (value , newNode );
39+ return newNode ;
40+ }
41+
42+ // todo
43+ public boolean hasPathDFS (E src , E dest ) {
44+ GraphNode <E > s = nodes .get (src );
45+ GraphNode <E > d = nodes .get (dest );
46+ Set <GraphNode <E >> visited = new HashSet <>();
47+ for (GraphNode <E > node : s .adjacentNodes ) {
48+ if (hasPathDFS (node , d , visited )) {
49+ return true ;
50+ }
51+ }
52+ return false ;
53+ }
54+
55+ // todo
56+ public boolean hasPathDFS (GraphNode <E > src , GraphNode <E > dest , Set <GraphNode <E >> visited ) {
57+ if (src .value .compareTo (dest .value ) == 0 ) {
58+ return true ;
59+ } else if (!visited .contains (src )) {
60+ visited .add (src );
61+ for (GraphNode <E > node : src .adjacentNodes ) {
62+ if (hasPathDFS (node , dest , visited )) {
63+ return true ;
64+ }
65+ }
66+ }
67+ return false ;
68+ }
69+
3870 public void print () {
3971 Set <E > visited = new HashSet <>();
4072 System .out .print ("[" );
41- for (E val : nodes .keySet ()) {
42- if (!visited .contains (val )) {
43- visited .add (val );
44- System .out .print (val + ", " );
73+ Iterator <E > iterator = nodes .keySet ().iterator ();
74+ while (iterator .hasNext ()) {
75+ E node = iterator .next ();
76+ if (!visited .contains (node )) {
77+ visited .add (node );
78+ System .out .print (node );
79+ if (iterator .hasNext ()) {
80+ System .out .print (", " );
81+ }
4582 }
4683 }
4784 System .out .println ("]" );
4885 }
4986
5087 public static void main (String [] args ) {
5188 Graph <Integer > graph = new Graph <>();
52- graph .add (1 , 4 , 5 );
53- graph .add (4 , 1 , 5 , 6 , 7 );
54- graph .add (5 , 1 , 4 , 6 );
55- graph .add (6 , 5 , 4 );
56- graph .add (7 , 4 );
89+ graph .addEdges (1 , 4 , 5 );
90+ graph .addEdges (4 , 1 , 5 , 6 , 7 );
91+ graph .addEdges (5 , 1 , 4 , 6 );
92+ graph .addEdges (6 , 5 , 4 );
93+ graph .addEdges (7 , 4 );
5794 graph .print ();
95+ // todo
96+ System .out .println (graph .hasPathDFS (1 ,5 ));
97+ System .out .println (graph .hasPathDFS (1 ,6 ));
5898 }
5999}
0 commit comments