Unit 2 AI
Unit 2 AI
DFS
• LIFO(Stack)
• Deepest Node
• Incomplete
• No guarantee to provide Optimal Solution
• Time complexity O(bd)
• During DFS traversal, a depth first search forest is also constructed.
• The starting vertex of the traversal serves as the root of the first tree.
• For the first time, if a new unvisited vertex is reached, then it is attached as a child to
the vertex.
• This edge is called tree edge, the set of all such edges form a forest.
• The traversal encounter an edge leading to a previously visited vertex.
• Such edge is a called back edge.
• 1->2, 2->3, 3->1, the edge from 3 back to 1 is a back edge, forming a cycle.
DFS
Input: Graph G = (V, E) Output: Depth-First Search traversal and numbering of vertices
dfs(G)
begin
count = 0
for(each vertex v in V)
if (v is marked with 0)
dfs(v)
dfs(v)
Visits recursively all the unvisited vertices connected to vertex v and assign them a number
in the order they are encountered
count = count + 1
for(each vertex w in V adjacent to v)
if (w is marked with 0)
dfs(w)
• Consider a simple graph with vertices and edges:
• Graph G = (V, E)
• V = {A, B, C, D, E, F}
• E = {(A, B), (A, C), (B, D), (C, E), (D, F)}
Let's represent the graph visually:
A 1.Visit A.
2.Visit B (A's neighbor).
/\ 3.Visit D (B's neighbor).
4.Visit F (D's neighbor).
B C
5.Return to D (no more unvisited neighbors).
/ \ 6.Return to B (no more unvisited neighbors).
7.Visit C (A's neighbor).
D E 8.Visit E (C's neighbor).
\
F
A->B->D->F->C->E.
BFS
• FIFO (queue)
• Shallowest Node
• Complete
• It generates Optimal Solution
• Time complexity O(b )
d
• BFS
Input: Graph G = (V, E) Output: Breadth First Search traversal and numbering of vertices
bfs(G)
begin
count = 0;
for(each vertex v in V)
if (v is marked with 0)
bfs(v);
bfs(v);
Visits all the unvisited vertices connected to vertex v and assigns them a number in the order they are encountered
count = count + 1; mark v with count and initialize a queue with v;
While(the queue is not empty)
for(each vertex w in V adjacent to the fronts vertex v)
if (w is marked with 0)
count=count+1; add w to queue;
remove vertex v from the front of the queue;
end
Generate and Test
• Generate and Test Search is a heuristic search technique based on Depth First
Search with Backtracking which guarantees to find a solution
• In this technique, all the solutions are generated and tested for the best solution.
It ensures that the best solution is checked against all possible generated
solutions.
Algorithm
• Non Redundant: Good Generators should not yield a duplicate solution at any
point of time as it reduces the efficiency of algorithm thereby increasing the time
of search and making the time complexity exponential.
• Informed: Good Generators have the knowledge about the search space which
they maintain in the form of an array of knowledge.
Advantages:
• May not guarantee optimality: Might miss the best solution if not
thoroughly explored.
3. If a candidate passes all tests, it's a valid solution; otherwise, try a different
candidate.
2. Test each path to see if it leads to the exit without hitting walls.