-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.py
More file actions
112 lines (98 loc) · 2.69 KB
/
prim.py
File metadata and controls
112 lines (98 loc) · 2.69 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
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
import numpy as np
import pandas as pd
import math
import random
import matplotlib.pyplot as plt
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
import collections
from collections import Counter
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
"""
Constants
"""
large_graph = 16384
max_weight = 0.00038189538245798719
num_nodes = 131072
num_trials = 1
"""
create_complete_graph(n)
"""
def create_complete_graph(n):
vs = list(range(0, n))
graph = collections.defaultdict(dict)
for u in xrange(0, n):
weights = np.random.random_sample((1,n))[0]
for w in xrange(u+1,n):
if n >= large_graph:
if weights[w] < max_weight:
graph[u][w] = weights[w]
graph[w][u] = weights[w]
else:
graph[u][w] = weights[w]
graph[w][u] = weights[w]
print "created graph"
return graph
"""
Delete min dict
input: {s:0}
deletes smallest priority element
"""
def delete_min_dict(H):
v = min(H, key=H.get)
edge_weight = H[v]
del H[v]
return v, edge_weight, H
"""
Prim's algorithm
takes a graph of size n
returns a tree
"""
def prim(graph, n):
# keep
dist= collections.defaultdict(int)
# keep track of previous node for each vertex v
prev = collections.defaultdict(int)
vs = list(range(0, n))
for v in vs:
dist[v] = float("inf")
s = 0
dist[s] = 0
# for now, heap to keep track of what to add is just an array
H = {s:0}
T = {}
edge_weights = []
while H != {}:
v, edge_weight, H = delete_min_dict(H)
T[v] = edge_weight
edge_weights.append(edge_weight)
# loop through all the edges of graph (v,w) not already connected in the current tree
nodes_in_other_cut = list(set(range(0,n)) - set(T.keys()))
for w in nodes_in_other_cut:
try:
weight_vw = graph[v][w]
if dist[w] > weight_vw:
dist[w] = weight_vw
prev[w] = v
# replace later with distance of w
H[w] = dist[w]
except:
pass
return T, sum(edge_weights)
"""
Get stats
"""
def get_average(num_trials, N):
maxes = []
weights = []
for i in xrange(0, num_trials):
graph = create_complete_graph(N)
T, weight = prim(graph, N)
weights.append(weight)
max_el = max(T.values())
maxes.append(max_el)
# print "finished graph" + str(i)
return max(maxes), mean(weights)
max_of_maxes, mean_weights = get_average(num_trials, num_nodes)
print (max_of_maxes, mean_weights)