CS curriculum consists n courses, all mandatory. prerequisite graph G node each course, edge course v course w if and only if v prerequisite w. directly graph representation, computes minimum number semesters necessary complete curriculum student can take number courses one semester. running time your algorithm should linear. Input Format: input text file which describes prerequisite graph G. prerequisite graph G DAG. first line file specifies n & m, next m lines specify m edges graph, one line per edge. edge described end points. number vertices n, assume vertices graph numbered (0,1,..n-1). Output Format: Print minimum number of semesters required complete curriculum, given prerequisite graph, minimum number semesters will fixed number, there may be more one way taking courses each semesters. Sample Input ( graph shown right): 9 8 0 3 1 3 2 4 3 5 4 5 5 6 6 7 5 8 Sample Output Minimum number semesters: 5 Sem 1: 0, 1, 2 Sem 2: 3, 4 Sem 3: 5 Sem 4: 6, 8 Sem 5: 7
import networkx as nx
import math
def strongRelation(n, m):
G = nx.Graph()
p = math.ceil(m / n)
edges_m = []
for j in range(p):
for i in range(n):
if m > 0:
if i + j >= n - 1:
edges_m.append((i, i + j + 1 - n))
else:
edges_m.append((i, i + j + 1))
m = m - 1
G.add_edges_from(edges_m)
cliques = nx.find_cliques(G)
cliques1 = [clq for clq in cliques]
return max([len(i) for i in cliques1])
strongRelation(5, 10)
Comments
Leave a comment