-1

Can anyone offer some suggestions on how to write a method that returns if a graph is directed or undirected in python?

Thanks.


class DiGraph :
    def __init__ ( self ) :
        self._adj = {}
        
    def add_node ( self , u ) :
        if u not in self._adj :
            self._adj [ u ] = []
            
    def add_edge ( self , u , v , weight =1) :
        self.add_node ( u )
        self.add_node ( v )
        self._adj [ u ].append (( v , weight ) )
  • 3
    What graph are you talking about? Graphs must be *represented* somehow. That representation is often enough to determine if it is directed. If you are using a library, you should look at the docs or at least mention the library. – Mark Dec 27 '20 at 17:17
  • 2
    But there is no `graph` object in python, so nobody knows what you are talking about. Graphs must be *implemented* in python. Without seeing that implementation your question is not answerable. Can you show the code for your graph? – Mark Dec 27 '20 at 17:25
  • This is pretty clearly a directed graph class. Instances of `DiGraph` are directed. – user2357112 supports Monica Dec 27 '20 at 17:32
  • 1
    On this class I have to add a method that determined if the graph is directed or undirected. – user166977 Dec 27 '20 at 17:33
  • It might be the case that in a particular instance of `DiGraph`, the reversed versions of all edges in the graph are also in the graph. That might be what you really want to check for, and if so, you can write a method to check for it. – user2357112 supports Monica Dec 27 '20 at 17:34
  • How can I check that ? – user166977 Dec 27 '20 at 17:36
  • In your implementation when you add an edge you are only adding the connected node to a single node’s connections. This is typical (but not required) of a directed graph. In an undirected graph when you add an edge between `u` and `v` you would typically append the node to *both* arrays. In other words something like `self._adj[v].append ((u, weight))` in addition to what you are already doing. – Mark Dec 27 '20 at 17:39

1 Answers1

0

I'm not sure if this is what you are looking for but here is how I though about it.

some_graph = DiGraph()
some_graph.add_edge("a","b")
some_graph.is_directed()

This returns True since there is only an edge in one direction.

some_graph = DiGraph()
some_graph.add_edge("a","b")
some_graph.add_edge("b","a")
some_graph.is_directed()

This returns False since there are edges in both directions with the same weight.

some_graph = DiGraph()
some_graph.add_edge("a","b")
some_graph.add_edge("b","a",2)
some_graph.is_directed()

This returns True since even though there are edges in both directions they have different weights.

Full code:

class DiGraph :
    def __init__ ( self ) :
        self._adj = {}
        
    def add_node ( self , u ) :
        if u not in self._adj :
            self._adj [ u ] = []
            
    def add_edge ( self , u , v , weight =1) :
        self.add_node ( u )
        self.add_node ( v )
        self._adj [ u ].append (( v , weight ) )

    def is_directed ( self ) :
        for u, neigh in self._adj.items():
            for n in neigh:
                print("u", u, "neigh:", neigh)
                if n[0] != u and not ([(node,w) for node,w in self._adj[n[0]] if (node==u and w==n[1])]):
                    return True
        return False

willcrack
  • 1,496
  • 7
  • 18