Finding routes A path in a graph can be represented by the list of nodes on the path. Key Task: design a function find-route that consumes a graph plus origin and destination nodes, and produces a path from the origin to the destination, or false if no such path exists. (find-route orig dest G) First we create an auxiliary function neighbours that consumes a node v and a graph G and produces the list of out-neighbours of v. (neighbours v G) CS 135 Fall 2018 12: Graphs 10
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Finding routesA path in a graph can be represented by the list of nodes on the
path.
Key Task: design a function find-route that consumes a graph plus
origin and destination nodes, and produces a path from the origin to
the destination, or false if no such path exists.
(find-route orig dest G)
First we create an auxiliary function neighbours that consumes a
node v and a graph G and produces the list of out-neighbours of v.
(neighbours v G)
CS 135 Fall 2018 12: Graphs 10
Neighbours in our example
(neighbours ’A G)⇒ (list ’C ’D ’E)
(neighbours ’H G)⇒ empty
A
B
C
D
E
F
K H
J
CS 135 Fall 2018 12: Graphs 11
;; (neighbours v G) produces list of out-neighbours of v in G
;; neighbours: Node Graph→ (listof Node)
;; requires: v is a node in G
(define (neighbours v G)
(cond [(symbol=? v (first (first G))) (second (first G))]
[else (neighbours v (rest G))]))
Purpose: (neighbours v G) produces the list of out-neighbours of v
(a vertex in the graph G).
Algorithm: recursively search for v in the adjacency list (which
represents the graph G) and produce the list of v’s out-neighbours,
the second element in the (list Node (listof Node)) pair.
CS 135 Fall 2018 12: Graphs 12
Cases for find-routeStructural recursion does not work for find-route; we must use
generative recursion.
1. Base Case: If the origin equals the destination, the path consists
of just this node.
2. Recursive Case: Otherwise, if there is a path, the second node
on that path must be an out-neighbour of the origin node, so
search there.
Key Idea: Each out-neighbour defines a subproblem (finding a route
from it to the destination).
CS 135 Fall 2018 12: Graphs 13
In our example, any route from A to H must pass through C, D, or E.
Those are three subproblems.
If we knew a route 1) from C to H, or 2) from D to H, or 3) from E to
H, we could create the route from A to H.
A
B
C
D
E
F
K H
J
CS 135 Fall 2018 12: Graphs 14
Backtracking algorithmsBacktracking algorithms try to find a route from an origin to a
destination.
Key Idea: If the initial attempt does not work, such an algorithm
“backtracks” and tries another choice.
Eventually, either a route is found, or all possibilities are exhausted,
meaning there is no route.
CS 135 Fall 2018 12: Graphs 15
Backtracking in our exampleIn our example, we can see the “backtracking” since the search for a
route from A to H can be seen as moving forward in the graph
looking for H.
Key Point: If this search fails (for example, at C), then the algorithm
“backs up” to the previous vertex (A) and tries the next neighbour
(D).
If we find a path from D to H, we can just add A to the beginning of
this path.
CS 135 Fall 2018 12: Graphs 16
find-route/list
We need to apply find-route on each of the out-neighbours of a
given node.
All those out-neighbours are collected into a list associated with that
node.
This suggests writing find-route/list which does this for the entire list
of out-neighbours.
Key Task: The function find-route/list will apply find-route to each of
the nodes on that list until it finds a route to the destination.
CS 135 Fall 2018 12: Graphs 17
Mutual recursion: find-route and find-route/list
This is the same recursive pattern that we saw in the processing of
expression trees (and descendant family trees, in HtDP).
For expression trees, we had two mutually recursive functions, eval
and apply.
Here, we have two mutually recursive functions, find-route and
find-route/list.
find-route finds out-neigbours and applies find-route/list to them.
find-route/list applies find-route to each out-neigbour.
CS 135 Fall 2018 12: Graphs 18
;; (find-route orig dest G) finds route from orig to dest in G if it exists