예제 #1
0
def createRandomDagIter(n: int) -> DirectedGraph:
    newGraph = DirectedGraph()

    # creates n nodes for the graph
    for i in range(0, n):
        newGraph.addNode(i)

    nodes = newGraph.getAllNodes()
    for i in range(0, random.randint(1, n * n)):
        first = nodes[random.randint(0, n - 1)]
        second = nodes[random.randint(0, n - 1)]

        if (first == second):
            continue

        if second in newGraph.adjList[first] or first in newGraph.adjList[
                second]:
            continue

        if (len(newGraph.adjList[first]) == 2):
            continue

        newGraph.addDirectedEdge(first, second)
        if newGraph.isAcyclic(first) is False:
            newGraph.removeDirectedEdge(first, second)
    return newGraph
예제 #2
0
def createRandomDAGIter(n):
    """Creates n random nodes with randomly assigned, unweighted, directed edges."""
    g = DirectedGraph()

    for i in range(n):

        g.addNode(i)

        if i < 2:
            continue

        edge = random.sample(g.getAllNodes(), 2)
        g.addDirectedEdge(min(edge), max(edge))

    return g
예제 #3
0
def createRandomDAGIter(n):
    # Create new DirectedGraph and add n amount of nodes to it
    g = DirectedGraph()
    for i in range(1, n + 1):
        g.addNode(createLabel(i))

    # Copy the list of the nodes from the graph
    # so we can pop from list
    nodes = g.getAllNodes().copy()

    # Shuffle the nodes so the graph doesn't
    # start with "A" every time
    shuffle(nodes)
    # While there are nodes in the list
    while len(nodes) > 0:
        cur = nodes.pop(0)
        if len(nodes) <= 1:
            break

        # XXX Choose a random amount of children XXX
        # Make nodes have 2 children
        num = 2  # randrange(1,len(nodes))

        # Add a random sample of num length
        # the neighbor of the cur
        for i in sample(nodes, num):
            g.addDirectedEdge(cur, i)

        # For every neighbor of cur do the same
        for n in cur.neighbors:
            nodes.pop(nodes.index(n))
            if len(nodes) <= 1:
                break
            num = 2  # randrange(1,len(nodes))
            for i in sample(nodes, num):
                g.addDirectedEdge(n, i)
    return g
예제 #4
0
from Main import createRandomDAGIter
from DirectedGraph import DirectedGraph
from TopSort import *

ignore = True  # To ignore invalid paths where a node does not exist

x = DirectedGraph()
for char in "ABCDEFGH":
    x.addNode(char)

x.addDirectedEdge(x.getNode("A"), x.getNode("B"))
x.addDirectedEdge(x.getNode("A"), x.getNode("D"))

x.addDirectedEdge(x.getNode("C"), x.getNode("D"))
x.addDirectedEdge(x.getNode("C"), x.getNode("G"))
x.addDirectedEdge(x.getNode("C"), x.getNode("H"))

x.addDirectedEdge(x.getNode("D"), x.getNode("G"))

x.addDirectedEdge(x.getNode("H"), x.getNode("E"))
x.addDirectedEdge(x.getNode("H"), x.getNode("F"))

graphs = [createRandomDAGIter(5), createRandomDAGIter(50), x]

if __name__ == "__main__":
    for g in graphs:
        print(g)
        print()
        for func in [TopSort.Kahns, TopSort.mDFS]:
            arr = func(g)
            print("Node Count: {} | {}".format(len(arr), arr))