def __init__(self, digraph): self.marked_array = [False] * digraph.V() self.pre_order = Queue_LinkedList() self.post_order = Queue_LinkedList() self.reverse_post_order = Stack_ResizingArray() for i in range(digraph.V()): if not self.marked_array[i]: self.dfs(i, digraph)
def __init__(self, ew_digraph): self.ew_digraph = ew_digraph self.marked_array = [False] * self.ew_digraph.V() self.pre_order = Queue_LinkedList() self.post_order = Queue_LinkedList() self.reverse_post_order = Stack_ResizingArray() for i in range(self.ew_digraph.V()): if self.marked_array[i] != True: self.dfs(i)
def __init__(self, root_node): self.root_node = root_node self.current_node = root_node self.queue = Queue_LinkedList() # Build the queue (default is from least node to greatest node) self.build_queue(self.current_node)
def __init__(self, ewdg, s): self._distTo = [float('inf') for i in range(ewdg.V())] self._distTo[s] = 0 self.edgeTo = [None] * ewdg.V() self.s = s self.vertex_queue = Queue_LinkedList() self.vertex_queue.enqueue(self.s) self.on_queue = [False] * ewdg.V() self.on_queue[self.s] = True self.cycle = None # Keeps track of the number of relax calls that are required in a pass # self.num_rcip = self.num_vaip after each pass self.num_rcip = 1 # Keeps track of the number of edges that were eligible to be relaxed in the pass self.num_vaip = 0 # Keeps track of the number of "passes" in the algorithm # When self.num_rcip reaches 0, we know we are starting a new "pass" self.pass_count = 1 # some way of determining the order methodology of choosing v # In this case, we only look at edges coming from vertices that were relaxed in the previous pass instead of all of the edges as in sp_bellmanford_manual. print('Pass: '******'\n\n Negative cycle is reachable from s. Shortest paths cannot be computed.' ) print( '--------------------------------------------------------------------------------- \n\n' ) self.findNegativeCycle() return v = self.vertex_queue.dequeue() self.on_queue[v] = False if self.num_rcip == 0: self.num_rcip = self.num_vaip self.num_vaip = 0 self.pass_count += 1 print('\n') print('Pass: '******'Queue is empty. Shortest paths for all reachable vertices from s have been found.' ) print( '--------------------------------------------------------------------------------- \n\n' )
def bfs(self, v, digraph): queue = Queue_LinkedList() queue.enqueue(v) while queue: v = queue.dequeue() for points_towards in digraph.adjacent(v): if not self.marked_array[points_towards]: self.marked_array[points_towards] = True self.paths_array[points_towards] = v queue.enqueue(points_towards)
def bfs(self, graph, v): queue = Queue_LinkedList() self.marked_array[v] = True queue.enqueue(v) while queue: vertex = queue.dequeue() neighbors_list = graph.adj[vertex] for i in neighbors_list: if not self.marked_array[i]: queue.enqueue(i) self.marked_array[i] = True self.path_array[i] = vertex
def bfs(self, graph, v): queue = Queue_LinkedList() queue.enqueue(v) while queue: v = queue.dequeue() # if the vertex has already been marked as true, this means that this vertex was placed on the queue twice which can only occur if there is a cycle if self._marked_array[v]: self.has_cycle = True self._marked_array[v] = True neighbors = graph.adjacent(v) for neighbor in neighbors: if not self._marked_array[neighbor]: queue.enqueue(neighbor)
def bfs(self, graph, v): queue = Queue_LinkedList() queue.enqueue(v) while queue: v = queue.dequeue() if self._marked_array[v]: continue self._marked_array[v] = True self._id[v] = self._count neighbors = graph.adjacent(v) for neighbor in neighbors: if not self._marked_array[neighbor]: queue.enqueue(neighbor)
def bfs(self, graph, v): queue = Queue_LinkedList() queue.enqueue(v) while queue: v = queue.dequeue() if self._marked_array[v]: continue self._marked_array[v] = True neighbors = graph.adjacent(v) for neighbor in neighbors: if (self._marked_array[neighbor]) and (neighbor != self.prev_vertex[v]): self.has_cycle = True elif not self._marked_array[neighbor]: queue.enqueue(neighbor) self.prev_vertex[neighbor] = v
def bfs(self, graph, v): queue = Queue_LinkedList() queue.enqueue(v) while queue: v = queue.dequeue() for neighbor in graph.adjacent(v): if (self.marked_array[neighbor]) and ( self.biparite_array[neighbor] == self.biparite_array[v]): self._isBiparite = False elif not self.marked_array[neighbor]: #self.previous_vertex_array[neighbor] = v self.biparite_array[neighbor] = not self.biparite_array[v] self.marked_array[neighbor] = True queue.enqueue(neighbor)
def __init__(self, ewg): self.mst_vertices = [False] * ewg.V() self.mst_edges = Queue_LinkedList() self.crossing_edges = PriorityQueue_Min_BinaryHeap() self.visit(ewg, 0) while self.crossing_edges: min_weight_crossing_edge = self.crossing_edges.delMin() # Need to check for ineligible edges: vert_1 = min_weight_crossing_edge.either() vert_2 = min_weight_crossing_edge.other(vert_1) if self.mst_vertices[vert_1] and self.mst_vertices[vert_2]: continue else: self.mst_edges.enqueue(min_weight_crossing_edge) # Need to determine which vertex in min_weight_crossing_edge is not already in mst: if self.mst_vertices[vert_1]: self.visit(ewg, vert_2) if self.mst_vertices[vert_2]: self.visit(ewg, vert_1)
def __init__(self, ewg): self.mst_edges = Queue_LinkedList() self.edges_pq = PriorityQueue_Min_BinaryHeap() # Takes E*lg(E) time for edge in ewg.edges(): self.edges_pq.insert(edge) # Note that we can construct a heap of size E in E time w/ heapq.heapify() (see algorithm for heap construction in heapsort) self.union_find = UF_WeightedQuickUnion(num_sites=ewg.V()) # Takes E*(lg(E) + lg(V)) + (V-1)*lg(V) time which simplies to ~ E*lg(E) time while self.edges_pq and len(self.mst_edges) < (ewg.V() - 1): min_edge = self.edges_pq.delMin() # Before adding min_edge to self.mst_edges queue, need to confirm that adding this edge won't create a cycle: # Can do this by using Union Find data structure # If the two vertices are already connected in the union_find data structure, we know that adding an edge would create a cycle vertex_1 = min_edge.either() vertex_2 = min_edge.other(vertex_1) if not self.union_find.connected(vertex_1, vertex_2): self.mst_edges.enqueue(min_edge) self.union_find.union(vertex_1, vertex_2)
def __init__(self, ewdg, s): self.dist = [float('inf') for i in range(ewdg.V())] self.dist[s] = 0 self.edgeTo = [None]*ewdg.V() self.queue = Queue_LinkedList() self.queue.enqueue(s) self.on_queue = [False]*ewdg.V() self.on_queue[s] = True # keeps track of the # of edges from source to vertex in shortest path (should be a max of V-1, otherwise we have a negative cycle) self.len_path=[float('inf')]*ewdg.V() self.len_path[s]=0 self.cycle = False self.s=s while self.queue: u = self.queue.dequeue() self.on_queue[u]=False for edge in ewdg.adjacent(u): v=edge.towards_vert() weight=edge.weight() if self.dist[u]+weight < self.dist[v]: self.dist[v] = self.dist[u]+weight self.len_path[v] = self.len_path[u]+1 if self.len_path[v] >= ewdg.V(): self.cycle=True print('Negative cycle detected') return self.queue.enqueue(v) self.on_queue[v]=True self.edgeTo[v]=edge
def keys(self): queue = Queue_LinkedList() self.collect(node=self.root, pre="", queue=queue) return queue
def keysWithPrefix(self, pre): node = self._get(self.root, 0, pre) queue = Queue_LinkedList() self.collect(node=node.middle, pre=pre, queue=queue) return queue
def keysThatMatch(self, pat): queue = Queue_LinkedList() self.wildcard_collect(node=self.root, pre="", pat=pat, queue=queue) return queue