-
Notifications
You must be signed in to change notification settings - Fork 277
/
ShortestPathVisitingAllNodes.java
106 lines (78 loc) · 2.89 KB
/
ShortestPathVisitingAllNodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class Solution {
public int shortestPathLength(int[][] graph) {
if(graph.length==1)
return 0;
int finalState = (1 << graph.length) - 1;
// {i, BitState}
Queue<int []> qu = new LinkedList<>();
//Adding all nodes initially because we can start anywhere.
for(int i=0; i<graph.length; i++) {
qu.add(new int [] {i, 1 << i});
}
//[no of many nodes][each node may have 2^n visited bit]
int [][] visitedMap = new int [graph.length][finalState+1];
int shortestPath = 0;
while(!qu.isEmpty()){
int size = qu.size();
shortestPath++;
for(int i=0; i<size; i++){
int [] head = qu.poll();
int nodeId = head[0];
int visitedNodeBitState = head[1];
for(int neighbor : graph[nodeId]){
int newVisitedNodeBitState = visitedNodeBitState | (1 << neighbor);
//If the same node was visited again with same visitedNodeBit, it means this node can be skipped, For example: 1->0->1->0. First 1 we have {1, 10}, then we have {0, 11}, then we will have {1, 11}. Lastly, we have {0, 11} which is a state we already had before. So we don't visit this again.
if(visitedMap[neighbor][newVisitedNodeBitState] == 1) continue;
visitedMap[neighbor][newVisitedNodeBitState] = 1; if(newVisitedNodeBitState==finalState)
return shortestPath;
qu.add(new int [] {neighbor, newVisitedNodeBitState});
}
}
}
return -1;
}
}
// @saorav21994
// TC : O(n*2^n)
// SC : O(n*2^n)
class Solution {
public int shortestPathLength(int[][] graph) {
if (graph.length == 1)
return 0;
int n = graph.length;
Set<Integer> [] visited = new HashSet[n];
Queue<Node> queue = new LinkedList<>();
int finalMask = (1 << n) - 1; // (2^n) - 1 -> all bit set
// Add all nodes to queue, as all of them can be starting nodes
for (int i = 0; i < n; i++) {
int mask = (1 << i);
queue.offer(new Node(i, 0, mask));
visited[i] = new HashSet<>();
visited[i].add(mask);
}
// BFS traversal
while (!queue.isEmpty()) {
Node curNode = queue.poll();
if (curNode.mask == finalMask)
return curNode.steps;
for (int neighbor : graph[curNode.nodeVal]) {
int newMask = curNode.mask | (1 << neighbor);
if (!visited[neighbor].contains(newMask)) {
visited[neighbor].add(newMask);
queue.offer(new Node(neighbor, curNode.steps+1, newMask));
}
}
}
return -1;
}
public class Node {
int nodeVal;
int steps;
int mask;
Node (int nodeVal, int steps, int mask) {
this.nodeVal = nodeVal;
this.steps = steps;
this.mask = mask;
}
}
}