Title: Graph Traversal
1Graph Traversal
2Outline
- Prerequisites
- List manipulation
- Objectives
- Basic Path Finding
- Greedy Algorithms
- Dynamic Programming
- Reference
- HTDP Section 28.1
3Graph Traversal
- Consider a directed graph
4Representing this Graph
- a node has data and a list of
- children links
- (define-struct node (data children))
- each child link has a cost and a
- destination
- (define-struct link (cost dest))
- (define b (make-node 'b empty))
- (define f-b (make-link 7 b))
- (define f (make-node 'f (list f-b)))
- (define e-b (make-link 5 b))
- (define e (make-node 'e (list e-b)))
- (define g-e (make-link 7 e))
- (define g-b (make-link 6 b))
c
f
e
b
d
a
g
- As before, the graph is described as a set of
nodes with connecting child links
5Traversing this Graph
c
f
- To traverse this graph, we need a function to
find a route from an origin to a destination
e
b
d
a
g
- find-route node symbol -gt
- (listof symbol)
- find the sequence of nodes to travel from the
first - node to the node named as the target
- (define (find-route from to)
- ... )
6Traversing this Graph
- What if there is no such route like
- (find-route 'e 'a graph) ?
c
f
e
b
d
a
- Need to return something bad looking
g
- find-route node symbol -gt
- (listof symbol)
- find the sequence of nodes to travel from the
first - node to the second
- (define (find-route from to)
- ... )
or false
7The Trivial Case
- The first thing to find is the answer
- (find-route 'e 'e graph)
c
f
e
b
d
a
g
- find-route node symbol -gt
- (listof symbol) or false
- find the sequence of nodes to travel from the
first - node to the second
- (define (find-route from to)
- (cond (symbol? (node-data from) to) (list
to) - ... )
8Searching for a Path
- If this is not the trivial case, how do we go
from one node to another? - E.g. from d to e
c
f
e
b
d
a
g
- d has a list of neighbors c, e and g
- We must search to determine whether there is a
path from each of those neighbors to the
destination e - In this case, the second neighbor matches the
trivial case
9More Searching for a Path
- What if the result of this neighbor search is
not trivial? - E.g. from a to e
c
f
e
b
d
a
g
- a has a list of neighbors c, d and g
- We must continue to search to determine whether
there is a path from each of those neighbors to
the destination e - In this case, there is no trivial solution we
have to use (find-route )on each neighbor - This may produce an answer or false
10Back Tracking
- Suppose we started from a and took the first path
each time
c
f
e
b
d
a
g
- we don't know yet whether a-c is bad,
- but when we tried c-f, it tried f-b
- and returned false since there is no path from b
11Back Tracking
- We conclude that c-f is bad, and must back-track
to try another path from c
c
f
e
b
d
- This happens to be a trivial solution returning
- (list e)
- What should the attempt c-e return?
a
g
- (list c e)
- So what should the attempt a-c return?
(list a c e)
12The Next Step
- So we need to examine all of the neighbors of the
source to see whether there is a route to the
destination
c
f
e
b
d
a
- This is too complex to do in-line we need a
helper function
g
- find-route/list (listof links) symbol -gt
- (listof symbol) or false
- find the sequence of nodes to travel from some
node - on the first list to the second
- (define (find-route/list lst to)
- ... )
13Using (find-route/list )
- Now, we can write find-route using
- (find-route/list )
c
f
e
b
d
a
- call a route a (list-of-symbols) or false
- find-route node symbol -gt route
- find the sequence of nodes to travel from
- the first node to the second (may be false)
- (define (find-route from to)
- (if (symbol? (node-data from) to)
- (list to)
- (let ((possible-route
- (find-route/list (node-children
from) - to)))
- (if (boolean? possible-route)
- false
- (cons (node-data from)
possible-route)))))
g
14Writing (find-route/list )
- Now, we can write
- (find-route/list )
c
f
e
b
d
a
- find-route/list (listof links) symbol
- -gt route
- find the sequence of nodes to travel from
- some node referred to in the link list to
the second - (define (find-route/list lst to)
- (if (empty? lst)
- false
- (let ((possible-route (find-route
(link-dest (first lst)) - to)))
- (if (boolean? possible-route)
- (find-route/list (rest lst) to)
- possible-route))))
g
15Observations
- Control alternates between
- (find-route from to) and
- (find-route/list lst to)
- Results in another example of generative
recursion - Can we guarantee convergence?
16Graph (find-route)
17Questions?
18Optimization Problems
19Optimization Algorithms
- Many real-world problems involve maximizing or
minimizing a value - How can a car manufacturer get the most parts out
of a piece of sheet metal? - How can a moving company fit the most furniture
into a truck of a certain size? - How can the phone company route calls to get the
best use of its lines and connections? - How can a university schedule its classes to make
the best use of classrooms without conflicts?
20Optimal vs. Approximate Solutions
- Often, we can make a choice
- Do we want the guaranteed optimal solution to the
problem, even though it might take a lot of
work/time to find? - Do we want to spend less time/work and find an
approximate solution (near optimal)?
21Approximate Solutions
22Greedy Algorithms
- Approximates optimal solution
- May or may not find optimal solution
- Provides quick and dirty estimates
- A greedy algorithm makes a series of
short-sighted decisions and does not look
ahead - Spends less time
23A Greedy Algorithm
- Consider the weight and value of some foreign
coins - foo 6.00 500 grams
- bar 4.00 450 grams
- baz 3.00 410 grams
- qux 0.50 300 grams
- If we can only fit 860 grams in our pockets...
- A greedy algorithm would choose
- 1 foo 500 grams 6.00
- 1 qux 300 grams 0.50
- Optimal solution is
- 1 bar 450 grams 4.00
- 1 baz 410 grams 3.00
Total of 6.50
Total of 7.00
24Is a Greedy Algorithm bad?
25Short-Sighted Decisions
1
Start
2
End
1
12
26The Shortest Path Problem
- Given a directed, acyclic, weighted graph
- Start at some vertex a
- What is the shortest path from start vertex a to
some end vertex a?
27A Greedy Algorithm
c
2
f
7
5
Start
11
3
b
e
5
3
7
d
a
End
7
6
14
6
g
28A Greedy Algorithm
c
2
f
7
5
Start
11
3
b
e
5
3
7
d
a
End
7
6
14
6
g
29A Greedy Algorithm
c
2
f
7
5
Start
11
3
b
e
5
3
7
d
a
End
7
6
14
6
g
30A Greedy Algorithm
c
2
f
7
5
Start
11
3
b
e
5
3
7
d
a
End
7
6
14
6
Path 15
g
31A Greedy Algorithm
c
2
f
7
5
Start
11
3
b
e
5
3
7
d
a
End
7
6
14
6
Shortest Path 13
g
32Greedy Algorithm Design
- Generative recursion
- Should arrive eventually at the answer
- Steps for reaching the answer are indirect
- greedy node name -gt list of links
- given a current node and the name of
- the target, find the list of nodes to
- take you there
- Note the solution makes use of Tail Recursion
- Referred to in the book as Accumulation
33graph (greedy)
34Code Used 1
- greedy node name -gt list of links
- given a current node and the name of
- the target, find the list of links to
- take you there
- (define (greedy from target)
- (if (symbol? (node-data from) target)
- (list target)
- (let ((this-link (best-link from)))
- (if (not this-link)
- empty
- (cons (node-data from)
- (greedy (link-dest this-link)
- target))))))
-
35Code Used 2
- best-link node -gt link
- given a node, find the least cost link to
somewhere else - (define (best-link from)
- (best-link-r (node-children from) 100000
false)) - best-link-r list-of-links current -gt link
- given a list and a current cost, find the best
link - (define (best-link-r lst cost current)
- (cond (empty? lst)
- current
- (lt (link-cost (first lst)) cost)
- (best-link-r (rest lst)
- (link-cost (first lst))
- (first lst))
- else
- (best-link-r (rest lst)
- cost
- current)))
-
36Code Used 3
- (define b (make-node 'b empty))
- (define f-b (make-link 7 b))
- (define f (make-node 'f (list f-b)))
- (define e-b (make-link 5 b))
- (define e (make-node 'e (list e-b)))
- (define g-e (make-link 7 e))
- (define g-b (make-link 6 b))
- (define g (make-node 'g (list g-e g-b)))
- (define c-e (make-link 3 e))
- (define c-f (make-link 2 f))
- (define c (make-node 'c (list c-e c-f)))
- (define d-c (make-link 11 c))
- (define d-e (make-link 7 e))
- (define d-g (make-link 6 g))
- (define d (make-node 'd (list d-c d-e d-g)))
- (define a-c (make-link 5 c))
- (define a-d (make-link 3 d))
- (define a-g (make-link 14 g))
- (define a (make-node 'a (list a-c a-d a-g)))
37Dynamic Planning
38Bellmans Principle of Optimality
- Regardless of how you reach a particular state
(graph node), the optimal strategy for reaching
the goal state is always the same. - This greatly simplifies the strategy for
searching for an optimal solution.
39Dynamic Planning
- Calculates all of the possible solution options,
then chooses the best one. - Implemented recursively.
- Produces an optimal solution.
- Spends more time.
40The Shortest Path Problem
- Given a directed, acyclic, weighted graph
- What is the shortest path from the start vertex
to some end vertex? - Minimize the sum of the edge weights
41Dynamic Planning
c
2
f
7
5
Start
11
b
3
e
5
3
7
d
a
End
7
6
14
6
g
42Dynamic Planning
f
Notation x means shortest path to x
7
b
e
5
End
b is ( min ( 6 g) ( 5 e) ( 7 f))
6
g
43Dynamic Planning
c
2
f
3
e
7
d
a
7
6
14
g is (min ( 6 d) 14) e is (min ( 3 c) ( 7
d) ( 7 g)) f is ( 2 c)
g
44Dynamic Planning
c
5
Start
11
3
d
a
c is (min 5 ( 11 d)) d is 3
45b is ( min ( 6 g) ( 5 e) ( 7 f)) g
is (min ( 6 d) 14) e is (min ( 3 c) ( 7
d) ( 7 g)) f is ( 2 c) c is (min 5 ( 11 d)) d
is 3
46b is ( min ( 6 g) ( 5 e) ( 7 f)) g
is (min ( 6 d) 14) e is (min ( 3 c) ( 7
d) ( 7 g)) f is ( 2 c) c is (min 5 ( 11 d)) d
is 3 via a - d
47b is ( min ( 6 g) ( 5 e) ( 7 f)) g
is (min ( 6 3) 14) e is (min ( 3 c) ( 7
3) ( 7 g)) f is ( 2 c) c is (min 5 ( 11 3)) d
is 3 via a - d
48b is ( min ( 6 g) ( 5 e) ( 7 f)) g
is 9 via a d - g e is (min ( 3 c) 10
( 7 g)) f is ( 2 c) c is 5 via a - c d is 3
via a - d
49b is ( min ( 6 9) ( 5 e) ( 7 f)) g
is 9 via a d - g e is (min ( 3 5) 10
( 7 9)) f is ( 2 5) c is 5 via a - c d is 3
via a - d
50b is ( min 15 ( 5 e) ( 7 f)) g is 9
via a d - g e is (min 8 10 16) f is 7
via a c - f c is 5 via a - c d is 3
via a - d
51b is ( min 15 ( 5 e) ( 7 f)) g is 9
via a d - g e is 8 via a c - e
f is 7 via a c - f c is 5 via a - c d is
3 via a - d
52b is ( min 15 ( 5 8) ( 7 7)) g is 9
via a d - g e is 8 via a c - e
f is 7 via a c - f c is 5 via a - c d is
3 via a - d
53b is ( min 15 13 14 ) g is 9 via a
d - g e is 8 via a c - e f is 7
via a c - f c is 5 via a - c d is 3 via a
- d
54b is 13 via a c e b g is 9 via a d
- g e is 8 via a c - e f is 7 via
a c - f c is 5 via a - c d is 3 via a - d
55Dynamic Planning
c
2
f
7
5
Start
11
b
3
e
5
3
7
d
a
End
7
6
14
6
Shortest Path 13
g
56Questions?
57Optimization Summary
- Greedy algorithms
- Make short-sighted, best guess decisions
- Required less time/work
- Provide approximate solutions
- Dynamic planning
- Examines all possible solutions
- Requires more time/work
- Guarantees optimal solution
58Summary
- You should now know
- Basic Graph Structures
- Greedy Algorithms
- Dynamic Programming
59(No Transcript)