Repository: networkx/networkx

Issue:
maximal_independent_set does not work for DiGraph
Currently [maximal_independent_set](https://github.com/networkx/networkx/blob/d7d906e1d16ef331da0bc1d149953e7532155acc/networkx/algorithms/mis.py#L70) returns the wrong results for a `DiGraph` because it uses the `G.neighbors` method which returns only the successor nodes in a `DiGraph`.  I believe the [all_neighbors](https://github.com/networkx/networkx/blob/13b373bf6938c077d1e61adc60a48cb910a75755/networkx/classes/function.py#L540) function should be used instead to make `maximal_independent_set` work correctly for both graph types.



Discussion:
Is your suggestion a standard definition of maximal independent set for directed graphs?  It makes sense that it would be but I didn't find a definition after a little searching.  The alternative would be to return an error when the function is called with a directed graph.

I am not sure if MIS applies to directed graphs or not actually.  I just noted that `all_neighbors` would give the correct result, and I did not particularly see a reason to raise an exception.  Then the user would be forced to use `to_undirected` to get the MIS (hence making an unnecessary copy of the `DiGraph`).  However, if the literature states that MIS only applies to undirected graphs, then raising an exception makes the most sense.

Needs PR to raise exception if called with directed graph.

Resolving patch:
diff --git a/networkx/algorithms/mis.py b/networkx/algorithms/mis.py
--- a/networkx/algorithms/mis.py
+++ b/networkx/algorithms/mis.py
@@ -1,24 +1,26 @@
 # -*- coding: utf-8 -*-
 # $Id: maximalIndependentSet.py 576 2011-03-01 05:50:34Z lleeoo $
-"""
-Algorithm to find a maximal (not maximum) independent set.
-
-"""
 #    Leo Lopes <leo.lopes@monash.edu>
 #    Aric Hagberg <hagberg@lanl.gov>
 #    Dan Schult <dschult@colgate.edu>
 #    Pieter Swart <swart@lanl.gov>
 #    All rights reserved.
 #    BSD license.
+#
+# Authors: Leo Lopes <leo.lopes@monash.edu>
+#          Loïc Séguin-C. <loicseguin@gmail.com>
+"""
+Algorithm to find a maximal (not maximum) independent set.
 
-__author__ = "\n".join(["Leo Lopes <leo.lopes@monash.edu>",
-                        "Loïc Séguin-C. <loicseguin@gmail.com>"])
+"""
+import random
+import networkx as nx
+from networkx.utils import not_implemented_for
 
 __all__ = ['maximal_independent_set']
 
-import random
-import networkx as nx
 
+@not_implemented_for('directed')
 def maximal_independent_set(G, nodes=None):
     """Return a random maximal independent set guaranteed to contain
     a given set of nodes.
@@ -27,10 +29,10 @@ def maximal_independent_set(G, nodes=None):
     of G induced by these nodes contains no edges. A maximal
     independent set is an independent set such that it is not possible
     to add a new node and still get an independent set.
-    
+
     Parameters
     ----------
-    G : NetworkX graph 
+    G : NetworkX graph
 
     nodes : list or iterable
        Nodes that must be part of the independent set. This set of nodes
@@ -38,7 +40,7 @@ def maximal_independent_set(G, nodes=None):
 
     Returns
     -------
-    indep_nodes : list 
+    indep_nodes : list
        List of nodes that are part of a maximal independent set.
 
     Raises
@@ -47,6 +49,9 @@ def maximal_independent_set(G, nodes=None):
        If the nodes in the provided list are not part of the graph or
        do not form an independent set, an exception is raised.
 
+    NetworkXNotImplemented
+        If `G` is directed.
+
     Examples
     --------
     >>> G = nx.path_graph(5)
@@ -54,7 +59,7 @@ def maximal_independent_set(G, nodes=None):
     [4, 0, 2]
     >>> nx.maximal_independent_set(G, [1]) # doctest: +SKIP
     [1, 3]
-    
+
     Notes
     -----
     This algorithm does not solve the maximum independent set problem.
@@ -67,7 +72,7 @@ def maximal_independent_set(G, nodes=None):
     if not nodes.issubset(G):
         raise nx.NetworkXUnfeasible(
                 "%s is not a subset of the nodes of G" % nodes)
-    neighbors = set.union(*[set(G.neighbors(v)) for v in nodes])
+    neighbors = set.union(*[set(G.adj[v]) for v in nodes])
     if set.intersection(neighbors, nodes):
         raise nx.NetworkXUnfeasible(
                 "%s is not an independent set of G" % nodes)
@@ -76,6 +81,5 @@ def maximal_independent_set(G, nodes=None):
     while available_nodes:
         node = random.choice(list(available_nodes))
         indep_nodes.append(node)
-        available_nodes.difference_update(list(G.neighbors(node)) + [node])
+        available_nodes.difference_update(list(G.adj[node]) + [node])
     return indep_nodes
-


Test patch:
diff --git a/networkx/algorithms/tests/test_mis.py b/networkx/algorithms/tests/test_mis.py
--- a/networkx/algorithms/tests/test_mis.py
+++ b/networkx/algorithms/tests/test_mis.py
@@ -1,10 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 # $Id: test_maximal_independent_set.py 577 2011-03-01 06:07:53Z lleeoo $
-"""
-Tests for maximal (not maximum) independent sets.
-
-"""
 #    Copyright (C) 2004-2016 by
 #    Leo Lopes <leo.lopes@monash.edu>
 #    Aric Hagberg <hagberg@lanl.gov>
@@ -12,37 +8,42 @@
 #    Pieter Swart <swart@lanl.gov>
 #    All rights reserved.
 #    BSD license.
+#
+# Author: Leo Lopes <leo.lopes@monash.edu>
+"""
+Tests for maximal (not maximum) independent sets.
 
-__author__ = """Leo Lopes (leo.lopes@monash.edu)"""
+"""
 
 from nose.tools import *
 import networkx as nx
 import random
 
+
 class TestMaximalIndependantSet(object):
     def setup(self):
         self.florentine = nx.Graph()
-        self.florentine.add_edge('Acciaiuoli','Medici')
-        self.florentine.add_edge('Castellani','Peruzzi')
-        self.florentine.add_edge('Castellani','Strozzi')
-        self.florentine.add_edge('Castellani','Barbadori')
-        self.florentine.add_edge('Medici','Barbadori')
-        self.florentine.add_edge('Medici','Ridolfi')
-        self.florentine.add_edge('Medici','Tornabuoni')
-        self.florentine.add_edge('Medici','Albizzi')
-        self.florentine.add_edge('Medici','Salviati')
-        self.florentine.add_edge('Salviati','Pazzi')
-        self.florentine.add_edge('Peruzzi','Strozzi')
-        self.florentine.add_edge('Peruzzi','Bischeri')
-        self.florentine.add_edge('Strozzi','Ridolfi')
-        self.florentine.add_edge('Strozzi','Bischeri')
-        self.florentine.add_edge('Ridolfi','Tornabuoni')
-        self.florentine.add_edge('Tornabuoni','Guadagni')
-        self.florentine.add_edge('Albizzi','Ginori')
-        self.florentine.add_edge('Albizzi','Guadagni')
-        self.florentine.add_edge('Bischeri','Guadagni')
-        self.florentine.add_edge('Guadagni','Lamberteschi')
-        
+        self.florentine.add_edge('Acciaiuoli', 'Medici')
+        self.florentine.add_edge('Castellani', 'Peruzzi')
+        self.florentine.add_edge('Castellani', 'Strozzi')
+        self.florentine.add_edge('Castellani', 'Barbadori')
+        self.florentine.add_edge('Medici', 'Barbadori')
+        self.florentine.add_edge('Medici', 'Ridolfi')
+        self.florentine.add_edge('Medici', 'Tornabuoni')
+        self.florentine.add_edge('Medici', 'Albizzi')
+        self.florentine.add_edge('Medici', 'Salviati')
+        self.florentine.add_edge('Salviati', 'Pazzi')
+        self.florentine.add_edge('Peruzzi', 'Strozzi')
+        self.florentine.add_edge('Peruzzi', 'Bischeri')
+        self.florentine.add_edge('Strozzi', 'Ridolfi')
+        self.florentine.add_edge('Strozzi', 'Bischeri')
+        self.florentine.add_edge('Ridolfi', 'Tornabuoni')
+        self.florentine.add_edge('Tornabuoni', 'Guadagni')
+        self.florentine.add_edge('Albizzi', 'Ginori')
+        self.florentine.add_edge('Albizzi', 'Guadagni')
+        self.florentine.add_edge('Bischeri', 'Guadagni')
+        self.florentine.add_edge('Guadagni', 'Lamberteschi')
+
     def test_K5(self):
         """Maximal independent set: K5"""
         G = nx.complete_graph(5)
@@ -63,19 +64,22 @@ def test_exception(self):
         assert_raises(nx.NetworkXUnfeasible,
                       nx.maximal_independent_set, G, ["Salviati", "Pazzi"])
 
+    def test_digraph_exception(self):
+        G = nx.DiGraph([(1, 2), (3, 4)])
+        assert_raises(nx.NetworkXNotImplemented, nx.maximal_independent_set, G)
+
     def test_florentine_family(self):
         G = self.florentine
         indep = nx.maximal_independent_set(G, ["Medici", "Bischeri"])
         assert_equal(sorted(indep),
                      sorted(["Medici", "Bischeri", "Castellani", "Pazzi",
-                      "Ginori", "Lamberteschi"]))
+                             "Ginori", "Lamberteschi"]))
 
     def test_bipartite(self):
         G = nx.complete_bipartite_graph(12, 34)
         indep = nx.maximal_independent_set(G, [4, 5, 9, 10])
         assert_equal(sorted(indep), list(range(12)))
 
-
     def test_random_graphs(self):
         """Generate 50 random graphs of different types and sizes and
         make sure that all sets are independent and maximal."""
@@ -86,4 +90,3 @@ def test_random_graphs(self):
             neighbors_of_MIS = set.union(*(set(G.neighbors(v)) for v in IS))
             for v in set(G.nodes()).difference(IS):
                 assert_true(v in neighbors_of_MIS)
-