-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_degrees.py
More file actions
86 lines (83 loc) · 1.03 KB
/
Copy pathnode_degrees.py
File metadata and controls
86 lines (83 loc) · 1.03 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
def node_degree(nodes):
num_of_nodes,connections = nodes.split('\n')[0],nodes.split('\n')[1:]
node_dict = {}
i = 1
while i <= int(num_of_nodes):
node_dict[str(i)]=Node(i)
i += 1
for connection in connections:
first_node = node_dict[connection.split()[0]]
second_node = node_dict[connection.split()[1]]
first_node.degree += 1
second_node.degree += 1
first_node.connections += [second_node]
second_node.connections += [first_node]
for node in sorted(node_dict.values(), key= lambda node: node.value):
print "Node",node.value,'has a degree of ',node.degree
class Node:
def __init__(self,value):
self.value = value
self.degree = 0
self.connections = []
def __repr__(self):
return "Node " + str(self.value)
n1="""16
1 2
1 3
2 3
1 4
3 4
1 5
2 5
1 6
2 6
3 6
3 7
5 7
6 7
3 8
4 8
6 8
7 8
2 9
5 9
6 9
2 10
9 10
6 11
7 11
8 11
9 11
10 11
1 12
6 12
7 12
8 12
11 12
6 13
7 13
9 13
10 13
11 13
5 14
8 14
12 14
13 14
1 15
2 15
5 15
9 15
10 15
11 15
12 15
13 15
1 16
2 16
5 16
6 16
11 16
12 16
13 16
14 16
15 16"""
node_degree(n1)