Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lab02/Lab02odd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
num = []
for i in range(2, 101):
flag = 0
for j in range(1, i+1):
if i % j == 0:
flag += 1
if flag == 2:
num.append(i)
print(num)
168 changes: 168 additions & 0 deletions Lab04/Lab/adjacency.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 26,
"id": "c86ed400",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'1': ['2', '3'], '2': ['4', '3', '1'], '3': ['5', '4', '2', '1'], '4': ['8', '3', '5', '2'], '5': ['15', '11', '8', '9', '6', '3', '4'], '6': ['9', '5'], '7': ['14', '13', '8'], '8': ['12', '11', '5', '4', '7'], '9': ['26', '22', '15', '10', '6', '5'], '10': ['20', '17', '9'], '11': ['16', '12', '5', '8'], '12': ['16', '14', '13', '11', '8'], '13': ['14', '12', '7'], '14': ['19', '12', '13', '16', '18', '7'], '15': ['25', '16', '5', '9'], '16': ['25', '24', '18', '15', '11', '12', '14'], '17': ['23', '20', '10'], '18': ['24', '19', '16', '14'], '19': ['18', '24', '14'], '20': ['35', '33', '27', '22', '23', '32', '40', '17', '10'], '21': ['34', '24', '30', '9'], '22': ['28', '27', '26', '20', '9'], '23': ['32', '17', '20'], '24': ['30', '25', '16', '18', '21', '19'], '25': ['29', '15', '26', '16', '24'], '26': ['28', '22', '9', '25'], '27': ['33', '28', '20', '22'], '28': ['38', '29', '27', '33', '35', '22', '26'], '29': ['37', '30', '28', '25'], '30': ['37', '29', '24', '21'], '31': ['36', '34'], '32': ['41', '40', '23', '20'], '33': ['35', '20', '27', '28'], '34': ['42', '36', '21', '31'], '35': ['44', '38', '20', '33', '28'], '36': ['46', '39', '34', '42', '31'], '37': ['45', '38', '43', '29', '30'], '38': ['43', '35', '28', '37'], '39': ['46', '36'], '40': ['47', '41', '32', '20'], '41': ['47', '32', '40'], '42': ['34', '36'], '43': ['48', '45', '44', '38', '37'], '44': ['49', '48', '35', '43'], '45': ['48', '49', '37', '43'], '46': ['36', '39'], '47': ['40', '41'], '48': ['49', '44', '43', '45'], '49': ['44', '48', '45']}\n",
"The average connectivity is 4.1020408163265305\n",
"The maximum connectivity is 9 and the responding object number is 20\n",
"The minimum connectivity is 2 and the responding object number is 1\n",
"21 and 9 have a problematic neighbourhood relationship, the dictionary has asymmetry\n"
]
}
],
"source": [
"\n",
"with open('G:\\\\my_project\\PTUA2023\\Lab04\\Lab\\Lab04-1.gal', mode='r', encoding='utf-8') as f:\n",
" lines = f.readlines() # 读取文件\n",
" try:\n",
" lines = lines[1:] # 只读取第一行之后的内容\n",
" f.close() # 关闭文件\n",
" except:\n",
" pass\n",
"\n",
"node = {}\n",
"i=1\n",
"for line in lines:\n",
" \n",
" #line1\n",
" if i%2 == 1: \n",
" list1 = line[:-1].split(' ')\n",
" key = list1[0]\n",
" num = int(list1[1])\n",
" i = i+1\n",
" #line2\n",
" elif i%2 == 0:\n",
" list2 = line[:-1].split(' ')\n",
" node[key] = list2\n",
" i = i+1\n",
"print(node)\n",
"\n",
"#The code above is to get the dictionary of node.\n",
"\n",
"class Graph(object):\n",
" def __init__(self,n):\n",
" self.n = n\n",
" \n",
" def ave_connect(self):\n",
" total_connect = 0\n",
" i = 0\n",
" for list in self.n.values():\n",
" connect = len(list)\n",
" total_connect = total_connect + connect\n",
" i += 1\n",
" ave_connect = total_connect/i\n",
" return ave_connect\n",
" \n",
" def max_connect(self):\n",
" max = 0\n",
" i = 0\n",
" x = 0\n",
" for list in self.n.values():\n",
" connect = len(list)\n",
" i += 1\n",
" if connect >= max:\n",
" max = connect \n",
" max_key = i #max_key represents the serial number of the key\n",
" else:\n",
" pass\n",
" for key in self.n: #Print the corresponding key according to the serial number, I think there is another better way\n",
" x += 1\n",
" if x == max_key:\n",
" KEY = key\n",
" else:\n",
" pass\n",
" return max,KEY\n",
" \n",
" def min_connect(self):\n",
" min = 1000\n",
" i = 0\n",
" x = 0\n",
" for list in self.n.values():\n",
" connect = len(list)\n",
" i += 1\n",
" if connect < min:\n",
" min = connect \n",
" min_key = i #min_key represents the serial number of the key\n",
" else:\n",
" pass\n",
" for key in self.n: #Print the corresponding key according to the serial number, I think there is another better way\n",
" x += 1\n",
" if x == min_key:\n",
" KEY = key\n",
" else:\n",
" pass\n",
" return min,KEY\n",
" \n",
" def disconnect(self):\n",
" x = 0\n",
" for k in self.n:\n",
" value_list = self.n[k]\n",
" for value in value_list:\n",
" list = self.n[value]\n",
" if list.count(k) == 1:\n",
" continue\n",
" else:\n",
" print('{} and {} have a problematic neighbourhood relationship, the dictionary has asymmetry'.format(k,value))\n",
" x = 1\n",
" if x == 0:\n",
" print('There are no asymmetries in that dictionary')\n",
"\n",
"graph = Graph(node)\n",
"\n",
"#1\n",
"Average_connectivity = graph.ave_connect()\n",
"print('The average connectivity is ',Average_connectivity)\n",
"\n",
"#2\n",
"Maximum_connectivity = graph.max_connect()[0]\n",
"Maximum_object = graph.max_connect()[1]\n",
"print('The maximum connectivity is {} and the responding object number is {}'.format(Maximum_connectivity,Maximum_object))\n",
"\n",
"#3\n",
"Minimum_connectivity = graph.min_connect()[0]\n",
"Minimum_object = graph.min_connect()[1]\n",
"print('The minimum connectivity is {} and the responding object number is {}'.format(Minimum_connectivity,Minimum_object))\n",
"\n",
"#4\n",
"graph.disconnect()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16c1dacb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
127 changes: 127 additions & 0 deletions Lab04/Lab/adjacency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env python
# coding: utf-8

# In[26]:



with open('G:\\my_project\PTUA2023\Lab04\Lab\Lab04-1.gal', mode='r', encoding='utf-8') as f:
lines = f.readlines() # 读取文件
try:
lines = lines[1:] # 只读取第一行之后的内容
f.close() # 关闭文件
except:
pass

node = {}
i=1
for line in lines:

#line1
if i%2 == 1:
list1 = line[:-1].split(' ')
key = list1[0]
num = int(list1[1])
i = i+1
#line2
elif i%2 == 0:
list2 = line[:-1].split(' ')
node[key] = list2
i = i+1
print(node)

#The code above is to get the dictionary of node.

class Graph(object):
def __init__(self,n):
self.n = n

def ave_connect(self):
total_connect = 0
i = 0
for list in self.n.values():
connect = len(list)
total_connect = total_connect + connect
i += 1
ave_connect = total_connect/i
return ave_connect

def max_connect(self):
max = 0
i = 0
x = 0
for list in self.n.values():
connect = len(list)
i += 1
if connect >= max:
max = connect
max_key = i #max_key represents the serial number of the key
else:
pass
for key in self.n: #Print the corresponding key according to the serial number, I think there is another better way
x += 1
if x == max_key:
KEY = key
else:
pass
return max,KEY

def min_connect(self):
min = 1000
i = 0
x = 0
for list in self.n.values():
connect = len(list)
i += 1
if connect < min:
min = connect
min_key = i #min_key represents the serial number of the key
else:
pass
for key in self.n: #Print the corresponding key according to the serial number, I think there is another better way
x += 1
if x == min_key:
KEY = key
else:
pass
return min,KEY

def disconnect(self):
x = 0
for k in self.n:
value_list = self.n[k]
for value in value_list:
list = self.n[value]
if list.count(k) == 1:
continue
else:
print('{} and {} have a problematic neighbourhood relationship, the dictionary has asymmetry'.format(k,value))
x = 1
if x == 0:
print('There are no asymmetries in that dictionary')

graph = Graph(node)

#1
Average_connectivity = graph.ave_connect()
print('The average connectivity is ',Average_connectivity)

#2
Maximum_connectivity = graph.max_connect()[0]
Maximum_object = graph.max_connect()[1]
print('The maximum connectivity is {} and the responding object number is {}'.format(Maximum_connectivity,Maximum_object))

#3
Minimum_connectivity = graph.min_connect()[0]
Minimum_object = graph.min_connect()[1]
print('The minimum connectivity is {} and the responding object number is {}'.format(Minimum_connectivity,Minimum_object))

#4
graph.disconnect()


# In[ ]:




Loading