Skip to content

Commit

Permalink
[ADD] Graphs and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
estalvgs1999 committed Aug 15, 2019
1 parent 09d1b4c commit 70222c2
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 3 deletions.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
----------------------------------------------------------------------------|#
#lang racket

(provide apply-fun)

;; (member? ele list)
;; Checks if an element is part of the list

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname 23-vectors-and-matrix) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f () #f)))
#|------------------------------------------------------------------------------
VECTORS AND MATRIX (ALGEBRA)
(a) Vectors
Expand Down
File renamed without changes.
121 changes: 121 additions & 0 deletions code-examples/25-graphs-operators.rkt
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)))))
85 changes: 85 additions & 0 deletions code-examples/26-graph-paths-finding.rkt
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))))

0 comments on commit 70222c2

Please sign in to comment.