Skip to content

Commit

Permalink
BreadthFirstSearchIterative
Browse files Browse the repository at this point in the history
  • Loading branch information
kalpak92 committed Feb 12, 2021
1 parent f19332c commit 4618e4d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ This repository contains the solutions to problems done from various resources f
- [x] [Graph Adjacency List Representation](https://github.com/kalpak92/TechInterview2020/blob/master/src/Graph/AdjacencyListRepresentation.java)
- [x] [Depth First Search Recursive](https://github.com/kalpak92/TechInterview2020/blob/master/src/Graph/DepthFirstSearchRecursive.java)
- [x] [Depth First Search Iterative](https://github.com/kalpak92/TechInterview2020/blob/master/src/Graph/DepthFirstSearchIterative.java)
- [x] [Breadth First Search Recursive]()
- [x] [Breadth First Search Recursive](https://github.com/kalpak92/TechInterview2020/blob/master/src/Graph/BreadthFirstSearchRecursive.java)
- [x] [Breadth First Search Iterative]()
- [x] [Count the number of connected components](https://github.com/kalpak92/TechInterview2020/blob/master/src/Graph/ConnectedComponentsDFS.java)
- [x] [Number of connected components in an Undirected Graph - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/ConnectedComponentsUndirectedGraph.java)
- [x] [Graph Valid Tree - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/GraphValidTree.java)
Expand Down
68 changes: 68 additions & 0 deletions src/Graph/BreadthFirstSearchIterative.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package Graph;

import java.util.*;

public class BreadthFirstSearchIterative {
// Helper method to setup graph
private static void addUndirectedEdge(List<List<Integer>> graph, int from, int to) {
graph.get(from).add(to);
graph.get(to).add(from);
}

public static void bfsIterative(List<List<Integer>> graph, Deque<Integer> queue, boolean[] isVisited, int[] distance) {
while(!queue.isEmpty()) {
int currentNode = queue.poll();

// This node is already visited.
if (isVisited[currentNode])
continue;

// Visit this node.
isVisited[currentNode] = true;

// Add all neighbors to queue.
List<Integer> neighbors = graph.get(currentNode);

if (neighbors != null) {
for (int next : neighbors) {
if (!isVisited[next]) {
queue.add(next);
distance[next] = distance[currentNode] + 1;
}
}
}
}

System.out.println(Arrays.toString(distance));
}

public static void main(String[] args) {
int n = 14;
List<List<Integer>> graph = new ArrayList<>();

for (int i = 0; i < n; i++)
graph.add(new ArrayList<>());

addUndirectedEdge(graph, 0, 1);
addUndirectedEdge(graph, 0, 2);
addUndirectedEdge(graph, 0, 3);
addUndirectedEdge(graph, 2, 9);
addUndirectedEdge(graph, 8, 2);
addUndirectedEdge(graph, 3, 4);
addUndirectedEdge(graph, 10, 11);
addUndirectedEdge(graph, 12, 13);
addUndirectedEdge(graph, 3, 5);
addUndirectedEdge(graph, 5, 7);
addUndirectedEdge(graph, 5, 6);
addUndirectedEdge(graph, 0, 10);
addUndirectedEdge(graph, 11, 12);

Deque<Integer> queue = new LinkedList<>();
queue.offer(0);

int[] distance = new int[n];
distance[0] = 1;

bfsIterative(graph, queue, new boolean[n], distance);
}
}
1 change: 0 additions & 1 deletion src/Graph/BreadthFirstSearchRecursive.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package Graph;

import java.lang.reflect.Array;
import java.util.*;

public class BreadthFirstSearchRecursive {
Expand Down

0 comments on commit 4618e4d

Please sign in to comment.