Skip to content Skip to sidebar Skip to footer

Need Help With Networkx

Currently im faced with the following problem: I have a script that searches through a specific directory that contains documents. Each document is assigned a number within the fil

Solution 1:

This is a textbook example of a directed graph. You should read the NetworkX tutorial to get a better idea of how to work with them; basically, you need to add all the nodes (points), in this case file numbers, and then add edges between them.

import os
import networkx as nx

g = nx.DiGraph( )
for filename in os.listdir( <dir> ):
    # do something to filename to get the number
    g.add_node( <number> )

for filename in os.listdir( <dir> ):
    # do something to filename to get the source
    with open( filename ) as theFile:
        # do something to theFile to get the targets
        for target in <targets>:
            g.add_edge( <source>, <target> )

import matplotlib.pyplot as plt
nx.draw( g )

Post a Comment for "Need Help With Networkx"