Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement graphs and graph algorithms in a functional programming language?

Basically, I know how to create graph data structures and use Dijkstra's algorithm in programming languages where side effects are allowed. Typically, graph algorithms use a structure to mark certain nodes as 'visited', but this has side effects, which I'm trying to avoid.

I can think of one way to implement this in a functional language, but it basically requires passing around large amounts of state to different functions, and I'm wondering if there is a more space-efficient solution.

like image 551
brad Avatar asked Jun 08 '10 16:06

brad


People also ask

How do you make a graph in programming?

A graph is a type of non-linear data structure that is used to store data in the form of nodes and edges. The following is a typical representation of Graph: G = (V, E) Here G is the Graph, V is the set of vertices or nodes and E is the set of edges in the Graph G.

How do you write an algorithm for a graph?

Steps of Prim's AlgorithmSelect any vertex, say v1 of Graph G. Select an edge, say e1 of G such that e1 = v1 v2 and v1 ≠ v2 and e1 has minimum weight among the edges incident on v1 in graph G. Now, following step 2, select the minimum weighted edge incident on v2. Continue this till n–1 edges have been chosen.

Which data structure is used with functional programming languages?

Typically tuples, lists, and partially-evaluated functions are very common data structures in functional programming languages. Mutable data structures, like arrays and (real) hash tables, are used much less because they don't fit in as well with Haskell.

How is graph theory used in programming?

Some applications of graph theory in computer science include: Modelling of complex networks, like social networks or in the simulation of a disease like the new coronavirus. Each node can represent one person or a population, and edges can represent probability/easiness of transmission.


2 Answers

You might check out how Martin Erwig's Haskell functional graph library does things. For instance, its shortest-path functions are all pure, and you can see the source code for how it's implemented.

Another option, like fmark mentioned, is to use an abstraction which allows you to implement pure functions in terms of state. He mentions the State monad (which is available in both lazy and strict varieties). Another option, if you're working in the GHC Haskell compiler/interpreter (or, I think, any Haskell implementation which supports rank-2 types), another option is the ST monad, which allows you to write pure functions which deal with mutable variables internally.

like image 190
Antal Spector-Zabusky Avatar answered Oct 08 '22 08:10

Antal Spector-Zabusky


If you were using haskell, the only functional language with which I am familiar, I would recommend using the State monad. The State monad is an abstraction for a function that takes a state and returns an intermediate value and some new state value. This is considered idiomatic haskell for those situations where maintaining a large state is necessary.

It is a much nicer alternative to the naive "return state as a function result and pass it as a parameter" idiom that is emphasized in beginner functional programming tutorials. I imagine most functional programming languages have a similar construct.

like image 31
fmark Avatar answered Oct 08 '22 08:10

fmark