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.
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), (1,4) , (2,3), (2,4) , (3,4) ]
Print out the graph, its edges, and its vertices:
main = do print $ "The edges are " ++ (show.edges) myGraph print $ "The vertices are " ++ (show.vertices) myGraph