Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ae5cd25

Browse files
committed
Undirected graph added
1 parent 4f619e5 commit ae5cd25

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.rampatra.common;
2+
3+
/**
4+
* A rudimentary Graph having all the basic methods.
5+
*
6+
* @author rampatra
7+
* @since 2019-02-10
8+
*/
9+
public class Graph<E extends Comparable<E>> {
10+
}

src/main/java/com/rampatra/common/GraphNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.rampatra.common;
22

3+
import java.util.HashSet;
34
import java.util.Set;
45

56
/**
@@ -11,15 +12,17 @@
1112
*/
1213
public class GraphNode<E extends Comparable<E>> {
1314
public E value;
14-
public Set<GraphNode<E>> adjacentNodes;
15+
public Set<GraphNode<E>> adjacentNodes = new HashSet<>();
1516

1617
public GraphNode(E value) {
1718
this(value, null);
1819
}
1920

2021
public GraphNode(E value, Set<GraphNode<E>> adjacentNodes) {
2122
this.value = value;
22-
this.adjacentNodes = adjacentNodes;
23+
if (adjacentNodes != null) {
24+
this.adjacentNodes = adjacentNodes;
25+
}
2326
}
2427

2528
@Override
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,48 @@
1-
package com.rampatra.graphs;
1+
package com.rampatra.common;
22

3-
import com.rampatra.common.GraphNode;
4-
5-
import java.util.Arrays;
63
import java.util.HashMap;
74
import java.util.HashSet;
85
import java.util.Iterator;
96
import java.util.Map;
107
import java.util.Set;
11-
import java.util.stream.Collectors;
128

139
/**
14-
* A rudimentary Graph having all the basic methods.
15-
*
1610
* @author rampatra
17-
* @since 2019-02-10
11+
* @since 2019-02-14
1812
*/
19-
public class Graph<E extends Comparable<E>> {
13+
public class UndirectedGraph<E extends Comparable<E>> extends Graph<E> {
2014
// map for a fast lookup
2115
private Map<E, GraphNode<E>> nodes = new HashMap<>();
2216

23-
public GraphNode<E> addEdges(E value, E... adjacentValues) {
24-
return addOrUpdateNodes(value, Arrays.stream(adjacentValues).map(GraphNode::new).collect(Collectors.toSet()));
25-
}
26-
27-
private GraphNode<E> addOrUpdateNodes(E value, Set<GraphNode<E>> adjacentNodes) {
17+
public GraphNode<E> addEdge(E value, E adjacentValue) {
2818
GraphNode<E> node = nodes.get(value);
19+
GraphNode<E> adjNode = nodes.get(adjacentValue);
2920
if (node == null) {
30-
return add(value, adjacentNodes);
21+
node = new GraphNode<>(value);
22+
nodes.put(value, node);
3123
}
32-
node.adjacentNodes.addAll(adjacentNodes);
24+
if (adjNode == null) {
25+
adjNode = new GraphNode<>(adjacentValue);
26+
nodes.put(adjacentValue, adjNode);
27+
}
28+
node.adjacentNodes.add(adjNode);
29+
adjNode.adjacentNodes.add(node); // as this is an undirected graph
3330
return node;
3431
}
3532

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-
4233
// todo
4334
public boolean hasPathDFS(E src, E dest) {
4435
GraphNode<E> s = nodes.get(src);
4536
GraphNode<E> d = nodes.get(dest);
4637
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;
38+
return hasPathDFS(s, d, visited);
5339
}
54-
40+
5541
// todo
5642
public boolean hasPathDFS(GraphNode<E> src, GraphNode<E> dest, Set<GraphNode<E>> visited) {
43+
if (src == null || dest == null) {
44+
return false;
45+
}
5746
if (src.value.compareTo(dest.value) == 0) {
5847
return true;
5948
} else if (!visited.contains(src)) {
@@ -85,15 +74,20 @@ public void print() {
8574
}
8675

8776
public static void main(String[] args) {
88-
Graph<Integer> graph = new Graph<>();
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);
77+
UndirectedGraph<Integer> graph = new UndirectedGraph<>();
78+
graph.addEdge(1, 4);
79+
graph.addEdge(4, 5);
80+
graph.addEdge(4, 6);
81+
graph.addEdge(4, 7);
82+
graph.addEdge(5, 1);
83+
graph.addEdge(5, 6);
84+
graph.addEdge(8, null);
9485
graph.print();
9586
// todo
96-
System.out.println(graph.hasPathDFS(1,5));
97-
System.out.println(graph.hasPathDFS(1,6));
87+
System.out.println(graph.hasPathDFS(1, 5));
88+
System.out.println(graph.hasPathDFS(1, 6));
89+
System.out.println(graph.hasPathDFS(1, 8));
90+
System.out.println(graph.hasPathDFS(4, 8));
91+
System.out.println(graph.hasPathDFS(4, 100));
9892
}
9993
}

0 commit comments

Comments
 (0)