0

Given the following structure:

class G {
    Node[] nodes;
}
class Node {
    Node neighbour;
}

The deep copy operations can be defined as:

function G copy (G g) {
    G r = new G();
    Map isom = new Map();
    for (Node node in g.nodes) {
        Node c = isom.get(node);
        if (c == null) {
            c = copy(node, isom);
            isom.put(node, c);
        }
        r.nodes.add(c);
    }
    return r;
}
function Node copy(Node n, Map isom) {
    Node r = isom.get(n);
    if (r == null) {
        r = new Node();
        isom.put(n, r);
        r.neighbour = copy(n.neighbour);
    }
    return r;
}

My question is how to design a function copy(Node n, Map isom), such that it does not mutate the argument isom, in a functional programming style.

象嘉道
  • 3,657
  • 5
  • 33
  • 49

1 Answers1

2

After having posted this question, I did some investigations seriously. My finding is that functional programming is not good at handling the popular graph algorithms.

People with purely functional favour have to treat graph in a distinct way from the normal literature. That's the motivation of pushing guys to generate the following works:

  • functional graph algorithms with depth-first search
  • graph algorithm lazy functional programming language
  • inductive graphs and functional graph algorithms
  • purely functional data structures
  • graph algorithms with a functional flavour

Graph algorithms have long been a challenge to program in a pure functional language. Previous attempts have either tended to be unreadable, or have failed to achieve standard asymptotic complexity measures.

---John Launchbury. 1995. Graph Algorithms with a Functional Flavous. In Advanced Functional Programming, First International Spring School on Advanced Functional Programming Techniques-Tutorial Text, Johan Jeuring and Erik Meijer (Eds.). Springer-Verlag, London, UK, 308-331.

象嘉道
  • 3,657
  • 5
  • 33
  • 49