|
| 1 | +import numpy as np |
| 2 | +import multiprocessing |
| 3 | +from concurrent.futures import ThreadPoolExecutor |
| 4 | +import threading |
| 5 | + |
| 6 | + |
| 7 | +DAMPING = 0.85 |
| 8 | + |
| 9 | + |
| 10 | +def pagerank_single(matrix: np.ndarray, num_iterations: int) -> np.ndarray: |
| 11 | + """Single-threaded PageRank implementation""" |
| 12 | + size = matrix.shape[0] |
| 13 | + # Initialize scores |
| 14 | + scores = np.ones(size) / size |
| 15 | + |
| 16 | + for _ in range(num_iterations): |
| 17 | + new_scores = np.zeros(size) |
| 18 | + for i in range(size): |
| 19 | + # Get nodes that point to current node |
| 20 | + incoming = np.where(matrix[:, i])[0] |
| 21 | + for j in incoming: |
| 22 | + # Add score contribution from incoming node |
| 23 | + new_scores[i] += scores[j] / np.sum(matrix[j]) |
| 24 | + |
| 25 | + # Apply damping factor |
| 26 | + new_scores = (1 - DAMPING) / size + DAMPING * new_scores |
| 27 | + scores = new_scores |
| 28 | + |
| 29 | + return scores |
| 30 | + |
| 31 | + |
| 32 | +def _process_chunk( |
| 33 | + matrix: np.ndarray, scores: np.ndarray, start_idx: int, end_idx: int |
| 34 | +) -> np.ndarray: |
| 35 | + """Helper function for multiprocessing implementation""" |
| 36 | + size = matrix.shape[0] |
| 37 | + chunk_scores = np.zeros(size) |
| 38 | + |
| 39 | + for i in range(start_idx, end_idx): |
| 40 | + incoming = np.where(matrix[:, i])[0] |
| 41 | + for j in incoming: |
| 42 | + chunk_scores[i] += scores[j] / np.sum(matrix[j]) |
| 43 | + |
| 44 | + return chunk_scores |
| 45 | + |
| 46 | + |
| 47 | +def pagerank_multiprocess( |
| 48 | + matrix: np.ndarray, num_iterations: int, num_processes: int |
| 49 | +) -> np.ndarray: |
| 50 | + """Multi-process PageRank implementation""" |
| 51 | + size = matrix.shape[0] |
| 52 | + scores = np.ones(size) / size |
| 53 | + |
| 54 | + # Split work into chunks |
| 55 | + chunk_size = size // num_processes |
| 56 | + chunks = [ |
| 57 | + (matrix, scores, i, min(i + chunk_size, size)) |
| 58 | + for i in range(0, size, chunk_size) |
| 59 | + ] |
| 60 | + |
| 61 | + for _ in range(num_iterations): |
| 62 | + with multiprocessing.Pool(processes=num_processes) as pool: |
| 63 | + # Process chunks in parallel |
| 64 | + chunk_results = pool.starmap(_process_chunk, chunks) |
| 65 | + # Combine results |
| 66 | + new_scores = sum(chunk_results) |
| 67 | + new_scores = (1 - DAMPING) / size + DAMPING * new_scores |
| 68 | + scores = new_scores |
| 69 | + |
| 70 | + return scores |
| 71 | + |
| 72 | + |
| 73 | +def _thread_worker( |
| 74 | + matrix: np.ndarray, |
| 75 | + scores: np.ndarray, |
| 76 | + new_scores: np.ndarray, |
| 77 | + start_idx: int, |
| 78 | + end_idx: int, |
| 79 | + lock: threading.Lock, |
| 80 | +): |
| 81 | + """Helper function for multi-threaded implementation""" |
| 82 | + size = matrix.shape[0] |
| 83 | + local_scores = np.zeros(size) |
| 84 | + |
| 85 | + for i in range(start_idx, end_idx): |
| 86 | + incoming = np.where(matrix[:, i])[0] |
| 87 | + for j in incoming: |
| 88 | + local_scores[i] += scores[j] / np.sum(matrix[j]) |
| 89 | + |
| 90 | + with lock: |
| 91 | + new_scores += local_scores |
| 92 | + |
| 93 | + |
| 94 | +def pagerank_multithread( |
| 95 | + matrix: np.ndarray, num_iterations: int, num_threads: int |
| 96 | +) -> np.ndarray: |
| 97 | + """Multi-threaded PageRank implementation""" |
| 98 | + size = matrix.shape[0] |
| 99 | + scores = np.ones(size) / size |
| 100 | + |
| 101 | + # Split work into chunks |
| 102 | + chunk_size = size // num_threads |
| 103 | + chunks = [(i, min(i + chunk_size, size)) for i in range(0, size, chunk_size)] |
| 104 | + |
| 105 | + for _ in range(num_iterations): |
| 106 | + new_scores = np.zeros(size) |
| 107 | + lock = threading.Lock() |
| 108 | + with ThreadPoolExecutor(max_workers=num_threads) as executor: |
| 109 | + # Process chunks in parallel |
| 110 | + executor.map( |
| 111 | + lambda args: _thread_worker(*args), # starmap isn't available |
| 112 | + [ |
| 113 | + (matrix, scores, new_scores, start_idx, end_idx, lock) |
| 114 | + for start_idx, end_idx in chunks |
| 115 | + ], |
| 116 | + ) |
| 117 | + # Apply damping factor |
| 118 | + new_scores = (1 - DAMPING) / size + DAMPING * new_scores |
| 119 | + scores = new_scores |
| 120 | + |
| 121 | + return scores |
0 commit comments