예제 #1
0
    def initializeInDegreeMap(graph: DirectedGraph) -> dict:
        inDegree = {}
        for node in graph.getAllNodes():
            inDegree[node] = 0

        for node in graph.getAllNodes():
            for neighbor in node.neighbors:
                inDegree[neighbor] += 1

        return inDegree
예제 #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: 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
예제 #4
0
    def mDFS(graph: DirectedGraph) -> list():
        stack = []
        visited = [] 

        for node in graph.getAllNodes():
            if node not in visited:
                TopSort.DFSHelper(node, stack, visited, graph)

        output = [] 
        while (len(stack) > 0):
            output.append(stack.pop())
        return output 
예제 #5
0
    def mDFS(graph: DirectedGraph) -> list:
        topSort = []
        visited = set()

        for node in graph.getAllNodes():
            stack = [node]
            while len(stack) != 0:
                curr = stack.pop()

                if curr not in visited:
                    visited.add(curr)

                n = len(stack)
                for neighbor in node.neighbors:
                    if neighbor not in visited:
                        stack.insert(n, neighbor)

            topSort.append(node)

        return topSort[::-1]
예제 #6
0
    def Kahns(graph: DirectedGraph):
        output = []
        queue = []

        for node in graph.getAllNodes():
            if node.inDegree is 0:
                queue.append(node)

        while len(queue) is not 0:
            currNode = queue.pop(0)
            output.append(currNode)

            for neighbor in graph.adjList[currNode]:
                neighbor.inDegree = neighbor.inDegree - 1

                if neighbor.inDegree is 0:
                    queue.append(neighbor)

            currNode.inDegree = -1

        return output
예제 #7
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
예제 #8
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;