if line.endswith("\\\n"): # continuation line, buffer, goto next oldline = line.strip("\\\n") continue (headname, tails) = line.split(":") # head numfind = re.compile("^\d+") # re to find the number of this word head = numfind.findall(headname)[0] # get the number G.add_node(head) for tail in tails.split(): if head == tail: print("skipping self loop", head, tail, file=sys.stderr) G.add_edge(head, tail) return G if __name__ == '__main__': G = roget_graph() print("Loaded roget_dat.txt containing 1022 categories.") print("digraph has %d nodes with %d edges" % (nx.number_of_nodes(G), nx.number_of_edges(G))) UG = G.to_undirected() print(nx.number_connected_components(UG), "connected components") nx.draw_circular(UG) plt.show()
def AllocateSmallGroups(G, n, teamCount, m, showAnimation, peopleNumbers, peopleUnvisited): # Setup graph animation window # Display all nodes in a circular layout with labels pos = nx.circular_layout(G) if showAnimation: pylab.show() nx.draw_circular(G, with_labels=True) # Determine the number of edges will present in the clique # by taking the number of teams and multiplying it by # one less of itself cliqueEdges = teamCount * (teamCount - 1) # Determine the likely number of groups that will be allocated # for each night by doing integer division with n divided by m # Sometimes, there will be less groups due to edge cases where a # group would only have had one person numGroups = n // m # Create a list to contain the lists of each group for each night superList = [] # Start a counter for the number of nights nightNum = 0 # Create a list of the teams that were hosts the last night previousHosts = [] # While there are still teams who have not visited a different team # (Alternatively, if there are still edges missing to form a clique) while len(sum(list(peopleUnvisited.values()), [])) > 0: # Make a deep copy of the peopleUnvisited map as it currently is people = deepcopy(peopleUnvisited) # Create a list to hold the groups for this night # Create a list within this that will hold the number of individuals # assigned to each group of this night night = [[]] # Create a list of the teams that are hosts for this night hostNames = [] # Create the expected number of groups for i in range(numGroups): # Retrieve the list of unassigned teams peopleList = list(people.keys()) peopleNum = len(peopleList) # If there is only one unassigned team, do not create a new # group, but let the remainingGuests list allocate # this team to an existing group if peopleNum == 1: break # Create a map for teams that could be a host for this group potentialHosts = {} # Populate the map with unassigned teams that # were not hosts last night for person in Difference(peopleList, previousHosts): potentialHosts[person] = people[person] # If there are potential hosts for this group if len(potentialHosts) > 0: # Select the host that has been visited least, which # is the same as host with most unvisited teams # (Alternatively, node of smallest degree) host = max(potentialHosts, key=lambda k: len(potentialHosts[k])) # Find the unassigned teams that were hosts last night, but # are not hosts for this night previousHostOptions = Intersection( peopleList, Difference(previousHosts, hostNames)) if len(previousHostOptions) > 0: # Reset the map to store possible hosts from last night potentialHosts = {} for person in previousHostOptions: potentialHosts[person] = peopleUnvisited[person] # Find the host that has been visited least, which # was a host last night, but not tonight prevHost = max(potentialHosts, key=lambda k: len(potentialHosts[k])) # If the host from last night has been visited less # than the host selected at the moment, then # select the host from last night instead if (len(peopleUnvisited[host]) < len( peopleUnvisited[prevHost])): host = prevHost # Otherwise, if there are no unassigned teams that # that were not hosts last night else: # Find the unassigned teams that were hosts last night, but # are not hosts for this night previousHostOptions = Intersection( peopleList, Difference(previousHosts, hostNames)) if len(previousHostOptions) > 0: # Reset the map to store possible hosts from last night potentialHosts = {} for person in previousHostOptions: potentialHosts[person] = peopleUnvisited[person] # Select the host that has been visited least, which # was a host last night, but not tonight host = max(potentialHosts, key=lambda k: len(potentialHosts[k])) # Otherwise, if there is no unassigned teams that could # be hosts for this night, then do not create # a group and just add any unassigned people # using remainingGuests else: break # Create a list for a new group for this night, # starting with the selected host group = [host] # Initialize the initial list of the night list # by setting the current size of this group # to the number of individuals the host night[0].append(peopleNumbers[host]) hostNames.append(host) # Since the host has been assigned to a group, remove them from map del people[host] # If the host already fills up the group if peopleNumbers[host] >= m: # Find any unassigned guests who have not yet visited this host potentialGuests = Intersection(peopleUnvisited[host], list(people.keys())) if len(potentialGuests) > 0: # Add a guest anyway to this group, so that # this edge case is resolved by allowing teams # to visit this host, even though group is filled guestToAdd = potentialGuests[0] group.append(guestToAdd) if showAnimation: AnimateEdge(G, pos, guestToAdd, host) # Increment group size by number of individuals of guest night[0][i] += peopleNumbers[guestToAdd] # Denote guest as assigned by removing from map del people[guestToAdd] # Remove guest from univisited list of host peopleUnvisited[host].remove(guestToAdd) # While this group is not filled and there are unassigned guests while night[0][i] < m and len(list(people.keys())) > 0: guestToAdd = None peopleList = list(people.keys()) peopleNum = len(peopleList) # Find any unassigned guests who have not yet visited this host potentialGuests = Intersection(peopleUnvisited[host], peopleList) potentialNum = len(potentialGuests) # If adding any guest at any point will overflow for this host if peopleNumbers[host] + 2 > m: # Then add a guest anyway with preference toward unvisited if potentialNum > 0: guestToAdd = potentialGuests[0] else: guestToAdd = peopleList[0] # If there are potential guests but still no guest selected if potentialNum > 0 and guestToAdd is None: # If a couple can fit if night[0][i] + 2 <= m: # Then add a couple, if one is available for guest in potentialGuests: if peopleNumbers[guest] == 2: guestToAdd = guest break # Otherwise, if still no guest selected and # if a single can fit if guestToAdd is None and night[0][i] + 1 <= m: for guest in potentialGuests: # Then add a single, if one is available if peopleNumbers[guest] == 1: guestToAdd = guest break # Otherwise, stop attempting to add guests to this group and # simply allow the remainingGuests list to fill this group if guestToAdd is None: break # Otherwise, if there are, in fact, no potential guests and # still no guest selected but there are unassigned guests elif potentialNum == 0 and guestToAdd is None and peopleNum > 0: # Then add a guest anyway guestToAdd = peopleList[0] potentialIndex = 1 # Try the next guest if the current one will not fit while (potentialIndex < peopleNum and peopleNumbers[guestToAdd] + night[0][i] > m): guestToAdd = peopleList[potentialIndex] potentialIndex += 1 # If there is a guest selected if guestToAdd is not None: # Add the guest to this group group.append(guestToAdd) if showAnimation: AnimateEdge(G, pos, guestToAdd, host) # Increment group size by number of individuals of guest night[0][i] += peopleNumbers[guestToAdd] # Denote guest as assigned by removing from map del people[guestToAdd] # Remove guest from univisited list of host, if the # guest is still in that list if guestToAdd in peopleUnvisited[host]: peopleUnvisited[host].remove(guestToAdd) # Add this group to the current night night.append(group) # As a catch-all, compile a list of any guests that were not assigned # in the current night remainingGuests = list(people.keys()) # If there were any extra unassigned guests if len(remainingGuests) > 0: # Sort them with couples at the beginning sortedRemainingGuests = [] for guest in remainingGuests: if peopleNumbers[guest] == 2: sortedRemainingGuests.insert(0, guest) else: sortedRemainingGuests.append(guest) # Add each guest to the smallest group for guest in sortedRemainingGuests: # Find the group with least number of individuals smallestGroupIndex = night[0].index(min(night[0])) # Find who the host is for that group host = night[smallestGroupIndex + 1][0] # Add the guest to that group night[smallestGroupIndex + 1].append(guest) if showAnimation: AnimateEdge(G, pos, guest, host) # Increment group size by number of individuals of extra guest night[0][smallestGroupIndex] += peopleNumbers[guest] # Remove guest from univisited list of host, if the # guest is still in that list if guest in peopleUnvisited[host]: peopleUnvisited[host].remove(guest) # Add this filled night list to the list of lists superList.append(night) # Set the list of current hosts as the previous hosts for the next night previousHosts = hostNames # Increment the night counter nightNum += 1 # Print out the details for the night, including the # size of each group and the groups themselves print("Night #", nightNum) print("Group sizes are =", night[0]) for j, group in enumerate(night[1:], start=1): print("Group #", j, "=", night[j]) # Determine how much progress has been made on forming a clique if showAnimation: edges = G.number_of_edges() else: edges = cliqueEdges - len(sum(list(peopleUnvisited.values()), [])) # Print out progress of algorithm as a percentage, which is # how many edges have been added so far divided by # the total number of edges required to form a clique print("Clique is", int(100 * (edges / cliqueEdges)), "% complete (" + str(edges), "of", str(cliqueEdges) + " edges).\n") return superList
G.add_edge( osp.index.levels[0][x],osp.index.levels[1][y] , weight = 4,color='navy') elif v > rank3 : G.add_edge( osp.index.levels[0][x],osp.index.levels[1][y] , weight = 4 ,color='mediumpurple') elif v< rank6: G.add_edge( osp.index.levels[0][x],osp.index.levels[1][y] , weight = 2 ,color='brown') elif v < rank5 : G.add_edge( osp.index.levels[0][x],osp.index.levels[1][y], weight = 2 ,color='coral') elif v < rank4 : G.add_edge( osp.index.levels[0][x],osp.index.levels[1][y] , weight = 2 ,color='salmon') edges = G.edges() colors = [G[u][v]['color'] for u,v in edges] width = [G[u][v]['weight'] for u,v in edges] nx.draw_circular(G,with_labels=True,edge_color=colors,node_size=500,font_size=12,font_color='white',width=width) # In[140]: #2.4.2 แนะนำของคนที่มีความชอบคล้ายกันที่สุด suggestL =[] for x in range(0,len(randomlist)): Vmaxi = 0 Imaxi = 0 for y in range(0,len(randomlist)): v = pearson_similarity.iloc[x,y] if v > Vmaxi and x !=y : Vmaxi = v
numfind = re.compile(r"^\d+") # re to find the number of this word head = numfind.findall(headname)[0] # get the number G.add_node(head) for tail in tails.split(): if head == tail: print("skipping self loop", head, tail, file=sys.stderr) G.add_edge(head, tail) return G G = roget_graph() print("Loaded roget_dat.txt containing 1022 categories.") print( f"digraph has {nx.number_of_nodes(G)} nodes with {nx.number_of_edges(G)} edges" ) UG = G.to_undirected() print(nx.number_connected_components(UG), "connected components") options = { "node_color": "black", "node_size": 1, "edge_color": "gray", "linewidths": 0, "width": 0.1, } nx.draw_circular(UG, **options) plt.show()