# Path: lfd/rapprentice/knot_identification.py
# def intersect_segs(ps_n2, q_22):
#     def cross(a_n2, b_n2):
# def rope_has_intersections(ctl_pts):
# def compute_dt_code(ctl_pts, plotting=False):
# def _dt_code_to_knot(dt_code):
# def dt_code_to_knot(dt_code):
#     def dt_code_to_knot_wrapper(q, x):
# def identify_knot(ctl_pts):
# def main():
#     TIMEOUT = 1


import networkx as nx, numpy as np, scipy.spatial.distance as ssd, scipy.interpolate as si
import itertools
    import matplotlib.pyplot as plt; plt.ion()
from collections import deque
from numpy.random import rand
from lfd.rapprentice import knot_identification
        from mayavi import mlab
        from mayavi import mlab
    from mayavi import mlab
                if neilength is not None:
                    lengths_paths.append(
                        (thislength+G.edge[opp][nei]["weight"]+neilength,thispath+neipath))

    if len(lengths_paths) > 0:
        return max(lengths_paths)    
    else: 
        return None,None
    
def longest_path_from(G,start,used):  
    opp = start - 1 if start%2==1 else start+1
    thislength, thispath = (G.edge[start][opp]["weight"],[start,opp])
    lengths_paths = [(thislength, thispath)]
    newused = used.union([start,opp])
    for nei in G.neighbors_iter(opp):
        if nei not in newused:
            neilength,neipath = longest_path_from(G,nei,newused)
            lengths_paths.append(
                (thislength+G.edge[opp][nei]["weight"]+neilength,thispath+neipath))
    return max(lengths_paths)    

def longest_path_through_segment_graph(G):
    best_score = -np.inf
    best_nodes = []

    for i_start in G.nodes():
        (score,nodes) = longest_path_from(G, i_start, set([]))
        if score > best_score:
            best_score = score
            best_nodes = nodes

# Path: lfd/rapprentice/knot_identification.py
# def intersect_segs(ps_n2, q_22):
#     def cross(a_n2, b_n2):
# def rope_has_intersections(ctl_pts):
# def compute_dt_code(ctl_pts, plotting=False):
# def _dt_code_to_knot(dt_code):
# def dt_code_to_knot(dt_code):
#     def dt_code_to_knot_wrapper(q, x):
# def identify_knot(ctl_pts):
# def main():
#     TIMEOUT = 1


# Path: lfd/rapprentice/rope_initialization.py
import networkx as nx, numpy as np, scipy.spatial.distance as ssd, scipy.interpolate as si
import itertools
    import matplotlib.pyplot as plt; plt.ion()
from collections import deque
from numpy.random import rand
from lfd.rapprentice import knot_identification
        from mayavi import mlab
        from mayavi import mlab
    from mayavi import mlab

                if neilength is not None:
                    lengths_paths.append(
                        (thislength+G.edge[opp][nei]["weight"]+neilength,thispath+neipath))

    if len(lengths_paths) > 0:
        return max(lengths_paths)    
    else: 
        return None,None
    
def longest_path_from(G,start,used):  
    opp = start - 1 if start%2==1 else start+1
    thislength, thispath = (G.edge[start][opp]["weight"],[start,opp])
    lengths_paths = [(thislength, thispath)]
    newused = used.union([start,opp])
    for nei in G.neighbors_iter(opp):
        if nei not in newused:
            neilength,neipath = longest_path_from(G,nei,newused)
            lengths_paths.append(
                (thislength+G.edge[opp][nei]["weight"]+neilength,thispath+neipath))
    return max(lengths_paths)    

def longest_path_through_segment_graph(G):
    best_score = -np.inf
    best_nodes = []

    for i_start in G.nodes():
        (score,nodes) = longest_path_from(G, i_start, set([]))
        if score > best_score:
            best_score = score
            best_nodes = nodes

    return (best_score, best_nodes)