In: Computer Science
In python
Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
def reachable(G, v, setA): # This function checks if it's
possible to reach w from v
setA|={v}
try:
for w in set(G[v])-setA:reachable(G,w,setA)
except KeyError:
donothing = 0
return setA
## 2a ##
def reachable_vertices(G, v):
setA=set()
setA|={v}
try:
for n in set(G[v])-setA:reachable(G,n,setA)
except KeyError:
donothing = 0
return setA
def cfb_graph():
svertex = []
evertex = []
count= 0
file = open("cfb2010.csv","r")
for line in file:
fields = line.split(",")
if fields[5].replace("\n", "") == 'W':
svertex.append(fields[1])
evertex.append(fields[2])
if count == 0:
count = count +1
graph = {}
for i in range(len(svertex)):
v = svertex[i]
if v in graph:
graph[v] |= set([evertex[i]])
else:
graph[v] = set([evertex[i]])
for key, value in graph.items():
graph[key] = dict.fromkeys(value,1)
return(graph)
######Part 2 c############
auburn_answer =
list(set(cfb_graph().keys()).difference(set(reachable_vertices(cfb_graph(),
"Auburn"))))
notre_dame_answer = reachable_vertices(cfb_graph(), "Notre
Dame")
alabama_answer =
list(set(cfb_graph().keys()).difference(set(reachable_vertices(cfb_graph(),
"Alabama"))))
Note: Plzzz don' t give dislike.....Plzzz comment if u have any problem i will try to resolve it.......