Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import java.util.Set;

public class Person {
public class Person
{

private String name;
private int age;
private Set<Person> confidants;

public Person(String name, int age, Set<Person> confidants) {
public Person(String name, int age, Set<Person> confidants)
{
this.name = name;
this.age = age;
this.confidants = confidants;
}

public String getName() {
public String getName()
{
return name;
}

public int getAge() {
public int getAge()
{
return age;
}

public Set<Person> getConfidants() {
public Set<Person> getConfidants()
{
return confidants;
}
}
79 changes: 79 additions & 0 deletions src/Traverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,84 @@ public static void main(String[] args) {
v45.neighbors = new ArrayList<>(List.of(v23));
v23.neighbors = new ArrayList<>(List.of());
v67.neighbors = new ArrayList<>(List.of(v91));

//call the method and run with passed in
// printGossipers(grace);

//call reachable
System.out.println(reachable(graph, 3, 45));


}


public static boolean reachable(Map<Integer, Set<Integer>> graph, int start, int end)
{
Set<Integer> visited = new HashSet<>();
return reachable(graph, start, end, visited);
}

public static boolean reachable(Map<Integer, Set<Integer>> graph, int start, int end, Set<Integer> visited)
{
//if start equals end then return true code ends
//basecase returns true
if(start == end) return true;

//if starts not even in the list, graph cannot be null, grapsh needs to contain start already, the key start cant be null
//if visited contains start then we already looped through it, never reached the end, we want to reach the end
if(visited.contains(start) ||
//if grapsh is null its donzo
graph == null ||
//if graph doesnt contain start cant even start
!graph.containsKey(start) ||
//if you have to get start then its bad, graph should already have start
graph.get(start) == null)
//breaks
return false;


visited.add(start);

//it will either be true or false
for(int neighbor : graph.get(start))
{
//hint: can we keep going or stop there. on the final.
//evaluates true or false
//if any path returns true...end. If none goes through and return true then return false
if(reachable(graph, neighbor, end, visited)) return true;
}

return false;

}


public static void printGossipers(Person person)
{
//visited tracker
//called person
Set<Person> visited = new HashSet<>();
printGossipers(person, visited);


}

//helper method
public static void printGossipers(Person person, Set<Person> visited)
{
//person or visited contains person then return
if(person == null || visited.contains(person)) return;
//verified not in list so when visited put person in the list with .add
visited.add(person);
//then print the person and name associated with the person, in this case will render whoever they are linked to in rumors gossips
System.out.println(person.getName());

//have to get it since it is private
for(Person confidant : person.getConfidants())
{

printGossipers(confidant, visited);
}

}
}