Skip to content

GraphAlgo

LielVaknin edited this page Aug 12, 2021 · 4 revisions

This class implemented in the GraphAlgo file which included in the src package.
It inherits from the given GraphAlgoInterface abstract class and represents a Directed Weighted Graph Theory algorithms.

The class includes a set of operations applicable on a graph type of DiGraph.

GraphAlgo methods:

  • def _ _ init _ _(self, graph: DiGraph) - This _ _ init _ _ method initializes a variable called graph with a given graph type of DiGraph class.
  • def get_graph(self) -> DiGraph - This method returns the graph which the Graph_Algo works on.
  • def load_from_json(self, file_name: str) -> bool - Loads a graph from a json file and inits it. This method returns True if the file successfully loaded, False otherwise.
  • def save_to_json(self, file_name: str) -> bool - Saves the graph at JSON format into a file(by given path name). Returns True if the save was successful, False otherwise.
  • def shortest_path(self, id1: int, id2: int) -> (float, list) - Returns the shortest path from node id1 to node id2 and the distance of it using Dijkstra's Algorithm. It returns a Tuple with the length of path and a list of the nodes ids that the path goes through - If there is no path between id1 and id2, or one of them dose not exist the function returns (float('inf'),[]).
  • def connected_component(self, id1: int) -> list - Finds the Strongly Connected Component(SCC) that node id1 is part of and returns a list of nodes in the SCC. If the graph is None or id1 is not in the graph, the function should return an empty list [].
  • def connected_components(self) -> List[list] - Finds all the Strongly Connected Component(SCC) in the graph and returns a list of all SCC. If the graph is None the function should return an empty list [].
  • def plot_graph(self) -> None - Plots the graph and returns None. If the nodes have a position, the nodes will be placed there. Otherwise, they will be placed in a random but elegant manner.

Clone this wiki locally