o
    3ήcw                     @   sX   d dl mZ d dlZd dlZd dlmZmZmZm	Z	m
Z
 eeee	e
gZG dd dZdS )    )combinationsN)boykov_kolmogorovdinitzedmonds_karppreflow_pushshortest_augmenting_pathc                   @   sl   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
jdd Zdd Zdd Zdd Zdd ZdS )TestGomoryHuTreec                    s6   t j ||dd}t fddt||dd  D S )Nweight)r	   c                 3   s,    | ]\}} | | d  ||ffV  qdS )r	   N ).0uvTr
   T/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/flow/tests/test_gomory_hu.py	<genexpr>   s   * z7TestGomoryHuTree.minimum_edge_weight.<locals>.<genexpr>   )nxshortest_pathminzip)selfr   r   r   pathr
   r   r   minimum_edge_weight   s   $z$TestGomoryHuTree.minimum_edge_weightc                    sd   |  }|j|  tt|\}t } fdd|D D ]\}|fdd|D  q|S )Nc                 3   s    | ]	}| | fV  qd S Nr
   )r   n)Gr
   r   r   !   s    z2TestGomoryHuTree.compute_cutset.<locals>.<genexpr>c                 3   s     | ]}| v r|fV  qd S r   r
   )r   y)Vxr
   r   r   "   s    )copyremove_edgelistr   connected_componentssetupdate)r   r   T_origedger   Ucutsetnbrsr
   )r   r   r   r   compute_cutset   s   
zTestGomoryHuTree.compute_cutsetc                 C   sn   t  }t |dd t |}t |sJ t|dD ]\}}| |||\}}t ||||ks4J qd S )Nr   capacity   )r   karate_club_graphset_edge_attributesgomory_hu_treeis_treer   r   minimum_cut_value)r   r   r   r   r   	cut_valuer'   r
   r
   r   ,test_default_flow_function_karate_club_graph%   s   
z=TestGomoryHuTree.test_default_flow_function_karate_club_graphc                 C   |   t  }t |dd tD ].}t j||d}t |sJ t|dD ]\}}| |||\}}t ||||ks:J q"qd S Nr   r,   )	flow_funcr-   )	r   r.   r/   
flow_funcsr0   r1   r   r   r2   r   r   r7   r   r   r   r3   r'   r
   r
   r   test_karate_club_graph.      z'TestGomoryHuTree.test_karate_club_graphc                 C   r5   r6   )	r   davis_southern_women_graphr/   r8   r0   r1   r   r   r2   r9   r
   r
   r   test_davis_southern_women_graph8   r;   z0TestGomoryHuTree.test_davis_southern_women_graphc                 C   r5   r6   )	r   florentine_families_graphr/   r8   r0   r1   r   r   r2   r9   r
   r
   r   test_florentine_families_graphB   r;   z/TestGomoryHuTree.test_florentine_families_graphc                 C   r5   r6   )	r   les_miserables_graphr/   r8   r0   r1   r   r   r2   r9   r
   r
   r    test_les_miserables_graph_cutsetL   s   z1TestGomoryHuTree.test_les_miserables_graph_cutsetc                 C   sj   t  }t |dd t |}t |sJ d\}}| |||\}}| |||}|t|ks3J d S )Nr   r,   )r   !   )r   r.   r/   r0   r1   r   r+   len)r   r   r   r   r   r3   r'   r)   r
   r
   r   test_karate_club_graph_cutsetW   s   
z.TestGomoryHuTree.test_karate_club_graph_cutsetc                 C   s~   t  }|d tD ]1}t j|d|d}t |sJ t|dD ]\}}| |||\}}t j|||dd|ks;J q!qd S )N)	)r   r   r   )r   r-      )r   r-   r   )r      rF   )r      r-   )r-   rG   rG   )rF   rG   r   )rF         )rG   rH   r-   r	   )r,   r7   r-   )r,   )	r   Graphadd_weighted_edges_fromr8   r0   r1   r   r   r2   r9   r
   r
   r   test_wikipedia_examplea   s   z'TestGomoryHuTree.test_wikipedia_examplec                 C   D   t tj t }t|}W d    d S 1 sw   Y  d S r   )pytestraisesr   NetworkXNotImplementedDiGraphr0   r   r   r   r
   r
   r   test_directed_raisesx      "z%TestGomoryHuTree.test_directed_raisesc                 C   rM   r   )rN   rO   r   NetworkXErrorempty_graphr0   rR   r
   r
   r   test_empty_raises}   rT   z"TestGomoryHuTree.test_empty_raisesN)__name__
__module____qualname__r   r+   r4   r:   r=   r?   rN   markslowrA   rD   rL   rS   rW   r
   r
   r
   r   r      s    		





r   )	itertoolsr   rN   networkxr   networkx.algorithms.flowr   r   r   r   r   r8   r   r
   r
   r
   r   <module>   s    		