-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.py
770 lines (665 loc) · 36.1 KB
/
Graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
import heapq
import networkx as nx
import pandas as pd
import plotly
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import json
import pickle
import os
import ast
class Graph:
def __init__(self, movies_file, credits_file):
self.movies_file = movies_file
self.credits_file = credits_file
self.movies_df = None
self.credits_df = None
self.graph = nx.Graph()
self.options = []
def read_data(self, filename="processed_data.pkl"):
"""
Reads and processes the movie and credits data. If a processed file exists, load it to save time.
"""
# Check if the preprocessed file exists
if os.path.exists(filename):
print(f"Loading preprocessed data from {filename}...")
self.movies_df = pd.read_pickle(filename)
return
# Process data if no preprocessed file exists
print("Preprocessed file not found. Reading and processing raw data...")
self.movies_df = pd.read_csv(self.movies_file, low_memory=False)
self.credits_df = pd.read_csv(self.credits_file, low_memory=False)
self.movies_df['id'] = self.movies_df['id'].astype(str)
self.credits_df['id'] = self.credits_df['id'].astype(str)
self.credits_df['cast'] = self.credits_df['cast'].apply(ast.literal_eval)
self.credits_df['crew'] = self.credits_df['crew'].apply(ast.literal_eval)
# Merge movies and credits data
self.movies_df = self.movies_df.merge(self.credits_df, on='id', how='inner')
# Save the processed data for future use
print(f"Saving preprocessed data to {filename}...")
self.movies_df.to_pickle(filename)
def save_graph(self, filename="graph.pkl"):
"""
Save the built graph to a file.
"""
with open(filename, "wb") as f:
pickle.dump(self.graph, f)
print(f"Graph saved to {filename}")
def load_graph(self, filename="graph.pkl"):
"""
Load a graph from a file. Returns True if successful, False otherwise.
"""
try:
with open(filename, "rb") as f:
self.graph = pickle.load(f)
print(f"Graph loaded from {filename}")
return True
except FileNotFoundError:
print("No saved graph found. Building a new graph.")
return False
def build_graph(self, filename="graph.pkl"):
"""
Build a graph if no saved graph exists; otherwise, load the saved graph.
"""
# Try to load the graph
if self.load_graph(filename):
return # Graph was successfully loaded, no need to rebuild
# If no graph exists, build it
actor_to_movies = {}
# Create a mapping of actor to movies they appear in
for _, row in self.movies_df.iterrows():
movie_id = row['id']
cast = row['cast']
for actor in cast:
actor_name = actor['name']
if actor_name not in actor_to_movies:
actor_to_movies[actor_name] = []
actor_to_movies[actor_name].append(movie_id)
# Add edges between movies based on shared actors
for actor, movies in actor_to_movies.items():
for i, movie_a in enumerate(movies):
for movie_b in movies[i + 1:]:
if self.graph.has_edge(movie_a, movie_b):
self.graph[movie_a][movie_b]['weight'] += 1 # Increment weight
self.graph[movie_a][movie_b]['actors'].append(actor) # Add actor
else:
self.graph.add_edge(movie_a, movie_b, weight=1, actor=actor, actors=[actor]) # Initialize edge
# Save the graph after building
self.save_graph(filename)
# function to see if there are duplicate movies
def get_movie_list(self, movie_name):
if movie_name is None:
return json.dumps({'error': 'No movie name provided.'})
# Find matching movies
matching_movies = self.movies_df[self.movies_df['original_title'].str.lower() == movie_name.lower()]
if matching_movies.empty:
return json.dumps({'error': f"Movie '{movie_name}' not found."})
if len(matching_movies) > 1:
# Make a list of matching movies for user selection
movie_data = matching_movies[['original_title', 'id', 'release_date', 'cast']].to_dict(orient='records')
# Add processed release date and cast names to the dictionary
for movie in movie_data:
movie['release_date'] = movie['release_date'] if pd.notna(movie['release_date']) else 'Unknown Year'
for idx, row in matching_movies.iterrows():
cast_names = [actor['name'] for actor in row['cast'][:5]] # Get the top 5 actors' names
cast_str = ', '.join(cast_names)
# Find the movie in movie_data by matching IDs or another unique identifier
for movie in movie_data:
if movie['id'] == row['id']:
movie['cast'] = cast_str # Update the cast in the movie data
# return list of movie data including release date and cast
return movie_data
# If a single movie is found, return its ID
return matching_movies.iloc[0]['id']
def visualize_graph(self, movie_name=None, max_connections=15, dark_mode=False):
if movie_name is None:
return json.dumps({'error': 'No movie name provided.'})
# Find matching movies
matching_movies = self.movies_df[self.movies_df['original_title'].str.lower() == movie_name.lower()]
if matching_movies.empty:
return json.dumps({'error': f"Movie '{movie_name}' not found."})
# if there are duplicate movies gets the data for each movie from other function
if len(matching_movies) > 1:
return self.get_movie_list(movie_name)
# If a single movie is found, get its ID
movie_id = matching_movies.iloc[0]['id']
# returns original title from dataset so there's no capitalization errors
return self.visualize_graph_by_id(movie_id, matching_movies.iloc[0]['original_title'], max_connections, dark_mode=dark_mode)
def visualize_graph_by_id(self, movie_id=None, movie_title=None, max_connections=15, movie_id2=None,
movie_title2=None, max_hops=2, dark_mode = False):
print(dark_mode)
fig = make_subplots()
if movie_id is not None:
movie_id = str(movie_id)
if movie_id2 is not None:
movie_id2 = str(movie_id2)
# if only visualizing one movie
if movie_id is not None and movie_id2 is None:
# create a subgraph for the movie and its neighbors
subgraph = self.graph.subgraph([movie_id] + list(self.graph.neighbors(movie_id)))
# Limit the number of connections per node
# Get the neighbors and sort by the number of connections (edges)
neighbors = list(self.graph.neighbors(movie_id))
neighbors_sorted = sorted(neighbors, key=lambda x: len(list(self.graph.neighbors(x))), reverse=True)
# Only keep the top `max_connections` neighbors
limited_neighbors = neighbors_sorted[:max_connections]
# Create a subgraph with the limited number of neighbors
subgraph = self.graph.subgraph([movie_id] + limited_neighbors)
# if visualizing two movies
elif movie_id is not None and movie_id2 is not None:
neighbors1 = set(self.graph.neighbors(movie_id))
neighbors2 = set(self.graph.neighbors(movie_id2))
# Perform BFS to find all nodes within max_hops
def bfs(start_node, max_hops):
visited = set()
queue = [(start_node, 0)] # (node, current_hop)
reachable_nodes = set()
while queue and len(reachable_nodes) < max_connections:
current_node, current_hop = queue.pop(0)
if current_hop < max_hops and current_node not in visited:
visited.add(current_node)
reachable_nodes.add(current_node)
for neighbor in self.graph.neighbors(current_node):
if neighbor not in visited:
queue.append((neighbor, current_hop + 1))
return reachable_nodes
reachable_from_movie1 = bfs(movie_id, max_hops)
reachable_from_movie2 = bfs(movie_id2, max_hops)
# Combine reachable nodes and find shared neighbors
combined_reachable = reachable_from_movie1 | reachable_from_movie2
shared_neighbors = neighbors1 & neighbors2
# Limit the number of shared neighbors
if shared_neighbors:
shared_neighbors_sorted = sorted(shared_neighbors, key=lambda x: len(list(self.graph.neighbors(x))),
reverse=True)
limited_shared_neighbors = shared_neighbors_sorted[:max_connections]
else:
return json.dumps(
{'error': f"No shared actors found between movies '{movie_title}' and '{movie_title2}'."})
# Create a subgraph with the two movies and their reachable neighbors
subgraph = self.graph.subgraph([movie_id, movie_id2] + list(combined_reachable) + list(limited_shared_neighbors))
else:
return json.dumps({'error': f"Movie '{movie_title}' not found."})
# Layout of the graph
pos = nx.spring_layout(subgraph, scale=None)
# Create edges for the plot, using the edge weights
edge_traces = []
for edge in subgraph.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
weight = subgraph[edge[0]][edge[1]]['weight']
edge_trace = go.Scatter(
x=[x0, x1, None],
y=[y0, y1, None],
line=dict(color='#444', width=weight * 0.5 if weight < 10 else 4), # Use weight for the line width
hoverinfo='text',
text=""
)
edge_traces.append(edge_trace)
# Create nodes for the plot
node_trace = go.Scatter(
x=[],
y=[],
text=[],
hovertext=[],
textposition='top center',
mode='markers+text',
hoverinfo='text',
marker=dict(
showscale=True,
colorscale='YlGnBu',
size=20,
color=[],
colorbar=dict(
thickness=25,
xanchor='left'
),
line=dict(width=2, color=[])
)
)
# Add nodes to the plot
node_circle_colors = []
for node in subgraph.nodes():
# Fetch the movie name (title) and budget from the movies dataframe
movie_name = self.movies_df.loc[self.movies_df['id'] == node, 'original_title'].values
movie_budget = self.movies_df.loc[self.movies_df['id'] == node, 'budget'].values # Assuming 'rating' column exists
movie_date = self.movies_df.loc[self.movies_df['id'] == node, 'release_date'].values[0]
movie_revenue = self.movies_df.loc[self.movies_df['id'] == node, 'revenue'].values
movie_rating = self.movies_df.loc[self.movies_df['id'] == node, 'vote_average'].values
movie_company = self.movies_df.loc[self.movies_df['id'] == node, 'production_companies'].values
movie_name = movie_name[0] if movie_name.size > 0 else node # Fallback to node ID if no name found
budget_value = float(movie_budget[0]) if movie_budget.size > 0 else 0 # Fallback to 0 if no rating found
year_value = float(movie_date[-4:]) if len(movie_date) > 0 else 0
revenue_value = float(movie_revenue[0]) if movie_revenue.size > 0 else 0
rating_value = float(movie_rating[0]) if movie_rating.size > 0 else -1
movie_companies = self.movies_df.loc[self.movies_df['original_title'] == movie_title, 'production_companies'].values
target_companies = set()
if movie_companies.size > 0 and isinstance(movie_companies[0], str):
target_companies = {company['name'] for company in ast.literal_eval(movie_companies[0]) if
isinstance(company, dict)}
company_value = ast.literal_eval(movie_company[0]) if movie_companies.size > 0 and isinstance(
movie_companies[0], str) else []
company_match = any(company['name'] in target_companies for company in company_value if isinstance(company, dict))
x, y = pos[node]
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])
if len(self.options) > 0:
if self.options[0] == "Budget Value":
node_trace['marker']['color'] += tuple([budget_value]) # Set color based on rating
elif "Date Released" in self.options:
node_trace['marker']['color'] += tuple([year_value])
elif "Revenue" in self.options:
node_trace['marker']['color'] += tuple([revenue_value])
elif "Rating" in self.options:
node_trace['marker']['color'] += tuple([rating_value])
else:
node_trace['marker']['color'] += tuple([1])
if "Production Company" in self.options:
if company_match:
node_circle_colors.append('red')
else:
node_circle_colors.append('black')
else:
node_circle_colors.append('black')
else:
node_trace['marker']['color'] += tuple([1])
node_trace.marker.line.color = node_circle_colors
# If a base movie is provided, include shared actors in the hover text
if movie_id and node != movie_id and subgraph.has_edge(movie_id, node) and not movie_id2:
shared_actors = ', '.join(subgraph[movie_id][node]['actors']) # Get shared actors
node_text = f"Movie: {movie_name}"
node_hover_text = f"Shared Actors with {movie_title}: {shared_actors}"
# if two movies given and there is a connection between both
elif movie_id and node != movie_id and subgraph.has_edge(movie_id, node) and movie_id2 and node != movie_id2 and subgraph.has_edge(movie_id2, node):
shared_actors_1 = {}
shared_actors_2 = {}
# Get the shared actors for the first movie (movie_id)
shared_actors_1[node] = set(subgraph[movie_id][node].get('actors', []))
# Get the shared actors for the second movie (movie_id2)
shared_actors_2[node] = set(subgraph[movie_id2][node].get('actors', []))
shared_actors_1_text = ', '.join(shared_actors_1.get(node, []))
shared_actors_2_text = ', '.join(shared_actors_2.get(node, []))
node_text = f"Movie: {movie_name}"
node_hover_text = f"Shared Actors with {movie_title}: {shared_actors_1_text}"
node_hover_text += f"<br>Shared Actors with {movie_title2}: {shared_actors_2_text}"
# if two movies are given and there is only a connection with one (max_distance>1)
elif movie_id and node != movie_id and movie_id2 and node != movie_id2 and (subgraph.has_edge(movie_id,node) or subgraph.has_edge(movie_id2, node)):
# if connection is with the first movie
if subgraph.has_edge(movie_id,node):
shared_actors = {}
# Get the shared actors for the first movie (movie_id)
shared_actors[node] = set(subgraph[movie_id][node].get('actors', []))
shared_actors_text = ', '.join(shared_actors.get(node, []))
node_text = f"Movie: {movie_name}"
node_hover_text = f"Shared Actors with {movie_title}: {shared_actors_text}"
path = nx.shortest_path(subgraph, source=movie_id2, target=node)
path_text = " -> ".join([subgraph.nodes[n].get('name', f"{self.movies_df.loc[self.movies_df['id'] == n, 'original_title'].values[0]}") for n in path])
node_hover_text += f"<br>Path from {movie_title2}: {path_text}"
# if connection is with the second movie
elif subgraph.has_edge(movie_id2, node):
shared_actors = {}
# Get the shared actors for the second movie (movie_id2)
shared_actors[node] = set(subgraph[movie_id2][node].get('actors', []))
shared_actors_text = ', '.join(shared_actors.get(node, []))
node_text = f"Movie: {movie_name}"
node_hover_text = f"Shared Actors with {movie_title2}: {shared_actors_text}"
path = nx.shortest_path(subgraph, source=movie_id, target=node)
path_text = " -> ".join([subgraph.nodes[n].get('name',
f"{self.movies_df.loc[self.movies_df['id'] == n, 'original_title'].values[0]}")
for n in path])
node_hover_text += f"<br>Path from {movie_title}: {path_text}"
# if two movies are given and they connect with each other
elif movie_id and movie_id2 and subgraph.has_edge(movie_id, movie_id2):
shared_actors = ', '.join(subgraph[movie_id][movie_id2]['actors']) # Get shared actors
node_text = f"Movie: {movie_name}"
if node == movie_id:
node_hover_text = f"Shared Actors with {movie_title2}: {shared_actors}"
elif node == movie_id2:
node_hover_text = f"Shared Actors with {movie_title}: {shared_actors}"
else:
node_text = f"Movie: {movie_name}"
node_hover_text = ""
else:
node_text = f"Movie: {movie_name}"
node_hover_text = ""
if len(self.options) > 0:
if "Budget Value" in self.options:
if budget_value > 0:
node_hover_text += f"<br> Budget Value: ${int(budget_value):,}" # Set color based on rating
else:
node_hover_text += f"<br>Budget Value: Not Available"
if "Date Released" in self.options:
if year_value > 0:
node_hover_text += f"<br> Year Released: {int(year_value)}"
else:
node_hover_text += f"<br> Year Released: Not Available"
if "Revenue" in self.options:
if revenue_value > 0:
node_hover_text += f"<br> Revenue: ${int(revenue_value):,}"
else:
node_hover_text += f"<br> Revenue: Not Available"
if "Rating" in self.options:
if rating_value >= 0:
node_hover_text += f"<br> Rating: {int(rating_value)}"
else:
node_hover_text += f"<br> Rating: Not Available"
if "Production Company" in self.options:
if company_value != "":
company_names = {company['name'] for company in company_value}
if len(company_names) > 1:
node_hover_text += f"<br> Production Companies: {', '.join(company_names)}"
elif len(company_names) == 1:
node_hover_text += f"<br> Production Company: {', '.join(company_names)}"
else:
node_hover_text += f"<br> Production Company: Not Available"
node_trace['text'] += tuple([node_text]) # Add hover text for the node
node_trace['hovertext'] += tuple([node_hover_text])
# Add the traces to the figure
for edge_trace in edge_traces: # edge_traces is the list of edge Scatter objects
fig.add_trace(edge_trace)
fig.add_trace(node_trace)
if not dark_mode:
fig.update_layout(
showlegend=False,
plot_bgcolor='white', # Background of the plot area
paper_bgcolor='white', # Background of the entire figure
xaxis=dict(showline=False, zeroline=False, showticklabels=False),
yaxis=dict(showline=False, zeroline=False, showticklabels=False)
)
else:
fig.update_layout(
showlegend=False,
plot_bgcolor='grey',
paper_bgcolor='grey',
xaxis=dict(showline=False, zeroline=False, showticklabels=False),
yaxis=dict(showline=False, zeroline=False, showticklabels=False)
)
fig.update_xaxes(showgrid=False, showticklabels=False, showline=False)
fig.update_yaxes(showgrid=False, showticklabels=False, showline=False)
# Return the figure as JSON
return json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
# Helper function to disambiguate movie titles
def disambiguate_movie(self, movie_name):
matching_movies = self.movies_df[self.movies_df['original_title'].str.lower() == movie_name.lower()]
if matching_movies.empty:
print(f"Movie '{movie_name}' not found.")
return None
if len(matching_movies) == 1:
return matching_movies.iloc[0]['id']
print(f"Multiple movies found with the title '{movie_name}':")
for idx, row in matching_movies.iterrows():
cast_names = [actor['name'] for actor in row['cast'][:5]]
print(
f" [{idx}] {row['original_title']} ({row['release_date'] if pd.notna(row['release_date']) else 'Unknown Year'})")
print(f" Cast: {', '.join(cast_names)}")
while True:
try:
choice = int(input("Enter the number corresponding to the correct movie: "))
if choice in matching_movies.index:
return matching_movies.loc[choice, 'id']
except ValueError:
print("Invalid input. Please enter a number.")
def find_kevin_bacon_number_bfs(self, start_movie_name, target_movie_name, start_movie_id=None, target_movie_id=None, dark_mode=False):
# Disambiguate the start and target movies
if start_movie_id is None:
start_movie_id = self.disambiguate_movie(start_movie_name)
if target_movie_id is None:
target_movie_id = self.disambiguate_movie(target_movie_name)
if start_movie_id is None or target_movie_id is None:
return
# BFS Initialization
visited = set() # Track visited nodes
queue = [(start_movie_id, [start_movie_id])] # Queue holds (current_node, path_to_current_node)
print("Started BFS Search")
while queue:
current_node, path = queue.pop(0)
# If target movie is found, print the path and actors
if current_node == target_movie_id:
movie_names = [
self.movies_df.loc[self.movies_df['id'] == movie_id, 'original_title'].values[0]
for movie_id in path
]
print(f"The Kevin Bacon number from '{start_movie_name}' to '{target_movie_name}' is {len(path) - 1}.")
print("Path:")
for i in range(len(path) - 1):
movie_a = path[i]
movie_b = path[i + 1]
shared_actors = self.graph[movie_a][movie_b]['actors']
movie_a_name = movie_names[i]
movie_b_name = movie_names[i + 1]
print(f" {movie_a_name} -> {movie_b_name} (Shared Actors: {', '.join(shared_actors)})")
# Visualize the path from start movie to target movie
subgraph = self.graph.subgraph(path) # Create subgraph with the BFS path
return self.visualize_graph_from_subgraph(subgraph, movie_names[0], dark_mode=dark_mode)
# Mark current node as visited
if current_node not in visited:
visited.add(current_node)
# Enqueue all unvisited neighbors
for neighbor in self.graph.neighbors(current_node):
if neighbor not in visited:
queue.append((neighbor, path + [neighbor]))
print(f"'{target_movie_name}' is not reachable from '{start_movie_name}'.")
def dijkstra(self, start_movie_name, target_movie_name, start_movie_id=None, target_movie_id=None, dark_mode=False):
# Ensure start and target movie IDs are specified
if start_movie_id is None:
start_movie_id = self.disambiguate_movie(start_movie_name)
if target_movie_id is None:
target_movie_id = self.disambiguate_movie(target_movie_name)
# Output for improper start/target movie name
if start_movie_id is None or target_movie_id is None:
print("Your movie doesn't exist")
return
# Initialize distances and previous nodes
distances = {node: float('inf') for node in self.graph.nodes}
previous_nodes = {node: None for node in self.graph.nodes}
distances[start_movie_id] = 0
# Create a priority queue using heapq
priority_queue = []
heapq.heappush(priority_queue, (0, start_movie_id)) # (distance, node)
while priority_queue:
current_distance, current_node = heapq.heappop(priority_queue)
# If we reached the target node, we can stop
if current_node == target_movie_id:
break
# If the distance is greater than the recorded distance, continue
if current_distance > distances[current_node]:
continue
# Iterate over neighbors
for neighbor in self.graph.neighbors(current_node):
edge_weight = self.graph[current_node][neighbor]['weight']
new_distance = distances[current_node] + edge_weight
# Only consider this new path if it's better
if new_distance < distances[neighbor]:
distances[neighbor] = new_distance
previous_nodes[neighbor] = current_node
heapq.heappush(priority_queue, (new_distance, neighbor))
# Reconstruct the path
path = []
current_node = target_movie_id
while current_node is not None:
path.append(current_node)
current_node = previous_nodes[current_node]
path = path[::-1]
# Output results
if distances[target_movie_id] == float('inf'):
print(f"No path found between '{start_movie_name}' and '{target_movie_name}'.")
else:
movie_names = [
self.movies_df.loc[self.movies_df['id'] == movie_id, 'original_title'].values[0]
for movie_id in path
]
print(
f"The shortest path from '{start_movie_name}' to '{target_movie_name}' has a weight of {distances[target_movie_id]:.2f}.")
print("Path:")
for i in range(len(path) - 1):
movie_a = path[i]
movie_b = path[i + 1]
shared_actors = self.graph[movie_a][movie_b]['actors']
movie_a_name = movie_names[i]
movie_b_name = movie_names[i + 1]
print(f" {movie_a_name} -> {movie_b_name} (Shared Actors: {', '.join(shared_actors)})")
# Visualize the subgraph if needed
subgraph = self.graph.subgraph(path)
return self.visualize_graph_from_subgraph(subgraph, movie_names[0], dark_mode=dark_mode)
def visualize_graph_from_subgraph(self, subgraph, movie_title, dark_mode=False):
fig = make_subplots()
# Layout of the graph
pos = nx.spring_layout(subgraph, scale=None)
# Create edges for the plot, using the edge weights
edge_traces = []
for edge in subgraph.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
weight = subgraph[edge[0]][edge[1]]['weight']
edge_trace = go.Scatter(
x=[x0, x1, None],
y=[y0, y1, None],
line=dict(color='#444', width=weight * 0.5 if weight < 10 else 4), # Use weight for the line width
hoverinfo='text',
text=""
)
edge_traces.append(edge_trace)
# Create nodes for the plot
node_trace = go.Scatter(
x=[],
y=[],
text=[],
hovertext=[],
textposition='top center',
mode='markers+text',
hoverinfo='text',
marker=dict(
showscale=True,
colorscale='YlGnBu',
size=20,
color=[],
colorbar=dict(
thickness=25,
title='Node Connections',
xanchor='left',
),
line=dict(width=2, color=[])
)
)
node_circle_colors = []
# Add nodes to the plot
for node in subgraph.nodes():
# Fetch the movie name (title) and budget from the movies dataframe
movie_name = self.movies_df.loc[self.movies_df['id'] == node, 'original_title'].values
movie_budget = self.movies_df.loc[self.movies_df['id'] == node, 'budget'].values
movie_date = self.movies_df.loc[self.movies_df['id'] == node, 'release_date'].values[0]
movie_revenue = self.movies_df.loc[self.movies_df['id'] == node, 'revenue'].values
movie_company = self.movies_df.loc[self.movies_df['id'] == node, 'production_companies'].values
movie_name = movie_name[0] if movie_name.size > 0 else node
budget_value = float(movie_budget[0]) if movie_budget.size > 0 else 0
year_value = float(movie_date[-4:]) if len(movie_date) > 0 else 0
revenue_value = float(movie_revenue[0]) if movie_revenue.size > 0 else 0
movie_companies = self.movies_df.loc[
self.movies_df['original_title'] == movie_title, 'production_companies'].values
target_companies = set()
if movie_companies.size > 0 and isinstance(movie_companies[0], str):
target_companies = {company['name'] for company in ast.literal_eval(movie_companies[0]) if
isinstance(company, dict)}
company_value = ast.literal_eval(movie_company[0]) if movie_company.size > 0 and isinstance(
movie_company[0], str) else []
company_match = any(
company['name'] in target_companies for company in company_value if isinstance(company, dict))
x, y = pos[node]
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])
# Set colors based on options
if len(self.options) > 0:
if self.options[0] == "Budget Value":
node_trace['marker']['color'] += tuple([budget_value])
elif "Date Released" in self.options:
node_trace['marker']['color'] += tuple([year_value])
elif "Revenue" in self.options:
node_trace['marker']['color'] += tuple([revenue_value])
else:
node_trace['marker']['color'] += tuple([1])
if "Production Company" in self.options:
node_circle_colors.append('red' if company_match else 'black')
else:
node_circle_colors.append('black')
else:
node_trace['marker']['color'] += tuple([1])
node_trace.marker.line.color = node_circle_colors
# Prepare hover text for the node
node_text = f"Movie: {movie_name}"
node_hover_text = ""
# shows shared actors in the hover text for all neighbors
neighbors = list(subgraph.neighbors(node))
for neighbor in neighbors:
if subgraph.has_edge(node, neighbor):
shared_actors = ', '.join(subgraph[node][neighbor]['actors'])
neighbor_name = self.movies_df.loc[self.movies_df['id'] == neighbor, 'original_title'].values
neighbor_name = neighbor_name[0] if neighbor_name.size > 0 else neighbor
if neighbor_name != movie_name:
node_hover_text += f"<br>Shared Actors with {neighbor_name}: {shared_actors}"
# Add additional information based on options
if len(self.options) > 0:
if "Budget Value" in self.options:
node_hover_text += f"<br>Budget Value: ${int(budget_value):,}" if budget_value > 0 else f"<br>Budget Value: Not Available"
if "Date Released" in self.options:
node_hover_text += f"<br>Year Released: {int(year_value) if year_value > 0 else 'Not Available'}"
if "Revenue" in self.options:
node_hover_text += f"<br>Revenue Value: ${int(revenue_value):,}" if budget_value > 0 else f"<br>Revenue Value: Not Available"
if "Production Company" in self.options:
company_names = {company['name'] for company in company_value}
if len(company_names) > 1:
node_hover_text += f"<br> Production Companies: {', '.join(company_names)}"
elif len(company_names) == 1:
node_hover_text += f"<br> Production Company: {', '.join(company_names)}"
node_trace['text'] += tuple([node_text]) # Add hover text for the node
node_trace['hovertext'] += tuple([node_hover_text])
# Add the traces to the figure
for edge_trace in edge_traces:
fig.add_trace(edge_trace)
fig.add_trace(node_trace)
if not dark_mode:
fig.update_layout(
showlegend=False,
plot_bgcolor='white', # Background of the plot area
paper_bgcolor='white', # Background of the entire figure
xaxis=dict(showline=False, zeroline=False, showticklabels=False),
yaxis=dict(showline=False, zeroline=False, showticklabels=False)
)
else:
fig.update_layout(
showlegend=False,
plot_bgcolor='grey', # Background of the plot area
paper_bgcolor='grey', # Background of the entire figure
xaxis=dict(showline=False, zeroline=False, showticklabels=False),
yaxis=dict(showline=False, zeroline=False, showticklabels=False)
)
fig.update_xaxes(showgrid=False, showticklabels=False, showline=False)
fig.update_yaxes(showgrid=False, showticklabels=False, showline=False)
return json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
def choose_options(self, new_options):
self.options = new_options
import time
if __name__ == "__main__":
full_time = time.time()
movies_file = "movies_metadata.csv"
credits_file = "credits.csv"
movie_graph = Graph(movies_file, credits_file)
print("Begin Read")
movie_graph.read_data()
print("Read Finished")
print("Begin Build")
movie_graph.build_graph()
print("Build Finished")
# Measure BFS timing
print("\nRunning BFS:")
start_time = time.time()
movie_graph.find_kevin_bacon_number_bfs("Avatar", "Toy Story")
end_time = time.time()
print(f"BFS took {end_time - start_time:.4f} seconds.")
# Measure Dijkstra timing
print("\nRunning Dijkstra:")
start_time = time.time()
movie_graph.dijkstra("Avatar", "Toy Story")
end_time = time.time()
print(f"Dijkstra's algorithm took {end_time - start_time:.4f} seconds.")
finished_time = time.time()
print(f"Entire main function took {finished_time - full_time:.4f} seconds.")