Skip to content

Commit 3ab3e7a

Browse files
committed
BFS in graph: done
1 parent 66f1d52 commit 3ab3e7a

File tree

5 files changed

+123
-8
lines changed

5 files changed

+123
-8
lines changed

Diff for: src/main/java/com/ctci/treesandgraphs/GraphNode.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package com.ctci.treesandgraphs;
22

3+
import java.util.HashSet;
4+
import java.util.Set;
5+
36
/**
47
* @author rampatra
58
* @since 2019-03-21
69
*/
710
public class GraphNode {
8-
}
11+
int value;
12+
Set<GraphNode> adjacent = new HashSet<>();
13+
14+
GraphNode(int value) {
15+
this.value = value;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,87 @@
11
package com.ctci.treesandgraphs;
22

3+
import java.util.ArrayDeque;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
import java.util.Queue;
8+
import java.util.Set;
9+
310
/**
411
* @author rampatra
512
* @since 2019-03-21
613
*/
714
public class RouteBetweenNodes {
15+
16+
public static void main(String[] args) {
17+
Graph g = new Graph();
18+
g.addEdge(1, 2);
19+
g.addEdge(2, 3);
20+
g.addEdge(4, 5);
21+
g.addEdge(5, 6);
22+
System.out.println("Route exists from 1 to 2: " + g.isRoutePresent(1, 2));
23+
System.out.println("Route exists from 2 to 5: " + g.isRoutePresent(2, 5));
24+
System.out.println("Route exists from 1 to 3: " + g.isRoutePresent(1, 3));
25+
System.out.println("Route exists from 4 to 6: " + g.isRoutePresent(4, 6));
26+
System.out.println("Route exists from 6 to 4: " + g.isRoutePresent(6, 4));
27+
System.out.println("Route exists from 6 to 5: " + g.isRoutePresent(6, 5));
28+
}
29+
}
30+
31+
class Graph {
32+
33+
private static final Map<Integer, GraphNode> nodes = new HashMap<>();
34+
35+
void addEdge(int v1, int v2) {
36+
GraphNode n1 = nodes.get(v1);
37+
GraphNode n2 = nodes.get(v2);
38+
39+
if (n1 == null) {
40+
n1 = new GraphNode(v1);
41+
nodes.put(v1, n1);
42+
}
43+
if (n2 == null) {
44+
n2 = new GraphNode(v2);
45+
nodes.put(v2, n2);
46+
}
47+
48+
n1.adjacent.add(n2); // as it is a directed graph
49+
}
50+
51+
/**
52+
* Checks for a path from a node with value {@code v1} to another node with value {@code v2} in a breadth-first
53+
* manner.
54+
*
55+
* @param v1 the value of the first node or starting node.
56+
* @param v2 the value of the ending node.
57+
* @return {@code true} if path exists, {@code false} otherwise.
58+
*/
59+
boolean isRoutePresent(int v1, int v2) {
60+
Queue<GraphNode> queue = new ArrayDeque<>();
61+
Set<GraphNode> visited = new HashSet<>();
62+
63+
GraphNode n1 = nodes.get(v1);
64+
GraphNode n2 = nodes.get(v2);
65+
66+
if (n1 == null || n2 == null) {
67+
return false;
68+
}
69+
70+
queue.add(n1);
71+
72+
while (!queue.isEmpty()) {
73+
GraphNode n = queue.poll();
74+
75+
if (visited.contains(n)) {
76+
continue;
77+
}
78+
if (n.adjacent.contains(n2)) {
79+
return true;
80+
}
81+
queue.addAll(n.adjacent);
82+
visited.add(n);
83+
}
84+
85+
return false;
86+
}
887
}

Diff for: src/main/java/com/rampatra/base/UndirectedGraph.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public class UndirectedGraph<E extends Comparable<E>> extends Graph<E> {
1818
private Map<E, GraphNode<E>> nodes = new HashMap<>();
1919

2020
/**
21-
* Adds an edge between a node with value {@code value} and another node with value {@code adjacentValue}.
22-
* If nodes with respective values are not present in the graph then this method creates them.
21+
* Adds an edge between a node with value {@code value} and another node with value {@code adjacentValue}. As the
22+
* graph is undirected, the edges are added in both the nodes. If nodes with respective values are not present in
23+
* the graph then this method creates the nodes.
2324
*
2425
* @param value refers to the value for first node
2526
* @param adjacentValue refers to the value for the second node
@@ -143,7 +144,7 @@ public static void main(String[] args) {
143144
graph.print();
144145

145146
System.out.println("----");
146-
147+
147148
// has path DFS
148149
System.out.println(graph.hasPathDFS(1, 5));
149150
System.out.println(graph.hasPathDFS(1, 6));

Diff for: src/main/java/com/rampatra/bits/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Basic Operators
44

5-
#### AND:
5+
#### AND:
66

77
| x | y | x `&` y |
88
----|---|:-------:|
@@ -63,4 +63,25 @@ _Disclaimer: We are taking `byte` (8 bits) as our datatype to explain the
6363
6464
-64 >>> 2 = 56
6565

66-
> 111000000 >>> 2 = 00111000
66+
> 111000000 >>> 2 = 00111000
67+
68+
### Helpful Masks
69+
70+
#### 1. Set the 4th bit from right:
71+
72+
```java
73+
int mask = 1 << 3;
74+
```
75+
76+
For example,
77+
78+
```
79+
00100000 | mask = 00101000
80+
```
81+
82+
#### 2. Set the first 3 bits from right:
83+
84+
```java
85+
int mask = (1 << 4) - 1;
86+
```
87+

Diff for: src/main/java/com/rampatra/blockchain/MessageHandler.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ public void run() {
3333
while ((message = (Message) in.readObject()) != null) {
3434
handleMessage(message);
3535
}
36-
in.close();
37-
out.close();
3836
} catch (Exception e) {
3937
if (!(e instanceof SocketException)) {
4038
throw new RuntimeException(e);
4139
}
40+
} finally {
41+
try {
42+
in.close();
43+
out.close();
44+
} catch (Exception e) {
45+
// do nothing
46+
}
4247
}
4348
}
4449

0 commit comments

Comments
 (0)