Пример #1
0
with open('jobsPC_2.txt', 'r') as f:
    #with open('jobsPC.txt', 'r') as f:
    N = int(f.readline().strip())  # N is number of jobs to schedule
    # build digraph to model job sequencing: 1 src, 1 target, each job has 2 vertices (start, end)
    G = EdgeWeightedDigraph(N * 2 + 2)
    src = 2 * N
    sink = 2 * N + 1

    lines = f.read().split('\n')  # drop last line which is ''
f.close()
job = 0  # 'job' is job index
for line in lines:
    fields = line.split()  # split line on whitepace
    duration = float(fields[0])
    G.addEdge(DEdge(job, job + N,
                    duration))  # add edge betw 'start of job' and 'end of job'
    G.addEdge(DEdge(src, job, 0))
    G.addEdge(DEdge(job + N, sink, 0))
    succ = map(int, fields[1:])
    for w in succ:
        # add precedence edges
        G.addEdge(DEdge(job + N, w, 0))
    job += 1

lp = AcyclicLP(G, src)  # checks whether G is a DAG
print 'Start times:'
for i in range(N):
    print '%4d: %5.1f\n' % (i, lp.distTo(i))
print 'Finish time: %5.1f\n' % lp.distTo(sink)
from GraphLib import EdgeWeightedDigraph
from BaseClassLib import GraphBase
from DirectedEdge import DEdge 		# directed, weighted edge
from LongestPath import AcyclicLP 
from ShortestPath import AcyclicSP	

with open('tinyEWDAG.txt', 'r') as f:
	V = int(f.readline().strip())
	E = int(f.readline().strip())
	text = f.read()
f.close()

#dag = EdgeWeightedDigraph(V)
class DAG(GraphBase):
	pass
dag = DAG.graphfactory(V, directed=True, weighted=True)
lines = text.split('\n')
for line in lines[:-1]:		# last line is empty
	l = line.split()
	v = int(l[0])
	w = int(l[1])
	weight = float(l[2])
	dag.addEdge(DEdge(v, w, weight))

#find the longest path from 0 => 5
d = AcyclicLP(dag, 5)
d.distTo(0)

#find the shortest path from 0 => 5
sh = AcyclicSP(dag, 5)
sh.distTo(0)
Пример #3
0
f.close()
job = 0 												# 'job' is job index
for line in lines:
	fields = line.split() 								# split line on whitepace
	duration = float(fields[0])
	G.addEdge(DEdge(job, job + N, duration))			# add edge betw 'start of job' and 'end of job'
	G.addEdge(DEdge(src, job, 0))
	G.addEdge(DEdge(job + N, sink, 0))
	succ = map(int, fields[1:])
	for w in succ:
		# add precedence edges
		G.addEdge(DEdge(job + N, w, 0))
	job += 1

lp = AcyclicLP(G, src) 									# checks whether G is a DAG
print 'Start times:'
for i in range(N):
	print '%4d: %5.1f\n' % (i, lp.distTo(i))
print 'Finish time: %5.1f\n' % lp.distTo(sink)