Update graph remove_neighbor error handling (#93)

This commit is contained in:
Donne Martin 2016-07-20 07:29:48 -04:00 committed by GitHub
parent 0e866dc7c0
commit 113eddd3c3
2 changed files with 8 additions and 14 deletions

View File

@ -30,14 +30,11 @@ class Node:
self.adj_nodes[neighbor.key] = neighbor self.adj_nodes[neighbor.key] = neighbor
def remove_neighbor(self, neighbor): def remove_neighbor(self, neighbor):
if neighbor is None: if neighbor is None or neighbor.key not in self.adj_nodes:
raise Exception('Invalid neighbor') raise Exception('Invalid neighbor')
if neighbor.key in self.adj_nodes:
neighbor.incoming_edges -= 1 neighbor.incoming_edges -= 1
del self.adj_weights[neighbor.key] del self.adj_weights[neighbor.key]
del self.adj_nodes[neighbor.key] del self.adj_nodes[neighbor.key]
else:
raise Exception('Invalid neighbor')
class Graph: class Graph:

View File

@ -194,14 +194,11 @@
" self.adj_nodes[neighbor.key] = neighbor\n", " self.adj_nodes[neighbor.key] = neighbor\n",
"\n", "\n",
" def remove_neighbor(self, neighbor):\n", " def remove_neighbor(self, neighbor):\n",
" if neighbor is None:\n", " if neighbor is None or neighbor.key not in self.adj_nodes:\n",
" raise Exception('Invalid neighbor')\n", " raise Exception('Invalid neighbor')\n",
" if neighbor.key in self.adj_nodes:\n",
" neighbor.incoming_edges -= 1\n", " neighbor.incoming_edges -= 1\n",
" del self.adj_weights[neighbor.key]\n", " del self.adj_weights[neighbor.key]\n",
" del self.adj_nodes[neighbor.key]\n", " del self.adj_nodes[neighbor.key]\n",
" else:\n",
" raise Exception('Invalid neighbor')\n",
"\n", "\n",
"\n", "\n",
"class Graph:\n", "class Graph:\n",