c# – Generating a random directed graph

I wrote a function that returns a directed graph as an adjacency matrix. The function requires two arguments: the amount of nodes and the amount of edges. At first, nodes are placed and instantly connected to the graph. When all nodes are added, random edges are created until all edges are placed.

Code:

public int(,) GenerateMatrix(int Nodes, int Edges)
    {
        if (Edges < Nodes - 1) throw new Exception("Too few edges");
        if (Edges > Nodes * (Nodes - 1)) throw new Exception("Too many edges");

        int(,) adjacencyMatrix = new int(Nodes, Nodes);

        // Gives every cell a value of zero
        for (int y = 0; y < Nodes; y++)
        {
            for (int x = 0; x < Nodes; x++)
            {
                adjacencyMatrix(x, y) = 0;
            }
        }

        int placedEdges = 0;

        for (int i = 1; i < Nodes; i++)
        {
            // produce edge between rnd(0, amountofnodes) to new node
            int fromVertex = random.Next(0, i);
            int weight = random.Next(1, 10);

            adjacencyMatrix(i, fromVertex) = weight;
            placedEdges++;
        }

        while (placedEdges < Edges)
        {
            int fromVertex = random.Next(0, Nodes);
            int weight = random.Next(1, 10);

            int targetVertex = random.Next(0, Nodes);
            while (targetVertex == fromVertex || adjacencyMatrix(targetVertex, fromVertex) != 0) //|| adjacencyMatrix(fromVertex, targetVertex) != 0)// tredje condition tar bort parallella kanter
            {
                fromVertex = random.Next(0, Nodes);
                targetVertex = random.Next(0, Nodes);
            }

            adjacencyMatrix(targetVertex, fromVertex) = weight;
            placedEdges++;
        }

        return adjacencyMatrix;
    }

algorithms – Approximate max weight path in directed graph

Context

This question is related to the fact one can’t use Bellman-Ford to find max weight paths in directed graphs with cycles. The reason is that giving a new graph $tilde{G}$ with negative weights (e.g. $tilde{w}_{ij} = – w_{ij}$) will result in cycles with negative sum, thus no minimum cost walk exists in $tilde{G}$.

General question

How well can we approximate the maximum weight path between $i$ and $j$ in $G$, using the shortest path algorithm on a graph $tilde{G}$ with transformed weights $tilde{w}_{ij}$ ?
In my case, one has $w_{ij}inmathbb{N}setminus{0}$. The transformed weights $tilde{w}_{ij}$ can be obtained using any function, but I assume a decreasing function $f$ is well adapted so that $w < w’ iff f(w) > f(w’)$.

My approach

I am currently trying to use $f(w) = dfrac{1}{w}$, in this context we have two important paths given a fixed path length $L$.

Denote a path $p = (i_0i_1, i_1i_2, i_2i_3, dots i_{L-1}i_L$) with weights $W = (w^{(1)}, w^{(2)}, w^{(3)}, dots, w^{(L)})$,

denote the maximum weight path of length $L$ by $p^*$ with weights $W^*$ achieving
$$ W^* = argmax_W{mathtt{Cost}(W)} = argmax_W sum_{k=1}^L w^{(k)}$$
and the path $p_*$ achieving minimum transformed cost with weights $W_*$
$$ W_* = argmin_W sum_{k=1}^L tilde{w}^{(k)} = argmin_W sum_{k=1}^L f(w^{(k)}) = argmin_W sum_{k=1}^L dfrac{1}{w^{(k)}}$$

Specific question

In this specific context, do we have the approximation $mathtt{Cost}(W^*) approx mathtt{Cost}(W_*)$ ?

Or is it “very wrong” to replace $W^*$ by $W_*$ ? What would be the distribution of the relative error defined as
$$E_r = dfrac{lvertmathtt{Cost}(W^*) – mathtt{Cost}(W_*)rvert}{mathtt{Cost}(W^*)}$$

Statistical analysis

I have tried a quick statistical analysis as follows:

  • Letting $Lin{3,4,5,6,7,8}$
  • sampling $w sim mathrm{Uniform}(1,w_{max})$
  • The value for $w_{max}$ was also picked from the set ${50, 100, 150, 200, 500}$
  • for each combination of $L,w_{max}$ I computed $10^5$ values of $W^*, W_*$ using $10^3$ candidate weights $W_i$

The results:

  • In $80.8%$ of cases we have $mathtt{Cost}(W^*) = mathtt{Cost}(W_*)$ and even better $W^*=W_*$
  • When computing the relative error $E_r$ one finds that $82.6%$ of error values are less than $0.001$, $89.7%$ less than $0.01$ and $99.9%$ less than $0.1$

The histogram for $E_r > 0$ is as follows

relative_error distribution

dag – Edges and in-degree in directed graphs

I wanted to know if I have a directed graph $Gamma$ with $V$ vertices and $E$ edges with some adjacency list representation what would be:

(1) the maximum in-degree of a vertex in $Gamma$?

(2) the time complexity required to find all edges $(u, v)$ in $Gamma$ for some given/arbitrary vertex $v$?

c# – Directed acyclic graph with distributed graph nodes

I’m interested of setting up calculations through an acyclic directed graph, whose calculation nodes shall be distributed across different processes/servers.

For a simple example, imagine I’m setting up a somewhat stupid graph to calculate “x + 2x”:

Example graph layout

A) Node which receives numeric input from some external source, and forwards that on to its dependents

B) Node which gets the value of its dependency, and multiplies it by 2

C) Node which gets values of two dependencies, and adds them together

interface INode
{
  double calculate();
}

class A : INode
{
  private double _value;

  public void ReceiveInput(double value) {
    _value = value;
    // Notify some graph runner that this node has changed, i.e.
    // calculate() shall be invoked on this node and its dependents
  }

  public double calculate() => _value;
}

class B : INode
{
  private INode _a;

  public double calculate() => 2.0 * _a.calculate();
}

class C {
  private INode _a;
  private INode _b;

  public double calculate() => _a.calculate() + _b.calculate();
}

I’m trying to come up with a clever mechanism of managing calculations in topological order here.

For example, I’d need to make sure that when calculates, B calculates before C does (and C “knows” the most recent values of A and B, when calculating).

Since all nodes would live on different servers, this may be a little annoying. I might need to replicate the graph layout on each of the 3 servers; where e.g. server C only runs the actual implementation of node C, and has proxy implementations of nodes A and B (which purely rely on messaging from the other nodes for their results).
At the same time, node A might trigger several times before node B or C even receive the message, so I may also need to version-stamp each calculation cycle and ensure proxy nodes receive updates in their expected sequence order.

It seems solvable, but I don’t want to re-invent the wheel here if I don’t need to.

I suspect my problem is one that has already been solved by some smarter person – however, I haven’t been able to find much in terms of practical implementations yet when searching for “distributed acyclic directed graph” and related terms.

Are there any commonly used/known approaches and algorithms for running a DAG with distributed nodes?

algorithms – Eliminate equal sub-paths of directed cyclic graph

Suppose we have a directed cyclic graph and a list of paths.
What is the shortest way (including parallelization) of eliminating all equal paths and paths that are sub-paths of others?
For example, lets suppose that the vertices are marked by letters from A to Z (for simplicity lets suppose we have only 26 vertices.
The paths are:
(A->A->B->D->E->G->K->L->A)
(B->D->E)
(A->A->A)
(K->L->A->M->N->C)
(M->N)

The output should be:
(A->A->B->D->E->G->K->L->A)
(A->A->A)
(K->L->A->M->N->C)

I thought to first sort all paths by length and start with the longest.
Holding a dictionary of vertices (letters), where the key is the letter and the value is a dictionary of lengths of paths that start from this letter, and the value is the paths, and if there isn’t a match of the current path with equal or bigger paths in the same letter, the path is saved in the results, and is inserted to the dictionary, each time removing the leading vertex. For examples —
We start with (A->A->B->D->E->G->K->L->A),
The dictionary is {A: {0, ()}, B: {0, ()} … Z: {0, ()}
Length of current path: 9
First vertex: A.
No paths that are 9-length that start with A, so we know that the path is unique.
Lets divide it, we should have:
{A: {9: (AABDEGKLA), 8: (ABDEGKLA)}, B: {7: (BDEGKLA)}, D: {6, (DEGKLA), E: {5, (EGKLA), G: {4, (GKLA), K: {3, (KLA)}, L: {2, (LA)}}
The next path (by length) is (K->L->A->M->N->C) :
No 6-lenght paths that start with K, so we know this is unique. Dividing it into:
{A: {9: (AABDEGKLA), 8: (ABDEGKLA), 4: (AMNC)}, B: {7: (BDEGKLA)}, D: {6, (DEGKLA), E: {5, (EGKLA), G: {4, (GKLA), K: {3, (KLA), 6: (KLAMNC)}, L: {2, (LA), 5: (LAMNC)}, M: {3: (MNC)}, N: {3:(MNC)}}
Next is (B->D->E), 3-length.
In B we have >= 3 path, so we compare — BDE is a sub-path of BDEGKLA, so it’s not unique and omitted.
Next is (A->A->A) – we have >= 3 paths in A, (AABDEGKLA), (ABDEGKLA), (AMNC), but AAA isn’t a sub-path of any of them so it’s unique and the dictionary is now:
{A: {9: (AABDEGKLA), 8: (ABDEGKLA), 4: (AMNC), 3: (AAA), 2: (AA)}, B: {7: (BDEGKLA)}, D: {6, (DEGKLA), E: {5, (EGKLA), G: {4, (GKLA), K: {3, (KLA), 6: (KLAMNC)}, L: {2, (LA), 5: (LAMNC)}, M: {3: (MNC)}, N: {3:(MNC)}}

I wonder if there is a more efficient way do do that.

Thank you.

algorithms – LCA for directed graph with cycles

I am trying to find the lowest common ancestor of 2 nodes in a graph with more than 250000 nodes and million edges. The graph is directed and has many cycles. Each node may or may not have an edge comping back to it from the child node. I tried finding an Euler tour using Hierholzer’s algorithm for directed graphs. But gave up on it. Most LCA algorithms in the public domain are for binary trees, DAGs, n-ary trees. I did not come across an algorithm for LCA for a large directed graph with cycles.
I am interested in the approach to take to solve this problem.

directed graphs – length of longest representative “uu” in suffix automaton

Trying to find the length of the longest representative of an equivalence class in suffix automaton, such that it has the form:
${vin{}Sigma|v$ is the longest representative $wedge$ $(exists{}uinSigma*)(v=uu)}$|. In other words how can we find the length of the longest node $v$, such that $overset{overset{w}longleftarrow{}}{v}=v$, where $overset{overset{w}longleftarrow{}}{v}$ is just the notation for the longest infix $u$ of $w$, i.e. $big($ i.e. $uin{}Inf(w)big)$ , such that $u_{equiv{Suff(w)}}v$ ($u$ is in one class of equivalence with $v$: $end_pos(u)=end_pos(v)big)$.

I know that the charactarization of a representative, i.e. a state in the suffix automata for $w$ can be $v$, either $v$ is a prefix of the word $w$, OR there exists $a,binSigma,ane{}b$ such that $av,bvin{}Inf(w)$.

Knowing that fact, I tried to prove that if there is a state with represents $Inf(w)$ of the form $uu$, it must be either created only from one letter, or it must be the whole word $w$, but I failed, because that assumption was wrong: the suffix automaton for word $ababc$ has a representative “abab”. Does anyone have any ideas how to find that longest length? (Consider we have build or that we can build a suffix automaton for the word $w$ and we have the suffix states, suffix transitions and etc… but that may be a wrong direction, I don’t know, because maybe we can find the answer for the task while building the SA). Help?

A directed graph is semi-connected if the corresponding undirected graph is connected

The following is a problem in CLRS:

A directed graph $G = (V, E)$ is semiconnected if, for all pairs of vertices $u,v in V$,
we have $u$ reachable from $v$ or $v$ reachable from $u$. Give an efficient algorithm to determine whether
or not $G$ is semiconnected. Prove that your algorithm is correct, and analyze its
running time.

I know there exist correct solutions to this using topological sorting and strongly-connected components, but I had a different approach.

My approach:

Construct a new undirected graph $G’$, having an edge ${u,v}$ whenever $(u,v) in E$ or $(v,u) in E$.
The graph $G$ is semiconnected if $G’$ is connected.
Will this method always be correct?

directed acyclic graphs – How can I encode a rule tree of multiple depths in Dhall?

I’m trying to move some error prone YAML into Dhall to make some system configuration simpler. I have a tree that looks like:

composite:
  condition: And
  rules:
    - composite:
        condition: And
        rules:
          - leaf:
              static: true
          - leaf:
              exists: some-property-to-lookup
    - composite:
        condition: Or
        rules:
          - composite:
              condition: And
              rules:
                - leaf:
                    static: true
                - leaf:
                    exists: some-property-to-lookup         

I’m trying to encode this in Dhall and I can’t seem to provide the compiler the right information. My latest try looks like:

let Field
    : Type
    = < B : { static : Bool } | S : { exists : Text } >

let Condition
    : Type
    = < And | Or | Not >

let Node
    : Type
    = ∀(Node : Type) →
      ∀(Leaf : { leaf : Field }) →
      ∀(Branch : { composite : { condition : Condition, rules : List Node } }) →
        Node

let example
    : Node
    = ∀(Node : Type) →
      ∀(Leaf : { leaf : Field }) →
      ∀(Branch : { composite : { condition : Condition, rules : List Node } }) →
        Branch
          { composite =
            { condition = Condition.And
            , rules =
              ( Branch
                  { composite =
                    { condition = Condition.And
                    , rules = ( Leaf { leaf = Field.S { exists = "hi" } } )
                    }
                  }
              , Branch
                  { composite =
                    { condition = Condition.Or
                    , rules = ( Leaf { leaf = Field.S { static = true } } )
                    }
                  }
              , Branch
                  { composite =
                    { condition = Condition.And
                    , rules =
                      ( Branch
                          { composite =
                            { condition = Condition.And
                            , rules =
                              ( Leaf { leaf = Field.S { exists = "hi" } } )
                            }
                          }
                      , Branch
                          { composite =
                            { condition = Condition.Or
                            , rules =
                              ( Leaf { leaf = Field.S { static = true } } )
                            }
                          }
                      )
                    }
                  }
              )
            }
          }

in  example

But I get Error: Not a function. Any pointers would be appreciated.

I also tried w/ the Graph module, but I can’t seem to convert that to YAML directly.

depth first search – Detecting a cycle in a directed graph via an adjacency list implementation and a recursive DFS

Here’s my attempt. I iterate thru each vertex in the graph and do a DFS to see if I reach back on a vertex I already visited in this vertex’s iteration. It seems to work but I am not satisfied with how I short-circuit my code when it found a cycle using if clauses, could not think of a better way to do that.

    public boolean isCyclic(Map<T, List<T>> adjacencyList) {

        for (T node: adjacencyList.keySet()) {
            Set<T> visited = new HashSet<>();
            visited.add(node);
            if (isCyclic(visited, node) == true)
                return true;
        }

        return false;
    }

    private boolean isCyclic(Set<T> visited, T node) {
        boolean retval;
        for (T connectedNode: map.get(node)) {
            if (visited.contains(connectedNode)) {
                // We've reached back to a vertex, i.e. a back-edge
                return true;
            } else {
                visited.add(connectedNode);
                if (isCyclic(visited, connectedNode) == true)
                    return true;
            }
        }

        return false;
    }