Book Image

Learning F# Functional Data Structures and Algorithms

By : Adnan Masood
Book Image

Learning F# Functional Data Structures and Algorithms

By: Adnan Masood

Overview of this book

Table of Contents (21 chapters)
Learning F# Functional Data Structures and Algorithms
Credits
Foreword
Foreword
Foreword
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Finding the minimal path sum


The problem provides an 80 x 80 matrix in a text file, allowing traversal from the top left to the bottom right by only moving right and down.

As we discussed above, Dijkstra has one cost function which helps to find the shortest path from the source node to every other node by considering only the real cost (in contrast with A* heuristic). In order to solve the matrix problem, let's see how the graph implementation helps us.

First of all we will iterate through the matrix file and read the comma-delimited costs in the array. You can see the implementation in the following code snippet:

let weights = File.ReadAllLines("matrix.txt")
  |> Array.map(fun line -> line.Split(',') |> Array.map int32)
let matrixHeight = weights.Length;
let matrixWidth = weights.[0].Length;

As in the graph type described above, let's declare a node. A node consists of a list of coordinates (2D) for the matrix coordinates and the parent node, as well as the cost (weight).

type Node...