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 căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
Vince 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
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Luis Goldster
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Tony Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Fraboni Ec
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
James Wong
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Young Alista
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Harry 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

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

Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdfFinancial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
MinawBelay
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdfFinancial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 

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)