69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
# 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 didn’t 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() |