Skip to content

Graph 1 - DO NOT MERGE#7

Open
markreynoso wants to merge 130 commits into
masterfrom
graph_1
Open

Graph 1 - DO NOT MERGE#7
markreynoso wants to merge 130 commits into
masterfrom
graph_1

Conversation

@markreynoso
Copy link
Copy Markdown
Owner

No description provided.

Chaitanya.Narukulla and others added 30 commits October 23, 2017 17:04
Comment thread src/graph.py Outdated

def add_node(self, val):
"""Add a node with value of val to graph."""
self._graph[val] = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I add the same node twice, I'm going to overwrite what I had before?

Comment thread src/graph.py Outdated
self._graph[val1][val2] = weight
else:
self._graph[val1] = {val2: weight}
self._graph[val2] = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could refactor this whole set of if statements to ask questions and do operations like...

is val1 in the graph? if not, add it
is val2 in the graph? if not, add it
is val2 one of val1's neighbors? if not, add it as a neighbor with the given weight. If so, re-assign the weight of that edge to be the given weight.

Comment thread src/graph.py Outdated
for key in self._graph:
for i in self._graph[key]:
if i == val:
del self._graph[key][val]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if val in self._graph:
    del self._graph[val]
    for key in self._graph:
        if val in self._graph[key]:
            del self._graph[key][val]

Comment thread src/graph.py Outdated
if val in self._graph:
return True
else:
return False
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return val in self._graph

Comment thread src/graph.py Outdated
if child == val and key not in neighbors:
neighbors.append(key)
else:
return neighbors
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"neighbors" only goes one way. If "val" points to some other node in the graph, that other node is "val"'s neighbor. However, "val" is not that node's neighbor.

Comment thread src/graph.py Outdated
else:
return neighbors
except KeyError:
raise ValueError('This node dosent exit')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant "exist" here

Comment thread src/graph.py
if val2 in self._graph[val1]:
return True
else:
return False
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return val2 in self._graph[val1]

Comment thread src/graph.py Outdated
else:
return False
else:
raise ValueError('These edges do not exist.')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not why this error is being raised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants