Title: CS1321: Introduction to Programming
1CS1321Introduction to Programming
- Georgia Institute of Technology
- College of Computing
- Module 27
- Utilizing/Manipulating Graphs DFS BFS
2Searching
In this module, well explore the idea of
searching for a value in a Graph. Well start
with trees (a specialized version of a graph) and
work our way to a complex graph structure. Weve
already explored the idea of searching data
structures that are really just seriously
simplified versions of graphs
3Lists
1
2
3
Searching for a value in a list of items was a
relatively straight-forward process. We started
at the beginning of the list and recurred down
our links until we reached either the end or the
value we were looking for.
4Binary Search Trees
50
Target 70
10
100
4
70
40
150
Binary Search Trees added some complexity to our
algorithm. We no longer had a straight path to
explore. We now had the possibility of branching
paths. But by using the property of BSTs
5Binary Search Trees
50
Target 70
10
100
4
70
40
150
We could essentially reduce our BST to a single
path that would either have the value, or not.
6Binary Trees
40
Target 70
100
10
70
44
40
1
But what about regular Binary Trees, where there
is no ordering relationship between different
nodes? Or N-ary trees? Or Graphs?
7Exhaustive Searching
40
Target 70
100
10
70
44
40
1
In these cases, we have to perform some sort of
exhaustive search that will traverse as much of
the tree as necessary. We stop when we find the
sought value or we run out of places to look.
8Why BTs?
Were starting with Binary Trees because theyre
simplified graphs. The principals that we
discuss here will apply (with few modifications)
to n-ary trees, and even to graphs. But here,
were taking advantage of two properties of
Binary Trees
- Theyre binary. There will be 0, 1, or 2 child
nodes connected to each node. - As this is a tree, there is no possibility of
cycles, a concept that adds complexity
9Depth First Search The basic idea
Given a binary tree to search and a target value,
we can simplify the possible locations of our
target value to one of three places
- The current node
- Somewhere in the Left Subtree
- Somewhere in the Right Subtree
10Isnt that Pre-order traversal?
Its very close to the idea of pre-order
traversal. The key difference is that pre-order
traversal TRAVERSES the ENTIRE tree, whereas our
Depth First Search will attempt to search the
whole tree, but will STOP searching if it finds
the value. Which means we need to put that sort
of logic into our algorithm.
11Lets re-examine our Statement
- Our value might not be in the tree at all, but we
can only say that for sure if the tree is
empty. - The target value might be at our current node.
We should check it. - If it is not in the current node, our target
value could be in the left subtree OR the right
subtree
12Lets re-examine our Statement
- (define (DFS in-BT target)
- (cond (not in-BT) false
- The target value might be at our current node.
We should check it. - If it is not in the current node, our target
value could be in the left subtree OR the right
subtree
13Lets re-examine our Statement
- (define (DFS in-BT target)
- (cond (not in-BT) false
- else (cond ( (node-data in-BT)
target) - target
- If its not in the current node, our taret value
could be in the left subtree OR the right subtree
14Lets re-examine our Statement
- (define (DFS in-BT target)
- (cond (not in-BT) false
- else (cond ( (node-data in-BT)
target) - target
- else (or
- (DFS (node-left in-BT) target)
-
(DFS (node-right in-BT) target)))) -
15Err.
If you were paying attention, you might have
noticed something a bit odd about the solution to
the last problem. If we found the value we were
looking for, we returned that value. But we were
also using the function (or) which tests for a
condition in a series of conditions to be true.
If our function potentially returns a number,
how can we use that value in (or )?
16The awful Truth about Truth
It turns out that advanced scheme and real scheme
introduce a concept that is found in most
programming languages As long as we define a
specific value for false, we can state that
ANYTHING that is not false is therefore true.
17The awful Truth about Truth
In Scheme, FALSE is defined as false (or f). So
anythingtrue, numbers, symbols, structures, etc
that is not false, is true. Whats more,
functions such as (and ) and (or ) return the
truth VALUE it encountered when it made its final
determination. So in this case, (or ) will
return the target value or false if the target
was not found.
18Behavior of DFS
130
100
10
70
44
40
90
Lets trace this on the board!
19Breadth First Search
An alternative to Depth First Search, is Breadth
First Search. DFS basically guessed that the
target value will be down a particular path, and
races down that path until it finds it or fails,
at which point it backs up and tries a different
path. BFS takes the viewpoint that if the
target value is not the first node encountered,
it might be close to that first node. So it
carefully examines every level of nodes one at
a time, hoping to find the value.
20In Other Words
130
BFS examines this node first
Then every node on this level
100
10
And then the nodes on this level
70
44
40
90
21Queues
To accomplish this, BFS takes advantage of a
queue of nodes. You can think of a queue as a
specialized sort of list. You can only add items
(enqueue) to the back of the list. You can only
take items off (dequeue) from the front of the
list. Examples the line of people at a bank or
movie theater.
22The basic idea of BFS
For this example, well be working with an n-ary
tree, which will be represented by
mutually-referential data definitions (define-str
uct node (data lod)) Where data is a number, and
lod is a list-of-data A list-of-data is
either 1) the empty list empty 2)
(cons d lod) where d is a node, and lod is a
list-of-data
23The basic idea of BFS
- Lets assume that our function will take in a
queue of nodes to examine - If the queue is empty, theres nothing to search,
lets return false. - Otherwise, dequeue the first item.
- If its the thing were looking for, return that
node. - Otherwise, enqueue all the children of this node,
and recur.
24The basic idea of BFS
- (define (BFS in-tree-node target)
- (BFS-helper (enqueue in-tree-node) target))
- If the queue is empty, theres nothing to search,
lets return false. - Otherwise, dequeue the first item.
- If its the thing were looking for, return that
node. - Otherwise, enqueue all the children of this node,
and recur.
25The basic idea of BFS
- (define (BFS in-tree-node target)
- (BFS-helper (list in-tree-node) target))
- (define (BFS-helper in-queue target)
- (cond (empty? in-queue) false
- Otherwise, dequeue the first item.
- If its the thing were looking for, return that
node. - Otherwise, enqueue all the children of this node,
and recur.
26The basic idea of BFS
- (define (BFS in-tree-node target)
- (BFS-helper (enqueue in-tree-node) target))
- (define (BFS-helper in-queue target)
- (cond (empty? in-queue) false
- else (local ((define item (first
in-queue))) - If its the thing were looking for, return that
node. - Otherwise, enqueue all the children of this node,
and recur.
27The basic idea of BFS
- (define (BFS in-tree-node target)
- (BFS-helper (enqueue in-tree-node) target))
- (define (BFS-helper in-queue target)
- (cond (empty? in-queue) false
- else (local ((define item (first
in-queue))) - (cond ( target
(node-data item)) item - Otherwise, enqueue all the children of this node,
and recur.
28The basic idea of BFS
(define (BFS in-tree-node target) (BFS-helper
(enqueue in-tree-node) target)) (define
(BFS-helper in-queue target) (cond (empty?
in-queue) false else (local
((define item (first in-queue)))
(cond ( target (node-data item))
item else (BFS-helper (append (rest
in-queue) (node-lod item))
target)))))
29Does it work?
100
(
)
74
90
180
(
14
45
)
130
300
(
)
160
)
3
30
89
10
(
Lets try it out on the board!
30Next Up
DFS BFS in graphs!