def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # initialize a stack data structure and append starting_vertex s = Stack() s.push(starting_vertex) # initialize an empty set of visited vertices visited_vs = set() # while the stack is not empty, keep traversing while s.size(): # pop the next vertex and store it as a variable v = s.pop() # if the vertex has not already been visited if v not in visited_vs: print(v) # add the vortex to the visited set visited_vs.add(v) # for each connected vertex in the vertex's set, add to the stack for next_vertex in self.vertices[v]: s.push(next_vertex)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # Create an empty stack and push the starting vertex ID s = Stack() s.push(starting_vertex) # Create a set to store the visited vertices visited = set() # While the stack is not empty while s.size() > 0: # Pop the first vertex v = s.pop() # If that vertex has not been visited if v not in visited: # Mark it as visited by adding and print # print(v) visited.add(v) # Then add all of its neighbors to the top of the stack for neighbors in self.vertices[v]: s.push(neighbors)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ s = Stack() # Enqueue A PATH TO the starting vertex s.push([starting_vertex]) visited = set() while s.size() != 0: path = s.pop() vertex = path[-1] if vertex not in visited: visited.add(vertex) if vertex == destination_vertex: return path for e in self.get_neighbors(vertex): copy = path.copy() copy.append(e) s.push(copy)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ stack = Stack() visited = set() stack.push(starting_vertex) while stack.size() > 0: vertex = stack.pop() if vertex in visited: continue else: visited.add(vertex) print(vertex) for neighbor in self.get_neighbors(vertex): stack.push(neighbor)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ stack = Stack() visited = set() path = [] stack.push(starting_vertex) while stack.size() > 0: curr = stack.pop() path.append(curr) if curr == destination_vertex: return path for neighbor in self.get_neighbors(curr): stack.push(neighbor)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ stack = Stack() # Start up an empty Stack. stack.push(starting_vertex) # Add start point to stack. visited = set() # Set an empty visited set. visited.add(starting_vertex) # Add starting point to set. while stack.size() > 0: # Continue while stack exists. # Set a pointer and remove the last item from stack. new_node = stack.pop() visited.add(new_node) # Add that node to set. # Find all neighbors for that node. for neighbor in self.get_neighbors(new_node): if neighbor not in visited: # Check if they are already in set. stack.push(neighbor) # If not, add to stack. if neighbor is destination_vertex: # Check if neighbor is the destination point. visited.add(neighbor) # Add to set. # Return set, but cast to a list to pass the test. return list(visited)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # Create an empty stack and push the starting vertex s = Stack() s.push(starting_vertex) # Create an empty list to store visited vertices visited = [] while s.size() > 0: # Pop the first vertex current = s.pop() if current not in visited: visited.append(current) # Add all of its neighbors to the top of the stack for next_vert in self.vertices[current]: s.push(next_vert) print(f"DFT: {visited}")
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ stack = Stack() stack.push(starting_vertex) visited = set() visited.add(starting_vertex) while stack.size() > 0: new_node = stack.pop() visited.add(new_node) neighbors = self.get_neighbors(new_node) for neighbor in neighbors: if neighbor not in visited: stack.push(neighbor) if neighbor is destination_vertex: visited.add(neighbor) return list(visited)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ stack = Stack() # Create list of visited nodes visited = set() # Put starting node in the queue stack.push(starting_vertex) # While: queue not empty while stack.size() > 0: # Pop first node out of queue vertex = stack.pop() # If not visited if vertex not in visited: #Mark as visited visited.add(vertex) print(vertex) # Get adjacent edges and add to list for next_vert in self.vertices[vertex]: stack.push(next_vert)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ # Initialize a set for keeping track of visited indices visited = set() # Initialize a path with the starting_vertex path = [starting_vertex] # Set up a stack for pushing paths s = Stack() # Push the path onto the stack s.push(path) # Pop off the latest path while it's not empty while s.size() > 0: # Pop off the top path in the stack current_path = s.pop() # current_vertex is the last vertex in the current path current_vertex = current_path[-1] # If we're at the destination, return the current path if current_vertex == destination_vertex: return current_path # If we haven't visited the current vertex, add it to the visited set if current_vertex not in visited: visited.add(current_vertex) # Iterate over the current vertex's neighbors for neighbor in self.get_neighbors(current_vertex): # Make a new path for each neighbor, where the new path # has the neighbor added to the end, and push it onto the stack s.push(current_path + [neighbor])
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ stack = Stack() stack.push([starting_vertex]) visited = set() while stack.size() > 0: #while the queue size is greater than zero current_path = stack.pop() current_node = current_path[-1] if current_node == destination_vertex: return current_path else: if current_node not in visited: visited.add(current_node) edges = self.get_neighbors(current_node) for edge in edges: path_copy = list(current_path) path_copy.append(edge) stack.push(path_copy)
def dft_recursive(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. This should be done using recursion. """ s = Stack() visited = set() # Init: s.push(starting_vertex) v = s.pop() if v not in visited: print(v) # "Visited" the node visited.add(v) for neighbor in self.get_neighbors(v): s.push(neighbor) # While queue isn't empty while s.size() > 0: self.dft_recursive(v)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ stack = Stack() stack.push([starting_vertex]) visited = set() while stack.size() > 0: path = stack.pop() vertex = path[-1] if vertex not in visited: if vertex == destination_vertex: return path else: visited.add(vertex) for neighbor in self.get_neighbors(vertex): new_path = list(path) new_path.append(neighbor) stack.push(new_path)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ # Create an empty Stack s = Stack() # Create an empty Visited set visited_set = set() # Push the PATH TO the starting vertex to the stack s.push([starting_vertex]) # While the Stack is not empty... while s.size() > 0: # Pop the first PATH path = s.pop() # Grab the last vertex of the PATH vertex = path[-1] # Check if it's our destination if vertex == destination_vertex: return path # If it has not been visited... if vertex not in visited_set: # Mark it as visited (print it and add it to the visited set) print(vertex) visited_set.add(vertex) # Then push each of its neighbors in the stack for neighbor in self.vertices[vertex]: new_path = list(path) new_path.append(neighbor) s.push(new_path) return None
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ s = Stack() s.push([starting_vertex]) visited = set() while s.size() > 0: path = s.pop() v = path[-1] if v not in visited: if v == destination_vertex: return path visited.add(v) for next_vert in self.vertices[v]: new_path = list(path) new_path.append(next_vert) s.push(new_path) return None
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # create an empty stack and push the starting vertex ID stack = Stack() stack.push(starting_vertex) # create an empty Set to store the visited vertices visited = set() # while the stack is not empty ... while stack.size() > 0: # pop the first vertex vert = stack.pop() # if that vertex has not been visited .. if vert not in visited: # mark it is visited visited.add(vert) print(vert) # then add all of its neighbors to the top of the stack for neighbor in self.vertices[vert]: #self.get_neighbors(vert) stack.push(neighbor)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ ss = Stack() ss.push([starting_vertex]) visited = set() while ss.size() > 0: path = ss.pop() vertex = path[-1] if vertex not in visited: if vertex == destination_vertex: return path visited.add(vertex) for next in self.get_neighbors(vertex): updated_path = list(path) updated_path.append(next) ss.push(updated_path) return None
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # create to_visit stack and add starting vertex to_visit = Stack() to_visit.push(starting_vertex) # create set for visting verticies visited_ver = set() # while to_visit is not empty # pop the first vertex on to_visit while to_visit.size() > 0: current_vertex = to_visit.pop() # if it has not been visited if current_vertex not in visited_ver: print(current_vertex) # mark as visited by addign to visited_ver visited_ver.add(current_vertex) for neighbor in self.get_neighbors(current_vertex): if neighbor not in visited_ver: to_visit.push(neighbor)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # create an empty stack and push the starting vertex ID s = Stack() s.push(starting_vertex) # create a set to store te visited vertices visited = set() # while stack is not empty while s.size() > 0: # pop the first vertex v = s.pop() # check if that vertex has not been visited if v not in visited: # mark it as visited print('dft', v) visited.add(v) # add all of its neighbours to the top of the stack for next_vertex in self.vertices[v]: s.push(next_vertex)
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first [2nd Edition: p 75, 3rd Edition: p 87] Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm [2nd Edition: Fig. 3.18, 3rd Edition: Fig 3.7]. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successors:", problem.getSuccessors(problem.getStartState()) """ "*** YOUR CODE HERE ***" from util import Stack pilha = Stack() explorados = [] acoes = [] inicial = (problem.getStartState(),None,None,0) "(Estado atual, Estado Pai, Aчуo, Custo)" pilha.push(inicial) while pilha.isEmpty() == False: noAtual = pilha.pop() explorados.append(noAtual[0]) if problem.isGoalState(noAtual[0]): return gerarCaminho(noAtual) else: for item in problem.getSuccessors(noAtual[0]): est = item[0] aco = item[1] cus = item[2] if est not in explorados: tupla = (est,noAtual,aco,cus) pilha.push(tupla)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ s = Stack() s.push([starting_vertex]) visited = set() while s.size() > 0: path = s.pop() last_vertex = path[-1] if last_vertex not in visited: if last_vertex == destination_vertex: return path else: visited.add(last_vertex) for next_vertex in self.vertices[last_vertex]: new_path = [*path, next_vertex] s.push(new_path)
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ stack = Stack() stack.push([starting_vertex]) visited = set() while stack.size(): current_path = stack.pop() last_vertex = current_path[len(current_path)-1] if last_vertex not in visited: if last_vertex == destination_vertex: return current_path visited.add(last_vertex) neighbors = self.get_neighbors(last_vertex) for neighbor in neighbors: new_path = current_path.copy() new_path.append(neighbor) stack.push(new_path)
def dfs(self, starting_vertex, destination_vertex): s = Stack() visited = [] s.push([starting_vertex]) while s.size(): path = s.pop() # print("Path", path) vertex = path[-1] if vertex not in visited: if vertex == destination_vertex: return path visited.append(vertex) for next_vertex in self.vertices[vertex]: new_path = list(path) new_path.append(next_vertex) s.push(new_path) return None
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # Create an empty stack and enque the starting vertex ID s = Stack() s.push(starting_vertex) # Create an empty set to store visited vertices visited = set() # While the queue is not empty... while s.size() > 0: # pop the first vertex vert = s.pop() # If that vertex has not been visited... if vert not in visited: # Mark it as visited print(f'dft - {vert}') visited.add(vert) # Add all of its neighbors to the back of the stack for neighbor in self.vertices[vert]: s.push(neighbor)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # Create a q/stack and enqueue starting vertex stack = Stack() visited = set() stack.push(starting_vertex) # While queue is not empty: while stack.size() > 0: # dequeue/pop the first vertex vertex = stack.pop() # if not visited if vertex not in visited: # DO THE THING!!!!!!! print(vertex) # mark as visited visited.add(vertex) # enqueue all neightbors for next_vert in self.get_neighbors(vertex): stack.push(next_vert)
def dfs_recursive(self, starting_vertex, destination_vertex,visited = None,copy=None,s= None): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. This should be done using recursion. """ if visited == None: s = Stack() s.push([starting_vertex]) visited = set() copy = [] if s.size() != 0: path = s.pop() vertex = path[-1] if vertex not in visited: visited.add(vertex) if vertex == destination_vertex: return path for e in self.get_neighbors(vertex): copy = path.copy() copy.append(e) s.push(copy) return self.dfs_recursive(starting_vertex, destination_vertex,visited,copy,s) else: if s.size() != 0: path = s.pop() vertex = path[-1] if vertex not in visited: visited.add(vertex) if vertex == destination_vertex: return path for e in self.get_neighbors(vertex): copy = path.copy() copy.append(e) s.push(copy) return self.dfs_recursive(starting_vertex, destination_vertex,visited,copy,stack)
def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ # create an empty stack and push the starting vertex stack = Stack() stack.push(starting_vertex) # create a set to store the visited vertices visited = set() # while the stack is not empty while not stack.is_empty(): # pop the first vertex v = stack.pop() # if that vertex has not been visited if v not in visited: # mark it as visited and print for traversal visualization visited.add(v) print(v) # then push all of its direct neighbour(s) for next_vertex in self.vertices[v]: stack.push(next_vertex)
def dfs_recursive(self, starting_vertex, destination_vertex, path=Stack(), visited=set()): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. This should be done using recursion. """ # Create a path, this will be None the first time this function runs currentPath = path.pop() # If currentPath is None if currentPath == None: # Make currentPath the starting vertex currentPath = [starting_vertex] # Check if the last node in the currentPath is not in visited if currentPath[-1] not in visited: # Add the last node to visited visited.add(currentPath[-1]) # For each of the last nodes neighbors for neighbor in self.get_neighbors(currentPath[-1]): # If the neighbor is the destination if neighbor == destination_vertex: # Append that neighbor to the currentPath currentPath.append(neighbor) # Return the currentPath return currentPath # Create a copy of the currentPath copy = currentPath.copy() # Add the neighbor to the copy copy.append(neighbor) # Push the copy to the reoccuring path path.push(copy) # Rerun the function with updated values return self.dfs_recursive(starting_vertex, destination_vertex, path, visited)
def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print("Start:", problem.getStartState()) print("Is the start a goal?", problem.isGoalState(problem.getStartState())) print("Start's successors:", problem.getSuccessors(problem.getStartState())) """ "*** YOUR CODE HERE ***" from util import Stack dfs_stack = Stack() now_state = problem.getStartState() # 현재 상태 저장 dfs_stack.push((now_state, [])) # (현재 위치,현재 위치에 오기까지 경로) 튜플을 푸시 is_visited = [now_state] # 방문체크 list while not dfs_stack.isEmpty(): now_state = dfs_stack.pop() is_visited.append(now_state[0]) if problem.isGoalState(now_state[0]): return now_state[1] for (location, direction, cost) in problem.getSuccessors(now_state[0]): # 다음 노드 탐색 if location not in is_visited: dfs_stack.push( (location, now_state[1] + [direction])) # 방문한적이 없는 node 면 stack 에 푸시 return [] # 답안 존재 x
def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ # Create empty stack s = Stack() # Push the starting node s.push([starting_vertex]) # Create a set for visited vertices visited = set() # While stack is not empty while s.size() > 0: # Create current path variable set to first node in s currentPath = s.pop() # Set the last node in the currentPath to a variable lastNode = currentPath[-1] # If it hasnt been visited if lastNode not in visited: # Check if it is the destination if lastNode == destination_vertex: # Return the path if it is return currentPath # If it is not the target else: # Add the lastNode to visited visited.add(lastNode) # Set the lastNode neighbors to variable neighbors = self.get_neighbors(lastNode) # For each of lastNodes neighbors for neighbor in neighbors: # Copy the path current path copy = currentPath[:] # Add the neighbor copy.append(neighbor) # Add the copy to the s s.push(copy)