N1Chapter6 Student
N1Chapter6 Student
Graph
Search
2.Breadth-FirstSearch (BFS)
vs.Depth-First Search
(DFS) Instead 0f
Chapter 6: Using Heuristic in Games
Tree Search vs. Graph Search
Tree Search:
• Does NOT remember visited states
• May revisit the same state via different paths
• Can create redundant paths and loops
Graph Search:
• Keeps track of visited states
• Avoids revisiting the same states
• More memory efficient and faster
• Unweighted Graph: In unweighted graphs, all edges have the same weight (or
cost).
• BFS algorithm is well-suited for finding the shortest path in unweighted
graphs. This is because BFS systematically explores the graph level by level,
ensuring that the first path found to any node is the shortest path.
• BFS's Level-by-Level Exploration: BFS starts at a source node and explores all its
immediate neighbors, then their neighbors, and so on, in a layer-by-layer fashion.
• Shortest Path Guarantee: Because BFS visits all nodes at a given level before
moving to the next, the first time it reaches a target node, it has found the
shortest path from the source.
• Analogyتشبيه: Think of it like exploring a maze. BFS systematically checks all
paths at each step before moving to a deeper level, ensuring it doesn't miss the
shortest route.
• Use BFS when you want to compute a shortest-path tree (for a graph with edges all
with weight 1) where the path length is the minimum number of edges. This is
computed exactly by BFS, the forward/discover edges form exactly such a tree
(following the forward edges will get you such a shortest path).
• Remember that BFS will explore the graph in layers/levels, so any path you find
between two vertices via forward edges will use a minimum number edges. Well,
if the graph is unweighted, then you can imagine each edge having weight 1,
meaning the length of a path is simply the number of edges.
• Both DFS and BFS ensures that they don't visit any vertex
twice. In DFS or BFS, if a node is visited, it won't be processed
again.
• It is a common misconception though, people saying "DFS"
when they mean backtracking.
• Beware that the number of paths may be huge, in worst case
there may be O (N!) paths.
• Question: What is the best algorithm in finding the shortest path?
Answer: There is no ‘best’ algorithm in general, it depends on
you actual problem.
• DFS can be obtained from the BFS algorithm by using a stack K
in place of Q, and by writing new paths at the top rather than at
the bottom of K.
• In BFS, we insert first node in a queue then we keep removing
nodes from rear and keep inserting their adjacent nodes in the
same queue if they are not already visited. This restricts visiting
a non adjacent node before all adjacent ones.
Can the shortest path problem for cyclic graphs be
solved by BFS?
• Yes, BFS works on cyclic graphs. You maintain a
set of vertices you've already seen, and when a
vertex that has previously been seen is seen again,
you avoid adding it to the queue of vertices to
explore. Thus, we guarantee that each vertex will
be added to the queue and hence explored only
once.
• Remember that the time complexity is O (V + E).
The BFS algorithm is particularly useful for one thing: Finding the shortest path on unweighted graph
https://www.youtube.com/watch?v=oDqjPvD54Ss
How many distinct possible states are there
in a 2×2 Rubik’s Cube?
A 2x2 cube has 3,674,160 distinct possible states. This is
because:
There are 8 corner pieces that can move around 8! =
40,320 ways.
Each corner can twist in 3 directions, but only 7 can
twist freely (the last one is fixed) → 3^7 = 2,187 ways.
But we don’t count cube rotations (like turning the
whole cube in your hands) → so we divide by 24.
8!*38/24/3= FACT(8)*POWER(3;8)/24/3= 3,674,160 different positions.
Trees in CS are upside
down. The roots are at
the top. The leaves are at
the bottom because we
want to think about
starting at the beginning
of the tree. Furthermore,
any pair of nodes are حقيب
connected. ة