-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09d1b4c
commit 70222c2
Showing
26 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions
3
23-vectors-and-matrix.rkt → code-examples/23-vectors-and-matrix.rkt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#|------------------------------------------------------------------------------ | ||
GRAPHS IN FUNCTIONAL PROGRAMMING | ||
A graph G consists of a set of nodes that we call N and edges / arcs that | ||
connect them. It is possible that a graph has arcs of a node v to w as well | ||
as w to v. In these cases both arcs can be represented, or the direction of | ||
the arc will be ignored. | ||
The graph structure is defined as: | ||
( (node1 (neighbour1 neighbour2 ... neighbourN )) ... (nodeN(ng1 ...ngN)))) | ||
A graph has elementary operations: | ||
* solution? - Indicates if a given path is a solution for the graph | ||
* neighbors? - Indicate if two nodes are neighbors | ||
* get-neighbors - Returns all neighbors of a given node | ||
* extend - Find new paths from a certain path | ||
* weight-btw-nodes - Returns the weight between two nodes | ||
* path-weight - Returns the weight of a given path | ||
* node? - Check if a node is part of a graph and returns his neighbors | ||
The functions implemented were inspired by the book: "Introducción a la | ||
programación en Scheme"(José E. Helio Guzmán) | ||
------------------------------------------------------------------------------|# | ||
#lang racket | ||
|
||
(provide solution? | ||
neighbors? | ||
get-neighbors | ||
extend | ||
weight-btw-nodes | ||
path-weight) | ||
|
||
;; (solution? end path) | ||
;; Indicates if a given path is a solution for the graph | ||
> (define (solution? end path) | ||
(equal? end (car path))) | ||
|
||
;; (neighbors? node1 node2 graph) | ||
;; Indicate if two nodes are neighbors | ||
> (define (neighbors? node1 node2 graph) | ||
(cond ((null? graph) | ||
#f) | ||
((equal? node1 (caar graph)) | ||
(neighbors-aux node2 (cadar graph))) | ||
(else | ||
(neighbors? node1 node2 (cdr graph))))) | ||
|
||
> (define (neighbors-aux node direct-ngbs) | ||
(cond ((null? direct-ngbs) | ||
#f ) | ||
((equal? node (caar direct-ngbs)) | ||
#t) | ||
(else | ||
(neighbors-aux node (cdr direct-ngbs))))) | ||
|
||
;; (node? node graph) | ||
;; Check if a node is part of a graph and returns his neighbors | ||
> (define (node? node graph) | ||
(cond ((null? graph) | ||
'()) | ||
((equal? node (caar graph)) | ||
(cadar graph)) ;; -> ( (ng1 w1) (ng2 w2) ... (ngN wN) ) | ||
(else | ||
(node? node (cdr graph))))) | ||
|
||
;; (get-neighbours node graph) | ||
;; Returns all neighbors of a given node | ||
> (define (get-neighbors node graph) | ||
(neighbors-to-list (node? node graph) '())) | ||
|
||
> (define (neighbors-to-list neighbors result) | ||
(cond ((null? neighbors) | ||
result) ;; -> (ng1 ng2 ... ngN) | ||
(else | ||
(neighbors-to-list (cdr neighbors) | ||
(append result (list (caar neighbors))))))) | ||
|
||
;; (extend path graph) | ||
;; Find new paths from a certain path | ||
;; Check if the element exists in the partial route, to avoid cyclical evaluations | ||
> (define (extend path graph) | ||
(extend-aux (get-neighbors (car path) graph) '() path)) | ||
|
||
> (define (extend-aux neighbors result path) | ||
(cond ((null? neighbors) | ||
result) | ||
;; Check if the element exists in the partial route, to avoid | ||
;; cyclical evaluations | ||
((member (car neighbors) path) | ||
(extend-aux (cdr neighbors) result path)) | ||
(else | ||
(extend-aux (cdr neighbors) | ||
(append result (list(list* (car neighbors) path))) | ||
path )))) | ||
|
||
;; (weight-btw-nodes node1 node2 graph) | ||
;; Returns the weight between two nodes | ||
> (define (weight-btw-nodes node1 node2 graph) | ||
(cond ((null? graph) | ||
0) | ||
((equal? node1 (caar graph)) | ||
(wbn-aux node2 (cadar graph))) | ||
(else | ||
(weight-btw-nodes node1 node2 (cdr graph))))) | ||
|
||
> (define (wbn-aux node direct-ngbs) | ||
(cond ((null? direct-ngbs) | ||
0 ) | ||
((equal? node (caar direct-ngbs)) | ||
(cadar direct-ngbs)) | ||
(else | ||
(wbn-aux node (cdr direct-ngbs))))) | ||
|
||
;; (path-weight path graph) | ||
;; Returns the weight of a given path | ||
> (define (path-weight path graph) | ||
(cond ((<= (length path) 1) | ||
0) | ||
(else | ||
(+ (weight-btw-nodes (car path) (cadr path) graph) | ||
(path-weight (cdr path) graph))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#|------------------------------------------------------------------------------ | ||
GRAPS PATHFINDING | ||
Implementation of path search algorithms in graphs, used to show routes on | ||
maps. | ||
The search algorithms implemented are: | ||
* Deep First Search | ||
* Breadh First Search | ||
The functions implemented were inspired by the book: "Introducción a la | ||
programación en Scheme"(José E. Helio Guzmán) | ||
------------------------------------------------------------------------------|# | ||
#lang racket | ||
|
||
(require "25-graphs-operators.rkt" | ||
"17-lists-operators.rkt") | ||
|
||
;; (DFS start end graph) | ||
;; Search for a route by searching in depth | ||
;; * Based on book page 382 | ||
> (define (DFS start end graph) | ||
(cond ((equal? start end) | ||
(list start)) | ||
(else | ||
(DFS-aux (list (list start)) end graph)))) | ||
|
||
> (define (DFS-aux paths end graph) | ||
(cond ((null? paths) | ||
'()) | ||
((equal? end (caar paths)) | ||
(reverse (car paths))) | ||
(else | ||
(DFS-aux (append (extend (car paths) graph) | ||
(cdr paths)) | ||
end | ||
graph)))) | ||
|
||
;; (DFS-ALL start end graph) | ||
;; Search for all paths by depth search first | ||
;; * Based on book pages 383 - 384 | ||
> (define (DFS-ALL start end graph) | ||
(cond ((equal? start end) | ||
(list start)) | ||
(else | ||
(DFS-ALL-aux (list (list start)) end graph '())))) | ||
|
||
> (define (DFS-ALL-aux paths end graph total) | ||
(cond ((null? paths) | ||
(apply-fun reverse total)) | ||
((solution? end (car paths)) | ||
(DFS-ALL-aux (cdr paths) | ||
end | ||
graph | ||
(cons (car paths) total))) | ||
(else | ||
(DFS-ALL-aux (append (extend (car paths) graph) | ||
(cdr paths)) | ||
end | ||
graph | ||
total)))) | ||
|
||
;; (BFS-ALL start end graph) | ||
;; Search for a route by breadh search first | ||
;; * Based on book page 384 | ||
> (define (BFS-ALL start end graph) | ||
(cond ((equal? start end) | ||
(list start)) | ||
(else | ||
(BFS-ALL-aux (list (list start)) end graph '())))) | ||
|
||
> (define (BFS-ALL-aux paths end graph total) | ||
(cond ((null? paths) | ||
(apply-fun reverse total)) | ||
((solution? end (car paths)) | ||
(BFS-ALL-aux (cdr paths) | ||
end | ||
graph | ||
(cons (car paths) total))) | ||
;; The implementation between DFS and BFS only differs in this line of code | ||
(else | ||
(BFS-ALL-aux (append (cdr paths) | ||
(extend (car paths) graph)) | ||
end | ||
graph | ||
total)))) |