Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Breadth First Search [Java] #190

Merged
merged 44 commits into from Jul 1, 2017
Merged

Conversation

jitendra3109
Copy link
Member

Fixes #73

By submitting this pull request I confirm I've read and complied with the below declarations.

  • I have read the Contribution guidelines and I am confident that my PR reflects them.
  • I have followed the coding guidelines for this project.
  • My code follows the skeleton code structure.
  • This pull request has a descriptive title. For example, Added {Algorithm/DS name} [{Language}], not Update README.md or Added new code.
  • This pull request will be closed if I fail to update it even once in a continuous timespan of 7 days.

README.md Outdated
@@ -11,6 +11,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa

| Algorithm | C | CPP | Java | Python | Golang |
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|
| [Breadth First Traversal](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [\[X\]](breadth_first_traversal/BreadthFirstTraversal.java) | | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entry will come after Binary Search

addEdge(2, 1);
addEdge(2, 3);
addEdge(3, 3);
System.out.println("Breadth First Traversal starting from vertex 0");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would this function be searching for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@singhpratyush not searching. It would give you the path of BFT from root 0 to all node.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But since we are searching for things, there should be a target node.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@singhpratyush Is that okay?

@jitendra3109
Copy link
Member Author

@aviaryan Please can check why travis-ci fail?

System.out.println("Breadth First Traversal starting from source to destination");
breadthFirstTraversal(1,3);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the output of this program?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gives the BFT path from source to destination.

@aviaryan
Copy link
Member

@jsroyal Build fixed now. It was a coala problem, nothing wrong with your code.

Copy link
Member

@singhpratyush singhpratyush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pleasse see comments.

@@ -0,0 +1,64 @@
import java.util.HashSet;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent file and class names.

while (i.hasNext()) {
int n = i.next();
if (n == destination) {
System.out.println(n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not print anything in algorithm function.

adj[source].add(w);
}

private static void breadthFirstTraversal(int source, int destination) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will other classes use this if it is private?

public class BreadthFirstSearch {

// Array of lists for Adjacency List Representation
public static LinkedList<Integer>[] adj;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bound to int? This can be generic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@singhpratyush Please, can you explain a little bit more?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are saying that the adjacency list may contain only integer items, while they can be anything. They can be names, addresses, tweets, a state of N-Queen board, etc. You just need to define that they are somehow linked.

So, instead of using Integer, you can go with generic objects.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
how should I go about? Can please write some code snippet?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use templates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can please write some code snippet? I'm not getting, using the template in linked list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -2,7 +2,7 @@

import java.util.*;

public class breadthFirstTraversal {
public class BreadthFirstTraversal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better change the names to BreadthFirstSearch instead of BreadthFirstTraversal.

Copy link
Member

@mohitkyadav mohitkyadav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comments

README.md Outdated
@@ -12,6 +12,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| Algorithm | C | CPP | Java | Python | Golang |
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [\[X\]](binary_search/binary_search.c) | | [\[X\]](binary_search/BinarySearch.java) | [\[X\]](binary_search/binary_search.py) | |
| [Breadth First Traversal](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [\[X\]](breadth_first_traversal/BreadthFirstTraversal.java) | | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsroyal Please take a look at space inconsistency in | | |.

Copy link
Member

@aviaryan aviaryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent using tabs. Related: Tabs vs Spaces - StackExchange

@jitendra3109
Copy link
Member Author

Hi! @aviaryan I think now indentation is consistant. I'm using IntelliJ IDE where the standard configuration of Java Coding Convention.

@aviaryan aviaryan dismissed their stale review June 8, 2017 03:25

Okay. Let's go with spaces for now.

addEdge(2, 3);
addEdge(3, 3);
System.out.println("Breadth First Search starting from source to destination");
ArrayList arrayList = breadthFirstSearch(1,3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if you can make this 1, 3.

Copy link
Member

@singhpratyush singhpratyush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#190 (comment) is still not solved.

You may refer to this implementation if you need something.

@singhpratyush
Copy link
Member

@jsroyal: You have been inactive for 3 days now. Just as a reminder, this PR will be closed if you stay inactive for 7 days.

@iiitv iiitv deleted a comment Jun 11, 2017
@iiitv iiitv deleted a comment Jun 11, 2017
README.md Outdated
@@ -14,6 +14,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|
| [Bin Sort](http://www.cdn.geeksforgeeks.org/bucket-sort-2/)| | |[\[X\]](bin_sort/BinSort.java)| | |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [\[X\]](binary_search/binary_search.c) | | [\[X\]](binary_search/BinarySearch.java) | [\[X\]](binary_search/binary_search.py) | |
| [Breadth First Traversal](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [\[X\]](breadth_first_traversal/BreadthFirstTraversal.java) | | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to Breadth First Search.

@aviaryan
Copy link
Member

@jsroyal Please fix the build.

breadth_first_search/BreadthFirstSearch.java
    [10:01] ❌️ unexpected trailing whitespace (EditorConfig trim_trailing_whitespace https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#trim_trailing_whitespace)
        10|
    d1e28750 (Jitendra Singh <jsroyal55@gmail.com> 6/22/2017, 7:00:11 AM)
    [25:01] ❌️ unexpected trailing whitespace (EditorConfig trim_trailing_whitespace https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#trim_trailing_whitespace)
        25|
    f782a94a (Jitendra Singh <jsroyal55@gmail.com> 6/22/2017, 6:59:00 AM)
�```

@jitendra3109
Copy link
Member Author

@aviaryan trim_trailing_whitespace I do not get it. Please mention .

public class BreadthFirstSearch<T> {
// HasMap of lists for Adjacency List Representation
public HashMap<T, ArrayList<T>> adj = new HashMap<T, ArrayList<T>> ();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsroyal Sir, It is about removing the unnecessary whitespaces from blank lines 10 and 25.

Copy link
Member

@aviaryan aviaryan Jun 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsroyal You have to remove whitespace after line 10 and 25 here.

the line has trailing whitespace             
this line does not
this line has trailing whitespace       

⬆️ Select the contents of block above to understand.

public HashMap<T, ArrayList<T>> adj = new HashMap<T, ArrayList<T>> ();
//Function to add an edge
public void addEdges (T source, T destination) {
if (adj.containsKey (source)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove space between function name and parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is at a lot of places.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is too much 🔢 70

README.md Outdated
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [:white_check_mark:](binary_search/binary_search.c) | | [:white_check_mark:](binary_search/BinarySearch.java) | [:white_check_mark:](binary_search/binary_search.py) | [:white_check_mark:](binary_search/binary_search.go) | [:white_check_mark:](binary_search/binarySearch.js) |
| [Coin Change Problem](http://www.algorithmist.com/index.php/Coin_Change) | [:white_check_mark:](coin_change_problem/coin_change_problem.c) | | [:white_check_mark:](coin_change_problem/CoinChange.java) | [:white_check_mark:](coin_change_problem/coin_change_problem.py) | | [:white_check_mark:](coin_change_problem/coinChangeProblem.js) |
| [Counting Sort](http://www.geeksforgeeks.org/counting-sort/)| [:white_check_mark:](counting_sort/counting_sort.c) | | [:white_check_mark:](counting_sort/CountingSort.java) | | [:white_check_mark:](counting_sort/counting_sort.go) | |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [:white_check_mark:](binary_search/binary_search.c) | | [:white_check_mark:](binary_search/BinarySearch.java) | [:white_check_mark:](binary_search/binary_search.py) | [:white_check_mark:](binary_search/binary_search.go) | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good now. Please fix this diff.

for (int i = 0; i < temp.size(); i++) {
if (! visited.contains(temp.get(i))) {
bfsPath.add(temp.get(i));
if (temp.get(i) == destination) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use .equals() for comparison here as == compares object references rather than what is needed to be compared.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

@Monal5031
Copy link
Member

@jsroyal sir update README its showing some unnecessary diffs.

@aviaryan aviaryan merged commit 800f1a8 into iiitv:master Jul 1, 2017
@aviaryan
Copy link
Member

aviaryan commented Jul 1, 2017

@jsroyal Thanks for this PR. It might have taken a long time but this was some quality code. Looking forward to more contributions from you.

bfsPath.add(source);
// mark as visited
visited.add(source);
int flag = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only have to deal with 0 and 1 , we can use boolean here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Breadth First Search in a Graph [Java]
6 participants