UNIT-02
UNIT-02
Stacks
• A stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
• The last element added is the first one to be removed.
• Operations:
• Push: Adds an element to the top of the stack.
• Pop: Removes the top element from the stack.
• Peek/Top: Returns the top element without removing it.
• IsEmpty: Checks if the stack is empty.
• Applications in AI:
• Used in Depth-First Search (DFS) algorithms for traversing trees and graphs.
• Backtracking algorithms ( solving puzzles, pathfinding).
• Managing function calls in recursion.
Stacks
Queues
• A queue is a linear data structure that follows the First In, First Out (FIFO) principle. The first
element added is the first one to be removed.
• Operations:
• Enqueue: Adds an element to the rear of the queue.
• Dequeue: Removes an element from the front of the queue.
• Front: Returns the front element without removing it.
• IsEmpty: Checks if the queue is empty.
• Types:
• Simple Queue: Basic FIFO structure.
• Priority Queue: Elements are dequeued based on priority.
• Circular Queue: Rear and front are connected in a circle.
• Applications in AI:
• Used in Breadth-First Search (BFS) algorithms for traversing trees and graphs.
• Task scheduling and resource management.
• Simulation of real-world systems (e.g., customer service queues).
Queues
Trees
• A tree is a hierarchical data structure consisting of nodes connected by edges. It has a root node
and zero or more subtrees.
• Key Terms:
• Root: The topmost node.
• Parent/Child: Nodes connected by edges.
• Leaf: Nodes with no children.
• Depth/Height: Levels in the tree.
• Types:
• Binary Tree: Each node has at most two children.
• Binary Search Tree (BST): Left child < Parent < Right child.
• Balanced Trees: AVL, Red-Black Trees.
• Decision Trees: Used in machine learning for classification and regression.
• Applications in AI:
• Decision-making algorithms (e.g., decision trees in ML).
• Hierarchical data representation (e.g., organizational charts).
• Game trees for AI in games (e.g., Minimax algorithm).
Binary Tree:
• Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures,
trees are hierarchical data structures.
• A binary tree is a tree data structure in which each node has at most two children,
which are referred to as the left child and the right child.
• It is implemented mainly using Links.
• A Binary Tree is represented by a pointer to the topmost node in the tree.
• If the tree is empty, then the value of root is NULL.
A Binary Tree node contains the following parts.
• 1. Data
• 2. Pointer to left child
• 3. Pointer to the right child
Binary Search Tree(BST)
• Binary Search Tree is a type of binary tree in which each node's left subtree contains
only values smaller than the node, and each node's right subtree contains only
values greater than the node.
A Binary Search Tree is a Binary Tree following the additional properties:
• The left part of the root node contains keys less than the root node key.
• The right part of the root node contains keys greater than the root node key.
• There is no duplicate key present in the binary tree.
A Binary tree having the following properties is known as Binary search tree (BST).
Graphs
• A graph is a non-linear data structure consisting of nodes (vertices) connected by edges.
• It can represent relationships between entities.
• Vertex (Node): Represents an entity.
• Edge: Represents a connection between nodes.
• Directed/Undirected: Edges with or without direction.
• Weighted/Unweighted: Edges with or without weights.
• Types:
• Directed Graph: Edges have a direction.
• Undirected Graph: Edges have no direction.
• Cyclic/Acyclic: Contains cycles or not.
• Applications in AI:
• Pathfinding algorithms (e.g., Dijkstra’s, A*).
• Social network analysis.
• Knowledge representation (e.g., semantic networks).
• Neural networks (a type of graph structure).
Graphs
• Graph is a data structure that consists of a collection of nodes (vertices) connected by edges.
• Graphs are used to represent relationships between objects and are widely used in computer
science, mathematics, and other fields.
• Graphs can be used to model a wide variety of real-world systems, such as social networks,
transportation networks, and computer networks.
• Stacks and Queues are linear structures used for managing data in
specific orders (LIFO and FIFO, respectively).
• Trees are hierarchical structures used for representing data with
parent-child relationships.
• Graphs are versatile structures used to model relationships and
connections between entities.
General Search Algorithms
Example:
Question. Which solution would BFS find to move
from node S to node G if run on the graph below?
Solution.
• The equivalent search tree for the above graph is as follows.
• As BFS traverses the tree “shallowest node first”, it would always pick
the shallower branch until it reaches the solution (or it runs out of
nodes, and goes to the next branch).
• The traversal is shown in blue arrows.
Path: S -> D -> G
Uniform Cost Search:
UCS is different from BFS and DFS because here the costs come into play.
In other words, traversing via different edges might not have the same cost.
The goal is to find a path where the cumulative sum of costs is the least.
Example:
Question. Which solution would UCS find to move from node S to node G if run on the
graph below?
Solution.
• The equivalent search tree for the above graph is as follows.
• The cost of each node is the cumulative cost of reaching that node from the root. Based on the
UCS strategy, the path with the least cumulative cost is chosen.
• Note that due to the many options in the fringe, the algorithm explores most of them so long as
their cost is low, and discards them when a lower-cost path is found; these discarded traversals are
not shown below. The actual traversal is shown in blue.
Searching for Solutions
• The process of finding a sequence of actions that lead to a goal state from an
initial state.
• Example: In pathfinding, A* search finds the shortest route from a starting
city to a destination using heuristics.
Problem-Solving Agents
• Search Heuristics: In an informed search, a heuristic is a function that estimates how close a state is to the
goal state. For example – Manhattan distance, Euclidean distance, etc. (Lesser the distance, closer the goal.)
Different heuristics are used in different informed algorithms discussed below.
• Greedy Search:
• In greedy search, we expand the node closest to the goal node. The “closeness” is estimated by a heuristic
h(x).
Strategy: Expand the node closest to the goal state, i.e. expand the node with a lower h value.
Question. Find the path from S to G using greedy
search. The heuristic values h of each node below the
name of the node
Solution.
• Starting from S, we can traverse to A(h=9) or D(h=5).
• We choose D, as it has the lower heuristic cost.
• Now from D, we can move to B(h=4) or E(h=3). We choose E with a lower heuristic cost.
• Finally, from E, we go to G(h=0). This entire traversal is shown in the search tree below, in blue.
A* Tree Search:
• A* Tree Search, or simply known as A* Search, combines the strengths of uniform-cost search
and greedy search. In this search, the heuristic is the summation of the cost in UCS, denoted by
g(x), and the cost in the greedy search, denoted by h(x). The summed cost is denoted by f(x).
• Heuristic: The following points should be noted wrt heuristics in A* search. f(x) = g(x) + h(x)
• Here, h(x) is called the forward cost and is an estimate of the distance of the current node from the
goal node.
• And, g(x) is called the backward cost and is the cumulative cost of a node from the root node.
• A* search is optimal only when for all nodes, the forward cost for a node h(x) underestimates the
actual cost h*(x) to reach the goal. This property of A* heuristic is called admissibility.
Question. Find the path to reach from S to G using
A* search.
Solution
• . Starting from S, the algorithm computes g(x) + h(x) for all nodes in
the fringe at each step, choosing the node with the lowest sum. The
entire work is shown in the table below.
Note that in the fourth set of iterations, we get two paths with equal
summed cost f(x), so we expand them both in the next set. The path
with a lower cost on further expansion is the chosen path.
Solution
• Path: S -> D -> B -> E -> G
Cost: 7
A* Graph Search:
• A* tree search works well, except that it takes time re-exploring the
branches it has already explored. In other words, if the same node has
expanded twice in different branches of the search tree, A* search
might explore both of those branches, thus wasting time
• A* Graph Search, or simply Graph Search, removes this limitation by
adding this rule: do not expand the same node more than once.
• Heuristic. Graph search is optimal only when the forward cost between
two successive nodes A and B, given by h(A) – h (B), is less than or
equal to the backward cost between those two nodes g(A -> B). This
property of the graph search heuristic is called consistency.
Question. Use graph searches to find paths
from S to G in the following graph.
the Solution. We solve this question pretty much
the same way we solved last question, but in this
case, we keep a track of nodes explored so that we
don’t re-explore them.
• Path: S -> D -> B -> E -> G
Cost: 7
Informed Search Methods
• Use heuristics to prioritize nodes and guide the search.
• Example: A* Algorithm, Best-First Search
Difference between BFS and
DFS
BFS
• Approach: BFS explores the graph level by level, starting from the
source node. It visits all the nodes at one level before moving to the
next. BFS uses a queue to keep track of nodes that need to be
explored.
• How it works: From the source node, BFS moves to all its direct
neighbors (Layer 1), then moves to the next layer (Layer 2),
continuing until all nodes are visited.
DFS
• Approach: DFS explores as deep as possible along a branch before
backtracking. It uses a stack (or recursion) to keep track of nodes and
backtracks when it reaches a dead end.
• How it works: Starting from the source node, DFS goes deeper into
the graph, visiting one branch of the graph first before backtracking to
explore others.
DFS and BFS Time Complexity