Example #1
0
# find cycle in a weighted digraph
from GraphLib import EdgeWeightedDigraph
from DirectedEdge import DEdge  # directed, weighted edge

# build an edge-weighted digraph
#with open('tinyEWDigraph.txt', 'r') as f:
with open('mediumEWDigraph.txt', 'r') as f:
    V = int(f.readline().strip())
    E = int(f.readline().strip())
    text = f.read()
f.close()
#G = EdgeWeightedDigraph(V)
MG = EdgeWeightedDigraph(V)
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])
    #G.addEdge(DEdge(v, w, weight))
    MG.addEdge(DEdge(v, w, weight))
#print G.E() == E
print MG.E() == E

from DirectedCycle import EdgeWeightedDC
finder = EdgeWeightedDC(MG)
if finder.hasCycle(): finder.cycle()
else:
    print "no cycle"
# file format:
# N_jobs
# job_duration	[list of successor jobs]
# ...
from GraphLib import EdgeWeightedDigraph
from DirectedEdge import DEdge
from LongestPath import AcyclicLP

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