lab 3

docx

School

Arizona State University *

*We aren’t endorsed by this school

Course

360

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

3

Uploaded by ProfStraw17234

Report
Jordan Whitecar IFT 360 lab 3 The resulting path is different! Kept original breadth_first call for comparison. def depth_first_traversal (self, start_node_name, target_node_name): #check if start node is part of the graph if start_node_name not in self.graph.nodes: print(start_node_name, "not found in graph!") return #check if start node is the target node if start_node_name == target_node_name: print("Start node is the target node! No search needed.") return #setup data structures to keep track of reached nodes and #nodes that ae yet to be expanded per breadth-first reached_nodes = {} reached_nodes[start_node_name] = 1
nodes_to_expand = [start_node_name] #construct the traversal tree, starting at the root. self.tree = Tree() rootNode = TreeNode(start_node_name, None) self.tree.set_root(rootNode) while len(nodes_to_expand) > 0: #expand the node at the beginning of the espansion list node_name = nodes_to_expand.pop() children = self.graph.expand_node(node_name) for child in children: #for each new child child_name = child[0] child_edgewt = child[1] #if child is target, return solution! if child_name == target_node_name: n = TreeNode(child_name,node_name) self.tree.add_child_node( n , node_name, child_edgewt) print('Target Reached!') self.tree.print_path(n) return #if child is a newly reached node, add to expansion list, tree if child_name not in reached_nodes: reached_nodes[child_name] = 1 nodes_to_expand.append(child_name) n = TreeNode(child_name,node_name) self.tree.add_child_node( n, node_name, child_edgewt)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help