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
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
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
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
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]
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
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
#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;