def dijkstra(V, adj, W, s, infinity):
    d,p = __initialise_sssp(V, s, infinity)
    S = []
    node = {}
    Q = heap.make_heap()
    for u in V:
        node[u] = heap.make_node(VertexWeight(u, d[u]))
        heap.insert(Q, node[u])
    while not heap.is_empty(Q):
        x = heap.extract(Q)
        u = x.key.get_vertex()
        if DEBUG: print "visit", u
        S.append(u)
        for v in adj[u]:
            __relax(u, v, W, d, p)
            heap.decrease_key(Q, node[v], VertexWeight(v, d[v]))
    return (d, p)
Esempio n. 2
0
def dijkstra(V, adj, W, s, infinity):
    d, p = __initialise_sssp(V, s, infinity)
    S = []
    node = {}
    Q = heap.make_heap()
    for u in V:
        node[u] = heap.make_node(VertexWeight(u, d[u]))
        heap.insert(Q, node[u])
    while not heap.is_empty(Q):
        x = heap.extract(Q)
        u = x.key.get_vertex()
        if DEBUG: print "visit", u
        S.append(u)
        for v in adj[u]:
            __relax(u, v, W, d, p)
            heap.decrease_key(Q, node[v], VertexWeight(v, d[v]))
    return (d, p)
def prim(V, E, W, r):

    # initialise adj
    adj = graph.make_adj(V, E)

    # determine appropriate infinity
    max_weight = None
    for (u, v) in E:
        if max_weight == None or max_weight < W[(u, v)]:
            max_weight = W[(u, v)]
    infinity = max_weight + 10

    node = {}
    parent = {}
    inqueue = {}
    for u in V:
        if u == r:
            uw = VertexWeight(u, 0)
        else:
            uw = VertexWeight(u, infinity)
        node[u] = heap.make_node(uw)
        parent[u] = None
    Q = heap.make_heap()
    for u in V:
        heap.insert(Q, node[u])
        inqueue[u] = True
    while not heap.is_empty(Q):
        if DEBUG:
            print "--------------------------------------------------"
            heap.show_heap(Q)
        x = heap.extract(Q)
        u, w = x.key.get_info()
        assert (node[u].key.get_vertex() == u
                and node[u].key.get_weight() == w)
        inqueue[u] = False
        for v in adj[u]:
            if inqueue[v] and W[(u, v)] < node[v].key.weight:
                parent[v] = u
                heap.decrease_key(Q, node[v], VertexWeight(v, W[(u, v)]))
    return parent
Esempio n. 4
0
def prim(V, E, W, r):
        
    # initialise adj
    adj = graph.make_adj(V, E)

    # determine appropriate infinity
    max_weight = None
    for (u,v) in E:
        if max_weight == None or max_weight < W[(u,v)]:
            max_weight = W[(u,v)]
    infinity = max_weight + 10

    node = {}
    parent = {}
    inqueue = {}
    for u in V:
        if u == r:
            uw = VertexWeight(u, 0)
        else:
            uw = VertexWeight(u, infinity)
        node[u] = heap.make_node(uw)
        parent[u] = None
    Q = heap.make_heap()
    for u in V:
        heap.insert(Q, node[u])
        inqueue[u] = True
    while not heap.is_empty(Q):
        if DEBUG:
            print "--------------------------------------------------"
            heap.show_heap(Q)
        x = heap.extract(Q)
        u, w = x.key.get_info()
        assert(node[u].key.get_vertex() == u and node[u].key.get_weight() == w)
        inqueue[u] = False
        for v in adj[u]:
            if inqueue[v] and W[(u,v)] < node[v].key.weight:
                parent[v] = u
                heap.decrease_key(Q, node[v], VertexWeight(v, W[(u,v)]))
    return parent
Esempio n. 5
0
def max_aug_flow(n,s,t,Capacity,Flow,p):
	frozen=[-1 for x in range(n)]
	unfrozen=[-1 for x in range(n)]
	unfrozen_node_list=[-1 for x in range(n)]
	frozen_node_list=[]

	#freeze source
	frozen[0]=1;

	#initialize fib heap
	Uf_heap = make_heap()

	#first for source s.
	source_node= newNode(0,0,0)
	frozen_node_list.append(source_node)
	#unfrozen_node_list.append(source_node)
	new_s = s #initialize with source node 

	while new_s!=t :
		for j in range(0,n):
			if Capacity[new_s][j]<0 :
				if p[j]!= -1 :

					if Capacity[new_s][j] < Capacity[p[j]][j] :
						p[j]=new_s #updates parent list 
						unfrozen_node_list[j].key = Capacity[new_s][j]
						

				else:
					node = newNode(Capacity[new_s][j],j,new_s)
					unfrozen_node_list[j]=node
					unfrozen[j]= 1
					#print "added new node j"
					insert(Uf_heap, node)
					p[j]=new_s 

		ret_node= extract(Uf_heap)
		if ret_node == None:
			return 0
		frozen[ret_node.vertex]= 1 
		frozen_node_list.append(ret_node)
		unfrozen[ret_node.vertex]= -1
		new_s= ret_node.vertex

		#say no node left in unfrozen node list, hence see if everything is -1


	p[0]=-1
	flow=[]


	#Need to trace path from sink to source using parent p list
	trace_parent_index=n-1
	while p[trace_parent_index] != -1 :
		curr_flow=Capacity[p[trace_parent_index]][trace_parent_index]
		if curr_flow <0 :
			flow.append(curr_flow)
		
		trace_parent_index = p[trace_parent_index]

	#Accordingly update a new flow list 
	#Find the min. of flow list (or max. because negative capacities)
	#Min. is the maximum flow 
	final_flow = max(flow)
	return -final_flow



		
Esempio n. 6
0
def max_aug_flow(n,s,t,Capacity,Flow,p, No_Edge_Sat_Max_Flow):
	frozen=[-1 for x in range(n)]
	unfrozen=[-1 for x in range(n)]
	unfrozen_node_list=[-1 for x in range(n)]
	frozen_node_list=[]

	#p = [-1 for x in range(n)]
	print "Current source is: ", s
	print "Current sink is: ", t
	print "input parent list is: ", p
	print "input capacity graph is: ", Capacity
	#freeze source
	frozen[0]=1;

	#initialize fib heap
	Uf_heap = make_heap()

	#first for source s.
	source_node= newNode(0,s,0)
	frozen_node_list.append(source_node)
	#unfrozen_node_list.append(source_node)
	new_s = s #initialize with source node 

	temp_var = 0
	while new_s!=t :
		temp_var +=1
		for j in range(0,n):
			print "edge capacity for", new_s," : ",j, "is" ,Capacity[new_s][j]
			if Capacity[new_s][j]<0 :
				#test call
				print "this j is ", j, "and parent is: ", p[j]
				if p[j]!= -1 :
					if Capacity[new_s][j] < Capacity[p[j]][j] :
						p[j]=new_s #updates parent list 
						print "Current j is " , j
						unfrozen_node_list[j].key = Capacity[new_s][j]
						

				else:
					node = newNode(Capacity[new_s][j],j,new_s)
					print "new node ", j, "added" 
					unfrozen_node_list[j]=node
					unfrozen[j]= 1
					#print "added new node j"
					insert(Uf_heap, node)
					p[j]=new_s 

		print "parent list is : ", p
		ret_node= extract(Uf_heap)
		
		if ret_node == None:
			print "temp_var is: ", temp_var
			return 0

		print "returned node is ", ret_node.vertex 

		frozen[ret_node.vertex]= 1 
		frozen_node_list.append(ret_node)
		unfrozen[ret_node.vertex]= -1
		new_s= ret_node.vertex
		print "new_s is: ", new_s
		#say no node left in unfrozen node list, hence see if everything is -1


	p[s]=-1
	flow=[]


	#Need to trace path from sink to source using parent p list
	trace_parent_index=t

	print "n: ", n , " and current sink: ", t 

	max_curr_flow = -1000
	while p[trace_parent_index] != -1 :
		curr_flow=Capacity[p[trace_parent_index]][trace_parent_index]
		print "curr_flow: ", curr_flow
		if curr_flow <0 :
			flow.append(curr_flow)

		'''#store u and v for saturated edge(u,v)
		if curr_flow > max_curr_flow:
			sat_u = p[trace_parent_index] #store u and v for saturated edge(u,v)
			sat_v = trace_parent_index    '''
		
		trace_parent_index = p[trace_parent_index]

	#No_Edge_Sat_Max_Flow[sat_u][sat_v] += 1

	#Separate loop and not using only the bottleneck capacity because multiple edges with same capacity might get saturated 
	v = t 
	while p[v] != -1 :
		if max(flow) == Capacity[p[v]][v]:
			No_Edge_Sat_Max_Flow[p[v]][v] += 1 
		v = p[v]

	print "flow ", flow 
	print "parent here too: ", p
	#Accordingly update a new flow list 
	#Find the min. of flow list (or max. because negative capacities)
	#Min. is the maximum flow 
	final_flow = max(flow)
	return -final_flow
	#return -final_flow, sat_u, sat_v




		
Esempio n. 7
0
def max_aug_flow(n, s, t, Capacity, Flow, p, No_Edge_Sat_Max_Flow):
    frozen = [-1 for x in range(n)]
    unfrozen = [-1 for x in range(n)]
    unfrozen_node_list = [-1 for x in range(n)]
    frozen_node_list = []

    #p = [-1 for x in range(n)]
    print "Current source is: ", s
    print "Current sink is: ", t
    print "input parent list is: ", p
    print "input capacity graph is: ", Capacity
    #freeze source
    frozen[0] = 1

    #initialize fib heap
    Uf_heap = make_heap()

    #first for source s.
    source_node = newNode(0, s, 0)
    frozen_node_list.append(source_node)
    #unfrozen_node_list.append(source_node)
    new_s = s  #initialize with source node

    temp_var = 0
    while new_s != t:
        temp_var += 1
        for j in range(0, n):
            print "edge capacity for", new_s, " : ", j, "is", Capacity[new_s][
                j]
            if Capacity[new_s][j] < 0:
                #test call
                print "this j is ", j, "and parent is: ", p[j]
                if p[j] != -1:
                    if Capacity[new_s][j] < Capacity[p[j]][j]:
                        p[j] = new_s  #updates parent list
                        print "Current j is ", j
                        unfrozen_node_list[j].key = Capacity[new_s][j]

                else:
                    if j != s:
                        node = newNode(Capacity[new_s][j], j, new_s)
                        print "new node ", j, "added"
                        unfrozen_node_list[j] = node
                        unfrozen[j] = 1
                        #print "added new node j"
                        insert(Uf_heap, node)
                        p[j] = new_s

        print "parent list is : ", p
        ret_node = extract(Uf_heap)

        if ret_node == None:
            print "temp_var is: ", temp_var
            return 0

        print "returned node is ", ret_node.vertex

        frozen[ret_node.vertex] = 1
        frozen_node_list.append(ret_node)
        unfrozen[ret_node.vertex] = -1
        new_s = ret_node.vertex
        print "new_s is: ", new_s
        #say no node left in unfrozen node list, hence see if everything is -1

    p[s] = -1
    flow = []

    #Need to trace path from sink to source using parent p list
    trace_parent_index = t

    print "Current source is :", s, " and current sink: ", t

    max_curr_flow = -1000
    while p[trace_parent_index] != -1:
        curr_flow = Capacity[p[trace_parent_index]][trace_parent_index]
        print "curr_flow: ", curr_flow
        if curr_flow < 0:
            flow.append(curr_flow)
        '''#store u and v for saturated edge(u,v)
		if curr_flow > max_curr_flow:
			sat_u = p[trace_parent_index] #store u and v for saturated edge(u,v)
			sat_v = trace_parent_index    '''

        trace_parent_index = p[trace_parent_index]

    #No_Edge_Sat_Max_Flow[sat_u][sat_v] += 1

    #Separate loop and not using only the bottleneck capacity because multiple edges with same capacity might get saturated
    v = t
    while p[v] != -1:
        if max(flow) == Capacity[p[v]][v]:
            No_Edge_Sat_Max_Flow[p[v]][v] += 1
        v = p[v]

    print "flow ", flow
    print "parent here too: ", p
    #Accordingly update a new flow list
    #Find the min. of flow list (or max. because negative capacities)
    #Min. is the maximum flow
    final_flow = max(flow)
    return -final_flow
Esempio n. 8
0
def max_aug_flow(n, s, t, Capacity, Flow, p):
    frozen = [-1 for x in range(n)]
    unfrozen = [-1 for x in range(n)]
    unfrozen_node_list = [-1 for x in range(n)]
    frozen_node_list = []

    #p = [-1 for x in range(n)]
    print "input parent list is: ", p
    print "input capacity graph is: ", Capacity
    #freeze source
    frozen[0] = 1

    #initialize fib heap
    Uf_heap = make_heap()

    #first for source s.
    source_node = newNode(0, s, 0)
    frozen_node_list.append(source_node)
    #unfrozen_node_list.append(source_node)
    new_s = s  #initialize with source node

    temp_var = 0
    while new_s != t:
        temp_var += 1
        for j in range(0, n):
            print "edge capacity for", new_s, " : ", j, "is", Capacity[new_s][
                j]
            if Capacity[new_s][j] < 0:
                #test call
                print "this j is ", j, "and parent is: ", p[j]
                if p[j] != -1:
                    if Capacity[new_s][j] < Capacity[p[j]][j]:
                        p[j] = new_s  #updates parent list
                        print "Current j is ", j
                        unfrozen_node_list[j].key = Capacity[new_s][j]

                else:
                    node = newNode(Capacity[new_s][j], j, new_s)
                    print "new node ", j, "added"
                    unfrozen_node_list[j] = node
                    unfrozen[j] = 1
                    #print "added new node j"
                    insert(Uf_heap, node)
                    p[j] = new_s

        print "parent list is : ", p
        ret_node = extract(Uf_heap)

        if ret_node == None:
            print "temp_var is: ", temp_var
            return 0

        print "returned node is ", ret_node.vertex

        frozen[ret_node.vertex] = 1
        frozen_node_list.append(ret_node)
        unfrozen[ret_node.vertex] = -1
        new_s = ret_node.vertex
        print "new_s is: ", new_s
        #say no node left in unfrozen node list, hence see if everything is -1

    p[0] = -1
    flow = []

    #Need to trace path from sink to source using parent p list
    trace_parent_index = n - 1

    while p[trace_parent_index] != -1:
        curr_flow = Capacity[p[trace_parent_index]][trace_parent_index]
        if curr_flow < 0:
            flow.append(curr_flow)

        trace_parent_index = p[trace_parent_index]

    print "parent here too: ", p
    #Accordingly update a new flow list
    #Find the min. of flow list (or max. because negative capacities)
    #Min. is the maximum flow
    final_flow = max(flow)
    return -final_flow
Esempio n. 9
0
def max_aug_flow(n,s,t,Capacity,Flow,p):
	frozen=[-1 for x in range(n)]
	unfrozen=[-1 for x in range(n)]
	unfrozen_node_list=[-1 for x in range(n)]
	frozen_node_list=[]

	#p = [-1 for x in range(n)]
	print "input parent list is: ", p
	print "input capacity graph is: ", Capacity
	#freeze source
	frozen[0]=1;

	#initialize fib heap
	Uf_heap = make_heap()

	#first for source s.
	source_node= newNode(0,s,0)
	frozen_node_list.append(source_node)
	#unfrozen_node_list.append(source_node)
	new_s = s #initialize with source node 

	temp_var = 0
	while new_s!=t :
		temp_var +=1
		for j in range(0,n):
			print "edge capacity for", new_s," : ",j, "is" ,Capacity[new_s][j]
			if Capacity[new_s][j]<0 :
				#test call
				print "this j is ", j, "and parent is: ", p[j]
				if p[j]!= -1 :
					if Capacity[new_s][j] < Capacity[p[j]][j] :
						p[j]=new_s #updates parent list 
						print "Current j is " , j
						unfrozen_node_list[j].key = Capacity[new_s][j]
						

				else:
					node = newNode(Capacity[new_s][j],j,new_s)
					print "new node ", j, "added" 
					unfrozen_node_list[j]=node
					unfrozen[j]= 1
					#print "added new node j"
					insert(Uf_heap, node)
					p[j]=new_s 

		print "parent list is : ", p
		ret_node= extract(Uf_heap)
		
		if ret_node == None:
			print "temp_var is: ", temp_var
			return 0

		print "returned node is ", ret_node.vertex 

		frozen[ret_node.vertex]= 1 
		frozen_node_list.append(ret_node)
		unfrozen[ret_node.vertex]= -1
		new_s= ret_node.vertex
		print "new_s is: ", new_s
		#say no node left in unfrozen node list, hence see if everything is -1


	p[0]=-1
	flow=[]


	#Need to trace path from sink to source using parent p list
	trace_parent_index=n-1

	while p[trace_parent_index] != -1 :
		curr_flow=Capacity[p[trace_parent_index]][trace_parent_index]
		if curr_flow <0 :
			flow.append(curr_flow)
		
		trace_parent_index = p[trace_parent_index]

	print "parent here too: ", p
	#Accordingly update a new flow list 
	#Find the min. of flow list (or max. because negative capacities)
	#Min. is the maximum flow 
	final_flow = max(flow)
	return -final_flow



		
Esempio n. 10
0
		first_iter= False
		if curr.vertex == ind :
			curr.key= Capacity[new_ind][ind]
			break  #break statements because only one such vertex is gonna be there  
		if curr.child != None:
			if curr.vertex == ind: 
				curr.key= Capacity[new_ind][ind]
				break
		curr= curr.right


#freeze source
frozen[0]=1;

#initialize fib heap
Uf_heap = make_heap()

# Define source s and sink t
s=0
t=n-1

#first for source s.
source_node= newNode(0,0,0) #need to think about parent vertex of s, keep -1 but that might mess up later 
frozen_node_list.append(source_node)
for j in range(0,n):
	if Capacity[0][j]<0:
		#add corres. j nodes to unfrozen heap, i.e. make nodes for them in heap
		node = newNode(Capacity[0][j],j,0)
		unfrozen_node_list[j]=node #Does this thing pass a reference to node into the list or does it pass a copy?
		insert(Uf_heap, node)
		p[j]=0