Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f779934

Browse files
Simplified BFS
1 parent ee23538 commit f779934

File tree

7 files changed

+169
-207
lines changed

7 files changed

+169
-207
lines changed

‎Graphs/.idea/misc.xml

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/.idea/vcs.xml

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/.idea/workspace.xml

Lines changed: 69 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Graphs/out/production/Graphs/BFS.class

-412 Bytes
Binary file not shown.

‎Graphs/src/BFS.java

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,56 @@
1-
import java.util.Iterator;
2-
import java.util.LinkedList;
3-
import java.util.List;
4-
import java.util.Scanner;
1+
import java.util.*;
52

6-
public class BFS {
3+
class BFS{
4+
private int v;
5+
private List<Integer> graph[];
76

8-
private int V;
9-
private List<Integer>[] graph;
10-
public int result[];
11-
12-
public BFS(int v) {
13-
V = v;
14-
graph = new LinkedList[V];
15-
for (int i = 0; i < graph.length; i++) {
7+
public BFS(int v){
8+
this.v = v;
9+
this.graph = new List[v];
10+
for(int i=0; i<v; i++){
1611
graph[i] = new LinkedList<>();
1712
}
1813
}
1914

20-
public void addEdge(int u, int v){
21-
graph[u].add(0, v);
15+
public void addEdge(int u, int v){
16+
graph[u].add(v);
2217
}
2318

24-
public void BFSsearch(int s) {
25-
boolean visited[] = new boolean[V];
26-
int count = 0;
27-
result = new int[V];
28-
LinkedList<Integer> queue = new LinkedList<Integer>();
29-
visited[s] = true;
30-
queue.add(s);
19+
public void BFS(int s){
20+
Boolean visited[] = new Boolean[v];
21+
for(int i=0; i<visited.length; i++){
22+
visited[i] = false;
23+
}
3124

32-
while (queue.size() != 0) {
33-
count++;
34-
s = queue.poll();
25+
LinkedList<Integer> queue = new LinkedList<>();
26+
queue.add(s);
3527

36-
Iterator<Integer> i = graph[s].listIterator();
37-
while (i.hasNext()) {
38-
int n = i.next();
39-
if (!visited[n]) {
40-
visited[n] = true;
41-
result[n] = 6 * count;
42-
queue.add(n);
28+
while(queue.size() != 0){
29+
int n = queue.poll();
30+
visited[n] = true;
31+
System.out.println(n + " ");
32+
ListIterator<Integer> iterator = graph[n].listIterator();
33+
34+
while(iterator.hasNext()){
35+
int next = iterator.next();
36+
if(!visited[next]){
37+
visited[next] = true;
38+
queue.add(next);
4339
}
4440
}
4541
}
4642
}
4743

48-
public static void main(String[] args) {
49-
Scanner in = new Scanner(System.in);
50-
int t = in.nextInt();
51-
for (int q = 0; q < t; q++) {
52-
int n = in.nextInt();
44+
public static void main(String[] args){
45+
BFS obj = new BFS(4);
5346

54-
int m = in.nextInt();
55-
BFS obj = new BFS(n);
56-
for (int i = 0; i < m; i++) {
57-
obj.addEdge(in.nextInt() - 1, in.nextInt() - 1);
58-
}
59-
int s = in.nextInt();
60-
obj.BFSsearch(s - 1);
47+
obj.addEdge(0, 1);
48+
obj.addEdge(0, 2);
49+
obj.addEdge(1, 2);
50+
obj.addEdge(2, 0);
51+
obj.addEdge(2, 3);
52+
obj.addEdge(3, 3);
6153

62-
for (int i = 0; i < obj.result.length; i++) {
63-
if (i == s - 1)
64-
continue;
65-
if (obj.result[i] == 0)
66-
System.out.print(-1 + " ");
67-
else
68-
System.out.print(obj.result[i] + " ");
69-
}
70-
}
54+
obj.BFS(2);
7155
}
7256
}

‎MaxHeaps/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /