def __init__(self, node_id, node_port, config): self.id = str(node_id) self.n_start = time.time() # initialise node's socket self.s_port = int(node_port) self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._socket.bind(("127.0.0.1", int(node_port))) self._socket.settimeout(0.1) self.lsp = "" self.net_topology = graph(self.id) self.neighbours = {} # => {node_id: (cost, port, KA_count)} self.neighbour_ka = {} # iterate through config file and insert nieghbours # Assume the config file is in the same directory config_file = open(config, "r") config_file.readline() # Get rid of the first line, it's junk for line in config_file: line = line.rstrip('\n') line = line.split(" ") self.neighbours[line[0]] = (float(line[1]), int(line[2])) self.neighbour_ka[line[0]] = 0 self.net_topology.insert_edge(self.id, line[0], line[1])
def run_dash(): """ runs the dash app :param: conn [sqlite3.connection] -- connection to the db :param: curr [sqlite3.cursor] -- cursor in the db """ a = graph() app = dash.Dash() setup_tall_layout(app) player_graphs = ['player_a', 'player_b', 'player_c', 'player_d'] @app.callback( Output(component_id='team_bar', component_property='figure'), [Input(component_id='team_id_dropdown', component_property='value')] ) def update_player_output_div(input_value): return a.usa() @app.callback( Output(component_id='player_c', component_property='figure'), [Input(component_id='team_id_dropdown', component_property='value')] ) def update_player_graph_a(input_value): player_position = 1 return a.usa(1) #return main.my_graph() app.run_server(port=8066)
def test_graph(self): g = graph() airports = ["a","b","c"] for i in airports: g.add_vertex(i) for i in range(len(airports)): for j in range(len(airports)): if i != j: g.add_edge(airports[i], airports[j], i+j) self.assertTrue(g.weights["a","b"]==1) self.assertTrue(g.weights["b","c"]==3)
def test_BFS(): g = graph(4) g.addEdge(0, 1) g.addEdge(0, 2) g.addEdge(1, 2) g.addEdge(2, 0) g.addEdge(2, 3) g.addEdge(3, 3) BFS(g, 2)
def test_shortest_path(self): g = graph() airports = ["a","b","c"] for i in airports: g.add_vertex(i) for i in range(len(airports)): for j in range(len(airports)): if i != j: g.add_edge(airports[i], airports[j], i+j) self.assertTrue(sp(g,"a","b")==['a', 'b']) self.assertTrue(sp(g,"c","a")==['c', 'a']) self.assertFalse(sp(g,"c","b")==['c', 'a', 'b'])
def main(): #create common source vertex g1 = graph() source = node("s") g1.add_vertex(source) SO = node("so") g1.add_vertex(SO) SA = node("sa") g1.add_vertex(SA) SB = node("sb") g1.add_vertex(SB) SAB = node("sab") g1.add_vertex(SAB) DO = node("do") g1.add_vertex(DO) DA = node("da") g1.add_vertex(DA) DB = node("db") g1.add_vertex(DB) DAB = node("dab") g1.add_vertex(DAB) sink=node("si") g1.add_vertex(sink) g1.add_edge(source,SO,50) g1.add_edge(source,SA,36) g1.add_edge(source,SB,11) g1.add_edge(source,SAB,8) g1.add_edge(DO, sink, 45) g1.add_edge(DA, sink, 42) g1.add_edge(DB, sink, 8) g1.add_edge(DAB, sink, 3) g1.add_edge(SO,DO,float("inf")) g1.add_edge(SO, DA, float("inf")) g1.add_edge(SO, DB, float("inf")) g1.add_edge(SO, DAB, float("inf")) g1.add_edge(SA, DA, float("inf")) g1.add_edge(SA, DAB, float("inf")) g1.add_edge(SB, DB, float("inf")) g1.add_edge(SB, DAB, float("inf")) g1.add_edge(SAB, DAB, float("inf")) max_flow=g1.find_max_flow(source,sink) demand = g1.edges[(DO,sink)]+g1.edges[(DA,sink)]+g1.edges[(DB,sink)]+g1.edges[(DAB,sink)] print("MAX_FLOW = ",max_flow) print("Demand = ",demand) if(demand>max_flow): print("Max flow is lesser than demand. Hence, demand cannot be met.") else: print("Max flow is equal to or more than demand. Hence, demand can be met.")
def main(): cdf_graph = graph() print("*** CDF tool *** usage cdf filename1 cdfname1 filename2 cdfname2 \n") for arg in sys.argv: print("args: ",arg) file_name = sys.argv[1] file_name1 = sys.argv[3] handler = file_hander(file_name,0) handler1 = file_hander(file_name1,0) data=handler.read_file() data1 = handler1.read_file() print("data 1: ",data) print("name 1: ", sys.argv[2]) print("data 2: ", data1) print("name 2: ", sys.argv[4]) cdf_graph.cdf(data, data1, sys.argv[2], sys.argv[4])
def basic_operations(): # Create the dictionary with graph elements graph_elements = { "a": ["b", "c"], "b": ["a", "d"], "c": ["a", "d"], "d": ["e"], "e": ["d"] } print(graph_elements) new_graph = graph(graph_elements) """Adding a vertex""" new_graph.add_vertex("f") print(new_graph.get_vertices()) print(new_graph.edges()) """Adding an edge""" new_graph.add_edge({'a', 'e'}) new_graph.add_edge({'a', 'c'}) print(new_graph.edges())
def draw_graph_mith_MinMax_points(self, col, sheetname): read = readFile(sheetname) read.readMarks(col) points = Find_Points() points.find_min(read.Marks, read.registerations) points.find_max(read.Marks, read.registerations) points.find_avg(read.Marks, read.registerations) graphy = graph(sheetname) plt = graphy.draw_Simple_graph() plt.scatter(points.min_cell, points.min, color='r', label='Minimum Marks') plt.scatter(points.max_cell, points.max, color='b', label='Maximum Marks') #num=str(points.avg) #text='Average = ' + num # plt.figtext(text,0.2,1.3) plt.title(sheetname + 'Data') plt.show()
def reverse_graph(g): new_graph = graph(g.size()) for v in g.nodes(): for c in g.children(v): new_graph.addEdge(c, v) return new_graph
stack.append(n) def strongly_connected_components(g): stack = [] visited = [False] * g.size() for i in range(g.size()): if visited[i] is False: fill_order(i, g, stack, visited) rev_g = reverse_graph(g) visited = [False] * rev_g.size() print("Strongly Connected Components : ") while stack: v = stack.pop() if visited[v] is False: scc = [] dfs(v, rev_g, visited, scc) print(scc) g = graph(5) g.addEdge(1, 0) g.addEdge(0, 2) g.addEdge(2, 1) g.addEdge(0, 3) g.addEdge(3, 4) strongly_connected_components(g)
n = g.size() visited = [False]*n disc = [0]*n low = [0]*n parent = [-1]*n ap = [False]*n for i in range(n): if visited[i] is False: dfs(i, g, visited, disc, low, parent, ap) print("Articulation Points : ", [i for i in range(n) if ap[i] is True]) g1 = graph(5) g1.addEdge(1, 0) g1.addEdge(0, 2) g1.addEdge(2, 1) g1.addEdge(0, 3) g1.addEdge(3, 4) articulation_points(g1) g2 = graph(4) g2.addEdge(0, 1) g2.addEdge(1, 2) g2.addEdge(2, 3) articulation_points(g2) g3 = graph(7) g3.addEdge(0, 1)
if edge.getInfo()[1].display() in Stack: low_link[name] = min( low_link[name], index_dict[edge.getInfo()[1].display()]) if index_dict[name] == low_link[name]: curr_scc = [] while (True): curr_v = Stack.pop(-1) curr_scc.append(curr_v) if (curr_v == name): break scc.append(curr_scc) for vertex in self.getAllVertex(): if vertex not in reached: visit(vertex) self._SCC = scc Graph = graph() Graph.genRand(4) Graph.printAdjList() Reach = reachabilty(Graph) Reach.displayDFS(0) Reach.displayBFS(0) Reach.displaySCC()
def __init__(self, dict=None): """Expects a dictionary of lists, each int(key) pointing to a list containing ["body", "tags[]", "int(parent)"]""" self.dict = {} self.dict = dict self.graph = graph()
return path def longestPath(g, s): path = topologicalSort(g) dist = [-9999]*g.size() dist[s] = 0 while path: ele = path.pop() if dist[ele] != -9999: for c in g.children(ele): if dist[c] < dist[ele] + g.weight(ele, c): dist[c] = dist[ele] + g.weight(ele, c) print(dist) g = graph() g.addEdge(0, 1, 5) g.addEdge(0, 2, 3) g.addEdge(1, 3, 6) g.addEdge(1, 2, 2) g.addEdge(2, 4, 4) g.addEdge(2, 5, 2) g.addEdge(2, 3, 7) g.addEdge(3, 5, 1) g.addEdge(3, 4, -1) g.addEdge(4, 5, -2) longestPath(g, 1)
from Graph import graph graph = graph({ "a": [("b", 3)], "b": [("c", 2), ("d", 6), ("e", 3), ("f", 8)], "c": [("b", 2), ("d", 5)], "d": [("c", 5), ("b", 6)], "e": [("b", 3), ("f", 1)], "f": [("e", 1), ("b", 8)] }) graph.print_edges() #start at a #end at f graph.dijsktra("a", "f")
stack.append(n) def strongly_connected_components(g): stack = [] visited = [False]*g.size() for i in range(g.size()): if visited[i] is False: fill_order(i, g, stack, visited) rev_g = reverse_graph(g) visited = [False]*rev_g.size() print("Strongly Connected Components : ") while stack: v = stack.pop() if visited[v] is False: scc = [] dfs(v, rev_g, visited, scc) print(scc) g = graph(5) g.addEdge(1, 0) g.addEdge(0, 2) g.addEdge(2, 1) g.addEdge(0, 3) g.addEdge(3, 4) strongly_connected_components(g)
low[u] = min(low[u], disc[v]) def bridge(g): n = g.size() visited = [False] * n disc = [0] * n low = [0] * n parent = [-1] * n bridges = [] for i in g.nodes(): if visited[i] is False: dfs(i, g, visited, disc, low, parent, bridges) print(bridges) g1 = graph(5) g1.addEdge(1, 0) g1.addEdge(0, 2) g1.addEdge(2, 1) g1.addEdge(0, 3) g1.addEdge(3, 4) bridge(g1) g2 = graph(4) g2.addEdge(0, 1) g2.addEdge(1, 2) g2.addEdge(2, 3) bridge(g2)
disc = [-1]*n low = [0]*n global time time = 0 global scc scc = [] for i in g.nodes(): if disc[i] == -1: dfs(i, g, disc, low, stack, stack_mem) return scc print("Graph 1") g1 = graph(5) g1.addEdge(1, 0) g1.addEdge(0, 2) g1.addEdge(2, 1) g1.addEdge(0, 3) g1.addEdge(3, 4) print(strongly_connected_component(g1)) print("Graph 2") g2 = graph(4) g2.addEdge(0, 1) g2.addEdge(1, 2) g2.addEdge(2, 3) print(strongly_connected_component(g2)) print("Graph 3")
def bridge(g): n = g.size() visited = [False]*n disc = [0]*n low = [0]*n parent = [-1]*n bridges = [] for i in g.nodes(): if visited[i] is False: dfs(i, g, visited, disc, low, parent, bridges) print(bridges) g1 = graph(5) g1.addEdge(1, 0) g1.addEdge(0, 2) g1.addEdge(2, 1) g1.addEdge(0, 3) g1.addEdge(3, 4) bridge(g1) g2 = graph(4) g2.addEdge(0, 1) g2.addEdge(1, 2) g2.addEdge(2, 3) bridge(g2)
time = 0 n = g.size() visited = [False] * n disc = [0] * n low = [0] * n parent = [-1] * n ap = [False] * n for i in range(n): if visited[i] is False: dfs(i, g, visited, disc, low, parent, ap) print("Articulation Points : ", [i for i in range(n) if ap[i] is True]) g1 = graph(5) g1.addEdge(1, 0) g1.addEdge(0, 2) g1.addEdge(2, 1) g1.addEdge(0, 3) g1.addEdge(3, 4) articulation_points(g1) g2 = graph(4) g2.addEdge(0, 1) g2.addEdge(1, 2) g2.addEdge(2, 3) articulation_points(g2) g3 = graph(7) g3.addEdge(0, 1)
low = [0] * n global time time = 0 global scc scc = [] for i in g.nodes(): if disc[i] == -1: dfs(i, g, disc, low, stack, stack_mem) return scc print("Graph 1") g1 = graph(5) g1.addEdge(1, 0) g1.addEdge(0, 2) g1.addEdge(2, 1) g1.addEdge(0, 3) g1.addEdge(3, 4) print(strongly_connected_component(g1)) print("Graph 2") g2 = graph(4) g2.addEdge(0, 1) g2.addEdge(1, 2) g2.addEdge(2, 3) print(strongly_connected_component(g2)) print("Graph 3")
def union(parent, x, y): x_parent = find(parent, x) y_parent = find(parent, y) if x_parent == y_parent: return False else: parent[x_parent] = y_parent return True def isCycle(g): parent = [-1] * g.size() for e in g.edges(): if union(parent, e[0], e[1]) is False: return True return False g = graph(3) g.addEdge(0, 1) g.addEdge(1, 2) g.addEdge(0, 2) if isCycle(g) is True: print("Graph Contains Cycle") else: print("Graph does not contains cycle")
le = le_time(graph, toposeq, ee[graph.get_num() - 1]) return crt_path(graph, ee, le) if __name__ == '__main__': #先把图7.15用临接矩阵的形式表现出来 inf = float('inf') G715 = [[inf, 7, 13, 8, inf, inf, inf, inf, inf], [inf, inf, 4, inf, inf, 14, inf, inf, inf], [inf, inf, inf, inf, 5, inf, 8, 12, inf], [inf, inf, inf, inf, 13, inf, inf, 10, inf], [inf, inf, inf, inf, inf, 7, 3, inf, inf], [inf, inf, inf, inf, inf, inf, inf, inf, 5], [inf, inf, inf, inf, inf, inf, inf, inf, 7], [inf, inf, inf, inf, inf, inf, inf, inf, 8], [inf, inf, inf, inf, inf, inf, inf, inf, inf]] g715 = graph(G715, inf) tps = toposort(g715) print(tps) #正确,注意此时加入邻接矩阵的时候对角线元素不能再用0表示,即本身不是本身的临接边 print(' ') #验证AOV网络的关键路径是什么,以及关键路径上面的关键活动 aoe_path, ee, le = critical_paths(g715) print(ee) print(' ') print(le) print(' ') print(aoe_path) print(' ')
if parent[i] == -1: return i return find(parent, parent[i]) def union(parent, x, y): x_parent = find(parent, x) y_parent = find(parent, y) if x_parent == y_parent: return False else: parent[x_parent] = y_parent return True def isCycle(g): parent = [-1]*g.size() for e in g.edges(): if union(parent, e[0], e[1]) is False: return True return False g = graph(3) g.addEdge(0, 1) g.addEdge(1, 2) g.addEdge(0, 2) if isCycle(g) is True: print("Graph Contains Cycle") else: print("Graph does not contains cycle")
if a[i][j] > a[i][k] + a[k][j]: a[i][j] = a[i][k] + a[k][j] path[i][j] = path[i][k] #运用了性质vi到v的最短路径的v的前一个点v·, # 那么vi到v`也是最短路径,那么下一个顶点都是相同的 return (a, path) if __name__ == '__main__': #定义一个无穷数 inf = float('inf') #定义有向图G7 G7 = [[0, inf, 6, 3, inf, inf, inf], [11, 0, 4, inf, inf, 7, inf], [inf, 3, 0, inf, 5, inf, inf], [inf, inf, inf, 0, 5, inf, inf], [inf, inf, inf, inf, 0, inf, 9], [inf, inf, inf, inf, inf, 0, 10], [inf, inf, inf, inf, inf, inf, 0]] g7 = graph(G7, inf) #测试导入 # print(g7._mat) # for x in range(g7._vnum): # print(g7.out_edge(x),end=' ') # print(' ') #利用递归实现深度优先遍历DFS,得到生成树,正确 print(span_forest(g7)) print(' ') print(K_MST(g7)) print(' ') print(P_MST(g7)) #正确 print(' ') #检验单源点到其他所有顶点的最短路径的算法
visited[n] = True stack = list() stack.append(n) while stack: ele = stack.pop() childs = g.children(ele) for c in childs: if visited[c] is False: stack.append(c) elif c != ele: return True return False g = graph() g.addEdge(1, 0) g.addEdge(0, 2) g.addEdge(2, 0) g.addEdge(0, 3) g.addEdge(3, 4) if iscycle(g) is True: print("Graph contains cycle") else: print("Graph does not contain cycle") g = graph() g.addEdge(0, 1) g.addEdge(1, 2)