Last active
November 2, 2020 07:20
-
-
Save drem-darios/3fef3adf4e70ee044d0831f4d8c0875f to your computer and use it in GitHub Desktop.
Implement a function to return top rated movies in the network of movies reachable from the current movie.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.stream.Collectors; | |
public class MovieNetwork { | |
public static void main(String[] args) { | |
Movie A = new Movie(0, 11.2f); | |
Movie B = new Movie(1, 2.4f); | |
Movie C = new Movie(2, 3.6f); | |
Movie D = new Movie(3, 4.8f); | |
B.addSimilarMovie(D); | |
C.addSimilarMovie(D); | |
A.addSimilarMovie(B); | |
A.addSimilarMovie(C); | |
List<Movie> movies = Movie.getMovieRecommendations(A, 2); | |
System.out.println(movies.size()); | |
movies.stream().forEach(System.out::println); | |
} | |
} | |
class Movie { | |
private final int movieId; | |
private final float rating; | |
private List<Movie> similarMovies; // Similarity is bi directional | |
public Movie (int movieId, float rating) { | |
this.movieId = movieId; | |
this.rating = rating; | |
similarMovies = new ArrayList<Movie>(); | |
} | |
public int getId() { | |
return movieId; | |
} | |
public float getRating() { | |
return rating; | |
} | |
public void addSimilarMovie(Movie movie) { | |
similarMovies.add(movie); | |
movie.similarMovies.add(this); | |
} | |
public List<Movie> getSimilarMovies() { | |
return similarMovies; | |
} | |
@Override | |
public String toString() { | |
return "Movie id: "+Integer.toString(movieId) + " Movie Rating: " + Float.toString(rating); | |
} | |
public static List<Movie> getMovieRecommendations(Movie movie, int numTopRatedSimilarMovies) { | |
Set<Movie> seen = new HashSet<Movie>(); | |
Queue<Movie> movieQ = new LinkedList<Movie>(); | |
movieQ.add(movie); | |
seen.add(movie); | |
while (!movieQ.isEmpty()) { | |
Movie nextMovie = movieQ.remove(); | |
for (Movie m: nextMovie.getSimilarMovies()) { | |
if (!seen.contains(m)) { | |
seen.add(m); | |
movieQ.add(m); | |
} | |
} | |
} | |
if (seen.size() <= numTopRatedSimilarMovies) { | |
return new ArrayList<Movie>(seen); // Return full list since this is the max we can return | |
} else { | |
// Create max heap with seen movies | |
// PriorityQueue<Movie> heap = createMaxHeap(seen); | |
// List<Movie> result = new ArrayList<Movie>(); | |
// for (int i = 0; i < numTopRatedSimilarMovies; i++) { | |
// result.add(heap.poll()); | |
// } | |
// return result; | |
return seen.stream() | |
.sorted((movie1, movie2) -> Float.compare(movie2.rating, movie1.rating)) | |
.filter(movie1 -> movie1.movieId != movie.movieId) | |
.limit(numTopRatedSimilarMovies) | |
.collect(Collectors.toList()); | |
} | |
} | |
private static PriorityQueue<Movie> createMaxHeap(Set<Movie> movies) { | |
// Comparator<Movie> movieComparator = new Comparator<Movie>() { | |
// @Override | |
// public int compare(Movie movie1, Movie movie2) { | |
// return new Float(movie2.rating).compareTo(new Float(movie1.rating)); | |
// } | |
// }; | |
PriorityQueue<Movie> queue = new PriorityQueue<Movie>(movies.size(), (movie1, movie2) -> Float.compare(movie2.rating, movie1.rating)); | |
for (Movie m: movies) { | |
queue.add(m); | |
} | |
return queue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment