Vote Up 0 Vote Down

python graph code

Can anyone help me in this program: makecompletegraph(numnodes) - Takes the number of nodes numnodes and returns a dictionary corresponding to a complete directed graph with the specified number of nodes. A complete graph contains all possible edges subject to the restriction that self-loops are not allowed. The nodes of the graph should be numbered 0 to numnodes - 1 when numnodes is positive. Otherwise, the function returns a dictionary corresponding to the empty graph.

def make_complete_graph(num_nodes):
"""this is a method used for making dictionary for compleate graph"""
dic={}; #empty dictionary for graph
__node_a=0; #node A
__node_a=0; #node B
"""this function is used for calculate the dictionary for simple complete graph"""
for node_a in range(0,num_nodes): #two loops for finding the each combination of nodes
for node_b in range(0,num_nodes):
if(node_a!=node_b):
dic[node_a]=dic[node_a].append(node_b); #if two nodes are not equals the add and edge between them
return dic; # return the dictionary for graph

i=int(input()) # take no of node in graph
print(make_complete_graph(i)); # call method make_complete_graph
flag

Answers


Vote Up 0 Vote Down
Christian Calderon its a undirected complete graph where each node connected with other node using an edge. e.g. given bellow

alt text
flag | link |
Vote Up 0 Vote Down
Do you mean, a complete digraph? every pair of nodes is joined by two edges?

Finding every unique pair is a piece of cake. That is what you are doing right? Basically making an unbalanced tree to represent each pair of nodes?
flag | link |

Your Answer

Login before answering

Login with facebook