SlideShare a Scribd company logo
1 of 7
+1 (315)557-6473
https://www.pythonhomeworkhelp.com/
support@pythonhomeworkhelp.com
Kolb Rd. , Tucson, Arizona,
USA
Q.1 We wish to implement a set of classes to model graphs of the type that we used to
describe search problems.
Graphs have nodes and edges. A directed edge indicates a connection from the first
node to the second. An undirected edge can be thought of as two directed edges, one
from the first node to the second and another one going the other way. Nodes will be
identified with names that are represented using Python strings.
Implement a base class called DirectedGraph, which has the following methods
hasNode - given a node, returns True when the node exists in the graph and false
otherwise.
addNode - given a node, if it’s not already present, it adds it to the graph, initially with no
edges.
addEdge - given two nodes, add a connection from the first to the second. If either node
is not already present, it is added to the graph.
children - given a node, returns a list of nodes that can be reached from that node by
traversing a single arc. If the node is not present, it returns an empty list.
Here is an example use:
>>> g = DirectedGraph()
>>> g.addNode(’A’)
>>> g.addEdge(’A’,’B’)
>>> g.children(’A’) [’B’]
>>> g.children(’B’) []
Think very carefully about:
•How you will keep track of the nodes in the graph.
•How you will keep track of which nodes are connected.
Each of the methods should be short.
Define this class and its methods.
Solution:
class DirectedGraph:
nodes = {}
def hasNode(self,n):
return n in self.nodes
def addNode(self,n):
if not self.hasNode(n):
self.nodes[n] = {}
def addEdge(self,n,c):
self.addNode(n)
self.addNode(c)
if not (c in self.nodes[n]):
self.nodes[n][c] = True
def children(self,n):
return self.nodes[n].keys()
Q.2 Define a class UnDirectedGraph which has all the same methods as DirectedGraph,
except that addEdge, adds edges in both directions. Here is an example use:
>>> g = UnDirectedGraph()
>>> g.addEdge(’A’,’B’)
>>> g.children(’A’)
[’B’]
>>> g.children(’B’)
[’A’]
Define this class. Use inheritance to avoid rewriting all of the methods
Solution:
class UnDirectedGraph(DirectedGraph):
def addEdge(self,n,c):
DirectedGraph.addEdge(self,n,c)
DirectedGraph.addEdge(self,c,n)
Q.3 We can use the DirectedGraph class (previous problem) to define a search problem.
We will define a state machine class, GraphSM, whose states correspond to the nodes
in the graph and whose state transitions correspond to the edges in the graph.
The input to the machine is an integer indicating which of the children of the current
node will become the next state. If the input is greater than or equal to the length of the
list of children, then the state remains unchanged.
A GraphSM machine is initialized with an instance of DirectedGraph, a start node and an
integer indicating the maximum number of children of any node in the graph.
>>> g = DirectedGraph()
>>> for s in [’S’, ’A’, ’B’, ’C’, ’D’, ’E’, ’F’, ’G’]:
g.addNode(s)
>>> for (a, b) in [(’S’, ’A’), (’S’, ’B’), (’A’, ’C’), (’B’, ’C’), (’B’, ’D’),
(’C’, ’E’), (’D’, ’E’), (’D’, ’F’), (’F’, ’G’)]:
g.addEdge(a,b)
>>> m = GraphSM(g, ’S’, 2)
>>> m.transduce([1,1,1,0])
[’B’, ’D’, ’F’, ’G’]
>>> search.smSearch(GraphSM(g, ’S’, 2), goalTest=lambda s: s == ’E’)
[(None, ’S’), (0, ’A’), (0, ’C’), (0, ’E’)]
Define the GraphSM class. Define all the methods and attributes that you need for the
state ma chine to be used for a search, as shown in the example above. You do not
need to define a done method.
Solution:
class GraphSM(SM):
def _init_(self, graph, startNode, maxChildren):
self.graph = graph
self.startState = startNode
self.MaxChildren = maxChildren
self.legalInputs = range(maxChildren)
def getNextValues(self, state, inp)
c = self.graph.children(state)
if inp<len(c):
state = c[inp]
return (state,state)

More Related Content

Similar to Complex Python Programming.pptx

Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments rajni kaushal
 
Application of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdfApplication of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdfNancy Ideker
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3Vince Vo
 
SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...
SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...
SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...Kyong-Ha Lee
 
GraphFrames: Graph Queries in Spark SQL by Ankur Dave
GraphFrames: Graph Queries in Spark SQL by Ankur DaveGraphFrames: Graph Queries in Spark SQL by Ankur Dave
GraphFrames: Graph Queries in Spark SQL by Ankur DaveSpark Summit
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsLuis Goldster
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsTony Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsFraboni Ec
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJames Wong
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsYoung Alista
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHarry Potter
 

Similar to Complex Python Programming.pptx (20)

Graph in data structures
Graph in data structuresGraph in data structures
Graph in data structures
 
Twopi.1
Twopi.1Twopi.1
Twopi.1
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Application of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdfApplication of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdf
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Graphs
GraphsGraphs
Graphs
 
Jeop game-final-review
Jeop game-final-reviewJeop game-final-review
Jeop game-final-review
 
Ai part 1
Ai part 1Ai part 1
Ai part 1
 
Graphs
GraphsGraphs
Graphs
 
Programming Exam Help
 Programming Exam Help Programming Exam Help
Programming Exam Help
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...
SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...
SASUM: A Sharing-based Approach to Fast Approximate Subgraph Matching for Lar...
 
GraphFrames: Graph Queries in Spark SQL by Ankur Dave
GraphFrames: Graph Queries in Spark SQL by Ankur DaveGraphFrames: Graph Queries in Spark SQL by Ankur Dave
GraphFrames: Graph Queries in Spark SQL by Ankur Dave
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 

More from Python Homework Help

Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Homework Help
 

More from Python Homework Help (20)

Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Complete my Python Homework
Complete my Python HomeworkComplete my Python Homework
Complete my Python Homework
 
Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptx
 
Basic Python Programming.pptx
Basic Python Programming.pptxBasic Python Programming.pptx
Basic Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptx
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Perfect Python Homework Help
Perfect Python Homework HelpPerfect Python Homework Help
Perfect Python Homework Help
 

Recently uploaded

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Complex Python Programming.pptx

  • 2. Q.1 We wish to implement a set of classes to model graphs of the type that we used to describe search problems. Graphs have nodes and edges. A directed edge indicates a connection from the first node to the second. An undirected edge can be thought of as two directed edges, one from the first node to the second and another one going the other way. Nodes will be identified with names that are represented using Python strings. Implement a base class called DirectedGraph, which has the following methods hasNode - given a node, returns True when the node exists in the graph and false otherwise. addNode - given a node, if it’s not already present, it adds it to the graph, initially with no edges. addEdge - given two nodes, add a connection from the first to the second. If either node is not already present, it is added to the graph. children - given a node, returns a list of nodes that can be reached from that node by traversing a single arc. If the node is not present, it returns an empty list.
  • 3. Here is an example use: >>> g = DirectedGraph() >>> g.addNode(’A’) >>> g.addEdge(’A’,’B’) >>> g.children(’A’) [’B’] >>> g.children(’B’) [] Think very carefully about: •How you will keep track of the nodes in the graph. •How you will keep track of which nodes are connected. Each of the methods should be short. Define this class and its methods. Solution: class DirectedGraph: nodes = {} def hasNode(self,n): return n in self.nodes
  • 4. def addNode(self,n): if not self.hasNode(n): self.nodes[n] = {} def addEdge(self,n,c): self.addNode(n) self.addNode(c) if not (c in self.nodes[n]): self.nodes[n][c] = True def children(self,n): return self.nodes[n].keys() Q.2 Define a class UnDirectedGraph which has all the same methods as DirectedGraph, except that addEdge, adds edges in both directions. Here is an example use: >>> g = UnDirectedGraph() >>> g.addEdge(’A’,’B’) >>> g.children(’A’) [’B’] >>> g.children(’B’) [’A’] Define this class. Use inheritance to avoid rewriting all of the methods
  • 5. Solution: class UnDirectedGraph(DirectedGraph): def addEdge(self,n,c): DirectedGraph.addEdge(self,n,c) DirectedGraph.addEdge(self,c,n) Q.3 We can use the DirectedGraph class (previous problem) to define a search problem. We will define a state machine class, GraphSM, whose states correspond to the nodes in the graph and whose state transitions correspond to the edges in the graph. The input to the machine is an integer indicating which of the children of the current node will become the next state. If the input is greater than or equal to the length of the list of children, then the state remains unchanged. A GraphSM machine is initialized with an instance of DirectedGraph, a start node and an integer indicating the maximum number of children of any node in the graph. >>> g = DirectedGraph() >>> for s in [’S’, ’A’, ’B’, ’C’, ’D’, ’E’, ’F’, ’G’]: g.addNode(s)
  • 6. >>> for (a, b) in [(’S’, ’A’), (’S’, ’B’), (’A’, ’C’), (’B’, ’C’), (’B’, ’D’), (’C’, ’E’), (’D’, ’E’), (’D’, ’F’), (’F’, ’G’)]: g.addEdge(a,b) >>> m = GraphSM(g, ’S’, 2) >>> m.transduce([1,1,1,0]) [’B’, ’D’, ’F’, ’G’] >>> search.smSearch(GraphSM(g, ’S’, 2), goalTest=lambda s: s == ’E’) [(None, ’S’), (0, ’A’), (0, ’C’), (0, ’E’)] Define the GraphSM class. Define all the methods and attributes that you need for the state ma chine to be used for a search, as shown in the example above. You do not need to define a done method. Solution: class GraphSM(SM): def _init_(self, graph, startNode, maxChildren): self.graph = graph self.startState = startNode self.MaxChildren = maxChildren self.legalInputs = range(maxChildren)
  • 7. def getNextValues(self, state, inp) c = self.graph.children(state) if inp<len(c): state = c[inp] return (state,state)