Articles / AI · Part 31

Forward Chaining and Backward Chaining in Artificial Intelligence

Understand Forward Chaining and Backward Chaining in Artificial Intelligence. Learn how expert systems use data-driven and goal-driven reasoning, with examples, Python implementations, applications, and comparisons.

Forward Chaining and Backward Chaining in Artificial Intelligence

Imagine an expert system designed to identify problems in a machine.

A worker reports that the machine is overheating. The system checks its stored knowledge, examines the available facts, and tries to determine the possible cause.

Now imagine a different situation.

A worker asks, “Is the cooling system faulty?”

Instead of starting with all available information, the system begins with this specific question and searches for the facts needed to confirm or reject it.

Both situations involve reasoning, but the direction of reasoning is different.

These two approaches are known as Forward Chaining and Backward Chaining. They are important reasoning techniques used by expert systems to derive conclusions from stored knowledge.

Let’s understand how they work.

1. What Is Chaining in Artificial Intelligence?

In Artificial Intelligence, chaining refers to the process of applying logical rules to available facts to derive new information or reach a conclusion.

Expert systems commonly represent knowledge through IF–THEN rules.

For example:

IF temperature is high
THEN overheating is possible.

When the condition is satisfied, the system can apply the rule and derive a new fact.

Several rules can be connected together so that the conclusion of one rule becomes the input for another. This process is called chaining.

There are two major approaches:

  • Forward Chaining: Starts with known facts and moves towards conclusions.
  • Backward Chaining: Starts with a goal and works backwards to determine whether it can be established.

2. Forward Chaining

Forward Chaining is a data-driven reasoning technique.

It starts with the facts already available in the system and applies rules to generate new facts until a conclusion is reached or no more rules can be applied.

In simple terms, the system asks: “What can I conclude from the information I already have?”

Example of Forward Chaining

Suppose an expert system contains the following rules:

Rule 1:
IF temperature is high
THEN machine is overheating.

Rule 2:
IF machine is overheating
THEN inspect the cooling system.

Rule 3:
IF cooling system is damaged
THEN machine requires repair.

Now suppose the system receives this fact:

Fact:
Temperature is high.

The system begins applying its rules.

Step 1: It checks Rule 1.

Temperature is high, so it derives:

Machine is overheating.

Step 2: The new fact matches Rule 2.

The system derives:

Inspect the cooling system.

Step 3: If further information confirms that the cooling system is damaged, Rule 3 can be applied.

The system derives:

Machine requires repair.

The reasoning moves from known information towards new conclusions.

FORWARD CHAINING FLOW

       Known Facts
            |
            v
       Apply Rules
            |
            v
        New Facts
            |
            v
       Apply Rules
            |
            v
        Conclusion

How Forward Chaining Works

  1. The system receives initial facts.
  2. It checks which rules have their conditions satisfied.
  3. It applies the matching rules.
  4. New facts are added to the available information.
  5. The process repeats until a goal is reached or no more rules can be applied.

Forward chaining is useful when a system has information available and needs to discover what conclusions can be drawn from it.

3. Backward Chaining

Backward Chaining is a goal-driven reasoning technique.

Instead of starting with known facts, the system starts with a specific goal or conclusion and works backwards to determine whether the available facts support it.

In simple terms, the system asks:

“What information must be true for this conclusion to be reached?”

Example of Backward Chaining

Consider the same machine-diagnosis system.

Suppose the user asks:

Is the machine overheating?

The system treats this as its goal.

It searches the knowledge base for a rule that can establish this conclusion.

Rule 1:
IF temperature is high
THEN machine is overheating.

The system now needs to verify whether the temperature is high.

It may ask the user:

Is the machine temperature high?

If the user confirms it, the system establishes the goal.

The reasoning works backwards:

Goal:
Machine is overheating.
        |
        v
Find supporting rule
        |
        v
Required condition:
Temperature is high.
        |
        v
Check available facts
        |
        v
Goal established

If the required condition cannot be established, the system cannot confirm the goal through that rule.

How Backward Chaining Works

  1. The system receives a goal or query.
  2. It searches for rules that can establish that goal.
  3. It identifies the conditions required by those rules.
  4. It checks whether those conditions are already known.
  5. If information is missing, it searches for additional rules or asks for facts.
  6. It continues until the goal is established or cannot be supported.

Backward chaining is useful when the system needs to answer a specific question rather than discover every possible conclusion.

4. Forward Chaining vs Backward Chaining

Both techniques use rules and facts, but their reasoning directions are different.

Feature Forward Chaining Backward Chaining
Approach Data-driven Goal-driven
Starting point Known facts A goal or query
Direction Facts to conclusions Goal to required facts
Main question What can be concluded? Can this goal be proved?
Rule usage Applies rules whose conditions match known facts Finds rules that can establish the goal
Typical use Monitoring and discovering conclusions Diagnosis and answering specific queries

Neither approach is universally better. The suitable method depends on the problem being solved.

5. A Simple Python Example

Let’s implement a basic version of Forward Chaining and Backward Chaining.

We will use a small rule-based system.

A. Forward Chaining Implementation

rules = [
    ({"fever"}, "possible_infection"),
    ({"possible_infection", "cough"}, "medical_assessment_needed")
]

facts = {"fever", "cough"}

changed = True

while changed:
    changed = False

    for conditions, conclusion in rules:
        if conditions.issubset(facts):
            if conclusion not in facts:
                facts.add(conclusion)
                changed = True

print("Final facts:", facts)

Output:

Final facts: {
    'fever',
    'cough',
    'possible_infection',
    'medical_assessment_needed'
}

The system starts with the available facts and repeatedly applies rules to derive new information.

This is a simplified educational example, not a medical diagnostic tool.

B. Backward Chaining Implementation

Now let’s check whether a particular goal can be established.

rules = {
    "possible_infection": [{"fever"}],
    "medical_assessment_needed": [
        {"possible_infection", "cough"}
    ]
}

facts = {"fever", "cough"}

def backward_chain(goal, visited=None):
    if visited is None:
        visited = set()

    if goal in facts:
        return True

    if goal in visited:
        return False

    visited.add(goal)

    for conditions in rules.get(goal, []):
        if all(
            backward_chain(condition, visited.copy())
            for condition in conditions
        ):
            return True

    return False


goal = "medical_assessment_needed"

if backward_chain(goal):
    print("Goal established")
else:
    print("Goal not established")

Output:

Goal established

Here, the system begins with the goal medical_assessment_needed.

It searches for the conditions required to establish that goal, then checks whether those conditions can be supported by the available facts and rules.

These implementations demonstrate the basic reasoning ideas. Real expert systems need additional mechanisms for handling conflicting rules, repeated facts, missing information, and more complex knowledge.

6. Applications of Forward Chaining and Backward Chaining

Both approaches are used in different kinds of knowledge-based systems.

Forward Chaining Applications

Monitoring systems: A system receives continuous information from sensors and derives conclusions when certain conditions occur.

Industrial fault detection: Available machine readings can trigger rules that identify possible problems.

Business rule systems: New information can activate rules that produce decisions or recommendations.

Process automation: Systems can apply rules to incoming data to determine the next action.

Backward Chaining Applications

Medical decision support: A system can begin with a possible diagnosis and check whether the required symptoms and conditions are present.

Troubleshooting: A system can start with a suspected fault and investigate the evidence needed to support it.

Question-answering systems: A system can begin with a question and search for facts and rules that establish an answer.

Logic programming: Languages such as Prolog use goal-directed reasoning closely associated with backward chaining.

7. Advantages and Limitations

Forward Chaining

Advantages

  • Works well when a large amount of information is available.
  • Can discover multiple conclusions from the same set of facts.
  • Useful for monitoring and event-driven systems.
  • New facts can trigger further reasoning automatically.

Limitations

  • May derive conclusions that are not relevant to the user’s immediate question.
  • Can apply many rules unnecessarily.
  • Large rule sets may make the reasoning process expensive.

Backward Chaining

Advantages

  • Focuses on a specific goal or question.
  • Avoids exploring unrelated conclusions.
  • Useful for diagnosis and query-based systems.
  • Can reduce unnecessary reasoning when the goal is clearly defined.

Limitations

  • Depends on having a suitable goal.
  • May require checking many possible rules to establish a conclusion.
  • Can become complicated when goals depend on many conditions.
  • Poorly designed systems need mechanisms to handle repeated or circular reasoning.

Conclusion

Forward Chaining and Backward Chaining are two fundamental reasoning techniques used in expert systems.

Forward Chaining begins with known facts and moves towards conclusions. Backward Chaining begins with a goal and searches for the facts and rules needed to establish it.

The difference is mainly the direction in which reasoning takes place.

Both approaches help expert systems use stored knowledge to solve problems, answer questions, and provide recommendations.

In the next article, we will explore what happens when multiple rules can be applied at the same time, and how an expert system decides which rule to execute first.


Next Article: Conflict Resolution in Expert Systems

Quick quiz

5 questions
What is Forward Chaining?
What is the starting point of Backward Chaining?
Which technique is more suitable when a system needs to discover conclusions from incoming sensor data?
Which technique is commonly associated with Prolog's goal-directed reasoning?
What is the main difference between the two techniques?

Keep a copy

Read offline as a printout or PDF.

farhanpaal.com

Keep reading

AI · Part 8

Rationality and the Concept of a Rational Agent.

Understand rationality in Artificial Intelligence and how rational agents select actions using performance measures, percept sequences, built-in knowledge, and available information.

8 min read

Ask Farhan

Ask anything about Farhan