## assignment 6 | Python

April 26th, 2021 10:31:34 PM

```					"""
Group 5 - Brandon Sharp, Austin Leath, Hamed Jalali

Assignment 6

The goal is to make a graph of
who bit who and who was bitten.
There should be 10 nodes and 15 edges.
3 arrows of biting each other and
3 arrows of someone biting themselves.
Networkx can not do self biting
arrows, but it is in the code.
"""

from graphviz import Digraph as DDotGraph
from graphviz import Graph as UDotGraph
import networkx as nx
from networkx.algorithms.dag import transitive_closure
import graphviz as gv
import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import matrix_power

"""
class DGraph:
def __init__(self):
self.d = dict()

def clear(self):
self.d = dict()

if not self.d.get(n):
self.d[n] = set()

f,t=e
vs=self.d.get(f)
if not vs:
self.d[f] = {t}
else:

for e in es:

def edges(self):
for f in self.d:
for t in self.d[f]:
yield (f,t)

def number_of_nodes(self):
return len(self.d)

def __repr__(self):
return self.d.__repr__()

def show(self):
dot = gv.Digraph()
for e in self.edges():
#print(e)
f, t = e
dot.edge(str(f), str(t), label='')
#print(dot.source)
show(dot)

# displays graph with graphviz
def show(dot, show=True, file_name='graph.gv'):
dot.render(file_name, view=show)

def showGraph(g,label="",directed=True):
if directed:
dot = gv.Digraph()
else:
dot = gv.Graph()

for e in g.edges():
print(e)
f, t = e
dot.edge(str(f), str(t), label=label)
print(dot.source)
show(dot)

def bit():
G = DGraph()
showGraph(G, label="bit")

bit()

def bitten():
G=DGraph()
showGraph(G, label="bitten by")

#bitten()

"""

#Do transitive closure call out and the
#matrix power operation should be the same
D = nx.DiGraph()

T = transitive_closure(D)

for e in D.edges(): print(e)
for n in D.nodes(): print(n)

def show(H):
nx.draw(H, with_labels=True, font_weight='bold')
plt.show()

M = nx.to_numpy_matrix(D)
MT = nx.to_numpy_matrix(T)
M2 = [email protected]

def mPower(M, k): #M is numpy matrix
assert k >= 1
P = M
for _ in range(k):
P = P @ M
return P

def tc(M):
#compute transitive closure
pass

D1 = nx.DiGraph(M)
D2 = nx.DiGraph(M2)

print('Matrix for Original\n', M)
N = nx.to_numpy_array(D,dtype=int)
print('np_array for Original\n', N)
print('\nMatrix for Transitive Closure\n', MT)
N2 = nx.to_numpy_array(T,dtype=int)
print('np_array for Transitive Closure\n', N2)

show(D) #can use D, T, and numpy matrix power operation
show(T)
show(T)
```