예제 #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."""
    graph = DirectedGraph()

    for i in range(n):
        graph.addNode(i)
        if i < 2:
            continue
        edge = random.sample(graph.getAllNodes(), 2)
        while not graph.addDirectedEdge(min(edge), max(edge)):
            edge = random.sample(graph.getAllNodes(), 2)

    return graph
예제 #3
0
def createRandomDAGIter(n):
    randGraph = DirectedGraph()
    i = 0
    # Create n nodes
    while i < n:
        randGraph.addNode(i)
        i += 1

    # Assign each node 2 random nodesList (might assign 0, 1 or 2 nodesList)
    for i in range(len(randGraph.nodesList)):
        randList = []
        #retreive the length of nodesList
        size = len(randGraph.nodesList) - 1

        name = randGraph.nodesList[i].name
        #generate random number
        n1 = random.randint(name, size)  #d2
        n2 = random.randint(name, size)  #d1
        #if n1 generated is unique
        checkandCreate(randGraph, name, n1, n2, randList)

    return randGraph
예제 #4
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
예제 #5
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))
예제 #6
0
파일: Main.py 프로젝트: brijsavla/Project2
#Prijesh Dholaria
# CS 435 Section 6
# Question 4

from DirectedGraph import DirectedGraph
import random

class Main
    def createRandomDAGIter(n)
        g = DirectedGraph()
        d = {}
        for num in range (n)
            g.addNode(num)
        
        nodes = g.getAllNodes()
        for node in nodes
            numofNodes = random.randint()
            numsinList= [];
            while len(numsinList)!= numofNodes
                num = random.randint(1, nn)
                if num not in numsinList
                    numsinList.append(num)
                    graph.addDirectedEdge(node, num)
        return g;