def check_cycle_undirected(g, start): visited = [False] * g.nodes stack = myStack() stack.push((start,-1)) while not stack.isEmpty(): node, parent = stack.pop() # print('popping {}'.format((node, parent))) visited[node] = True headOfLinkedList = g.edges[node].head while headOfLinkedList: el = headOfLinkedList.data print(el, node) if el != parent: stack.push((el, node)) if visited[el] == True: # print('already visited') print('cycle detected, not a tree') return False # if considering tree or not return True # if detecting cycle headOfLinkedList = headOfLinkedList.next # print('stack : ', stack.stackList) # for checking cycle return false # return False # for checking if it is a tree if sum(visited) == g.nodes: print('all nodes reachable, it is a tree') return True # it is a tree else: print('disconnected nodes detected, not a tree') return False # disconnected
def dfs(g): ''' Depth First Search traversal ''' n = g.nodes visited = [False]*n print(visited) stack = myStack() res = '' if n == 0: return res stack.push(0) while not stack.isEmpty(): tmp = stack.pop() print('----------------') print('performance on node {}'.format(tmp)) visited[tmp] = True print('visited: ', visited) print('stack ', stack.stackList) head = g.edges[tmp].head res += '{} ->'.format(tmp) while head: print('node {} has child {}'.format(tmp, head.data)) if visited[head.data] != True: print('pushing {}'.format(head.data)) stack.push(head.data) visited[head.data] = True print('stack becomes ', stack.stackList) head = head.next print(res) return res
def detect_cycle(g): ''' detect cycle via Depth First Search traversal ''' n = g.nodes visited = [False] * n print(visited) stack = myStack() res = '' if n == 0: return False stack.push(0) while not stack.isEmpty(): tmp = stack.pop() visited[tmp] = True print('----------------') print('performance on node {}'.format(tmp)) print('visited: ', visited) print('stack ', stack.stackList) head = g.edges[tmp].head res += '{} ->'.format(tmp) while head: print('node {} has child {}'.format(tmp, head.data)) if visited[head.data] == True: print('already visited {}, cycle detected!'.format(head.data)) return True stack.push(head.data) print('stack becomes ', stack.stackList) visited[head.data] = True head = head.next print(res) return False
def sort_stack(stack): tmp_stack = myStack() while not stack.isEmpty(): value = stack.pop() if tmp_stack.top() is None or value >= tmp_stack.top(): tmp_stack.push(value) else: while not tmp_stack.isEmpty(): stack.push(tmp_stack.pop()) stack.push(value) # print(stack.stackList, tmp_stack.stackList) while not tmp_stack.isEmpty(): stack.push(tmp_stack.pop()) return stack
def reverseK(queue, k): if queue.isEmpty() or queue.size() < k or k < 0: return None n = queue.size() stack = myStack() for i in range(k): stack.push(queue.dequeue()) while not stack.isEmpty(): queue.enqueue(stack.pop()) for i in range(n - k): queue.enqueue(queue.dequeue()) return queue
def evaluatePostFix(exp): stack = myStack() try: for char in exp: if char.isdigit(): # Push numbers in stack stack.push(char) print(stack.stackList) else: # use top two numbers and evaluate right = stack.pop() left = stack.pop() stack.push(str(eval(left + char + right))) # final answer should be a number return int(float(stack.pop())) except TypeError: return "Invalid Sequence"
def balanced_paranthesis(s): closing = ['}', ')', ']'] stack = myStack() for character in exp: if character in closing: if stack.isEmpty(): return False topElement = stack.pop() if (character is '}' and topElement is not '{'): return False if (character is ')' and topElement is not '('): return False if (character is ']' and topElement is not '['): return False else: stack.push(character) if (stack.isEmpty() is False): return False return True
def check_path(g, start, end): ''' Depth First Search traversal ''' n = g.nodes visited = [False] * n stack = myStack() stack.push(start) while not stack.isEmpty(): tmp = stack.pop() visited[tmp] = True head = g.edges[tmp].head while head: if visited[head.data] != True: stack.push(head.data) visited[head.data] = True if head.data == end: return True head = head.next return False
def check_cycle_undirected(g, start): visited = [False] * g.nodes stack = myStack() stack.push((start, -1)) # keep track of parent while not stack.isEmpty(): node, parent = stack.pop() # print('popping {}'.format((node, parent))) visited[node] = True headOfLinkedList = g.edges[node].head while headOfLinkedList: el = headOfLinkedList.data print(el, node) if el != parent: stack.push((el, node)) if visited[el] == True: # print('') print('already visited, cycle detected') return True headOfLinkedList = headOfLinkedList.next # print('stack : ', stack.stackList) return False
def nextGreaterElement(lst): s = myStack() res = [-1] * len(lst) # Reverse iterate list for i in range(len(lst) - 1, -1, -1): # if stack has elements: if not s.isEmpty(): # While stack has elements # and current element is greater than top element # pop all elements while not s.isEmpty() and s.top() <= lst[i]: s.pop() # if stack has an element # Top element will be greater than ith element if not s.isEmpty(): res[i] = s.top() # push in the current element in stack s.push(lst[i]) print(lst[i], s.stackList, res) for i in range(len(lst)): print(str(lst[i]) + " -- " + str(res[i])) return res
from Stacks import myStack def sort_stack(stack): tmp_stack = myStack() while not stack.isEmpty(): value = stack.pop() if tmp_stack.top() is None or value >= tmp_stack.top(): tmp_stack.push(value) else: while not tmp_stack.isEmpty(): stack.push(tmp_stack.pop()) stack.push(value) # print(stack.stackList, tmp_stack.stackList) while not tmp_stack.isEmpty(): stack.push(tmp_stack.pop()) return stack stack = myStack() stack.push(1) stack.push(2) stack.push(3) stack.push(6) stack.push(5) print(stack.stackList) stack = sort_stack(stack) print(stack.stackList)