1"""
2**********
3Exceptions
4**********
5
6Base exceptions and errors for NetworkX.
7"""
8
9__all__ = [
10 "HasACycle",
11 "NodeNotFound",
12 "PowerIterationFailedConvergence",
13 "ExceededMaxIterations",
14 "AmbiguousSolution",
15 "NetworkXAlgorithmError",
16 "NetworkXException",
17 "NetworkXError",
18 "NetworkXNoCycle",
19 "NetworkXNoPath",
20 "NetworkXNotImplemented",
21 "NetworkXPointlessConcept",
22 "NetworkXUnbounded",
23 "NetworkXUnfeasible",
24]
25
26
27class NetworkXException(Exception):
28 """Base class for exceptions in NetworkX."""
29
30
31class NetworkXError(NetworkXException):
32 """Exception for a serious error in NetworkX"""
33
34
35class NetworkXPointlessConcept(NetworkXException):
36 """Raised when a null graph is provided as input to an algorithm
37 that cannot use it.
38
39 The null graph is sometimes considered a pointless concept [1]_,
40 thus the name of the exception.
41
42 Notes
43 -----
44 Null graphs and empty graphs are often used interchangeably but they
45 are well defined in NetworkX. An ``empty_graph`` is a graph with ``n`` nodes
46 and 0 edges, and a ``null_graph`` is a graph with 0 nodes and 0 edges.
47
48 References
49 ----------
50 .. [1] Harary, F. and Read, R. "Is the Null Graph a Pointless
51 Concept?" In Graphs and Combinatorics Conference, George
52 Washington University. New York: Springer-Verlag, 1973.
53
54 """
55
56
57class NetworkXAlgorithmError(NetworkXException):
58 """Exception for unexpected termination of algorithms."""
59
60
61class NetworkXUnfeasible(NetworkXAlgorithmError):
62 """Exception raised by algorithms trying to solve a problem
63 instance that has no feasible solution."""
64
65
66class NetworkXNoPath(NetworkXUnfeasible):
67 """Exception for algorithms that should return a path when running
68 on graphs where such a path does not exist."""
69
70
71class NetworkXNoCycle(NetworkXUnfeasible):
72 """Exception for algorithms that should return a cycle when running
73 on graphs where such a cycle does not exist."""
74
75
76class HasACycle(NetworkXException):
77 """Raised if a graph has a cycle when an algorithm expects that it
78 will have no cycles.
79
80 """
81
82
83class NetworkXUnbounded(NetworkXAlgorithmError):
84 """Exception raised by algorithms trying to solve a maximization
85 or a minimization problem instance that is unbounded."""
86
87
88class NetworkXNotImplemented(NetworkXException):
89 """Exception raised by algorithms not implemented for a type of graph."""
90
91
92class NodeNotFound(NetworkXException):
93 """Exception raised if requested node is not present in the graph"""
94
95
96class AmbiguousSolution(NetworkXException):
97 """Raised if more than one valid solution exists for an intermediary step
98 of an algorithm.
99
100 In the face of ambiguity, refuse the temptation to guess.
101 This may occur, for example, when trying to determine the
102 bipartite node sets in a disconnected bipartite graph when
103 computing bipartite matchings.
104
105 """
106
107
108class ExceededMaxIterations(NetworkXException):
109 """Raised if a loop iterates too many times without breaking.
110
111 This may occur, for example, in an algorithm that computes
112 progressively better approximations to a value but exceeds an
113 iteration bound specified by the user.
114
115 """
116
117
118class PowerIterationFailedConvergence(ExceededMaxIterations):
119 """Raised when the power iteration method fails to converge within a
120 specified iteration limit.
121
122 `num_iterations` is the number of iterations that have been
123 completed when this exception was raised.
124
125 """
126
127 def __init__(self, num_iterations, *args, **kw):
128 msg = f"power iteration failed to converge within {num_iterations} iterations"
129 exception_message = msg
130 superinit = super().__init__
131 superinit(self, exception_message, *args, **kw)