Hey everyone,

I really need help in solving a problem that I have to use recursion for.

I have a list of paper objects. Each paper object has its own author(s) objects.

Each author object knows which paper(s) objects they belong to.

I have to build a scholarly neighborhood for these authors.

The user can select an author and specify the "width" of the neighborhood that they wish to view.

The width of this neighborhood goes as follows:
If they enter "0" the neighborhood will consist of only the author that they selected.
If they enter "1" the neighborhood will consist of the author that they selected plus any additional authors that are direct co-authors in the paper(s) the author wrote.
If they enter "2" the neighborhood will consist of the author, the co-authors of that author in each paper, and the co-authors of the co-authors in the paper(s) that the co-authors wrote.

Kind of like this:

``````Author                                              0
Author Co authors                                   1
Author Co authors authors                           2
Author Co authors authors co authors                3
Author Co authors authors co authors authors        4
``````

Make sense yet? It makes a diagram kind of like a family tree.

Here is the code I have so far:

``````    JOptionPane.showMessageDialog(null, model.ScholarNeighborhoodList(view.getJlScholars().getSelectedIndex(), (int)view.getJspnrWidth().getValue(), scholarNeighborhood));

public String ScholarNeighborhoodList(int selectedIndex, int selectedWidth, String scholarNeighborhood) {

if (selectedWidth == 0) {
return scholarNeighborhood; // Base case
} else {
return scholarNeighborhood + ScholarNeighborhoodList(selectedIndex, selectedWidth-1, scholarNeighborhood);
}
}
``````

Please let me know if I need to post more information.

Thank you for taking the time to read this!

:)

hey there,
your base case is more than selectedWith == 0. Like what if selectedWith is not zero, but your current author has no co authors? Then, why are you passing around scholarNeighborhood? Sounds like scholarNeighborhood will hold the result. You can certainly pass it around and be within the definition of a recursive method, but that is for the iterative form of recursion not the linear form (ignore this, unless you already know the difference). what you need is the the linear form recursion, I believe. for that, you need to remove the third input param in your method.
from my understanding, you need to fix your base case, remove your third parameter, and make sure you add ALL co authors of the author that selectedWidth allows for, and if selectedWith is not zero, add all coauthors of all coauthors... I hope you can make sense of what I am saying.

This looks like a case of premature coding!
Try writing the algorithm in simple pseudo-code, or even plain English. Until you have a clear statement of the algorithm there's no way you will write a valid program to implement it. People here will help with your pseudo-code, of course.

I've updated my code (I'm the same guy just a different account).

``````    public String ScholarNeighborhoodList(Scholar scholar, int selectedWidth, String scholarNeighborhood) {
if (selectedWidth == 0) {
return scholar.toString() + "\n";
} else {
for (int i = 0; i < scholar.getScholarlyPubCollection().size(); i++) {
if (i > 0) scholarNeighborhood += "\n";
for (int j = 0; j < scholar.getScholarlyPubCollection().get(i).getScholars().size(); j++) {
scholarNeighborhood += ScholarNeighborhoodList(scholar.getScholarlyPubCollection().get(i).getScholars().get(j), selectedWidth-1, scholarNeighborhood);
}
}

return scholarNeighborhood;
}
}
``````

It allows the user to select a width of a neighborhood they wish to view of a scholar. They give the input and the method tests if the width is equal to 0, and if it is, it just returns the scholar. If it's say 1, then it will go through each of that scholar's papers and get each co-author of that paper through recursive calls. If the width is 2, it will do the same, but will go deeper, retrieving all of the co-authors' authors, and so forth for greater values.

I think it works for the most part.

Sheffield Dillon is an author of 2 papers, that each contain 2 authors. So it makes sense to display Sheffield Dillon and his co-author of each paper he contributed to.

When the width is 2, it should display the selected author, his co-authors of his papers, and the co-authors' authors of each of their papers.

Does this seem right at all? Thanks.

Thoughts:
Your outpout doesn't show any papers bu the co-authors, but maybe they come lower down in the list?
There seems a (small) problem with duplicate entries being displayed?
You could use the width parameter to indent co-author's papers more than the main author's, whuich would make the output clearer?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.