Representing a graph from a list of edges
A graph can be defined by a list of edges, where an edge is a tuple of vertices. In the Data.Graph
package, a vertex is simply Int
. In this recipe, we use the buildG
function to construct a graph data structure out of a list of edges.
Getting ready
We will be constructing the graph represented in the following diagram:
How to do it...
Create a new file, which we will name Main.hs
, and insert the following code:
- Import the
Data.Graph
package:import Data.Graph
- Construct a graph using the
buildG
function from the imported library:myGraph :: Graph myGraph= buildG bounds edges where bounds = (1,4) edges = [ (1,3), ...
Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.