tranzitív redukciós algoritmus: pszeudokódokra?

szavazat
30

Én már keresem egy algoritmus, hogy végre egy tranzitív csökkentése a grafikonon, de nem járt sikerrel. Nincs semmi az én algoritmusok biblia (Bevezetés az algoritmusok által Cormen és társai), és bár láttam rengeteg tranzitív lezárása pszeudokódokra, nem voltam képes lenyomozni semmit csökkentését. A legközelebbi Megvan az, hogy van egy a „Algorithmische Graphentheorie” Volker Turau (ISBN: 978-3-486-59057-9), de sajnos nem férnek hozzá ezt a könyvet! Wikipédia haszontalan és a Google még felbukkan semmit. : ^ (

Tudja valaki, hogy egy algoritmus végrehajtására tranzitív csökkentés?

A kérdést 06/11/2009 23:33
a forrás felhasználó
Más nyelveken...                            


7 válasz

szavazat
3

A Wikipedia szócikk a tranzitív csökkentés pontot végrehajtási belül GraphViz (ami nyílt forráskódú). Nem pontosan pszeudokódokra, de talán valahol kezdeni?

LEDA tartalmaz tranzitív redukciós algoritmus . Nem kell egy példányt a LÉDA könyv már, és ez a funkció volna ki, ha a könyv megjelent. De ha van benne, akkor lesz egy jó leírást az algoritmust.

Google rámutat egy olyan algoritmust , hogy valaki azt javasolta felvétele a kiemelés. Nem próbáltam olvasni, ezért talán nem megfelelő?

Továbbá, ez lehet érdemes egy pillantást.

Válaszolt 07/11/2009 16:42
a forrás felhasználó

szavazat
7

Az alapvető lényege a tranzitív zajcsökkentő algoritmus I használt


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

A tranzitív lezárása algoritmus használható ugyanabban a script nagyon hasonló, de az utolsó sor


         add edge xz if edges xy and yz OR edge xz exist
Válaszolt 03/03/2010 15:49
a forrás felhasználó

szavazat
3

Az algoritmus a „girlwithglasses” elfelejti, hogy a felesleges szélén is befogják a láncot a három élek. Kijavításához, Compute Q = R x R +, ahol R + jelentése a tranzitív lezárás majd törli az összes élek R, hogy megjelenik a Q. Lásd még a Wikipedia cikket.

Válaszolt 08/12/2010 20:42
a forrás felhasználó

szavazat
13

Harry Hsu. "Egy algoritmust találni minimális ekvivalens grafikon egy digráf.", Journal of the ACM, 22 (1): 11-16, 1975. január Az egyszerű köbös alábbi algoritmus (egy N * N útvonal mátrix) is elegendő a DAG-ok, de Hsu általánosítja azt gyűrűs grafikonok.

// 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;
Válaszolt 15/07/2011 03:47
a forrás felhasználó

szavazat
1

Mélységi algoritmus pszeudo-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)

Az algoritmus az optimális szint alatt, de foglalkozik a multi-edge-span probléma az előző megoldásokat. Az eredmények nagyon hasonló ahhoz, amit Tred származó Graphviz termel.

Válaszolt 28/06/2012 03:04
a forrás felhasználó

szavazat
3

Ennek alapján a referencia által nyújtott Alan Donovan, amely azt mondja, akkor használja az útvonal mátrix (amely 1, ha van egy út a i csomóponttól j) helyett a szomszédsági mátrix (amely 1 Csak ha van egy él a i csomóponttól j).

Néhány példa python kód követi alul látható a különbség a megoldások

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

output:

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
Válaszolt 03/05/2013 12:16
a forrás felhasználó

szavazat
0

portolták java / jgrapht, a python mintát ezen az oldalon honnan @Michael Clerx:

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

egységteszt :

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

teszt kimeneti

[[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
Válaszolt 25/07/2015 14:31
a forrás felhasználó

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