Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Expert System
3.
Inference Engine
4.
Forward Chaining
4.1.
Characteristics
4.2.
Example
4.3.
Advantages
4.4.
Disadvantages
5.
Backward Chaining
5.1.
Characteristics
5.2.
Example
5.3.
Advantages
5.4.
Disadvantages
6.
Forward Chaining vs Backward Chaining
7.
Frequently Asked Questions
7.1.
What is AI?
7.2.
What is forward chaining?
7.3.
What is backward chaining?
7.4.
What is an expert system?
8.
Conclusion
Last Updated: Mar 27, 2024

Forward and Backward Chaining in AI

Author Ravi Khorwal
1 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Forward and backward chaining are two important fields of artificial intelligence. These two processes are used by expert systems in order to mimic human intelligence. Both of these mechanisms are used to derive a conclusion based on a given set of rules. 

Forward and Backward Chaining in AI

In this blog, we will discuss forward and backward chaining in AI in detail. So without any further wait, let's start learning!

Expert System

Expert systems in AI are interactive computer software. They are designed and developed to work with the ability of a human expert in a particular domain. It obtains vital info from its knowledge base and uses it to solve or interpret users' problems. This knowledge base is mainly contributed by humans who are experts in their specific fields.

There are five components in the expert system which are:

  • Knowledge Base
  • Inference Engine
  • User Interface
  • Explanation Module
  • Knowledge Acquisition System.
     

In this article, we will focus on the inference engine, which involves forward and backward chaining.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Inference Engine

It is important to understand about inference engine before learning about forward and backward chaining. An inference engine is a system which applies logical reasoning to draw conclusions and solve a problem based on given facts. 

Inference Engine

It consists of algorithms that bring useful info from the knowledge base and use it to conclude new facts to the user's issues. The inference engine uses two mechanisms, forward and backward chaining, in order to extract data from the knowledge base. 

Let us now discuss both of them one by one.

Forward Chaining

Forward chaining is a data-driven reasoning approach used by inference engines. It starts with given facts and applies rules to derive new conclusions or facts from them. The engine keeps applying the rules until it reaches a conclusion or cannot apply any more rules. It is based on logical prediction methodology. One of its examples is the prediction of trends in the stock market.

Characteristics

  • It is a bottom-up approach.
     
  • It starts with the initial state and progresses toward the end state to reach a conclusion.
     
  • It is a data-driven approach which utilises given data.
     
  • It derives conclusions without any need for explicit guidance.

Example

Given below are some facts and some rules:

Facts: 

  1. Sohail is a coder.
     
  2. Sohail studies computer science.
     

Rules:

  1. If a person is a coder, it means he enrolled in a college.
     
  2. If a person is enrolled in a college and studies computer science, it means he learned DSA.
     

Now, we will use forward chaining to derive a conclusion based on given facts and rules.

Step 1: If a person is a coder, it means he enrolled in a college.

  • FOL: ∀x (coder(x) → Enrolled_IN(x, college))
     
  • Applying rule 1 to the fact: Enrolled_IN(Sohail, college)
     

Step 2: If a person is enrolled in a college and studied computer science, it means he learned DSA.

  • FOL: ∀x,y (Enrolled_In(x, college) ∧ studies(x, computer science) → Learned_DSA(x))
     
  • Applying rule 2 to the fact: Learned_DSA(Sohail)
     

Final conclusion: Learned_DSA(Sohail)

In the above example, we used forward chaining to derive a conclusion based on given facts and rules. We converted facts and rules into FOL and reached the final conclusion.

Note: The FOL (First Order Logic) representation helps the inference engine to reach a conclusion effectively.

Advantages

Below are a few pros of forward chaining in AI.

  • It is a great way to reach a conclusion based on available data.
     
  • Forward chaining can provide more information from limited data.
     
  • It is suited for expert systems where an application needs more control and monitoring.
     
  • It should be applied when there is a very few numbers of initial states available.

Disadvantages

Below are a few cons of forward chaining in AI.

  • The inference engine generates information without knowing which data is more relevant and useful to reach the final result.
     
  • The inference engine may fire many unnecessary rules to reach the goal state.
     
  • The user may need to enter a lot of information to reach the goal state.
     
  • It can result in a high cost for the chaining process.

Backward Chaining

Backward chaining is also called backward reasoning. In this technique, the inference engine starts with the goal and works backwards to find evidence that supports the goal. In simple words, it works from the goal state and reaches the initial state.

Characteristics

  • It is a top-down approach.
     
  • It uses a depth-first search strategy.
     
  • It is a goal-driven approach, as we start from the goal state and reach the initial state.
     
  • It divides objectives into sub-goals to validate the facts.

Example

Given below are some facts and rules from which we would derive the conclusion.

Fact:

  1. Sohail is a coder.
     
  2. Sohail studies computer science.
     

Rules:

  1. If a person is a coder, it means they enrolled in a college.
     
  2. If a person is enrolled in a college and studies computer science, it means they learned DSA.
     

Goal: Did Sohail learn DSA or not?
 

Now, we will use backward chaining to check whether Sohail learns DSA or not.
 

Step 1: If a person is enrolled in a college and studies computer science, it means they learned DSA. (Rule 1)

  • FOL: ∀x,y (Enrolled_In(x, college) ∧ studies(x, computer_science) → Learned(x, DSA))
     

Now, we need to check if Sohail is enrolled in a college and studies computer science.

Step 2: If a person is a coder, it means they enrolled in a college. (Rule 2)

  • FOL: ∀x (coder(x) → Enrolled_In(x, college))
     

Step 3: We need to check if Sohail is a coder and studies computer science. 

  • Based on fact 1, Sohail is a coder.
     
  • Based on fact 2, Sohail studies computer science.
     

From the above, we can conclude that Sohail is enrolled in a college (from Rule 1) and studies computer science. Therefore, using the backward chaining, we can conclude that Sohail learned DSA.

Advantages

Below are a few pros of backward chaining in AI.

  • In backward chaining, the process terminates once the fact is verified.
     
  • It is a faster process as it only analyses and verifies facts.
     
  • It only considers the relevant part of the data and eliminates unnecessary information.
     
  • It is very effective in solving problems like debugging and diagnosing.

Disadvantages

Below are a few cons of backward chaining in AI.

  • In backward chaining, the goal should be known before starting the process.
     
  • Backward chaining is very difficult and complex to implement.
     
  • It can only generate a limited number of outcomes.
     
  • It only tests for the required rules and neglects the others.

Forward Chaining vs Backward Chaining

Forward Chaining

Backward Chaining

In forward chaining, the decision is taken based on given data. In backward chaining, the process starts from the goal state and reaches the initial state.
It is a data-driven technique. It is a goal-driven technique.
It is a bottom-up approach. It is a top-down approach.
It uses a breadth-first search strategy. It uses a depth-first search strategy.
Its only goal is to reach a conclusion. Its goal is to validate the facts.
It is a slow process. It is a fast process.
It operates in the forward direction (initial state to goal state). It operates in the backward direction (goal state to initial state).
It may include multiple ASK questions from the information source. Backward chaining includes fewer ASK questions compared to forward chaining.

Frequently Asked Questions

What is AI?

AI stands for artificial intelligence. It is a field of computer science that mimics human intelligence to perform specific tasks.

What is forward chaining?

Forward chaining is a method of reasoning in which inference rules are applied to a given data to reach an end result.

What is backward chaining?

Backward chaining is a concept in AI that involves backtracking from the goal state to reach the steps that lead to the goal state.

What is an expert system?

The expert system in AI is computer software that has the ability, as that of humans, to derive decisions based on the given knowledge base.

Conclusion

This article discusses the concept of forward and backward chaining in AI. We discussed the characteristics, pros, and cons of both methods, along with examples. We hope this blog has helped you enhance your knowledge of forward and backward chaining in AI. If you want to learn more, then check out our articles.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your coding ability, you may check out the mock test series and participate in the contests hosted on Coding Ninjas!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass