This commit is contained in:
2022-10-24 17:59:59 -04:00
parent 62d3107259
commit bc0fc6cf5a
3 changed files with 255 additions and 0 deletions
Binary file not shown.
+69
View File
@@ -0,0 +1,69 @@
# File: Pease-hw6.py
# Author: PEASE, NICHOLAS
# Date: 24 OCT 2022
# Section: 1002-LAB
# E-mail: nicholas.pease@maine.edu
# Description:
# Submit a Python program that solve the following problem. Your answers must be Python3 code. To get full
# credit, your programs must be correct and the code must be neat. Each code file must contain the usual header
# comment (filled out) at the top of the file. Failure to include and fill out the header comment will result in a
# deduction. The collaboration line should contain the names of anyone in the course with whom you discussed
# the assignment. You do not need to include the names of staff members. If you did not collaborate, your
# collaboration should say: “I didnt collaborate with anyone”
# Collaboration:
# I collaborated with no one
import random
def main():
verticies = int(input("NUMBER OF VERTICES: "))
probability = float(input("PROBABILITY OF AN EDGE BETWEEN VERTICES: "))
AdjMatrix = GenerateAdjMatrix(verticies, probability)
AdjList = CreateAdjList(AdjMatrix)
PrintAdjMatrix(AdjMatrix)
PrintAdjList(AdjList)
def GenerateAdjMatrix(num_of_verts,prob):
matrix = []
for i in range(num_of_verts):
temp_matrix = [0]*num_of_verts
for j in range(num_of_verts):
rand = random.randint(0,1)
if rand < prob:
temp_matrix[j] = 1
else:
temp_matrix[j] = 0
matrix.append(temp_matrix)
return matrix
def CreateAdjList(adjMatrix):
adjList_temp = [0]*len(adjMatrix)
for i in range(len(adjMatrix)):
adjList_temp[i] = []
for j in range(len(adjMatrix[i])):
if adjMatrix[i][j]:
adjList_temp[i].append(j)
return adjList_temp
def PrintAdjMatrix(adjMatrix):
print("ADJACENCY MATRIX:\n")
print_string = ''
for i in range(len(adjMatrix)):
print_string+=" "+str(i)
print(" " + print_string)
print_string = ''
for i in range(len(adjMatrix)):
print_string = ''
for j in range(len(adjMatrix[i])):
print_string+=str(adjMatrix[i][j]) + " "
print(str(i) + " " + print_string)
def PrintAdjList(adjList):
print("\nADJACENCY LIST:\n")
for i in range(len(adjList)):
print_line = ''
for j in range(len(adjList[i])):
print_line += str(adjList[i][j]) + " "
print(str(i) + ": " + print_line)
main()
+186
View File
@@ -0,0 +1,186 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# File: Pease-hw6.py \n",
"# Author: PEASE, NICHOLAS \n",
"# Date: 24 OCT 2022\n",
"# Section: 1002-LAB\n",
"# E-mail: nicholas.pease@maine.edu \n",
"# Description: \n",
"# Submit a Python program that solve the following problem. Your answers must be Python3 code. To get full\n",
"# credit, your programs must be correct and the code must be neat. Each code file must contain the usual header\n",
"# comment (filled out) at the top of the file. Failure to include and fill out the header comment will result in a\n",
"# deduction. The collaboration line should contain the names of anyone in the course with whom you discussed\n",
"# the assignment. You do not need to include the names of staff members. If you did not collaborate, your\n",
"# collaboration should say: “I didnt collaborate with anyone”\n",
"# Collaboration: \n",
"# I collaborated with no one"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"def main():\n",
" verticies = int(input(\"NUMBER OF VERTICES: \"))\n",
" probability = float(input(\"PROBABILITY OF AN EDGE BETWEEN VERTICES: \"))\n",
" AdjMatrix = GenerateAdjMatrix(verticies, probability)\n",
" AdjList = CreateAdjList(AdjMatrix)\n",
" PrintAdjMatrix(AdjMatrix)\n",
" PrintAdjList(AdjList)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def GenerateAdjMatrix(num_of_verts,prob):\n",
" matrix = []\n",
" for i in range(num_of_verts):\n",
" temp_matrix = [0]*num_of_verts\n",
" for j in range(num_of_verts):\n",
" rand = random.randint(0,1)\n",
" if rand < prob:\n",
" temp_matrix[j] = 1\n",
" else:\n",
" temp_matrix[j] = 0\n",
" matrix.append(temp_matrix)\n",
" return matrix"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"def CreateAdjList(adjMatrix):\n",
" headers = [0,1,2,3,4,5,6,7,8,9]\n",
" adjList_temp = [0]*len(adjMatrix)\n",
" for i in range(len(adjMatrix)):\n",
" adjList_temp[i] = []\n",
" for j in range(len(adjMatrix[i])):\n",
" if adjMatrix[i][j]:\n",
" adjList_temp[i].append(j)\n",
" return adjList_temp"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"def PrintAdjMatrix(adjMatrix):\n",
" print(\"ADJACENCY MATRIX:\\n\")\n",
" print_string = ''\n",
" for i in range(len(adjMatrix)):\n",
" print_string+=\" \"+str(i)\n",
" print(\" \" + print_string)\n",
"\n",
" print_string = ''\n",
" for i in range(len(adjMatrix)):\n",
" print_string = ''\n",
" for j in range(len(adjMatrix[i])):\n",
" print_string+=str(adjMatrix[i][j]) + \" \"\n",
" print(str(i) + \" \" + print_string)\n"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"def PrintAdjList(adjList):\n",
" print(\"\\nADJACENCY LIST:\\n\")\n",
" for i in range(len(adjList)):\n",
" print_line = ''\n",
" for j in range(len(adjList[i])):\n",
" print_line += str(adjList[i][j]) + \" \"\n",
" print(str(i) + \": \" + print_line)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ADJACENCY MATRIX:\n",
"\n",
" 0 1 2 3 4 5 6 7 8 9\n",
"0 1 1 0 0 0 0 1 0 0 0 \n",
"1 0 1 0 1 0 1 0 1 1 0 \n",
"2 0 0 1 0 0 1 1 1 1 1 \n",
"3 0 1 0 1 0 1 1 0 0 0 \n",
"4 0 0 1 1 1 1 1 0 1 1 \n",
"5 1 0 0 0 1 1 0 1 0 1 \n",
"6 0 1 0 0 1 1 0 0 0 0 \n",
"7 0 0 0 0 1 0 1 0 0 0 \n",
"8 0 0 0 1 0 0 1 1 0 1 \n",
"9 1 1 1 1 0 1 0 0 0 1 \n",
"\n",
"ADJACENCY LIST:\n",
"\n",
"0: 0 1 6 \n",
"1: 1 3 5 7 8 \n",
"2: 2 5 6 7 8 9 \n",
"3: 1 3 5 6 \n",
"4: 2 3 4 5 6 8 9 \n",
"5: 0 4 5 7 9 \n",
"6: 1 4 5 \n",
"7: 4 6 \n",
"8: 3 6 7 9 \n",
"9: 0 1 2 3 5 9 \n"
]
}
],
"source": [
"main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.5 64-bit",
"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.10.5"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}