Exemplo n.º 1
0
        G.node[nextNode]["color"] = smalColr

        nextNode = find_next_vertex(G, nextNode)
        visited_counter += 1

    print()
    for i in G.nodes():
        print('vertex', i, ': color', G.node[i]['color'])
    print()
    print('The number of colors that Greedy computed is:', kmax)
    print()


print('Graph G1:')
G=graph1.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)


print('Graph G2:')
G=graph2.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)



print('Graph G3:')
G=graph3.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)
Exemplo n.º 2
0
                    print('node', i, ', color:', G.node[i]['color'])


def brute_force(G, k):
    global colored
    global color_count
    
    color_count = 0
    colored = 0
    recursion(G,1)
    if color_count == 0:
        print('The graph is not ' + str(k) + '-colorable.')


print()
G1=graph1.Graph()
print('Graph G1:')
k = int(input("Please enter a value of k: "))
brute_force(G1,k)
print()


G2=graph2.Graph()
print('Graph G2:')
k = int(input("Please enter a value of k: "))
brute_force(G2,k)
print()


G3=graph3.Graph()
print('Graph G3:')
Exemplo n.º 3
0
def find_next_vertex(G):










def find_smallest_color(G,i):
    n = len(G.nodes())









def greedy(G):
    n = len(G.nodes())
    global kmax
    global visited_counter











    print()
    for i in G.nodes():
        print('vertex', i, ': color', G.node[i]['color'])
    print()
    print('The number of colors that Greedy computed is:', kmax)
    print()



print('Graph G1:')
G=graph1.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)


print('Graph G2:')
G=graph2.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)


print('Graph G3:')
G=graph3.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)


print('Graph G4:')
G=graph4.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)


print('Graph G5:')
G=graph5.Graph()
G.add_nodes_from(G.nodes(), visited = 'no')
greedy(G)
Exemplo n.º 4
0
def Main():
	#args = processCmdLine()
	g = graph1.Graph()


    #######################################################
    ##    code when graph is given as                    ##
    ##    src,dest,edge cost                             ##
    #######################################################


	with open("advogato.txt" , "r") as f:
	    for line in f:
	        word= line.split()
	        g.add_edge(int(word[0]),int(word[1]),float(word[2]))
	    no_of_nodes = g.no_of_nodes+1
	    
	    f.close()

    ##########################################################
    ##                    end                               ##
    ##                                                      ##
    ##########################################################
	

    ##########################################################
    ##       code when graph is taken as matrix             ##
    ##                                                      ##
    ##########################################################

	# with open("wolf.csv" , "r") as f:
	#     i=0
	#     for line in f:
	#         word=line.split()
	#         p=0
	#         for x in word:
	#             if int(x)!=0:
	#                 g.add_edge(i,p,int(x))
	#             p=p+1
	#         i=i+1
	#     no_of_nodes = g.no_of_nodes+1
	#     print(no_of_nodes)
	#     f.close()
	# g.print_graph()

	##########################################################
    ##                    end                               ##
    ##                                                      ##
    ##########################################################

	num_particles = 100
	maxiter = 50
	start_node = 1
	end_node= 450
	maximum_path_length = 1000
	
	  
	start_time=time.time()
	
	pso=PSO(num_particles, maxiter,no_of_nodes,start_node,end_node,g,maximum_path_length)
	pso_time = time.time() - start_time
	

	print("OPtimum path---")
	for x in pso.global_path:
	    print(x,end=" ")
	print("\n")
	cost=pso.cost_fun(g,pso.global_path)
	print("cost=",cost)
	print("Total number of nodes is graph = ", no_of_nodes)
	print("Number of particle = ", num_particles)
	print("Number of iteration = ", maxiter)
	print("Time taken by pso in seconds ---", pso_time)

	start_time=time.time()
	print("shortest cost is =", isReachable(g,start_node,end_node))
	dijstra_time=time.time() - start_time
	print("Time taken by dijstra algo in  seconds ---",  dijstra_time)