Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c9925a

Browse files
authored
BFS Java implementation (#843)
* BFS Java implementation * Path Finding implementation
1 parent 3d466dc commit 1c9925a

File tree

1 file changed

+84
-41
lines changed

1 file changed

+84
-41
lines changed

src/graph/breadth-first-search.md

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,93 @@ You can also calculate the lengths of the shortest paths (which just requires ma
2626

2727
## Implementation
2828

29-
We write code for the described algorithm in C++.
30-
31-
```cpp
32-
vector<vector<int>> adj; // adjacency list representation
33-
int n; // number of nodes
34-
int s; // source vertex
35-
36-
queue<int> q;
37-
vector<bool> used(n);
38-
vector<int> d(n), p(n);
39-
40-
q.push(s);
41-
used[s] = true;
42-
p[s] = -1;
43-
while (!q.empty()) {
44-
int v = q.front();
45-
q.pop();
46-
for (int u : adj[v]) {
47-
if (!used[u]) {
48-
used[u] = true;
49-
q.push(u);
50-
d[u] = d[v] + 1;
51-
p[u] = v;
29+
We write code for the described algorithm in C++ and Java.
30+
31+
=== "C++"
32+
```cpp
33+
vector<vector<int>> adj; // adjacency list representation
34+
int n; // number of nodes
35+
int s; // source vertex
36+
37+
queue<int> q;
38+
vector<bool> used(n);
39+
vector<int> d(n), p(n);
40+
41+
q.push(s);
42+
used[s] = true;
43+
p[s] = -1;
44+
while (!q.empty()) {
45+
int v = q.front();
46+
q.pop();
47+
for (int u : adj[v]) {
48+
if (!used[u]) {
49+
used[u] = true;
50+
q.push(u);
51+
d[u] = d[v] + 1;
52+
p[u] = v;
53+
}
5254
}
5355
}
54-
}
55-
```
56-
56+
```
57+
=== "Java"
58+
```java
59+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); // adjacency list representation
60+
61+
int n; // number of nodes
62+
int s; // source vertex
63+
64+
65+
LinkedList<Integer> q = new LinkedList<Integer>();
66+
boolean used[] = new boolean[n];
67+
int d[] = new int[n];
68+
int p[] = new int[n];
69+
70+
q.push(s);
71+
used[s] = true;
72+
p[s] = -1;
73+
while (!q.isEmpty()) {
74+
int v = q.pop();
75+
for (int u : adj.get(v)) {
76+
if (!used[u]) {
77+
used[u] = true;
78+
q.push(u);
79+
d[u] = d[v] + 1;
80+
p[u] = v;
81+
}
82+
}
83+
}
84+
```
85+
5786
If we have to restore and display the shortest path from the source to some vertex $u$, it can be done in the following manner:
58-
59-
```cpp
60-
if (!used[u]) {
61-
cout << "No path!";
62-
} else {
63-
vector<int> path;
64-
for (int v = u; v != -1; v = p[v])
65-
path.push_back(v);
66-
reverse(path.begin(), path.end());
67-
cout << "Path: ";
68-
for (int v : path)
69-
cout << v << " ";
70-
}
71-
```
72-
87+
88+
=== "C++"
89+
```cpp
90+
if (!used[u]) {
91+
cout << "No path!";
92+
} else {
93+
vector<int> path;
94+
for (int v = u; v != -1; v = p[v])
95+
path.push_back(v);
96+
reverse(path.begin(), path.end());
97+
cout << "Path: ";
98+
for (int v : path)
99+
cout << v << " ";
100+
}
101+
```
102+
=== "Java"
103+
```java
104+
if (!used[u]) {
105+
System.out.println("No path!");
106+
} else {
107+
ArrayList<Integer> path = new ArrayList<Integer>();
108+
for (int v = u; v != -1; v = p[v])
109+
path.add(v);
110+
Collections.reverse(path);
111+
for(int v : path)
112+
System.out.println(v);
113+
}
114+
```
115+
73116
## Applications of BFS
74117

75118
* Find the shortest path from a source to other vertices in an unweighted graph.

0 commit comments

Comments
 (0)