def eulersch(graph): r = True for node in graph.nodes: if len(dsa.neighbors(graph, node)) % 2 == 1 or len( dsa.neighbors(graph, node)) == 0: r = False return r
def Zshgkmpt(graph, Zshglist=None): """returns a list [graph,zshgkmpt1,...,zshgkmptn]""" #print("graph:\n",graph) if Zshglist is None: Zshglist = [] graph1 = copy.deepcopy(graph) startnode = list(graph1.nodes)[0] neighborlist = dsa.neighbors(graph1, startnode) neighborlist.append(startnode) a = True while a: neighborlist2 = neighborlist for node in neighborlist2: nlist = dsa.neighbors(graph1, node) for node1 in nlist: if node1 not in neighborlist2: neighborlist2.append(node1) if len(neighborlist2) == len(neighborlist): a = False neighborlist = neighborlist2 if len(neighborlist) == len(graph1.nodes): if neighborlist not in Zshglist: Zshglist.append(neighborlist) return Zshglist else: Zshglist.append(neighborlist) for node in neighborlist: graph1.remove_node(node) return Zshgkmpt(graph1, Zshglist)
def hamiltonisch(graph, node="default", waynodes=[], lenght=0): """tries to find a Hamiltoncircle""" #setting the startnode if node == "default": node = list(graph.nodes)[0] waynodes = [node] #print("startknoten:",node) #print("hamiltonisch wird mit waynodes={} aufgerufen".format(waynodes)) for node in dsa.neighbors(graph, node): if node not in waynodes: #print("getesteter Knoten:",node,", nachbarknoten:",dsa.neighbors(graph,node),", waynodes:",waynodes) waynodes.append(node) if len(waynodes) == len(graph.nodes): if graph.get_edge(waynodes[-1], waynodes[0]): print("hamiltonweg:", waynodes) return True if len(waynodes) < len(graph.nodes): if hamiltonisch(graph, node, waynodes, lenght): return True else: waynodes.remove(node) else: break #print("forschleife ist durch") return False
def neighbours(graph, list1): list2 = [] for node in list1: neighborlist = dsa.neighbors(graph, node) for node1 in neighborlist: if node1 not in list2: list2.append(node1) return list2
def searchpath(graph, startnode, lenght, endnode, kreisliste=None): print("Aufruf startnode={}, lenght={}, kreisliste={}".format( startnode, lenght, kreisliste)) if lenght < 0: #print("Es gibt keine negativen Wege") return "Es gibt keine negativen Wege" if kreisliste is None: kreisliste = [startnode] newkreisliste = copy.deepcopy(kreisliste) if lenght == 0: print("lenght is 0") return [False, kreisliste] elif lenght == 1: print(kreisliste) for nnode in dsa.neighbors(graph, startnode): print(kreisliste) print("lenght is 1") print(nnode) if nnode == endnode: return [True, kreisliste] return [False, kreisliste] path = False for nnode in dsa.neighbors(graph, startnode): newkreisliste.append(nnode) print("Ist {} in {}?".format(nnode, kreisliste)) if nnode not in kreisliste: a = searchpath(graph, nnode, lenght - 1, endnode, newkreisliste) if a[0]: kreisliste = a[1] print("pfad gefunden") path = True break else: print("kein pfad gefunden") newkreisliste.remove(nnode) kreisliste = copy.deepcopy(newkreisliste) return [path, kreisliste]
def Hierarchie(graph, nodelist, hierarchiedict, ebene, hnodes, counter=0): #print("Hierarchie wird mit folgenden Parametern aufgerufen: hierarchiedict: {}, ebene: {}, hnodes: {}".format(hierarchiedict,ebene,hnodes)) counter += 1 if counter > 100: return "funktioniert nicht" ebene += 1 glist = [] nodelistout = [] for node in nodelist: for node1 in dsa.neighbors(graph, node): if node1 not in hnodes: nodelistout.append(node1) glist.append(grad(graph, node1)) hnodes.append(node1) hierarchiedict[ebene] = glist if len(graph.nodes) == len(hnodes): return hierarchiedict else: return Hierarchie(graph, nodelistout, hierarchiedict, ebene, hnodes, counter)
def grad(graph, node): return len(dsa.neighbors(graph, node))