added longest path detection
parent
f9e23889e9
commit
e0e322304b
|
|
@ -6,3 +6,7 @@
|
||||||
3 Cochin Mumbai 200
|
3 Cochin Mumbai 200
|
||||||
3 Mumbai Chennai 300
|
3 Mumbai Chennai 300
|
||||||
3 Chennai Cochin 500
|
3 Chennai Cochin 500
|
||||||
|
3 Cochin Bangalore 100
|
||||||
|
2 Mumbai Kolkata 600
|
||||||
|
2 Kolkata Bangalore 400
|
||||||
|
3 Kolkata Mumbai 100
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
2 Bangalore Kolkata 200
|
||||||
|
3 Kolkata Mumbai 100
|
||||||
|
3 Kolkata Chennai 100
|
||||||
|
3 Kolkata Cochin 100
|
||||||
|
4 Cochin Bangalore 50
|
||||||
|
|
@ -9,8 +9,12 @@ class Route(object):
|
||||||
self.src = src
|
self.src = src
|
||||||
self.dst = dst
|
self.dst = dst
|
||||||
|
|
||||||
|
def __eq__(self,rt):
|
||||||
|
eq = self.route_id == rt.route_id and self.contract_id == rt.contract_id
|
||||||
|
return eq
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self.route_id)+":"+self.src+" - "+self.dst+" "+self.load
|
return str(self.contract_id)+"-"+str(self.route_id)+":"+self.src+" - "+self.dst+" "+self.load
|
||||||
|
|
||||||
|
|
||||||
class Contract(object):
|
class Contract(object):
|
||||||
|
|
@ -44,53 +48,88 @@ class Transporter(object):
|
||||||
c_dict[c_id].append(Route(route_id,int(c_id),load,src,dst))
|
c_dict[c_id].append(Route(route_id,int(c_id),load,src,dst))
|
||||||
else:
|
else:
|
||||||
c_dict[c_id] = [Route(0,int(c_id),load,src,dst)]
|
c_dict[c_id] = [Route(0,int(c_id),load,src,dst)]
|
||||||
contrants = []
|
contracts = [Contract(int(i),c_dict[i]) for i in sorted(c_dict.keys())]
|
||||||
for c_id in c_dict:
|
|
||||||
contrants.append(Contract(int(c_id),c_dict[c_id]))
|
|
||||||
return contrants
|
|
||||||
|
|
||||||
def routes(self):
|
return contracts
|
||||||
|
|
||||||
|
def contract_routes(self):
|
||||||
all_routes = []
|
all_routes = []
|
||||||
routes_map = {}
|
|
||||||
for c in self.contracts:
|
for c in self.contracts:
|
||||||
for r in c.routes:
|
for r in c.routes:
|
||||||
all_routes.append(r)
|
all_routes.append(r)
|
||||||
if routes_map.has_key(r.src):
|
return all_routes
|
||||||
routes_map[r.src].append(r)
|
|
||||||
else:
|
|
||||||
routes_map[r.src]= [r]
|
|
||||||
for r in all_routes:
|
|
||||||
if not routes_map.has_key(r.dst):
|
|
||||||
routes_map[r.dst]= []
|
|
||||||
return routes_map
|
|
||||||
|
|
||||||
def is_cyclic(self):
|
def contracts_required(self):
|
||||||
return circular_dep(self.routes())
|
routes = self.contract_routes()
|
||||||
|
unfulfilled = rem_routes(routes)
|
||||||
|
print "No of contracts required : ",len(unfulfilled)
|
||||||
|
if len(unfulfilled) > 0:
|
||||||
|
print "Contracts from ",repr(unfulfilled)
|
||||||
|
|
||||||
CYCLE_LIMIT = 3
|
CYCLE_LIMIT = 3
|
||||||
|
|
||||||
def find_loop(elem,dep_elem,deps,checked,path):
|
def rem_routes(routes):
|
||||||
if dep_elem in checked or not deps.has_key(dep_elem):
|
loop_routes = routes
|
||||||
return (False,[])
|
while len(loop_routes)>0:
|
||||||
else:
|
circ,paths,loop_routes = largest_loop(loop_routes)
|
||||||
checked.add(dep_elem)
|
if not circ:
|
||||||
for c in deps[dep_elem]:
|
break
|
||||||
path.append(c)
|
partial_paths = []
|
||||||
if elem == c.dst and len(path) <= CYCLE_LIMIT:
|
partial_routes = loop_routes
|
||||||
return (True,path)
|
while len(partial_routes)>0:
|
||||||
loop_path = find_loop(elem,c.dst,deps,checked,path)
|
lp = longest_path(partial_routes)
|
||||||
if loop_path[0]:
|
partial_paths.append((lp[-1].dst,lp[0].src))
|
||||||
return loop_path
|
partial_routes = filter(lambda x:x not in lp,partial_routes)
|
||||||
|
return partial_paths
|
||||||
|
|
||||||
|
def route_deps(routes):
|
||||||
|
routes_map = {}
|
||||||
|
for r in routes:
|
||||||
|
if routes_map.has_key(r.src):
|
||||||
|
routes_map[r.src].append(r)
|
||||||
else:
|
else:
|
||||||
del(path[-1])
|
routes_map[r.src]= [r]
|
||||||
return (False,[])
|
if not routes_map.has_key(r.dst):
|
||||||
|
routes_map[r.dst]= []
|
||||||
|
return routes_map
|
||||||
|
|
||||||
def circular_dep(deps):
|
def largest_loop(routes):
|
||||||
dep_state = []
|
deps = route_deps(routes)
|
||||||
for k in deps.keys():
|
circ_paths = [find_loop(r.src,r.src,deps,set(),[]) for r in routes]
|
||||||
circ_dep_k = find_loop(k,k,deps,set(),[])
|
circ,paths = max(circ_paths,key=lambda p:len(p[1]))
|
||||||
# print path
|
# print filter(lambda p:not p[0],circ_paths)
|
||||||
dep_state.append((k,circ_dep_k))
|
rem_paths = filter(lambda x:x not in paths,routes)
|
||||||
return dep_state
|
return (circ,paths,rem_paths)
|
||||||
|
|
||||||
print Transporter.from_file('./contracts.txt').is_cyclic()
|
def longest_path(routes):
|
||||||
|
def route_path(r,deps):
|
||||||
|
if not deps.has_key(r.dst) or len(deps[r.dst])==0:
|
||||||
|
return [r]
|
||||||
|
all_dep_paths = [([r]+route_path(rt,deps)) for rt in deps[r.dst]]
|
||||||
|
rp = max(all_dep_paths,key=lambda p:len(p))
|
||||||
|
return rp
|
||||||
|
deps = route_deps(routes)
|
||||||
|
all_paths = [route_path(r,deps) for r in routes]
|
||||||
|
longest = max(all_paths,key=len)
|
||||||
|
return longest
|
||||||
|
|
||||||
|
def find_loop(elem,dep_elem,deps,checked,path):
|
||||||
|
found_loop = False
|
||||||
|
if dep_elem not in checked and deps.has_key(dep_elem):
|
||||||
|
checked.add(dep_elem)
|
||||||
|
for c in deps[dep_elem]:
|
||||||
|
path.append(c)
|
||||||
|
if elem == c.dst and len(path) <= CYCLE_LIMIT:
|
||||||
|
found_loop = True
|
||||||
|
break
|
||||||
|
(loop,path) = find_loop(elem,c.dst,deps,checked,path)
|
||||||
|
if loop:
|
||||||
|
found_loop = True
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
del(path[-1])
|
||||||
|
return (found_loop,path)
|
||||||
|
|
||||||
|
|
||||||
|
# Transporter.from_file('./contracts.txt').contracts_required()
|
||||||
|
Transporter.from_file('./contracts.txt').contracts_required()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue