jason
jason

Reputation: 4449

pygraphviz: finding the max rank node using successors

I'm trying to find the max rank node and the depth. Here is my code.

import pygraphviz as pgv


class Test:
    def __init__(self):
        self.G = pgv.AGraph(directed=True)

        self.G.add_node('a')
        self.G.add_node('b')
        self.G.add_node('c')
        self.G.add_node('d')
        self.G.add_node('e')
        self.G.add_node('f')

        self.G.add_edge('a', 'b')
        self.G.add_edge('b', 'c')
        self.G.add_edge('b', 'd')
        self.G.add_edge('d', 'e')
        self.G.add_edge('e', 'f')
        print(self.G.string())
        self.find_max_rank_node()

    def find_max_rank_node(self):
        nodes = self.G.nodes()
        depth = 0
        for n in nodes:
            layer1 = self.G.successors(n)
            if layer1:
                depth = depth + 1
                for layer_one in layer1:
                    layer2 = self.G.successors(layer_one)
                    print(n, layer2)


if __name__ == '__main__': Test()

The output should be f and 4. I started to code it but realized that I'm not going to know how depth the branch is ... and I'm not sure how to write the loop.

Upvotes: 1

Views: 326

Answers (2)

Michał Słapek
Michał Słapek

Reputation: 1572

Use algorithm from NetworkX Python library for graph processing.

Install NetworkX with pip install networkx. Then:

import networkx as nx
from networkx.drawing.nx_agraph import from_agraph
import pygraphviz as pgv

# constructing graph with pygraphviz
G = pgv.AGraph(directed=True)

G.add_node('a')
G.add_node('b')
G.add_node('c')
G.add_node('d')
G.add_node('e')
G.add_node('f')

G.add_edge('a', 'b')
G.add_edge('b', 'c')
G.add_edge('b', 'd')
G.add_edge('d', 'e')
G.add_edge('e', 'f')

# converting pygraphviz graph to networkx graph
X = from_agraph(G)

# dictionary {node: length}
lengths = nx.shortest_path_length(X, 'a')

result = max(lengths.items(), key=lambda p: p[1])

Result is ('f', 4).


Sidenote

Because question was about pygraphviz, I provided solution converting pygraphviz object to networkx.

You can alternatively use only networkx graphs in software and later convert then to pygraphviz graphs with networkx.drawing.nx_agraph.to_agraph.

Upvotes: 2

Anwarvic
Anwarvic

Reputation: 12992

The easiest solution for your problem can be done using recursion. The following find_max_rank_node function is created to take the node that we are going to start searching from and it returns the deepest node and the depth:

import pygraphviz as pgv

class Test:
    def __init__(self):
        self.G = pgv.AGraph(directed=True)

        self.G.add_node('a')
        self.G.add_node('b')
        self.G.add_node('c')
        self.G.add_node('d')
        self.G.add_node('e')
        self.G.add_node('f')

        self.G.add_edge('a', 'b')
        self.G.add_edge('b', 'c')
        self.G.add_edge('b', 'd')
        self.G.add_edge('d', 'e')
        self.G.add_edge('e', 'f')
        print(self.G.string())
        # try it out
        self.find_max_rank_node('a')    # ('f', 4)
        self.find_max_rank_node('b')    # ('f', 3)
        self.find_max_rank_node('c')    # ('c', 0)
        self.find_max_rank_node('d')    # ('f', 2)
        self.find_max_rank_node('e')    # ('f', 1)
        self.find_max_rank_node('f')    # ('f', 0)
        # visualize the graph
        self.viz()

    def find_max_rank_node(self, start_node):
        succ = self.G.successors(start_node)
        if len(succ) == 0:
            return (start_node, 0)
        else:
            deepest_node = None
            depth = 0
            for node in succ:
                n, d = self.find_max_rank_node(node)
                if d >= depth:
                    deepest_node = n
                    depth = d
            return (deepest_node, 1+depth)

    def viz(self):
        self.G.layout()
        self.G.draw('file.png')


if __name__ == '__main__': Test()

Also, I have created one more method called viz to visualize the graph and write it in an image called file.png shown below:

Graph

Hope this answers your question !!

Upvotes: 2

Related Questions