Algoritmo de reducción transitiva: pseudocódigo?

votos
30

He estado buscando un algoritmo para realizar una reducción transitiva en un gráfico, pero sin éxito. No hay nada en mis algoritmos bíblicos (Introducción a los algoritmos por Cormen et al) y aunque he visto muchos pseudocódigos de cierre transitivo, no he podido encontrar nada para una reducción. Lo más cercano que tengo es que hay uno en Algorithmische Graphentheorie de Volker Turau (ISBN: 978-3-486-59057-9), ¡pero desafortunadamente no tengo acceso a este libro! Wikipedia no es útil y Google aún no ha presentado nada. : ^ (

¿Alguien sabe de un algoritmo para realizar una reducción transitiva?

Publicado el 06/11/2009 a las 23:33
fuente por usuario
En otros idiomas...                            


7 respuestas

votos
3

El artículo de Wikipedia sobre reducción transitiva apunta a una implementación dentro de GraphViz (que es de código abierto). No es exactamente un pseudocódigo, pero tal vez sea un lugar para comenzar.

LEDA incluye un algoritmo de reducción transitiva . Ya no tengo una copia del libro LEDA , y esta función podría haberse agregado después de que se publicó el libro. Pero si está ahí, habrá una buena descripción del algoritmo.

Google apunta a un algoritmo que alguien sugirió para su inclusión en Boost. No intenté leerlo, ¿entonces quizás no sea correcto?

Además, esto podría valer la pena mirar.

Respondida el 07/11/2009 a las 16:42
fuente por usuario

votos
7

La esencia básica del algoritmo de reducción transitiva que utiliza es


foreach x in graph.vertices
   foreach y in graph.vertices
      foreach z in graph.vertices
         delete edge xz if edges xy and yz exist

El algoritmo de cierre transitivo que utilicé en el mismo guión es muy similar, pero la última línea es


         add edge xz if edges xy and yz OR edge xz exist
Respondida el 03/03/2010 a las 15:49
fuente por usuario

votos
3

El algoritmo de "girlwithglasses" olvida que un borde redundante podría abarcar una cadena de tres bordes. Para corregir, Q compute = R x R + en donde R + es el cierre transitivo y luego eliminar todos los bordes de R que aparecen en Q. Véase también el artículo de Wikipedia.

Respondida el 08/12/2010 a las 20:42
fuente por usuario

votos
13

Ver Harry Hsu. "Un algoritmo para encontrar un gráfico equivalente mínimo de un dígrafo.", Revista de la ACM, 22 (1): 11-16, enero de 1975. El algoritmo simple cúbico a continuación (usando una matriz de trayectoria x N N) es suficiente para los DAG, pero Hsu generaliza a gráficos cíclicos.

// reflexive reduction
for (int i = 0; i < N; ++i)
  m[i][i] = false;

// transitive reduction
for (int j = 0; j < N; ++j)
  for (int i = 0; i < N; ++i)
    if (m[i][j])
      for (int k = 0; k < N; ++k)
        if (m[j][k])
          m[i][k] = false;
Respondida el 15/07/2011 a las 03:47
fuente por usuario

votos
1

Profundidad-primer algoritmo en pseudo-Python:

for vertex0 in vertices:
    done = set()
    for child in vertex0.children:
        df(edges, vertex0, child, done)

df = function(edges, vertex0, child0, done)
    if child0 in done:
        return
    for child in child0.children:
        edge.discard((vertex0, child))
        df(edges, vertex0, child, done)
    done.add(child0)

El algoritmo es sub-óptimo, pero se ocupa del problema de múltiples bordes útil de las soluciones anteriores. Los resultados son muy similares a lo que tred de graphviz produce.

Respondida el 28/06/2012 a las 03:04
fuente por usuario

votos
3

Sobre la base de la referencia proporcionada por Alan Donovan, que dice que debe usar la matriz de ruta (que tiene un 1 si existe un camino desde el nodo i al nodo j) en lugar de la matriz de adyacencia (que tiene un 1 sólo si hay un borde desde el nodo i al nodo j).

Algunos ejemplos de código Python sigue a continuación para mostrar las diferencias entre las soluciones

def prima(m, title=None):
    """ Prints a matrix to the terminal """
    if title:
        print title
    for row in m:
        print ', '.join([str(x) for x in row])
    print ''

def path(m):
    """ Returns a path matrix """
    p = [list(row) for row in m]
    n = len(p)
    for i in xrange(0, n):
        for j in xrange(0, n):
            if i == j:
                continue
            if p[j][i]:
                for k in xrange(0, n):
                    if p[j][k] == 0:
                        p[j][k] = p[i][k]
    return p

def hsu(m):
    """ Transforms a given directed acyclic graph into its minimal equivalent """
    n = len(m)
    for j in xrange(n):
        for i in xrange(n):
            if m[i][j]:
                for k in xrange(n):
                    if m[j][k]:
                        m[i][k] = 0

m = [   [0, 1, 1, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1],
        [0, 0, 0, 0, 1],
        [0, 1, 0, 0, 0]]

prima(m, 'Original matrix')
hsu(m)
prima(m, 'After Hsu')

p = path(m)
prima(p, 'Path matrix')
hsu(p)
prima(p, 'After Hsu')

Salida:

Adjacency matrix
0, 1, 1, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 1, 1
0, 0, 0, 0, 1
0, 1, 0, 0, 0

After Hsu
0, 1, 1, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 1, 0
0, 0, 0, 0, 1
0, 1, 0, 0, 0

Path matrix
0, 1, 1, 1, 1
0, 0, 0, 0, 0
0, 1, 0, 1, 1
0, 1, 0, 0, 1
0, 1, 0, 0, 0

After Hsu
0, 0, 1, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 1, 0
0, 0, 0, 0, 1
0, 1, 0, 0, 0
Respondida el 03/05/2013 a las 12:16
fuente por usuario

votos
0

portado a Java / JGraphT, la muestra pitón en esta página de Clerx @ Michael:

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jgrapht.DirectedGraph;

public class TransitiveReduction<V, E> {

    final private List<V> vertices;
    final private int [][] pathMatrix;

    private final DirectedGraph<V, E> graph;

    public TransitiveReduction(DirectedGraph<V, E> graph) {
        super();
        this.graph = graph;
        this.vertices = new ArrayList<V>(graph.vertexSet());
        int n = vertices.size();
        int[][] original = new int[n][n];

        // initialize matrix with zeros
        // --> 0 is the default value for int arrays

        // initialize matrix with edges
        Set<E> edges = graph.edgeSet();
        for (E edge : edges) {
            V v1 = graph.getEdgeSource(edge);
            V v2 = graph.getEdgeTarget(edge);

            int v_1 = vertices.indexOf(v1);
            int v_2 = vertices.indexOf(v2);

            original[v_1][v_2] = 1;
        }

        this.pathMatrix = original;
        transformToPathMatrix(this.pathMatrix);
    }

    // (package visible for unit testing)
    static void transformToPathMatrix(int[][] matrix) {
        // compute path matrix 
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) { 
                if (i == j) {
                    continue;
                }
                if (matrix[j][i] > 0 ){
                    for (int k = 0; k < matrix.length; k++) {
                        if (matrix[j][k] == 0) {
                            matrix[j][k] = matrix[i][k];
                        }
                    }
                }
            }
        }
    }

    // (package visible for unit testing)
    static void transitiveReduction(int[][] pathMatrix) {
        // transitively reduce
        for (int j = 0; j < pathMatrix.length; j++) { 
            for (int i = 0; i < pathMatrix.length; i++) {
                if (pathMatrix[i][j] > 0){
                    for (int k = 0; k < pathMatrix.length; k++) {
                        if (pathMatrix[j][k] > 0) {
                            pathMatrix[i][k] = 0;
                        }
                    }
                }
            }
        }
    }

    public void reduce() {

        int n = pathMatrix.length;
        int[][] transitivelyReducedMatrix = new int[n][n];
        System.arraycopy(pathMatrix, 0, transitivelyReducedMatrix, 0, pathMatrix.length);
        transitiveReduction(transitivelyReducedMatrix);

        for (int i = 0; i <n; i++) {
            for (int j = 0; j < n; j++) { 
                if (transitivelyReducedMatrix[i][j] == 0) {
                    // System.out.println("removing "+vertices.get(i)+" -> "+vertices.get(j));
                    graph.removeEdge(graph.getEdge(vertices.get(i), vertices.get(j)));
                }
            }
        }
    }
}

prueba de unidad :

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;

public class TransitiveReductionTest {

    @Test
    public void test() {

        int[][] matrix = new int[][] {
            {0, 1, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 1},
            {0, 0, 0, 0, 1},
            {0, 1, 0, 0, 0}
        };

        int[][] expected_path_matrix = new int[][] {
            {0, 1, 1, 1, 1},
            {0, 0, 0, 0, 0},
            {0, 1, 0, 1, 1},
            {0, 1, 0, 0, 1},
            {0, 1, 0, 0, 0}
        };

        int[][] expected_transitively_reduced_matrix = new int[][] {
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0},
            {0, 0, 0, 0, 1},
            {0, 1, 0, 0, 0}
        };

        System.out.println(Arrays.deepToString(matrix) + " original matrix");

        int n = matrix.length;

        // calc path matrix
        int[][] path_matrix = new int[n][n];
        {
            System.arraycopy(matrix, 0, path_matrix, 0, matrix.length);

            TransitiveReduction.transformToPathMatrix(path_matrix);
            System.out.println(Arrays.deepToString(path_matrix) + " path matrix");
            Assert.assertArrayEquals(expected_path_matrix, path_matrix);
        }

        // calc transitive reduction
        {
            int[][] transitively_reduced_matrix = new int[n][n];
            System.arraycopy(path_matrix, 0, transitively_reduced_matrix, 0, matrix.length);

            TransitiveReduction.transitiveReduction(transitively_reduced_matrix);
            System.out.println(Arrays.deepToString(transitively_reduced_matrix) + " transitive reduction");
            Assert.assertArrayEquals(expected_transitively_reduced_matrix, transitively_reduced_matrix);
        }
    }
}

ouput prueba

[[0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0]] original matrix
[[0, 1, 1, 1, 1], [0, 0, 0, 0, 0], [0, 1, 0, 1, 1], [0, 1, 0, 0, 1], [0, 1, 0, 0, 0]] path matrix
[[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0]] transitive reduction
Respondida el 25/07/2015 a las 14:31
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more