Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a random graph generator : layer by layer method #6

Open
wants to merge 2,648 commits into
base: master
Choose a base branch
from

Conversation

huggingstar
Copy link

package dag.auxiliary;

import org.apache.commons.math3.distribution.NormalDistribution;
import org.jgrapht.DirectedGraph;
import org.jgrapht.Graph;
import org.jgrapht.VertexFactory;
import org.jgrapht.generate.GraphGenerator;
import org.jgrapht.graph.AbstractBaseGraph;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;



/**
 *
 * random graph generator : layer by layer generaotr
 * In the academic paper, it is shown below,
 * Algorithm:  Layer-by-Layer method.
 * Require: n, k, p ∈ N.
 * Distribute n vertices between k different sets enumerated as L1, . . . , Lk.
 * Let layer(v) be the layer assigned to vertex v.
 * Let M be an adjacency matrix n×n initialized as the zero matrix.
 * for all i = 1 to n do
 *     for all j = 1 to n do
 *         if layer(j) > layer(i) then
 *             if Random() < p then
 *                 M[i][j] = 1
 *             else
 *                 M[i][j] = 0
 * return a random DAG with k layers and n nodes
 *
 * the original algorithm indicates that a node in layer 2 may connect to nodes in layer 3
 * or layer 4 and so on.
 * I make some changes that the node only can connect to the nodes in the next layer.
 * for example, a node in layer 2 only can connect to the nodes in layer 3.
 *
 *     graph features:
 * 1. there is only one node in the first and the last layer, respectively.
 * 2. the only one node in the first layer connects to every node in the second layer.
 * 3. every node in the second to last layer connects to the only one node in the last layer.
 * 4. layers except the first layer and the last layer are called middle layers.
 * 5. Every node connect to the each node which is in next layer according to some probability.
 * 6. Every node in the middle layers has at least one predecessor and one successor.
 *
 * for example, there is a allocaion of nodes expressed in array.
 * int[][] allocation = {{0},                      // layer 0
 *                       {11, 12, 13, 14}},        // layer 1
 *                       {21, 22, 23, 24, 25},     // layer 2
 *                       {4}};                     // layer 3
 * the number of each entry is the node's name,
 * every one-dimensional array represents the content expressing the corresponding layer
 * the edges are like that,
 * 0 --> 11, 0 --> 12, 0 --> 13, 0 --> 14,
 * 11 --> 21, 11 --> 24,    (according to some probability)
 * 12 --> 22, 12 --> 23, 12 --> 25,     (according to some probability)
 * 13 --> 24, 13 --> 25       (according to some probability)
 * 14 --> 21, 14 --> 23     (according to some probability)
 * 21 --> 4,
 * 22 --> 4,
 * 23 --> 4,
 * 24 --> 4,
 * 25 --> 4,
 *
 */



/**
 * @param <V>
 * @param <E>
 */

public class LayerByLayerGenerator<V, E> implements GraphGenerator<V, E, V> {
    private static final boolean DEFAULT_ALLOW_LOOPS = false;


    private final int num_layer;    // 层数  the number of layer
    private final Random rng;
    private final int n;        // 节点数  the number of node
    private final double p;
    private final boolean loops;
    private HashMap<Integer, Integer> vertex_cost = new HashMap<>();    // every node has its cost
    private int total_cost = 0;
    // 每一层有哪些节点
    private int[][] detail;     // allocation of nodes

    /**
     * Create a new Layer-by-Layer random graph generator. The generator does not create self-loops.
     *
     * @param n the number of nodes
     * @param p the edge probability
     */
    public LayerByLayerGenerator(int n, double p, int num_layer) {
        this(n, p, new Random(), DEFAULT_ALLOW_LOOPS, num_layer);
    }


    /**
     * Create a new Layer-by-Layer random graph generator. The generator does not create self-loops.
     *
     * @param n    the number of nodes
     * @param p    the edge probability
     * @param seed seed for the random number generator
     */
    public LayerByLayerGenerator(int n, double p, long seed, int num_layer) {
        this(n, p, new Random(seed), DEFAULT_ALLOW_LOOPS, num_layer);
    }

    /**
     * Create a new Layer-by-Layer random graph generator.
     *
     * @param n     the number of nodes
     * @param p     the edge probability
     * @param seed  seed for the random number generator
     * @param loops whether the generated graph may create loops
     */
    public LayerByLayerGenerator(int n, double p, long seed, boolean loops, int num_layer) {
        this(n, p, new Random(seed), loops, num_layer);
    }

    /**
     * Create a new Layer-by-Layer random graph generator.
     *
     * @param n         the number of nodes
     * @param p         the edge probability
     * @param rng       the random number generator to use
     * @param loops     whether the generated graph may create loops
     * @param num_layer the number of layer
     */
    public LayerByLayerGenerator(int n, double p, Random rng, boolean loops, int num_layer) {
        if (n < 6) {
            // the number of node can not smaller than 6
            throw new IllegalArgumentException("节点个数不能小于 6 ");
        }
        this.n = n;
        if (p < 0.0 || p > 1.0) {
            throw new IllegalArgumentException("概率P在 (0, 1) 之间");
        }
        if (num_layer < 3 || num_layer > n) {

            throw new IllegalArgumentException("层数不能小于3,且层数不能大于节点数");
        }
        this.num_layer = num_layer;
        this.p = p;
        this.rng = rng;
        this.loops = loops;
    }

    /**
     * Generates a random graph based on the Layer-by-Layer model.
     *
     * @param target        the target graph
     * @param vertexFactory the vertex factory
     * @param resultMap     not used by this generator, can be null
     */
    @Override
    public void generateGraph(Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) {
        // special case
        if (n == 0) {
            return;
        }

        // check whether to also create loops
        boolean createLoops = loops;
        if (createLoops) {
            if (target instanceof AbstractBaseGraph<?, ?>) {
                AbstractBaseGraph<V, E> abg = (AbstractBaseGraph<V, E>) target;
                if (!abg.isAllowingLoops()) {
                    throw new IllegalArgumentException(
                            "Provided graph does not support self-loops");
                }
            } else {
                createLoops = false;
            }
        }

        /**
         *  创建节点
         *  create vertices
          */
        // 节点从 0 开始计数
        // name of node start from number 0

        // 操作 HashMap<Integer, Integer> vertex_cost;
        // 设置 每个节点的 执行时间
        int previousVertexSetSize = target.vertexSet().size();
        HashMap<Integer, V> vertices = new HashMap<>(n);
        for (int i = 0; i < n; i++) {
            V v = vertexFactory.createVertex();
            target.addVertex(v);
            vertices.put(i, v);
            // 设置节点的 cost
            // set node's cost value
            int cost = getVertexCost();
            total_cost += cost;
            vertex_cost.put(i, cost);
        }

        if (target.vertexSet().size() != previousVertexSetSize + n) {
            throw new IllegalArgumentException(
                    "Vertex factory did not produce " + n + " distinct vertices.");
        }

        // check if graph is directed
        boolean isDirected = target instanceof DirectedGraph<?, ?>;

        // 确定各层节点数分布情况
        // specify the allocation of the number of node of each layer
        detail = quantifyAllocation(n, num_layer);


        /**
         * 创建边
         * create edge
         *
         * 一层中,一个节点至少要与下一层中的一个节点相连接
         */
        // 对每一层
        // for each layer
        for (int i = 0; i < detail.length - 1; i++) {
            // 判断当前层的下一层中的每个节点是否都至少有一个前驱节点
            // check whether the nodes in middle has at least one predecessor
            boolean[] hasPredecessor = new boolean[detail[i + 1].length];
            for (int index = 0; index < hasPredecessor.length; index++) {
                hasPredecessor[index] = false;
            }
            // 对该层中的每一个元素
            // for each entry in current layer
            for (int j = 0; j < detail[i].length; j++) {
                boolean hasSuccessor = false;
                int a = detail[i][j];
                V s = vertices.get(a);
                // 对下一层中的每一个元素
                // for each entry in next layer
                for (int k = 0; k < detail[i + 1].length; k++) {
                    int b = detail[i + 1][k];
                    V t = vertices.get(b);
                    if (a == b) {
                        if (!createLoops) {
                            // no self-loops`
                            continue;
                        }
                    }
                    if (i == 0 || i == detail.length - 2) {
                        // 第一层和第二层中的所有节点都连接
                        // there is only one node in the first layer and the last layer respectively
                        // the only node in the first layer connects to the every node in the second layer
                        // 倒数第二层和最有一层的所有节点都连接
                        // every node in the second last layer connects to the only one node in the last layer

                        target.addEdge(s, t);
                        hasSuccessor = true;
                        hasPredecessor[k] = true;
                    } else {
                        if (rng.nextDouble() < p) {
                            // 其他层中的节点按照概率进行连接
                            // nodes in other layers connect to the nodes in correspondingly next layer according to a certain probability p
                            // addEdge
                            target.addEdge(s, t);
                            hasSuccessor = true;
                            hasPredecessor[k] = true;
                        }
//                        target.addEdge(s, t);
                    }
                }
                // 如果这个节点没有连接下一层的任何一个节点
                // if the node has no successor
                if (!hasSuccessor) {
                    Random random = new Random();
                    int index = random.nextInt(detail[i + 1].length);
                    int b = detail[i + 1][index];
                    V t = vertices.get(b);
                    target.addEdge(s, t);
                }
            }
            // 处理没有前驱节点的节点
            // process the nodes which has no predecessor
            for (int index = 0; index < hasPredecessor.length; index++) {
                if (!hasPredecessor[index]) {
                    int[] current = detail[i];
                    Random r = new Random();
                    int position = r.nextInt(current.length);
                    int a = current[position];
                    V s = vertices.get(a);
                    int b = detail[i + 1][index];
                    V t = vertices.get(b);
                    target.addEdge(s, t);
                }
            }
        }

//        showGraphInfo();


    }


    /**
     * 确定每一层都有哪些节点
     * specify the allocation of nodes
     *
     * @param n
     * @param num_layer
     * @return
     */
    public int[][] quantifyAllocation(int n, int num_layer) {
        return quantifyAllocation(getNumOfNodeOfEachLayer(n, num_layer));
    }


    /**
     * 确定每一层都是哪些节点
     * determine which nodes are on each level
     *
     * @param allocation
     */
    public int[][] quantifyAllocation(int[] allocation) {
        int[][] result = new int[allocation.length][];
        int value = 0;
        for (int i = 0; i < allocation.length; i++) {
            int[] temp = new int[allocation[i]];
            for (int j = 0; j < allocation[i]; j++) {
                int store = value++;
                temp[j] = store;
            }
            result[i] = temp;
        }
        return result;
    }


    /**
     * 分层方式
     * the way of layering
     * 确定不同层中节点个数
     * specify the number of node in each layer
     * 第一层和最后一层都只有一个节点
     * only one node in the first layer
     * only one node in the last layer
     *
     * @param n
     * @param num_layer
     * @return
     */
    public int[] getNumOfNodeOfEachLayer(int n, int num_layer) {
        int[] result = new int[num_layer];
        result[0] = 1;
        result[num_layer - 1] = 1;
        int available_n = n - 2;
        int available_num_layer = num_layer - 2;
        int mean = available_n / available_num_layer;
        double standardDeviation = 2;
        NormalDistribution normalDistribution = new NormalDistribution(mean, standardDeviation);
//        System.out.println("概要\t\t" + "node : " + n + "\t layer number : " + num_layer + "\tmean : " + mean);

        // 对数组的前一半进行赋值
        // assign values to the first half of array
        for (int i = 1; i < (num_layer / 2); i++) {
            while (true) {
                int random = (int) normalDistribution.sample();
                if (random > 0 && random < mean * 2) {
                    result[i] = random;
                    break;
                }

            }
        }

        // 对数组的后一半进行赋值
        // assign values to the second half of array
        // 使用均值的2倍减去在前半部分对应位置的值,从而获得后半部分对应位置的值


        if (num_layer % 2 == 0) {
            for (int i = 1; i < num_layer / 2; i++) {
                result[num_layer - i - 1] = mean * 2 - result[i];
            }
        } else {
            for (int i = 1; i < (num_layer - 1) / 2; i++) {
                result[num_layer - i - 1] = mean * 2 - result[i];
            }
        }

        // 计算误差
        // calculate error
        int sum = 0;
        for (int c : result) {
            sum += c;
        }
        int error = n - sum;

        // 处理误差
        // deal with error
        if (num_layer % 2 == 0) {
            result[(num_layer - 2) / 2] += error;
        } else {
            result[(num_layer - 1) / 2] += error;
        }

        return result;
    }

    // 显示分层内容
    // demonstrate the result after the layering
    public void showLayerInfo() {
        System.out.println("\t\t\t分层方式  detail");
        int count = 0;
        for (int[] i : detail) {
            System.out.print("layer : " + count++ + "|\t");
            for (int each : i) {
                System.out.print(each + "\t");
            }
            System.out.println();
        }
        System.out.println();
    }



    // 显示所有节点的 cost
    // show all nodes' cost
    public void showAllCost() {
        for (Integer key : vertex_cost.keySet()) {
            System.out.println("vertex : cost\t\t\t" + key + " : " + vertex_cost.get(key));
        }
        System.out.println();
    }





    // 获取一个节点的 cost
    // get a node's cost
    private static int getVertexCost() {
        int period = getPeriod();
        int n = getNumOfVertex();
        int max = period / n;
        while (true) {
            int result = (int) (Math.random() * max);
            if (result > 0) {
                return result;
            }
        }
    }


    // 获取节点数, [1, 30]
    // get the number of node
    private static int getNumOfVertex() {
        int max = 30;
        while (true) {
            int n = (int) (Math.random() * max);
            if (n > 5) {
                return n;
            }
        }
    }


    // 获取周期, [100, 1000]
    // get the period value
    private static int getPeriod() {
        while (true) {
            int a = (int) (Math.random() * 1000);
            if (a >= 100 && a <= 1000) {
                return a;
            }
        }
    }


    // 获取 HashMap<Integer, Integer> vertex_cost;
    // get HashMap<Integer, Integer> vertex_cost;
    public HashMap<Integer, Integer> getVertexCostMap() {
        return vertex_cost;
    }


    // 获取总的 cost
    // get all nodes' cost
    public int getToalCost() {
        return total_cost;
    }


    // 显示 graph 的信息
    // show the graph information
    public void showGraphInfo() {
        System.out.println("------------ graph info from generator -------------------");
        showLayerInfo();
        showAllCost();
        System.out.println("------------- end graph info -----------------");
        System.out.println();
    }



}

d-michail and others added 30 commits July 1, 2020 12:56
* Implemented BK flow algorithm

* Corrected the typo
* implemented transit node routing

* prereview1

* remove unrelated changes

* review 1

* proper code formatting in CHManyToManyShortestPaths

* review 2

* lazy computation of TransitNodeRouting in TNRShortestPath; made TransitNodeRouting package-private

* review 3

* review 4
* Added new random walk iterator

* Replaced with new iterator

* Deprecated old class

* Fixed typos
…#979)

* Fixed zero division bug when initializer returns points that coincide

* Fixes bug with isolated vertices (#980)
* added ZhangShasha tree edit distance algorithm

* decumentation improvements

* added package info

* review 1
syoon2 and others added 30 commits January 4, 2024 12:36
Bumps [org.apache.maven.plugins:maven-checkstyle-plugin](https://github.com/apache/maven-checkstyle-plugin) from 3.2.1 to 3.3.1.
- [Commits](apache/maven-checkstyle-plugin@maven-checkstyle-plugin-3.2.1...maven-checkstyle-plugin-3.3.1)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-checkstyle-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps org.apache.commons:commons-text from 1.10.0 to 1.11.0.

---
updated-dependencies:
- dependency-name: org.apache.commons:commons-text
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [org.apfloat:apfloat](https://github.com/mtommila/apfloat) from 1.10.1 to 1.12.0.
- [Commits](mtommila/apfloat@1.10.1...1.12.0)

---
updated-dependencies:
- dependency-name: org.apfloat:apfloat
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
#1187)

Bumps [org.apache.maven.plugins:maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.5.0 to 3.6.3.
- [Release notes](https://github.com/apache/maven-javadoc-plugin/releases)
- [Commits](apache/maven-javadoc-plugin@maven-javadoc-plugin-3.5.0...maven-javadoc-plugin-3.6.3)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-javadoc-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…1188)

Bumps [org.apache.maven.plugins:maven-shade-plugin](https://github.com/apache/maven-shade-plugin) from 3.4.1 to 3.5.1.
- [Release notes](https://github.com/apache/maven-shade-plugin/releases)
- [Commits](apache/maven-shade-plugin@maven-shade-plugin-3.4.1...maven-shade-plugin-3.5.1)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-shade-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
….5 (#1190)

Bumps [org.apache.maven.plugins:maven-failsafe-plugin](https://github.com/apache/maven-surefire) from 3.2.3 to 3.2.5.
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](apache/maven-surefire@surefire-3.2.3...surefire-3.2.5)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-failsafe-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps `maven-surefire-plugin.version` from 3.2.3 to 3.2.5.

Updates `org.apache.maven.surefire:surefire-junit-platform` from 3.2.3 to 3.2.5

Updates `org.apache.maven.plugins:maven-surefire-plugin` from 3.2.3 to 3.2.5
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](apache/maven-surefire@surefire-3.2.3...surefire-3.2.5)

---
updated-dependencies:
- dependency-name: org.apache.maven.surefire:surefire-junit-platform
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: org.apache.maven.plugins:maven-surefire-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
….0 (#1192)

Bumps [org.apache.maven.plugins:maven-assembly-plugin](https://github.com/apache/maven-assembly-plugin) from 3.5.0 to 3.6.0.
- [Commits](apache/maven-assembly-plugin@maven-assembly-plugin-3.5.0...maven-assembly-plugin-3.6.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-assembly-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps org.apache.felix:maven-bundle-plugin from 5.1.8 to 5.1.9.

---
updated-dependencies:
- dependency-name: org.apache.felix:maven-bundle-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Added a variable defining the literal "isFeasiblePair" instead of writing the literal several times
* Restore deque interface

This reverts commit b627ca6 (#1159).

* Implement DoublyLinkedList.reversed()

Ref: #1154

* Move anonymous inner class to ReversedDoublyLinkedList class

* Block inverts in the reversed view

* Make iterators from reversed view unmodifiable

* Block node modification operations

* Add basic tests for reversed view

* Make iterator impl work with reversed view

* Remove redundant override

* Add more iterator tests

* Avoid using size variable for iterator creation

* Fix incorrect test design

* Make Surefire happier with test discovery

* Add common ancestor for list nodes

* Update Javadoc

* Document exceptions for list nodes

* Reorder methods in AbstractListNode

* Move node value to the AbstractListNode

* Hide internal methods in implementation

* Minimize amount of casting involved with ListNode

* Make ListNode an abstract class

* Adjust setter method visibilities of list nodes

* Test that DoublyLinkedList implements both List and Deque

Ref: #1169 (review)
* first version

* details implementation changed to NI

Previous implementation was based on the jgrapht implementation of Nearest Neighbor. Now it was changed and now is based on Nearest Insertion.

* unit tests added

* coding conventions

* unit tests updated to jUnit5

* swap functions moved to ArrayUtil

* headers updated

* Apply suggestions from code review

Co-authored-by: Sung Ho Yoon <55358516+syoon2@users.noreply.github.com>

---------

Co-authored-by: John Sichi <jsichi@gmail.com>
Co-authored-by: Sung Ho Yoon <55358516+syoon2@users.noreply.github.com>
* Use latest build of MathJax v2

* Remove Javadoc build workaround

Issue resolved in v3.1.0

* Use same versioning key for Surefire and Failsafe

* Add Java version property in pom
* Update README

- Fix broken link to CI status badge
- Update dependency descriptions
- Update links to wiki pages
- Update links to licenses

* Add module Javadocs

* Javadoc: Document exceptions better

* Clarify exception Javadoc

* Further improve exception Javadoc
* Block edge weight modification in unmodifiable view

* Add tests for unmodiable view
Modify the parsing rules for DOT language according to https://graphviz.org/doc/info/lang.html.
In summary, I modify three files:
1. Modify the DOT.g4 to enable JGraphT identify legal text in DOT language which is omitted in current verions
2. Modify the DOTEventDerivenImporter to update the lookupMap rule to parse corresponding element.
3. Update existing unit test in DOTImporter2Test to obey the current grammar, and add a new unit test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.