FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add BFS · thai321/Algorithm-Problems-Java@8ec6930 · GitHub

Commit 8ec6930

Browse files
committed
Add BFS
1 parent 0f423db commit 8ec6930

4 files changed

Lines changed: 244 additions & 0 deletions

File tree

‎Breadth First Search/BFS.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class BFS {
5+
6+
public void bfs(Vertex root) {
7+
Queue<Vertex> queue = new LinkedList<>();
8+
root.setVisited(true);
9+
queue.add(root);
10+
11+
while( !queue.isEmpty() ) {
12+
13+
14+
Vertex actualVertex = queue.remove();
15+
System.out.println(actualVertex + " ");
16+
17+
for(Vertex v : actualVertex.getNeighbourList())
18+
if (!v.isVisited()) {
19+
v.setVisited(true);
20+
queue.add(v);
21+
}
22+
}
23+
}
24+
25+
public static void main(String[] args) {
26+
BFS f = new BFS();
27+
Vertex vertex1 = new Vertex(1);
28+
Vertex vertex2 = new Vertex(2);
29+
Vertex vertex3 = new Vertex(3);
30+
Vertex vertex4 = new Vertex(4);
31+
Vertex vertex5 = new Vertex(5);
32+
33+
vertex1.addNeighbourList(vertex2);
34+
vertex1.addNeighbourList(vertex4);
35+
vertex4.addNeighbourList(vertex5);
36+
vertex2.addNeighbourList(vertex3);
37+
38+
f.bfs(vertex1);
39+
}
40+
}

‎Breadth First Search/README.md‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Breadth First Search (BFS)
2+
- We have a graph and we want to visit every node -> we can do it with BFS
3+
- We visit every vertex exactly once
4+
- We visit the neighbours then the neighbours of these new vertices and so on
5+
- Running time complexity: <b>O(V+E)</b>
6+
- Memory complexity is not good: we have to store lots of references
7+
- That's why DFS is ususally preferred
8+
- BUT it constructs a shortest path: Dijkstra algorithm does a BFS if all the edge weights are equal to 1
9+
10+
### Iteration
11+
- We have an empty queue at the beginning and we keep checking whether we have visited the given node or not
12+
- Keep iterating until queue is not empty
13+
```ruby
14+
def bfs(vertex)
15+
Queue queue
16+
vertex set visited true
17+
queue.enqueue(vertex)
18+
19+
while queue not empty
20+
actual = quque.dequeue()
21+
22+
for v in actual neighbors
23+
if v is not visited
24+
v set visited true
25+
queue.enqueue(v)
26+
```
27+
28+
<img src='https://g.gravizo.com/g?
29+
graph G {
30+
A -- B; A -- F; A -- G;
31+
B -- C; B -- D;
32+
D -- E;
33+
G -- H;
34+
}
35+
'/>
36+
> Queue:{A}
37+
38+
> Queue: {A}; eplore A --> Queue: {G F B}
39+
40+
> Queue: {G F B}; explore B --> Queue: {D C G F}
41+
42+
> Queue: {D C G F}; explore F --> Queue: {D C G} ; F is leaf
43+
44+
> Queue: {D C G}; explore G --> Queue: {H D C}
45+
46+
> Queue: {H D C}; explore C --> Queue: {H D}; C is leaf
47+
48+
> Queue: {H D}; explore D --> Queue: {E H};
49+
50+
> Queue: {E H}; explore H --> Queue: {E}; H is leaf
51+
52+
> Queue: {E}; explore E --> Queue: {}; H is leaf
53+
54+
> Queue: {} is empty -> <b>FINFISHED !!!</b>
55+
56+
> <b>Visited order: A B F G C D H E</b>
57+
58+
### <u>Application</u>
59+
- In Artificial intelligence / machine learning it can prove to be very important: robots can sicover the surrounding mor easily with BFS than DFS
60+
- It is also very important in maximum flow: Edmonds-Karp algorithm uses BFS for finding augmenting paths
61+
- Cheyen's algorithm in garbage collection -> it help to maintain active references on the heap memory
62+
- It uses BFS to detect all the references on the heap
63+
- Serialization/ deserialization of a tree like structure (for example when order does matter) -> it allows the tree to be reconstructeed in an efficient manner !!!
64+
65+
<img src='https://g.gravizo.com/svg?
66+
graph G {
67+
1 -- 2; 1 -- 4;
68+
2 -- 3;
69+
4 -- 5;
70+
}
71+
'/>
72+
Visited Order: 1 2 4 3 5
73+
74+
```java
75+
import java.util.ArrayList;
76+
import java.util.List;
77+
78+
public class Vertex {
79+
private int data;
80+
private boolean visited;
81+
private List<Vertex> neighbourList;
82+
83+
public Vertex(int data) {
84+
this.data = data;
85+
this.neighbourList = new ArrayList<>();
86+
}
87+
88+
public int getData() { return data; }
89+
90+
public void setData(int data) { this.data = data; }
91+
92+
public boolean isVisited() { return visited; }
93+
94+
public void setVisited(boolean visited) { this.visited = visited; }
95+
96+
public List<Vertex> getNeighbourList() { return neighbourList; }
97+
98+
public void setNeighbourList(List<Vertex> neighbourList) { this.neighbourList = neighbourList; }
99+
100+
public void addNeighbourList(Vertex vertex) { this.neighbourList.add(vertex); }
101+
102+
@Override
103+
public String toString() { return "" + this.data; }
104+
}
105+
```
106+
107+
```java
108+
import java.util.LinkedList;
109+
import java.util.Queue;
110+
111+
public class BFS {
112+
113+
public void bfs(Vertex root) {
114+
Queue<Vertex> queue = new LinkedList<>();
115+
root.setVisited(true);
116+
queue.add(root);
117+
118+
while( !queue.isEmpty() ) {
119+
120+
121+
Vertex actualVertex = queue.remove();
122+
System.out.println(actualVertex + " ");
123+
124+
for(Vertex v : actualVertex.getNeighbourList())
125+
if (!v.isVisited()) {
126+
v.setVisited(true);
127+
queue.add(v);
128+
}
129+
}
130+
}
131+
132+
public static void main(String[] args) {
133+
BFS f = new BFS();
134+
Vertex vertex1 = new Vertex(1);
135+
Vertex vertex2 = new Vertex(2);
136+
Vertex vertex3 = new Vertex(3);
137+
Vertex vertex4 = new Vertex(4);
138+
Vertex vertex5 = new Vertex(5);
139+
140+
vertex1.addNeighbourList(vertex2);
141+
vertex1.addNeighbourList(vertex4);
142+
vertex4.addNeighbourList(vertex5);
143+
vertex2.addNeighbourList(vertex3);
144+
145+
f.bfs(vertex1); // visited order: 1 2 4 3 5
146+
}
147+
}
148+
```

‎Breadth First Search/Vertex.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Vertex {
5+
private int data;
6+
private boolean visited;
7+
private List<Vertex> neighbourList;
8+
9+
public Vertex(int data) {
10+
this.data = data;
11+
this.neighbourList = new ArrayList<>();
12+
}
13+
14+
public int getData() { return data; }
15+
16+
public void setData(int data) { this.data = data; }
17+
18+
public boolean isVisited() { return visited; }
19+
20+
public void setVisited(boolean visited) { this.visited = visited; }
21+
22+
public List<Vertex> getNeighbourList() { return neighbourList; }
23+
24+
public void setNeighbourList(List<Vertex> neighbourList) { this.neighbourList = neighbourList; }
25+
26+
public void addNeighbourList(Vertex vertex) { this.neighbourList.add(vertex); }
27+
28+
@Override
29+
public String toString() { return "" + this.data; }
30+
}

‎README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
# Algorithm Problems Java
2+
## Recursion
3+
## Backtracking
4+
## Selection
5+
## Dynamic Programming
6+
7+
## Graph
8+
- Graphs <b>G(V,E)</b> are mathematical structures to model pairwise relations between given objects
9+
- A graph is made up of vertices/nodes and edges
10+
- There are two types of graphs: <b>directed and undirected graphs</b>
11+
- We know wht are graphs
12+
- First of all how to model them in programming languages?
13+
1. Adjacency matrix
14+
- We have an <A> matrix constructed out of the vertices of the graph:
15+
- the <b>A(i,j)</b> value in the matrix is <b>1<b/> if there is a connection between node <b>i</b> and node <b>j</b>
16+
- Otherwise <b>A(i,j)</b> is <b>0</b>
17+
2. Edge list representation
18+
- We create a Vertix class
19+
- it stores the neighbors accordingly
20+
```java
21+
class Vertex
22+
vertex Name:
23+
visited;
24+
Vertex[] neighbors;
25+
```
26+
27+
## Breadth First Search

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL