import os import copy import numpy as np from Graph_tool import multigraph as mg from Graph_tool import direct_multigraph as dmg G_29 = dmg(6, 6) Graph_dir = r'C:\Users\Mr Nam\Desktop\TP\Graph' Graph_1_29_dir = os.path.join(Graph_dir, 'Graph_1_29') G_29.read_nodes(os.path.join(Graph_1_29_dir, 'nodes.txt')) G_29.read_arcs(os.path.join(Graph_1_29_dir, 'arcs.txt')) G_29.read_adjMatrix(os.path.join(Graph_1_29_dir, 'adjMatrix.txt')) G_29.read_inInMatrix(os.path.join(Graph_1_29_dir, 'inInMatrix.txt')) G_29.read_outInMatrix(os.path.join(Graph_1_29_dir, 'outInMatrix.txt')) def ROYWARSHALL(adjMatrix): m = copy.deepcopy(adjMatrix) for i in range(len(m)): m[i, i] = 1 for k in range(len(m)): for i in range(len(m)): for j in range(len(m)): m[i][j] = m[i][j] or (m[i][k] and m[k][j]) return m print(ROYWARSHALL(G_29.adjMatrix))
# In[2]: Graph_dir = r'C:\Users\Mr Nam\Desktop\TP\Graph' os.listdir(Graph_dir) # In[3]: Graph_1_1_dir = os.path.join(Graph_dir, 'Graph_1_1') os.listdir(Graph_1_1_dir) # # Graph 1.1 (trang 2) # In[3]: G = dmg(5, 10) G.set_node(["1", "2", "3", "4", "5"]) G.set_arc([ "(1,2)", "(2,2)", "(2,3)", "(2,5)", "(3,5)", "(4,4)", "(4,1)", "(4,5)", "(5,3)", "(5,4)" ]) # In[35]: G.write_nodes(os.path.join(Graph_1_1_dir, 'nodes.txt')) G.write_arcs(os.path.join(Graph_1_1_dir, 'arcs.txt')) # In[4]: G.generate_adjMatrix() G.generate_in_out_InMatrix()
import sys import os import numpy as np from Graph_tool import multigraph as mg from Graph_tool import direct_multigraph as dmg Graph_dir = r'C:\Users\Mr Nam\Desktop\TP\Graph' Graph_1_18_dir = os.path.join(Graph_dir, 'Graph_1_18') Graph_1_12_dir = os.path.join(Graph_dir, 'Graph_1_12') G_18 = dmg(6, 9) G_18.read_nodes(os.path.join(Graph_1_18_dir, 'nodes.txt')) G_18.read_arcs(os.path.join(Graph_1_18_dir, 'arcs.txt')) G_18.read_adjMatrix(os.path.join(Graph_1_18_dir, 'adjMatrix.txt')) G_18.read_inInMatrix(os.path.join(Graph_1_18_dir, 'inInMatrix.txt')) G_18.read_outInMatrix(os.path.join(Graph_1_18_dir, 'outInMatrix.txt')) G_18.read_Weight(os.path.join(Graph_1_18_dir, 'WeightMatrix.txt')) G_12 = dmg(6, 8) G_12.read_nodes(os.path.join(Graph_1_12_dir, 'nodes.txt')) G_12.read_arcs(os.path.join(Graph_1_12_dir, 'arcs.txt')) G_12.read_adjMatrix(os.path.join(Graph_1_12_dir, 'adjMatrix.txt')) G_12.read_inInMatrix(os.path.join(Graph_1_12_dir, 'inInMatrix.txt')) G_12.read_outInMatrix(os.path.join(Graph_1_12_dir, 'outInMatrix.txt')) G_12.read_Weight(os.path.join(Graph_1_12_dir, 'WeightMatrix.txt')) def min_distance(dist, sptSet): min_value = sys.maxsize min_index = -1 for v in range(len(dist)):
import os import copy import numpy as np from Graph_tool import multigraph as mg from Graph_tool import direct_multigraph as dmg Graph_dir = r'C:\Users\Mr Nam\Desktop\TP\Graph' Graph_1_18_dir = os.path.join(Graph_dir, 'Graph_1_18') G_18 = dmg(6, 9) G_18.read_nodes(os.path.join(Graph_1_18_dir, 'nodes.txt')) G_18.read_arcs(os.path.join(Graph_1_18_dir, 'arcs.txt')) G_18.read_adjMatrix(os.path.join(Graph_1_18_dir, 'adjMatrix.txt')) G_18.read_inInMatrix(os.path.join(Graph_1_18_dir, 'inInMatrix.txt')) G_18.read_outInMatrix(os.path.join(Graph_1_18_dir, 'outInMatrix.txt')) def TESTINGCYCLICITY(G): def source(node, inInMatrix): if len(node) == 0: return "None" for i in node: d = 0 for y in range(len(inInMatrix[0])): d += inInMatrix[int(i) - 1, y] if d == 0: return i return "None" inInMatrix = np.array(G.inInMatrix) while source(G.Nodes, inInMatrix) != "None": v = source(G.Nodes, inInMatrix) G.Nodes.remove(v)
from Graph_tool import multigraph as mg from Graph_tool import direct_multigraph as dmg G = mg(3, 4) G = mg.set_vertex(G, ["1", "2", "3"]) G = mg.set_edge(G, ["12", "23", "22", "33"]) mg.set_adjMatrix(G, 0, 1, 1) mg.set_adjMatrix(G, 1, 2, 1) mg.set_adjMatrix(G, 1, 1, 1) mg.set_adjMatrix(G, 2, 2, 1) mg.set_inMatrix(G, 0, 0, 1) mg.set_inMatrix(G, 1, 1, 1) mg.set_inMatrix(G, 1, 2, 1) mg.set_inMatrix(G, 2, 3, 1) mg.display_vertex(G) mg.display_edge(G) mg.display_adjMatrix(G) mg.display_inMatrix(G) del G G = dmg(3, 4) G = dmg.set_node(G, ["1", "2", "3"]) G = dmg.set_arc(G, ["(1,2)", "(2,1)", "(3,1)", "(3,2)"]) dmg.display_arc(G) dmg.display_node(G) dmg.display_adjMatrix(G) dmg.display_inInMatrix(G) dmg.display_outInMatrix(G) del G
# for i in G_11.Nodes: # if i != G_11.Nodes[-1] : # print(G_11.deg_in(i),'->', end =" ") # else: # print(G_11.deg_in(i)) # G_11.SCC() # #Display Graphs # G_11.display_node() # G_11.display_arc() # G_11.display_adjMatrix() # G_11.display_inInMatrix() # G_11.display_outInMatrix() #Read graph 1.12 from file G_12 = dmg(6, 8) G_12.read_nodes(os.path.join(Graph_1_12_dir, 'nodes.txt')) G_12.read_arcs(os.path.join(Graph_1_12_dir, 'arcs.txt')) G_12.read_adjMatrix(os.path.join(Graph_1_12_dir, 'adjMatrix.txt')) G_12.read_inInMatrix(os.path.join(Graph_1_12_dir, 'inInMatrix.txt')) G_12.read_outInMatrix(os.path.join(Graph_1_12_dir, 'outInMatrix.txt')) #Display Graphs # print('Dãy bậc ra của đồ thị G_12:',end =" ") # for i in G_12.Nodes: # if i != G_12.Nodes[-1] : # print(G_12.deg_out(i),'->', end =" ") # else: # print(G_12.deg_out(i)) # print('Dãy bậc vào của đồ thị G_12:',end =" ") # for i in G_12.Nodes: