Fer
Fer

Reputation: 3

Is there a way to modify the isdag matlab function in order for it to ignore cycles of length zero?

recently I've been tasked with programming an algorithm that optimizes job-shop scheduling problems and I'm following an approach which uses directed graphs for this. In these directed graphs nodes represent events and edges represent time precedence constraints between events, i.e. time inequalities. So, for example, 2 consecutive nodes A & B separated by a directed edge of length 2 which goes from A to B would represent the inequality tB-tA>=2. It follows that equality would be represented by 2 directed edges of opposite directions, one with positive length and the other one with negative length. Thus, we end up with a graph which has some cycles of length zero.

Matlab has a function called isdag which returns true if a directed graph has no cycles and false otherwise; is there a way to modify this function in order for it to ignore the cycles of length zero? If not, has anyone got any idea on how to program this? Thanks in advance!!

I also tried this but it doesn't work. I've tried it with the adjacency matrix adjMatrix = [0, 10, 0; -9, 0, 5; 0, 0, 0] it should return true as it has a cycle between nodes 1 and 2 of length 10+(-9)=1, but it returns false,

function result = hasCycleWithPositiveWeight(adjMatrix)
n = size(adjMatrix,1);
visited = false(1,n);
path = zeros(1,n);
result = false;
for i = 1:n
    pathStart = 1;
    pathEnd = 1;
    path(pathEnd) = i;
    totalWeight = 0;
    while pathStart <= pathEnd
        node = path(pathStart);
        visited(node) = true;
        for j = 1:n
            if adjMatrix(node,j) > 0
                totalWeight = totalWeight + adjMatrix(node,j);
                if visited(j)
                    if j == i && totalWeight > 0
                        result = true;
                        return;
                    end
                else
                    pathEnd = pathEnd + 1;
                    path(pathEnd) = j;
                end
            end
        end
        pathStart = pathStart + 1;
        totalWeight = totalWeight - adjMatrix(node, path(max(pathStart-1,1)));
        visited(node) = false;
    end
end
end

Upvotes: 0

Views: 57

Answers (1)

rahnema1
rahnema1

Reputation: 15837

If you want to find the cycle between just two consecutive nodes use this:

a = [0, 10, 0; -9, 0, 5; 0, 0, 0]; 
b = a.'; 
And = (a & b);
Add = (a + b);
result = any( And .* Add, 'all');

It returns true if there is a cycle that its length isn't 0.

Explanation:

In the graph if the length between node 2 and 5 is 12 we set the element A(2 , 5) of the adjacency matrix to 12 and if the length between node 5 and 2 is -8 we set the element A(5, 2) of the adjacency matrix to 8. So there is a symmetry between nodes relationship. The matrix transpose changes the position of (2, 5) to (5, 2) and (5, 2) to (2, 5).

If we And a matrix with its transpose the result matrix shows that there is a cycle between two nodes if it is 1.
If we Add a matrix to its transpose the result matrix shows the sum of the pairwise lengths between nodes.
If we multiplyelement-wise the two matrices Add and And the result matrix shows the sum of the pairwise lengths between nodes but the sum of lengths of two nodes that don't form a cycle is set to 0.
Now the function any can be used to test if the matrix has a cycle that its length isn't 0.

Upvotes: 0

Related Questions