60 likes | 174 Vues
This document provides detailed pseudocode for two fundamental search algorithms—Breadth-First Search (BFS) and Depth-First Search (DFS). It outlines the core logic behind both algorithms, utilizing a queue for BFS and a stack for DFS to navigate through nodes in a graph. The algorithms efficiently explore nodes and check for a goal state, while maintaining lists of visited nodes to prevent cycles. Follow the structured steps for implementing BFS and DFS, along with practical examples to solidify understanding of these essential graph traversal techniques.
E N D
Extra notes Modified from Callan R (2003) Artificial Intelligence Palgrave ISBN 0-333-80136-9 pg 50-55
Psudeocode for BFS • node current; • queue toVisit; • List alreadyVisited; • put root node in toVisit; • while toVisit is notEmpty • current=first node in toVisit; • remove first node in toVisit; • if current==goal • add current to alreadyVisited; • return true; • endif • for each child node C of current • add C to toVisit; • endfor; • add current to alreadyVisited; • endwhile;
toVisit alreadyVisited • A [] • BC A • CDE AB • DEFGH ABC • EFGHI ABCD • FGHIJK ABCDE • GHIJKLM ABCDEF • HIJKLM ABCDEFG • IJKLMN ABCDEFGH • JKLMNO ABCDEFGHI • KLMNO ABCDEFGHIJ • LMNO ABCDEFGHIJK • MNOP ABCDEFGHIJKL • NOPQ ABCDEFGHIJKLM • OPQ ABCDEFGHIJKLMN • PQ ABCDEFGHIJKLMNO • Q ABCDEFGHIJKLMNOP • [] ABCDEFGHIJKLMNOPQ
Psudeocode for DFS • node current; • stack toVisit; • List alreadyVisited; • put root node in toVisit; • while toVisit is notEmpty • current=first node in toVisit; • remove first node in toVisit; • if current==goal • add current to alreadyVisited; • return true; • endif • for each child node C of current • add C to toVisit; • endfor; • add current to alreadyVisited; • endwhile;
toVisit alreadyVisited • A [] • BC A • DEC AB • IEC ABD • OEC ABDI • EC ABDIO • JKC ABDIOE • KC ABDIOEJ • C ABDIOEJK • FGH ABDIOEJKC • LMGH ABDIOEJKCF • PMGH ABDIOEJKCFL • MGH ABDIOEJKCFLP • QGH ABDIOEJKCFLPM