liveBook · Manning

Undirected graph

As explained in Learn Haskell by Example

An undirected graph is a graph whose edges have no orientation and can be traversed in either direction.

An undirected graph connects nodes with edges that have no orientation, so each connection can be traversed in either direction. section 5.1.1

A simple Haskell representation captures the essential structure:

  • Use a polymorphic node type so the graph can store different kinds of elements. section 5.1.1
  • Ignore edge costs when every connection represents one equal step. section 5.1.1
  • Store each connection as a pair in an adjacency list, represented by an associative list. section 5.1.1
  • Recognize that checking edges, finding neighbors, and inserting edges may require scanning the whole list. section 5.1.1

Word ladders

A word-ladder program builds an undirected graph from a dictionary, with words as nodes and one-letter ladder steps as equal-cost edges, then searches for a shortest path. section 5.1.1

type Graph a = [(a,a)]

Inspect the compact adjacency-list type used to represent an undirected graph as connected node pairs. section 5.1.1

How Learn Haskell by Example covers it

How deep it goes
Defines nodes, edges, direction, costs, and adjacency lists, then identifies linear-scan performance drawbacks.
Examples
Uses a dictionary-based word ladder whose words become nodes and ladder steps become equal-cost edges.
What it assumes
Teaches graph direction and representation while relying on familiarity with associative lists.
What you can do after
Build it from scratch as a polymorphic Haskell adjacency list and assess its scanning costs.
How it connects
Connects undirected graphs with adjacency lists, associative lists, polymorphic types, and shortest-path search.

Also explained in

Compare how 5 books explain undirected graph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.