def minimumCrossingOrdering(refAdjList, queryAdjList):

    #get connected components cluster
    connectedRefs = graphUtil.findConnectedComps(refAdjList, queryAdjList)

    #use connected comps to decide the initial order
    refNodes, queryNodes = getOrderedLists(connectedRefs, refAdjList,\
                                               queryAdjList)

    
    #print 'refNodes b4 cross min: ', refNodes
    #print 'queryNodes b4 cross min: ', queryNodes
    
    #apply barycenter heuristices alternately on ref then query until their
    #position remains same in order
    positionChanged = True
    counter = 0

    #initialize query Rank Dict
    queryRankDict = {}

    #initialize ref Rank Dict
    refRankDict = {}

    #strikes to maintain atleast one more trial on failure
    strikeCount = 0
    
    while strikeCount < 2:
        positionChanged = False
        if counter%2 == 0:
            #choose ref nodes to play around
            queryRankDict = prepareRankDict(queryNodes, queryRankDict)
            refNodes, positionChanged = applyBarycenterHeuristics(refNodes,\
                                                                      refAdjList,\
                                                                      queryRankDict)
        else:
            #choose query nodes to play around
            refRankDict = prepareRankDict(refNodes, refRankDict)
            queryNodes, positionChanged = applyBarycenterHeuristics(queryNodes,\
                                                                        queryAdjList,\
                                                                        refRankDict)

        if not positionChanged:
            strikeCount += 1
        elif positionChanged:
            strikeCount = 0
            
        counter += 1
    return refNodes, queryNodes
def plotCrossMinimizedOrdering(scaffMap):

    #try to plot before removing 1-1 or 1-many mappings
    refAdjacencyList, queryAdjacencyList = prepareAdjacencyLists(scaffMap)
    #refList = refAdjacencyList.keys()
    #refList.sort()
    #queryList = queryAdjacencyList.keys()
    #queryList.sort()
    #scaffMapPlotter.plotFromLists(refList,\
    #                                  queryList,\
    #                                  refAdjacencyList)
    #remove contained in itself one-to-many mapping before
    #reordering
    removeOneToManyMapping(refAdjacencyList, queryAdjacencyList)
    removeOneToManyMapping(queryAdjacencyList, refAdjacencyList)

    #refList = refAdjacencyList.keys()
    #refList.sort()
    #queryList = queryAdjacencyList.keys()
    #queryList.sort()
    #print refList, queryList
    #scaffMapPlotter.plotFromLists(refList,\
    #                                  queryList,\
    #                                  refAdjacencyList)

    #get connected clusters
    connectedRefComps = graphUtil.findConnectedComps(refAdjacencyList,\
                                                         queryAdjacencyList)

    #order these clusters and minimize intersection b/w them
    orderedClusters = map(getOrderedClusterWNumIntersection,\
                              [(comp, refAdjacencyList, queryAdjacencyList)\
                                   for comp in connectedRefComps])

    totalIntersections = 0

    intersectingClusters = []
    nonIntersectingClusters = []
    
    #from ordered clusters separate clusters with intersections
    for orderedCluster in orderedClusters:
        numIntersect = orderedCluster[2]
        totalIntersections += numIntersect
        if numIntersect != 0:
            intersectingClusters.append(orderedCluster)
        else:
            nonIntersectingClusters.append(orderedCluster)

    print 'total intersections: ', totalIntersections

    """        
    print 'refOrderList: ', refOrderList, '\n'
    print 'queryOrderList: ', queryOrderList, '\n'
    print 'refAdjacencyList: ', refAdjacencyList, '\n'
    print 'queryAdjacencyList: ', queryAdjacencyList, '\n'
    """
    """displayMatchingRegion(scaffMap, refOrderList, refAdjacencyList,\
                              queryOrderList, queryAdjacencyList,\
                              False)"""

    #plot only these interesectiong refs cluster in the order, 
    #as reported after cross-minimization
    #print refOrderList, queryOrderList
    #scaffMapPlotter.plotFromLists(refOrderList, queryOrderList,\
    #                                  refAdjacencyList, 0, False)

    #plot non intersecting clusters
    #as reported after cross-minimization
    #scaffMapPlotter.plotFromLists(refNotIntersOrderList, queryNotIntersOrderList,\
    #                                  refAdjacencyList, 0, False)

    
    #write the ordered graph intersect file
    with open('orderedScaffs.gexf', 'w') as graphFile:
        xmlGraphStr = GEXFWriter.getGexfXMLStringFromCluster(refAdjacencyList,\
                                                      intersectingClusters)
        graphFile.write(xmlGraphStr)

    #write the ordered graph non-intersect file
    with open('orderedScaffsNonInters.gexf', 'w') as graphFile:
        xmlGraphStr = GEXFWriter.getGexfXMLStringFromCluster(refAdjacencyList,\
                                                      nonIntersectingClusters)
        graphFile.write(xmlGraphStr)