ó žÃÒYc@sRdZddlmZddlZddlZddlmZddl m Z ddl m Z ddd d d d gZ d „Z e dƒe dƒd„ƒƒZe dƒe dƒd„ƒƒZd„Ze dƒe dƒd„ƒƒZe dƒe dƒd„ƒƒZe dƒe dƒd„ƒƒZe dƒe dƒd„ƒƒZdS(s Functions concerning tournament graphs. A `tournament graph`_ is a complete oriented graph. In other words, it is a directed graph in which there is exactly one directed edge joining each pair of distinct nodes. For each function in this module that accepts a graph as input, you must provide a tournament graph. The responsibility is on the caller to ensure that the graph is a tournament graph. To access the functions in this module, you must access them through the :mod:`networkx.algorithms.tournament` module:: >>> import networkx as nx >>> from networkx.algorithms import tournament >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 0)]) >>> tournament.is_tournament(G) True .. _tournament graph: https://en.wikipedia.org/wiki/Tournament_%28graph_theory%29 iÿÿÿÿ(t combinationsN(tis_simple_path(tarbitrary_element(tnot_implemented_forthamiltonian_patht is_reachabletis_strongly_connectedt is_tournamenttrandom_tournamenttscore_sequencecCs]x*t|ƒD]\}}||ƒr |Sq Wy |dSWntk rXtdƒ‚nXdS(sReturns the index of the first element in `iterable` that satisfies the given condition. If no such element is found (that is, when the iterable is exhausted), this returns the length of the iterable (that is, one greater than the last index of the iterable). `iterable` must not be empty. If `iterable` is empty, this function raises :exc:`ValueError`. isiterable must be non-emptyN(t enumeratet NameErrort ValueError(titerablet conditiontitx((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pytindex_satisfying*s    t undirectedt multigraphcs8t‡fd†tˆdƒDƒƒo7tjˆƒdkS(sReturns True if and only if `G` is a tournament. A tournament is a directed graph, with neither self-loops nor multi-edges, in which there is exactly one directed edge joining each pair of distinct nodes. Parameters ---------- G : NetworkX graph A directed graph representing a tournament. Returns ------- bool Whether the given graph is a tournament graph. Notes ----- Some definitions require a self-loop on each node, but that is not the convention used here. c3s3|])\}}|ˆ|k|ˆ|kAVqdS(N((t.0tutv(tG(st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys _sii(tallRtnxtnumber_of_selfloops(R((Rst/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyREs%cs’tˆƒdkrgStˆƒdkr5tˆƒgStˆƒ‰tˆjtˆƒˆhƒƒ}t|‡‡fd†ƒ}|j|ˆƒ|S(s Returns a Hamiltonian path in the given tournament graph. Each tournament has a Hamiltonian path. If furthermore, the tournament is strongly connected, then the returned Hamiltonian path is a Hamiltonian cycle (by joining the endpoints of the path). Parameters ---------- G : NetworkX graph A directed graph representing a tournament. Returns ------- bool Whether the given graph is a tournament graph. Notes ----- This is a recursive implementation with an asymptotic running time of $O(n^2)$, ignoring multiplicative polylogarithmic factors, where $n$ is the number of nodes in the graph. iicsˆˆ|kS(N((R(RR(st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyt…s(tlenRRtsubgraphtsetRtinsert(Rthampathtindex((RRst/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyRcs  "cCs]d„t||ddƒDƒ}tt|ƒdƒ}d„t||ƒDƒ}tj|ƒS(sÞReturns a random tournament graph on `n` nodes. Parameters ---------- n : int The number of nodes in the returned graph. Returns ------- bool Whether the given graph is a tournament graph. Notes ----- This algorithm adds, for each pair of distinct nodes, an edge with uniformly random orientation. In other words, `\binom{n}{2}` flips of an unbiased coin decide the orientations of the edges in the graph. css|]}tjƒVqdS(N(trandom(RR((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys  siicss?|]5\\}}}|dkr-||fn ||fVqdS(gà?N((RRRtr((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys ¢s(trangeRtzipRtDiGraph(tntcoinstpairstedges((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyRŠs"cCstd„|jƒDƒƒS(sgReturns the score sequence for the given tournament graph. The score sequence is the sorted list of the out-degrees of the nodes of the graph. Parameters ---------- G : NetworkX graph A directed graph representing a tournament. Returns ------- list A sorted list of the out-degrees of the nodes of `G`. css|]\}}|VqdS(N((RRtd((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys ¹s(tsortedt out_degree(R((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyR ¦scCstj|ƒ}||jS(s Returns the tournament matrix for the given tournament graph. This function requires SciPy. The *tournament matrix* of a tournament graph with edge set *E* is the matrix *T* defined by .. math:: T_{i j} = \begin{cases} +1 & \text{if } (i, j) \in E \\ -1 & \text{if } (j, i) \in E \\ 0 & \text{if } i == j. \end{cases} An equivalent definition is `T = A - A^T`, where *A* is the adjacency matrix of the graph `G`. Parameters ---------- G : NetworkX graph A directed graph representing a tournament. Returns ------- SciPy sparse matrix The tournament matrix of the tournament graph `G`. Raises ------ ImportError If SciPy is not available. (Rtadjacency_matrixtT(RtA((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyttournament_matrix¼s&csWd„}d„‰gˆD]}|ˆ|ƒ^q}t‡‡‡‡fd†|DƒƒS(sÒDecides whether there is a path from `s` to `t` in the tournament. This function is more theoretically efficient than the reachability checks than the shortest path algorithms in :mod:`networkx.algorithms.shortest_paths`. The given graph **must** be a tournament, otherwise this function's behavior is undefined. Parameters ---------- G : NetworkX graph A directed graph representing a tournament. s : node A node in the graph. t : node A node in the graph. Returns ------- bool Whether there is a path from `s` to `t` in `G`. Notes ----- Although this function is more theoretically efficient than the generic shortest path functions, a speedup requires the use of parallelism. Though it may in the future, the current implementation does not use parallelism, thus you may not see much of a speedup. This algorithm comes from [1]. References ---------- .. [1] Tantau, Till. "A note on the complexity of the reachability problem for tournaments." *Electronic Colloquium on Computational Complexity*. 2001. cs‡‡fd†ˆDƒS(sKReturns the set of nodes at distance at most two from `v`. `G` must be a graph and `v` a node in that graph. The returned set includes the nodes at distance zero (that is, the node `v` itself), the nodes at distance one (that is, the out-neighbors of `v`), and the nodes at distance two. csTh|]J‰ˆˆksJˆˆˆksJt‡‡‡fd†ˆDƒƒrˆ’qS(c3s'|]}tˆˆ|ˆgƒVqdS(N(tis_path(Rtz(RRR(st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys #s(tany(R(RR(Rst/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys !s ((RR((RRst/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyttwo_neighborhoods cs't‡‡fd†tˆƒˆDƒƒS(sàDecides whether the given set of nodes is closed. A set *S* of nodes is *closed* if for each node *u* in the graph not in *S* and for each node *v* in *S*, there is an edge from *u* to *v*. c3s,|]"}ˆD]}|ˆ|kVq qdS(N((RRR(Rtnodes(st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys .s(RR(RR6((RR6st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyt is_closed%s c3s7|]-}ˆˆ|ƒo-ˆ|ko-ˆ|k VqdS(N((RtS(RR7tstt(st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys 2s(R(RR9R:R5Rt neighborhoods((RR7R9R:st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyRæs 0  "cst‡fd†ˆDƒƒS(sXDecides whether the given tournament is strongly connected. This function is more theoretically efficient than the :func:`~networkx.algorithms.components.is_strongly_connected` function. The given graph **must** be a tournament, otherwise this function's behavior is undefined. Parameters ---------- G : NetworkX graph A directed graph representing a tournament. Returns ------- bool Whether the tournament is strongly connected. Notes ----- Although this function is more theoretically efficient than the generic strong connectivity function, a speedup requires the use of parallelism. Though it may in the future, the current implementation does not use parallelism, thus you may not see much of a speedup. This algorithm comes from [1]. References ---------- .. [1] Tantau, Till. "A note on the complexity of the reachability problem for tournaments." *Electronic Colloquium on Computational Complexity*. 2001. c3s.|]$}ˆD]}tˆ||ƒVq qdS(N(R(RRR(R(st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pys _s(R(R((Rst/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyR6s)(t__doc__t itertoolsRR"tnetworkxRt networkx.algorithms.simple_pathsRR2tnetworkx.utilsRRt__all__RRRRR R1RR(((st/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/tournament.pyts,      &   ) O