From 9e738f870c1d209eba406104d3a12d8ad9d633ef Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Thu, 25 Feb 2021 16:17:23 +0000 Subject: [PATCH 1/3] Setting up GitHub Classroom Feedback From dccad2966f96aaffd4e14fb91982825279643492 Mon Sep 17 00:00:00 2001 From: Sonasah96 Date: Sun, 28 Feb 2021 13:22:16 +0400 Subject: [PATCH 2/3] Graph --- Graph.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Graph.py diff --git a/Graph.py b/Graph.py new file mode 100644 index 0000000..8517f43 --- /dev/null +++ b/Graph.py @@ -0,0 +1,70 @@ +import copy + + +class Node: + def __init__(self, vertex_id): + self.vertex_id = vertex_id + + +class Graph: + def __init__(self): + self.edge_dict = {} + self.vertex_dict = {} + self.neighbours_dict = {} + + def add_vertex(self, i): + if i not in self.vertex_dict.keys(): + self.vertex_dict[i] = Node(i) + self.neighbours_dict[i] = set() + else: + raise Exception("The Graph already has a vertex with this id") + + def delete_vertex(self, i): + if i not in self.vertex_dict.keys(): + raise Exception("The Graph does not have a vertex with that id") + else: + self.vertex_dict.pop(i) + self.neighbours_dict.pop(i) + new_dict = copy.deepcopy(self.edge_dict) + for key in new_dict: + if i in key: + self.edge_dict.pop(key) + for val in self.neighbours_dict.values(): + if i in val: + val.remove(i) + + def add_edge(self, i, j, weight=0): + # noinspection PyBroadException + try: + self.add_vertex(i) + except Exception: + pass + # noinspection PyBroadException + try: + self.add_vertex(j) + except Exception: + pass + if (i, j) in self.edge_dict.keys() or (j, i) in self.edge_dict.keys(): + raise Exception("The Graph already has an edge with these vertexes") + self.edge_dict[(i, j)] = weight + for key, val in self.neighbours_dict.items(): + if key == i: + val.add(j) + elif key == j: + val.add(i) + else: + continue + + def __contains__(self, graph_2): + if not isinstance(graph_2, Graph): + raise Exception("Graph type is expected") + if not self.neighbours_dict.keys() >= graph_2.neighbours_dict.keys(): + return False + for key in graph_2.neighbours_dict.keys(): + if not self.neighbours_dict[key] >= graph_2.neighbours_dict[key]: + return False + return True + + + + From ccabaf35f9a0654d28483a7b6bc6df41ec809153 Mon Sep 17 00:00:00 2001 From: Sonasah96 Date: Sun, 28 Feb 2021 14:54:59 +0400 Subject: [PATCH 3/3] Graph changing 1 --- Graph.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Graph.py b/Graph.py index 8517f43..9ec0aef 100644 --- a/Graph.py +++ b/Graph.py @@ -44,8 +44,10 @@ def add_edge(self, i, j, weight=0): self.add_vertex(j) except Exception: pass - if (i, j) in self.edge_dict.keys() or (j, i) in self.edge_dict.keys(): - raise Exception("The Graph already has an edge with these vertexes") + if (j, i) in self.edge_dict.keys(): + (i, j) = (j, i) + if (i, j) in self.edge_dict.keys() and self.edge_dict[(i, j)] == weight: + raise Exception("The Graph already has an edge with these vertexes and this weight") self.edge_dict[(i, j)] = weight for key, val in self.neighbours_dict.items(): if key == i: @@ -64,7 +66,3 @@ def __contains__(self, graph_2): if not self.neighbours_dict[key] >= graph_2.neighbours_dict[key]: return False return True - - - -