示例#1
0
def KosarajuSCC(graph):
    """
    Implementa el algoritmo de Kosaraju
    para encontrar los componentes conectados
    de un grafo dirigido
    Args:
        graph: El grafo a examinar
    Returns:
        Una estructura con los componentes
        conectados
    Raises:
        Exception
    """
    try:
        scc = {
            'idscc': None,
            'marked': None,
            'grmarked': None,
            'components': 0
        }

        scc['idscc'] = map.newMap(g.numVertex(graph),
                                  maptype='PROBING',
                                  comparefunction=graph['comparefunction'])

        scc['marked'] = map.newMap(g.numVertex(graph),
                                   maptype='PROBING',
                                   comparefunction=graph['comparefunction'])
        scc['grmarked'] = map.newMap(g.numVertex(graph),
                                     maptype='PROBING',
                                     comparefunction=graph['comparefunction'])

        # Se calcula el grafo reverso de graph
        greverse = reverseGraph(graph)

        # Se calcula el DFO del reverso de graph
        dforeverse = dfo.DepthFirstOrder(greverse)
        grevrevpost = dforeverse['reversepost']

        # Se recorre el grafo en el orden dado por reversepost (G-reverso)
        scc['components'] = 1
        while (not stack.isEmpty(grevrevpost)):
            vert = stack.pop(grevrevpost)
            if not map.contains(scc['marked'], vert):
                sccCount(graph, scc, vert)
                scc['components'] += 1
        return scc
    except Exception as exp:
        error.reraise(exp, 'scc:Kosaraju')
示例#2
0
def BreadhtFisrtSearch(graph, source):
    """
    Genera un recorrido BFS sobre el grafo graph
    Args:
        graph:  El grafo a recorrer
        source: Vertice de inicio del recorrido.
    Returns:
        Una estructura para determinar los vertices
        conectados a source
    Raises:
        Exception
    """
    try:
        search = {'source': source, 'visited': None}

        search['visited'] = map.newMap(
            numelements=g.numVertex(graph),
            maptype='PROBING',
            comparefunction=graph['comparefunction'])
        map.put(search['visited'], source, {
            'marked': True,
            'edgeTo': None,
            'distTo': 0
        })
        bfsVertex(search, graph, source)
        return search
    except Exception as exp:
        error.reraise(exp, 'bfs:BFS')
示例#3
0
def reverseGraph(graph):
    """
        Retornar el reverso del grafo graph
    """
    try:
        greverse = g.newGraph(size=g.numVertex(graph),
                              directed=True,
                              comparefunction=graph['comparefunction'])

        lstvert = g.vertices(graph)
        itervert = it.newIterator(lstvert)
        while it.hasNext(itervert):
            vert = it.next(itervert)
            g.insertVertex(greverse, vert)

        itervert = it.newIterator(lstvert)
        while it.hasNext(itervert):
            vert = it.next(itervert)
            lstadj = g.adjacents(graph, vert)
            iteradj = it.newIterator(lstadj)
            while it.hasNext(iteradj):
                adj = it.next(iteradj)
                g.addEdge(greverse, adj, vert)
        return greverse
    except Exception as exp:
        error.reraise(exp, 'scc:reverse')
def test_insertVertex(graph):
    g.insertVertex(graph, 'Bogota')
    g.insertVertex(graph, 'Yopal')
    g.insertVertex(graph, 'Cali')
    g.insertVertex(graph, 'Medellin')
    g.insertVertex(graph, 'Pasto')
    g.insertVertex(graph, 'Barranquilla')
    g.insertVertex(graph, 'Manizales')
    assert g.numVertex(graph) == 7
def initSearch(graph, source):
    """
    Inicializa la estructura de busqueda y deja
    todos los arcos en infinito.
    Se inserta en la cola indexada el vertice source
    Args:
        graph: El grafo a examinar
        source: El vertice fuente
    Returns:
        Estructura de busqueda inicializada
    Raises:
        Exception
    """
    try:
        search = {'source': source, 'visited': None, 'iminpq': None}

        search['visited'] = map.newMap(
            numelements=g.numVertex(graph),
            maptype='PROBING',
            comparefunction=graph['comparefunction'])
        vertices = g.vertices(graph)
        itvertices = it.newIterator(vertices)
        while (it.hasNext(itvertices)):
            vert = it.next(itvertices)
            map.put(search['visited'], vert, {
                'marked': False,
                'edgeTo': None,
                'distTo': math.inf
            })
        map.put(search['visited'], source, {
            'marked': True,
            'edgeTo': None,
            'distTo': 0
        })
        pq = iminpq.newIndexMinPQ(cmpfunction=graph['comparefunction'])

        search['iminpq'] = pq
        iminpq.insert(search['iminpq'], source, 0)
        return search
    except Exception as exp:
        error.reraise(exp, 'dks:init')
示例#6
0
def DepthFirstOrder(graph):
    try:
        search = {
            'marked': None,
            'pre': None,
            'post': None,
            'reversepost': None
        }
        search['pre'] = queue.newQueue()
        search['post'] = queue.newQueue()
        search['reversepost'] = stack.newStack()
        search['marked'] = map.newMap(numelements=g.numVertex(graph),
                                      maptype='PROBING',
                                      comparefunction=graph['comparefunction'])
        lstvert = g.vertices(graph)
        vertiterator = it.newIterator(lstvert)
        while it.hasNext(vertiterator):
            vertex = it.next(vertiterator)
            if not (map.contains(search['marked'], vertex)):
                dfsVertex(graph, search, vertex)
        return search
    except Exception as exp:
        error.reraise(exp, 'dfo:DFO')
def test_insertEdges(graph):
    assert g.numVertex(graph) == 7
    assert g.numEdges(graph) == 10