title: Implementing Graph for Bayes Net in FSharp
body: <p>I'm trying to translate a graph formulation from Python to F#</p>
  
  <p>The python "Node" class:</p>
  
  <pre><code>class Node:
      """ A Node is the basic element of a graph.  In its most basic form a graph is just a list of nodes.  A Node is a really just a list of neighbors.
      """
      def __init__(self, id, index=-1, name="anonymous"):
          # This defines a list of edges to other nodes in the graph.
          self.neighbors = set()
          self.visited = False
          self.id = id
          # The index of this node within the list of nodes in the overall graph.
          self.index = index
          # Optional name, most usefull for debugging purposes.
          self.name = name
  
      def __lt__(self, other):
          # Defines a &lt; operator for this class, which allows for easily sorting a list of nodes.
          return self.index &lt; other.index
  
      def __hash__(self):
          return hash(self.id)
  
      def __eq__(self, right):
          return self.id == right.id
  
      def add_neighbor(self, node):
          """ Make node a neighbor if it is not alreadly.  This is a hack, we should be allowing self to be a neighbor of self in some graphs.  This should be enforced at the level of a graph, because that is where the type of the graph would disallow it.
          """
          if (not node in self.neighbors) and (not self == node):
              self.neighbors.add(node)
  
      def remove_neighbor(self, node):
          # Remove the node from the list of neighbors, effectively deleting that edge from
          # the graph.
          self.neighbors.remove(node)
  
      def is_neighbor(self, node):
          # Check if node is a member of neighbors.
          return node in self.neighbors
  </code></pre>
  
  <p>My F# class so far:</p>
  
  <pre><code>type Node&lt;'T&gt;= string*'T
  type Edge&lt;'T,'G&gt; = Node&lt;'T&gt;*Node&lt;'T&gt;*'G
  type Graph = 
      | Undirected of seq(Node*list Edge) 
      | Directed of seq(Node*list Edge *list Edge)
  </code></pre>