0% found this document useful (0 votes)
12 views

Practical 6

Uploaded by

pramuapex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Practical 6

Uploaded by

pramuapex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Practical 06

Search Strategies - Python

By Malsha Prabuddhi
BSC, UOM
Breadth First Search
BFS pseudocode

The pseudocode for BFS in python goes as below:


create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbors of u
Example 01
Example 02
Depth First Search
DFS Pseudocode
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}
Example 01
Example 02
Best First Search
Pseudocode
Example 01
01
02
A* Search
• To the solution please implement the example given in the below link.

https://www.askpython.com/python/examples/a-star-
algorithm#:~:text=Generally%2C%20the%20A*%20algorithm%20is,low
est%20f(x)%20value.

• Try the example given in below link.


https://www.pythonpool.com/a-star-algorithm-python/
Propositional Logic
Propositional Logic In Python

• Let’s start with a Harry Potter example. Consider the following


sentences:
• 1. If it didn’t rain, Harry visited Hagrid today.
• 2. Harry visited Hagrid or Dumbledore today, but not both.
• 3. Harry visited Dumbledore today.
• Here is how we can go about it: looking at sentence 3, we know
Harry visited Dumbledore. Looking at sentence 2, we know
Harry visited either Dumbledore or Hagrid, and thus we can
conclude 4. Harry did not visit Hagrid.
• Now, looking at sentence 1, we understand that if it didn’t rain,
Harry would have visited Hagrid. However, knowing sentence
4, we know that this is not the case. Therefore, we can
conclude 5. It rained today.
01
'''"We make decision from knowledge base " ,
02 all of the statement will be true then knowlegde base will be
true'‘’
knowledge_base = And(
Implication(Not(rain), hagrid),
Or(hagrid, dumbledore),
Not(And(hagrid, dumbledore)), hagrid
)
print(knowledge_base.formula())
''' Here we use the model_check function and here two
arguments
are knoledge_base and another is it rain today or not. Then we
print it’’’
print(model_check(knowledge_base, rain))
• Reference
https://www.linkedin.com/pulse/knowledge-representation-using-
propositional-logic-python-tusar
Negation
Conjunction
Disjunction
Exclusive Disjunction
Implication
Bi-Implication
Compound Propositions
Edit the code above to reveal the truth value of the compound proposition:
(p∨¬q)∧¬p
Thank You

You might also like