transitive algorithme de réduction: pseudo-code?

voix
30

Je cherchais un algorithme pour effectuer une réduction transitive sur un graphique, mais sans succès. Il n'y a rien dans mes algorithmes bibliques (Introduction aux algorithmes par Cormen et al) et pendant que j'ai vu beaucoup de pseudo-code de fermeture transitive, je ne l'ai pas été en mesure de traquer quoi que ce soit pour une réduction. Le plus proche que j'ai est qu'il ya un dans « Algorithmische Graphentheorie » par Volker TURAU (ISBN: 978-3-486-59057-9), mais malheureusement, je n'ai pas accès à ce livre! Wikipedia est inutile et Google est encore à tourner quoi que ce soit. : ^ (

Quelqu'un sait-il d'un algorithme pour effectuer une réduction transitive?

Créé 06/11/2009 à 23:33
source utilisateur
Dans d'autres langues...                            


7 réponses

voix
3

L' article de Wikipédia sur les points de réduction transitive à une mise en œuvre au sein GraphViz (qui est open source). Pas exactement pseudocode, mais peut - être un endroit pour commencer?

LEDA comprend un algorithme de réduction transitive . Je n'ai pas une copie du livre LEDA plus, et cette fonction peut - être été ajoutée après la publication du livre. Mais si elle est là - dedans, alors il y aura une bonne description de l'algorithme.

Google souligne un algorithme que quelqu'un a suggéré d'inclure dans Boost. Je n'ai pas essayé de le lire, alors peut - être pas?

En outre, cela pourrait être intéressant de regarder.

Créé 07/11/2009 à 16:42
source utilisateur

voix
7

L'essentiel de base de l'algorithme de réduction transitive j'est


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

L'algorithme de fermeture transitive j'est très similaire dans le même scénario, mais la dernière ligne est


         add edge xz if edges xy and yz OR edge xz exist
Créé 03/03/2010 à 15:49
source utilisateur

voix
3

L'algorithme de « girlwithglasses » oublie qu'un bord redondante peut couvrir une chaîne de trois bords. Pour corriger, calculer Q = R x R + où R + est la fermeture transitive et puis supprimez tous les bords de R qui apparaissent dans Q. Voir aussi l'article de Wikipedia.

Créé 08/12/2010 à 20:42
source utilisateur

voix
13

Voir Harry Hsu. "Un algorithme pour trouver un graphique équivalent minimal d'un graphe orienté.", Journal of the ACM, 22 (1): 11-16, Janvier 1975. La simples algorithme cubique ci-dessous (en utilisant une matrice de trajectoire de N x N) suffit pour les DAG, mais Hsu il généralise aux graphes cycliques.

// 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;
Créé 15/07/2011 à 03:47
source utilisateur

voix
1

Profondeur premier algorithme 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)

L'algorithme est sous-optimale, mais traite du problème multi-bord durée des solutions précédentes. Les résultats sont très semblables à ce que tred de graphviz produit.

Créé 28/06/2012 à 03:04
source utilisateur

voix
3

Sur la base de la référence fournie par Alan Donovan, qui dit que vous devez utiliser la matrice de chemin (qui a 1 s'il y a un chemin à partir du noeud i au noeud j) au lieu de la matrice de contiguïté (qui a un 1 seulement s'il y a un bord du noeud i au noeud j).

Certains code python exemple suit ci-dessous pour montrer les différences entre les solutions

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')

Sortie:

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
Créé 03/05/2013 à 12:16
source utilisateur

voix
0

porté à java / JGraphT, l'échantillon de python sur cette page 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)));
                }
            }
        }
    }
}

Test de l'unité :

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);
        }
    }
}

Test ouput

[[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
Créé 25/07/2015 à 14:31
source utilisateur

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