def _DFS(self, g: Graph, v): self._unexploredV.remove(v) for e in g.incident_edges(v): if self._unexploredE.__contains__(e): self._unexploredE.remove(e) if self._unexploredV.__contains__(e.toVertex()): if e.toVertex == g.end(): self._counter = self._counter + 1 else: self._DFS(g, e.toVertex())
def max_value(graf : Graph, vertex): global mx if(mySet.__len__() == graf.num_vertices()): return mx if(vertex > mx): mx = vertex if not mySet.__contains__(vertex): mySet.add(vertex) LastVisitet = vertex for i in graf.adjacent_vertices(vertex): if not mySet.__contains__(i): return max_value(graf, i) return max_value(graf, LastVisitet)
def DFS(self, g: Graph): for u in g.vertices(): self._unexploredV.append(u) for u in g.edges(): self._unexploredE.append(u) print("Unexplored edge:") for i in self._unexploredE: print(i) print("Unexplored vertex") for i in self._unexploredV: print(i) for u in self._unexploredV: self._DFS(g, u)
def is_connected(self, g: Graph): counter = 0 for i in self.iterator_dfs(g, 15): counter += 1 if counter == g.num_vertices(): return True return False
def _DFS(self, g: Graph, v): self._unexploredV.remove(v) for e in g.incident_edges(v): if self._unexploredE.__contains__(e): self._unexploredE.remove(e) for w in e.endpoints(): if w != v and self._unexploredV.__contains__(w): self._DFS(g, w)
def opgD(f, s, g, u, d): g = Graph() #opretter alle hjørner for i in range(1, f): g.insert_vertex(i) #indsætter kanter myVertices = list(g.vertices()) for i in range(1, f, u): g.insert_edge(i, myVertices)
def kruskal(g: Graph): q = HeapPriorityQueue() clusters = [] T = [] for v in g.vertices(): clusters.append([v]) v.pos = len(clusters) - 1 for e in g.edges(): q.add(e._element, e) while len(T) < g.num_vertices() - 1: (w, e) = q.remove_min() v1 = g.end_vertices(e)[0] v2 = g.end_vertices(e)[1] A = clusters[v1.pos] B = clusters[v2.pos] if A != B: _merege(A, B) T.append(e) return T
def kruskal(g: Graph): q = HeapPriorityQueue() T = [] #Holder listen af edges med minimal vægt trees = {} union = unionFind() for v in g.vertices(): trees[v] = union.makeset(v) for e in g.edges(): q.add(e._element, e) while len(T) < g.num_vertices() - 1: (u, e) = q.remove_min() v1 = g.end_vertices(e)[0] v2 = g.end_vertices(e)[1] leaderv1 = union.quickFind(trees[v1]) leaderv2 = union.quickFind(trees[v2]) if leaderv1._element != leaderv2._element: union.quickUnion(leaderv1, leaderv2) T.append(e) return T
def build_graph(routes): G = Graph() for r in routes: # r = [a, b, c], a & b: Vertices, c: int edge label. a1 = r[0] a2 = r[1] c = r[2] v1 = Vertex(a1) v2 = Vertex(a2) vertices = list(G.vertices()) if vertices: found_v1 = False found_v2 = False v1_str = v1.__str__() v2_str = v2.__str__() for v in vertices: v_str = v.__str__() if v_str == v1_str: found_v1 = True va = v elif v_str == v2_str: found_v2 = True vb = v else: continue if not found_v1: va = G.insert_vertex(a1) if not found_v2: vb = G.insert_vertex(a2) else: va = G.insert_vertex(a1) vb = G.insert_vertex(a2) G.insert_edge(c, va, vb) return G
def _findResidualNetwork(self): graph = self._graph residualGraph = Graph() residualGraph._source = graph.source residualGraph._sink = graph.sink # laver en residual edge re med en eksra attribut cf, som indeholder # hvor meget mere flow denne edge kan tage for v in graph.vertices(): residualGraph.insert_vertex(v) print("***Residualnetwork edges before***") for e in graph.edges(): #re == residual edge re = Vertex(e.element()) re.cf = e.capacity() - e.element() residualGraph.insert_edge(e.element(), e.capacity(), e.vertexFrom(), e.vertexTo()) print("e.element: ", e.element(), "e.capacity: ", e.capacity()) print("re.cf ", re.cf) return residualGraph
def RobotOnAGrid(num, maze): graph = Graph() graph.setEnd(str(len(maze) - 1) + " " + maze[len(maze) - 1]) graph.setStart(str(0) + " " + maze[0]) v1 = None v3 = None v2 = None dfs = dfs_iterator() #Laver knuder for alle "," tegene i mazeen for i in range(len(maze)): #Skifter linje if i % 4 == 0 and i != 0: pass #Tilføjer knuder til højre elif maze[i] != '#' and maze[i + 1] != '#': v1 = graph.insert_vertex(str(i) + " " + str(maze[i])) v2 = graph.insert_vertex(str(i) + " " + str(maze[i + 1])) graph.insert_edge("R", v1, v2) #Tilføj knuder ned af elif maze[i + 5] != '#' and maze[i + num] != None: v3 = graph.insert_vertex(str(i) + " " + str(maze[i + num])) graph.insert_edge("D", v1, v3) #tilføj knuder til venstre elif maze[i] != '#' and maze[i - 1] != '#' and maze[i - 1] != None: v1 = graph.insert_vertex(str(i) + " " + str(maze[i])) v2 = graph.insert_vertex(str(i) + " " + str(maze[i + 1])) graph.insert_edge("L", v2, v1) #tilføj knuder op af elif maze[i] != '#' and maze[i - num] != None: v3 = graph.insert_vertex(str(i) + " " + str(maze[i - num])) graph.insert_edge("U", v3, v1) return dfs.DFS(graph)
def _merege(A, B): if len(A) > len(B): biggest = A smallest = B else: biggest = B smallest = A for i in range(len(smallest)): biggest.append(smallest[i]) smallest.pop(i) g = Graph() v15 = g.insert_vertex(15) v6 = g.insert_vertex(6) v38 = g.insert_vertex(38) v123 = g.insert_vertex(123) v66 = g.insert_vertex(66) g.insert_edge(10, v15, v38) g.insert_edge(23, v15, v6) g.insert_edge(90, v15, v66) g.insert_edge(8, v66, v6) g.insert_edge(2, v66, v38) g.insert_edge(76, v66, v123) g.insert_edge(7, v123, v6) g.insert_edge(55, v123, v38)
for u in g.edges(): self._unexploredE.append(u) print("Unexplored edge:") for i in self._unexploredE: print(i) print("Unexplored vertex") for i in self._unexploredV: print(i) for u in self._unexploredV: self._DFS(g, u) g = Graph() v15 = g.insert_vertex(15) v6 = g.insert_vertex(6) v38 = g.insert_vertex(38) v123 = g.insert_vertex(123) v66 = g.insert_vertex(66) g.insert_edge(10, v15, v38) g.insert_edge(23, v15, v6) g.insert_edge(90, v15, v66) g.insert_edge(8, v66, v6) g.insert_edge(2, v66, v38) g.insert_edge(76, v66, v123) g.insert_edge(7, v123, v6) g.insert_edge(55, v123, v38)
from edge_list_graph import Edge as Edge from edge_list_graph import Graph as Graph from edge_list_graph import Vertex as Vertex from adjacency_list_graph import Edge as Edge from adjacency_list_graph import Graph as Graph from adjacency_list_graph import Vertex as Vertex g = Graph() g.insert_vertex(15) g.insert_vertex(6) g.insert_vertex(38) g.insert_vertex(123) g.insert_vertex(66) g.insert_edge(10,15,38) g.insert_edge(23, 15,6) g.insert_edge(90, 15,66) g.insert_edge(8, 66,6) g.insert_edge(2, 66,38) g.insert_edge(76, 66,123) g.insert_edge(7, 123,6) g.insert_edge(55, 123,38) LastVisitet = 0 mx = -1 mySet = set() def max_value(graf : Graph, vertex): global mx
return self._unexploredV.remove(v) for e in graph.incident_edges(v): if self._unexploredE.__contains__(e): self._unexploredE.remove(e) if e.cf >0: self._augmentingPath.append(e) w = e.vertexTo() if w == graph.sink: return if self._unexploredV.__contains__(w): self._DFS(w) g = Graph() v15 = g.insert_vertex(15) v6 = g.insert_vertex(6) v38 = g.insert_vertex(38) v123 = g.insert_vertex(123) v66 = g.insert_vertex(66) g.source = v15 g.sink = v123 g.insert_edge(10,20,v15,v38) g.insert_edge(23, 30, v15,v6) g.insert_edge(90,100, v15,v66) g.insert_edge(8, 16,v66,v6) g.insert_edge(2, 16, v66,v38)