JSFiddle - React, Tailwind, and code Playground

by vivin

HTML

<script src="http://vivin.net/pub/viz.js"></script>
<pre id="json">
</pre>
<pre id="dot">   
</pre>
<pre id="results">
</pre>

JavaScript

var debug = false;

function log(str) {
    document.getElementById("results").innerHTML = document.getElementById("results").innerHTML + str + "\n";
}

function createGraph() {
    var nodes = {};
    var directed = (Math.floor(Math.random() * 2)) == 1; directed = true;
    var cyclic = (Math.floor(Math.random() * 2)) == 1; cyclic = false;
    var vertices = [];
    var processed = {};
    var discovered = {};

    var numNodes = Math.floor(Math.random() * 10) + 1; numNodes = 7;
    for(var i = 0; i < numNodes; i++) {
        var newNode = Math.floor(Math.random() * 30) + 1;

        while(typeof nodes[newNode] !== "undefined") {
            newNode = Math.floor(Math.random() * 30) + 1;
        }

        vertices.push(newNode);
        nodes[newNode] = [];
    }

    var numEdges = Math.floor((Math.random() * (numNodes * (numNodes - 1))) / 3) + 1; numEdges = 10;
    for(var i = 0; i < numEdges; i++) {
        var parent = vertices[Math.floor(Math.random() * vertices.length)];
        var child = vertices[Math.floor(Math.random() * vertices.length)];

        while(parent === child) {
            child = vertices[Math.floor(Math.random() * vertices.length)];
        }

        if(nodes[parent].indexOf(child) === -1) {
            if(!directed || cyclic) {
                nodes[parent].push(child);

                //If this is an undirected graph then we need connections going both ways
                if(!directed && nodes[child].indexOf(parent) === -1) {
                    nodes[child].push(parent);
                }
            } else {
                nodes[parent].push(child);
                
                var cycle = (function(node) {
                    var children = nodes[node];
                    var _cycle = false;
                    var i = 0;

                    while(i < children.length && !cycle) {
                        var _child = children[i];
                        _cycle = (_child === child);
                        if(!_cycle) {
   ...