-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphAlgoInterface.py
75 lines (65 loc) · 2.51 KB
/
GraphAlgoInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import List
from src.Interfaces import GraphInterface
class GraphAlgoInterface:
"""This abstract class represents an interface of a graph."""
def get_graph(self) -> GraphInterface:
"""
:return: the directed graph on which the algorithm works on.
"""
def load_from_json(self, file_name: str) -> bool:
"""
Loads a graph from a json file.
@param file_name: The path to the json file
@returns True if the loading was successful, False o.w.
"""
raise NotImplementedError
def save_to_json(self, file_name: str) -> bool:
"""
Saves the graph in JSON format to a file
@param file_name: The path to the out file
@return: True if the save was successful, False o.w.
"""
raise NotImplementedError
def shortest_path(self, id1: int, id2: int) -> (float, list):
"""
Returns the shortest path from node id1 to node id2 using Dijkstra's Algorithm
@param id1: The start node id
@param id2: The end node id
@return: The distance of the path, a list of the nodes ids that the path goes through
Example:
# >>> from GraphAlgo import GraphAlgo
# >>> g_algo = GraphAlgo()
# >>> g_algo.addNode(0)
# >>> g_algo.addNode(1)
# >>> g_algo.addNode(2)
# >>> g_algo.addEdge(0,1,1)
# >>> g_algo.addEdge(1,2,4)
# >>> g_algo.shortestPath(0,1)
# (1, [0, 1])
# >>> g_algo.shortestPath(0,2)
# (5, [0, 1, 2])
Notes:
If there is no path between id1 and id2, or one of them dose not exist the function returns (float('inf'),[])
More info:
https://en.wikipedia.org/wiki/Dijkstra's_algorithm
"""
raise NotImplementedError
def TSP(self, node_lst: List[int]) -> (List[int], float):
"""
Finds the shortest path that visits all the nodes in the list
:param node_lst: A list of nodes id's
:return: A list of the nodes id's in the path, and the overall distance
"""
def centerPoint(self) -> (int, float):
"""
Finds the node that has the shortest distance to it's farthest node.
:return: The nodes id, min-maximum distance
"""
def plot_graph(self) -> None:
"""
Plots the graph.
If the nodes have a position, the nodes will be placed there.
Otherwise, they will be placed in a random but elegant manner.
@return: None
"""
raise NotImplementedError