-
Notifications
You must be signed in to change notification settings - Fork 0
DiGraph
LielVaknin edited this page Jan 11, 2021
·
3 revisions
This class implemented in the DiGraph file which included in the src package using the definition of GraphInterface. It represents a directed weighted graph.
-
_ _ node_size - Represents the number of nodes in the graph.
-
_ _ edge_size - Represents the number of directional edges in the graph.
-
_ _ mc (mode count) - Counts changes in the graph.
-
_ _ nodes - Represents a dictionary of all nodes in the graph.
-
edges - Represents a nested dictionary of all nodes in the graph and their dictionary of neighbors connected from (out) each one of them.
- end_edges - Represents a nested dictionary of all nodes in the graph and their dictionary of neighbors connected to (into) each one of them.
_ _nodes, edges and end_edges which mentioned above implemented in a data structure - dictionary.
The first one implemented with a dictionary, the second and the third with a nested dictionary.
The reason for using this data structure is because operations like get item / set item / delete item executes at time complexity O(1).
- def _ _ init _ _(self) - This _ _ init _ _ method initializes all the variables mentioned above.
- def v_size(self) -> int - Returns the number of exist nodes in this graph.
- def e_size(self) -> int - Returns the number of exist edges in this graph.
- def get_all_v(self) -> dict - Returns a dictionary of all the nodes in the graph, each node is represented using a pair. (key: node_id, value: node_data).
- def all_in_edges_of_node(self, id1: int) -> dict - Returns a dictionary of all the edges connected to (into) node_id, each edge is represented using a pair (key: other_node_id, value: weight of edge).
- def all_out_edges_of_node(self, id1: int) -> dict - Returns a dictionary of all the edges connected from (out) node_id, each edge is represented using a pair (key: other_node_id, value: weight of edge).
- def get_mc(self) -> int - Returns the current version of this graph, on every change in the graph state - the MC should be increased.
- def add_edge(self, id1: int, id2: int, weight: float) -> bool - Adds an edge to the graph. This method returns True if the edge was added successfully, False otherwise. If the edge already exists or one of the nodes dose not exists the function returns False.
- def add_node(self, node_id: int, pos: tuple = None) -> bool - Adds a node to the graph. Returns True if the node was added successfully, False otherwise. If the node id already exists the node will not be added and the method will return False.
- def remove_node(self, node_id: int) -> bool - This method removes all the edges comes out and in of node_id and finally removes node_id from the graph. Returns True if the node was removed successfully, False otherwise. If the node id does not exists the function will return False.
- def remove_edge(self, node_id1: int, node_id2: int) -> bool - Removes an edge from the graph. Returns True if the edge was removed successfully, False otherwise. If such an edge does not exists the function will return False.
- def _ _ str _ _(self) - Returns a string which represents the graph.
- def _ _ copy _ _(self) - Performs a deep copy of the graph.