Python class for working with simple graphs
September 28th, 2009A graph is called simple if it is unweighted, undirected and without parallel edges of self-loops.
My python class for working with such graphs can be found here: download.
I will be adding different methods to this class gradually. If you have implemented additional method and would like to contribute to the development of this class or you have found a bug in the existing implementation please leave a comment to this post.
Note that to insure correct behavior, node names should be strings. If you want to store any other information in the nodes you can keep the dictionary with mapping (node name: data) or convert your node data to string (if applicable). Each graph has its ID.
This class implements the Bron-Kerbosch algorithm (Version 2) for finding all cliques in the graph. Note that, when the graph is sparse and does not contain exponential number of cliques, this method can process graphs of a decent size.
If you have any questions/comments regarding using this graph, leave a comment to this post.
Bellow is the description of this class (generated using pydoc):
class graph(__builtin__.object)
| A class for representing and manipulation undirected, unweighted simple graphs without self-loops
|
| Methods defined here:
|
| BFS(self, source)
| Implements Breadth-first search from node ’source’ in graph ’self’.
| Returns dictionary D {node: distance from source}
| distance=-1 if ‘node’ is unreachable from ’source’
|
| __init__(self, userID=None)
| Constructor
|
| add_edge(self, nd1, nd2)
| Adds edge (nd1,nd2) to the graph.
|
| add_node(self, node)
| Adds node to the graph.
|
| all_pairs_dist(self)
| Returns dictionary of all-pairs shortest paths in ’self’
| The dictionary has format {t=(nd1,nd2): distance},
| where t is a tuple.
|
| are_adjacent(self, nd1, nd2)
| Checks if nd1 and nd2 are connected
|
| create_circulant_graph(self, n, j)
| Creates a circulant graph with n nodes and m edges.
| Nodes are numbered from 1 to n.
| The chords are defined by parameters j.
| That is node i is connected to (i-j) mod n and (i+j) mod n.
| Note that not always the exact match in terms of edges is possible!
| Check the output graph for the number of edges!
|
| create_complete_graph(self, n)
| creates complete graph on n nodes
|
| create_cycle_graph(self, n)
| Creates graph-cycle on n nodes.
| Nodes are numbered from 1 to n.
|
| create_empty_graph(self, n)
| creates graph with n nodes but without edges
|
| create_path_graph(self, n)
| Create path-graph on n nodes
|
| degree(self, node)
| Returns the degree of a node
|
| dist(self, nd1, nd2)
| Returns shortest-path distance between nd1 and nd2
|
| find_all_cliques(self)
| Implements Bron-Kerbosch algorithm, Version 2
|
| get_edge_set(self)
| Returns set of edges in the graph.
|
| get_node_clust_coef(self, node)
| Returns the clustering coefficient of the node
|
| get_node_eccentricities_both(self, node)
| This function is for performance purposes.
| This is function returns standard and averaged eccentricities of the node.
| Note that both eccentricities of the node are within its connected component
|
| get_node_eccentricity(self, node)
| Returns the eccentricity of the node.
| Note that this function returns the eccentricity of a node within its
| connected component
|
| get_node_eccentricity_avg(self, node)
| Returns the averaged eccentricity of the node. That is, “avg”, not “max” distance
| Note that this function returns the eccentricity of a node within its
| connected component
|
| get_node_neighbors(self, nd)
| Returns set of node neighbors
|
| number_of_edges(self)
| Returns number of edges in the graph.
|
| number_of_nodes(self)
| Returns number of nodes in the graph.
|
| readFromEdgeList(self, path)
| Read graph from the file where it is represented as an edge list.
| The lines of the file should be formated as:
| node1[space]node2[newline]
| Duplicate edges and self loops are ignored.
|
| saveAsEdgeList(self, path)
| Saves graph as edge list
|
| ———————————————————————-