Method that adds edges to a graph until all nodes have the given degree

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












Exercise 2-3 in Think Complexity: Complexity Science and Computational Modeling by Allen B. Downey asks us to implement a method for a class of undirected graphs:




Write a method named add_regular_edges that starts with an edgeless graph and adds edges so that every vertex has the same degree. The degree of a node is the number of edges it is connected to.



To create a regular graph with degree 2, you would do something like this:



vertices = [ ... list of vertices ...]
g = Graph(vertices, )
g.add_regular_edges(2)


It is not always possible to create a regular graph with a given degree, so you should figure out and document the preconditions for this method.




This was a little difficult for me as I am still getting comfortable with Python. Here was what my solution ended up looking like (heavily commented for myself and others):



def add_regular_edges(self, deg):

#for every node in the graph
for v in self:

#set the degree tolerance to 1
tol = 1

#while the degree of current node is less than the given degree
while self.degree(v) < deg:

#traverse over every node
for w in self:

'''if the 2 nodes do not equal one another, the current degree (v) is less than the given degree,
and the other node (w) is less than the current tolerance -> add the edge'''
if v != w and (self.degree(v) < deg) and (self.degree(w) < tol):
self.add_edge(Edge(v,w))

#increase the tolerance in case we could not fill all nodes in the first pass
tol += 1

#if at any point the tolerance is more than the degree, then we cannot complete the graph with the given degree
if tol > (deg+1):
raise ValueError("The graph cannot be converted to the given degree")


However, I have a strong suspicion that this is not the most optimal way to go about this. One of my friends commented that I might be able to do this using lists and enumerating over them?







share|improve this question





















  • Where are the rest of functions coming from? Have you overridden the __iter__ method?
    – hjpotter92
    Feb 4 at 22:09










  • @hjpotter92 Yes I have overridden __iter__, it's only so I can initalize the graph with a list of Vertices and Edges if I need to though. The class just inherits from dict, so it's a dictionary of dictionaries (cause each vertex v is a dictionary as well). The other functions (degree() and add_edge()) were defined by me and just do exactly what they sound like.
    – avghdev
    Feb 4 at 22:19











  • Is Edge(v, w) different from Edge(w, v)?
    – hjpotter92
    Feb 5 at 8:16










  • @hjpotter92 Nope they are the same - that’s not to say that v and w are the same though.
    – avghdev
    Feb 5 at 9:34

















up vote
2
down vote

favorite












Exercise 2-3 in Think Complexity: Complexity Science and Computational Modeling by Allen B. Downey asks us to implement a method for a class of undirected graphs:




Write a method named add_regular_edges that starts with an edgeless graph and adds edges so that every vertex has the same degree. The degree of a node is the number of edges it is connected to.



To create a regular graph with degree 2, you would do something like this:



vertices = [ ... list of vertices ...]
g = Graph(vertices, )
g.add_regular_edges(2)


It is not always possible to create a regular graph with a given degree, so you should figure out and document the preconditions for this method.




This was a little difficult for me as I am still getting comfortable with Python. Here was what my solution ended up looking like (heavily commented for myself and others):



def add_regular_edges(self, deg):

#for every node in the graph
for v in self:

#set the degree tolerance to 1
tol = 1

#while the degree of current node is less than the given degree
while self.degree(v) < deg:

#traverse over every node
for w in self:

'''if the 2 nodes do not equal one another, the current degree (v) is less than the given degree,
and the other node (w) is less than the current tolerance -> add the edge'''
if v != w and (self.degree(v) < deg) and (self.degree(w) < tol):
self.add_edge(Edge(v,w))

#increase the tolerance in case we could not fill all nodes in the first pass
tol += 1

#if at any point the tolerance is more than the degree, then we cannot complete the graph with the given degree
if tol > (deg+1):
raise ValueError("The graph cannot be converted to the given degree")


However, I have a strong suspicion that this is not the most optimal way to go about this. One of my friends commented that I might be able to do this using lists and enumerating over them?







share|improve this question





















  • Where are the rest of functions coming from? Have you overridden the __iter__ method?
    – hjpotter92
    Feb 4 at 22:09










  • @hjpotter92 Yes I have overridden __iter__, it's only so I can initalize the graph with a list of Vertices and Edges if I need to though. The class just inherits from dict, so it's a dictionary of dictionaries (cause each vertex v is a dictionary as well). The other functions (degree() and add_edge()) were defined by me and just do exactly what they sound like.
    – avghdev
    Feb 4 at 22:19











  • Is Edge(v, w) different from Edge(w, v)?
    – hjpotter92
    Feb 5 at 8:16










  • @hjpotter92 Nope they are the same - that’s not to say that v and w are the same though.
    – avghdev
    Feb 5 at 9:34













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Exercise 2-3 in Think Complexity: Complexity Science and Computational Modeling by Allen B. Downey asks us to implement a method for a class of undirected graphs:




Write a method named add_regular_edges that starts with an edgeless graph and adds edges so that every vertex has the same degree. The degree of a node is the number of edges it is connected to.



To create a regular graph with degree 2, you would do something like this:



vertices = [ ... list of vertices ...]
g = Graph(vertices, )
g.add_regular_edges(2)


It is not always possible to create a regular graph with a given degree, so you should figure out and document the preconditions for this method.




This was a little difficult for me as I am still getting comfortable with Python. Here was what my solution ended up looking like (heavily commented for myself and others):



def add_regular_edges(self, deg):

#for every node in the graph
for v in self:

#set the degree tolerance to 1
tol = 1

#while the degree of current node is less than the given degree
while self.degree(v) < deg:

#traverse over every node
for w in self:

'''if the 2 nodes do not equal one another, the current degree (v) is less than the given degree,
and the other node (w) is less than the current tolerance -> add the edge'''
if v != w and (self.degree(v) < deg) and (self.degree(w) < tol):
self.add_edge(Edge(v,w))

#increase the tolerance in case we could not fill all nodes in the first pass
tol += 1

#if at any point the tolerance is more than the degree, then we cannot complete the graph with the given degree
if tol > (deg+1):
raise ValueError("The graph cannot be converted to the given degree")


However, I have a strong suspicion that this is not the most optimal way to go about this. One of my friends commented that I might be able to do this using lists and enumerating over them?







share|improve this question













Exercise 2-3 in Think Complexity: Complexity Science and Computational Modeling by Allen B. Downey asks us to implement a method for a class of undirected graphs:




Write a method named add_regular_edges that starts with an edgeless graph and adds edges so that every vertex has the same degree. The degree of a node is the number of edges it is connected to.



To create a regular graph with degree 2, you would do something like this:



vertices = [ ... list of vertices ...]
g = Graph(vertices, )
g.add_regular_edges(2)


It is not always possible to create a regular graph with a given degree, so you should figure out and document the preconditions for this method.




This was a little difficult for me as I am still getting comfortable with Python. Here was what my solution ended up looking like (heavily commented for myself and others):



def add_regular_edges(self, deg):

#for every node in the graph
for v in self:

#set the degree tolerance to 1
tol = 1

#while the degree of current node is less than the given degree
while self.degree(v) < deg:

#traverse over every node
for w in self:

'''if the 2 nodes do not equal one another, the current degree (v) is less than the given degree,
and the other node (w) is less than the current tolerance -> add the edge'''
if v != w and (self.degree(v) < deg) and (self.degree(w) < tol):
self.add_edge(Edge(v,w))

#increase the tolerance in case we could not fill all nodes in the first pass
tol += 1

#if at any point the tolerance is more than the degree, then we cannot complete the graph with the given degree
if tol > (deg+1):
raise ValueError("The graph cannot be converted to the given degree")


However, I have a strong suspicion that this is not the most optimal way to go about this. One of my friends commented that I might be able to do this using lists and enumerating over them?









share|improve this question












share|improve this question




share|improve this question








edited Mar 15 at 14:38









Gareth Rees

41.1k394168




41.1k394168









asked Feb 4 at 21:34









avghdev

283




283











  • Where are the rest of functions coming from? Have you overridden the __iter__ method?
    – hjpotter92
    Feb 4 at 22:09










  • @hjpotter92 Yes I have overridden __iter__, it's only so I can initalize the graph with a list of Vertices and Edges if I need to though. The class just inherits from dict, so it's a dictionary of dictionaries (cause each vertex v is a dictionary as well). The other functions (degree() and add_edge()) were defined by me and just do exactly what they sound like.
    – avghdev
    Feb 4 at 22:19











  • Is Edge(v, w) different from Edge(w, v)?
    – hjpotter92
    Feb 5 at 8:16










  • @hjpotter92 Nope they are the same - that’s not to say that v and w are the same though.
    – avghdev
    Feb 5 at 9:34

















  • Where are the rest of functions coming from? Have you overridden the __iter__ method?
    – hjpotter92
    Feb 4 at 22:09










  • @hjpotter92 Yes I have overridden __iter__, it's only so I can initalize the graph with a list of Vertices and Edges if I need to though. The class just inherits from dict, so it's a dictionary of dictionaries (cause each vertex v is a dictionary as well). The other functions (degree() and add_edge()) were defined by me and just do exactly what they sound like.
    – avghdev
    Feb 4 at 22:19











  • Is Edge(v, w) different from Edge(w, v)?
    – hjpotter92
    Feb 5 at 8:16










  • @hjpotter92 Nope they are the same - that’s not to say that v and w are the same though.
    – avghdev
    Feb 5 at 9:34
















Where are the rest of functions coming from? Have you overridden the __iter__ method?
– hjpotter92
Feb 4 at 22:09




Where are the rest of functions coming from? Have you overridden the __iter__ method?
– hjpotter92
Feb 4 at 22:09












@hjpotter92 Yes I have overridden __iter__, it's only so I can initalize the graph with a list of Vertices and Edges if I need to though. The class just inherits from dict, so it's a dictionary of dictionaries (cause each vertex v is a dictionary as well). The other functions (degree() and add_edge()) were defined by me and just do exactly what they sound like.
– avghdev
Feb 4 at 22:19





@hjpotter92 Yes I have overridden __iter__, it's only so I can initalize the graph with a list of Vertices and Edges if I need to though. The class just inherits from dict, so it's a dictionary of dictionaries (cause each vertex v is a dictionary as well). The other functions (degree() and add_edge()) were defined by me and just do exactly what they sound like.
– avghdev
Feb 4 at 22:19













Is Edge(v, w) different from Edge(w, v)?
– hjpotter92
Feb 5 at 8:16




Is Edge(v, w) different from Edge(w, v)?
– hjpotter92
Feb 5 at 8:16












@hjpotter92 Nope they are the same - that’s not to say that v and w are the same though.
– avghdev
Feb 5 at 9:34





@hjpotter92 Nope they are the same - that’s not to say that v and w are the same though.
– avghdev
Feb 5 at 9:34
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186758%2fmethod-that-adds-edges-to-a-graph-until-all-nodes-have-the-given-degree%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186758%2fmethod-that-adds-edges-to-a-graph-until-all-nodes-have-the-given-degree%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation