-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReducer.py
More file actions
41 lines (30 loc) · 1.1 KB
/
Reducer.py
File metadata and controls
41 lines (30 loc) · 1.1 KB
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
import igraph as ig
import random
def load_graph(file_path):
"""Load the graph from a file."""
return ig.Graph.Load(file_path, format='picklez')
def save_graph(graph, file_path):
"""Save the graph to a file."""
graph.save(file_path, format='picklez')
def remove_nodes(graph, fraction):
"""Remove a fraction of nodes from the graph."""
num_nodes = len(graph.vs)
num_nodes_to_remove = int(num_nodes * fraction)
# Select random nodes to remove
nodes_to_remove = random.sample(range(num_nodes), num_nodes_to_remove)
# Remove the nodes from the graph
graph.delete_vertices(nodes_to_remove)
return graph
def main():
input_file = 'graphs/examples/P2P_model.pyp2p'
output_file = 'graphs/examples/P2P_model_reduced.pyp2p'
# Load the graph
graph = load_graph(input_file)
# Remove approximately 3/4 of the nodes
fraction_to_remove = 0.85
graph = remove_nodes(graph, fraction_to_remove)
# Save the modified graph
save_graph(graph, output_file)
print(f"Graph saved to {output_file}")
if __name__ == '__main__':
main()