コード例 #1
0
def greedy_color(G):
    results = {}

    #init with none color
    for v in V(G):
        results[v] = None

    for v in V(G):

        neighbor_colors = []
        for n in neighbors(G, v):
            if results[n] != None:
                neighbor_colors.append(results[n])

        color = 0
        flag = False
        while not flag:
            if color not in neighbor_colors:
                flag = True
            else:
                color += 1

        results[v] = color

    return results
コード例 #2
0
def get_min_edge(G, T):
    min_cost = 100000000
    min_edge = None
    for v in V(T):
        candidates = neighbors(G, v)
        for c in candidates:
            if G[v][c]['weight'] < min_cost and c not in V(T):
                min_cost = G[v][c]['weight']
                min_edge = (v, c)

    return min_edge
コード例 #3
0
def greedy_coloring(G):
    colors = {v: None for v in V(G)}
    colors[V(G)[0]] = 1
    for v in V(G):
        if colors[v] == None:
            N = neighbors(G, v)
            bad_colors = [colors[w] for w in N]
            j = 1
            while colors[v] == None:
                if j not in bad_colors:
                    colors[v] = 1
                else:
                    j += 1
    return colors
コード例 #4
0
def is_dominating(G, S):
    S_complement = list(set(V(G)) - set(S))
    for v in S_complement:
        N = neighbors(G, v)
        if list(set(N) & set(S)) == []:
            return False
    return True
コード例 #5
0
def prism(G):
    T = nx.Graph()
    #add the first vertice to the empty tree
    T.add_node(list(V(G))[0])
    while is_spanning(G, T) == False:
        #print(get_min_edge(G,T)[0])
        T.add_edge(*get_min_edge(G, T))

    return T
コード例 #6
0
def maximum_independent_set(G):
       
    """Returns the list of the maximum possible independent set

    Parameters
    ----------
    G : int
    The graph containg the vertices and edges of graph G
    
    Returns
    -------
    list
    a list of the maximum possible independent set
    """
    n = len(V(G))
    for k in range(n-1,1, -1):
        for s in combinations(V(G),k):
            if is_independent(G,list(s)) == True:
                return list(s)
コード例 #7
0
def incident_edges(G,T):
    """
    Returns a list of edges adjacent to the starting vertex
    
    Parameters
    ----------
    G :     Networkx graph
            Undirected graph
            
    T :     Tuple
    
    Returns
    -------
    incident_e :   tuple
                   Returns a 3-element array that lists the edges
                   that are adjacent to the starting vertex
    """
    incident_e = []
    for e in E(G):
        if e[0] in V(T) or e[1] in V(T):
            if e[0] not in V(T) or e[1] not in V(T):
                incident_e.append(e)
    return incident_e
コード例 #8
0
def largest_clique_set(G):
    
"""Returns True if every two distinct vertices are adjacent

    Parameters
    ----------

    G:
    A Graph containg vertices and edges contained in Graph G
    
    S:
    
    Returns 
    -------
    x
        The maximum clique of the Graph

    """    
    n = len(V(G))
    for x in range(n,2,-1):    #increasing
        for x in combinations(V(G),x):
            if is_clique(G,x) == True:
                return x
コード例 #9
0
def greedy_coloring (G):
   """Returns vertices with their corresponding colors

   Parameters
   ----------
   G: int
   A graph containing the vertices and edges contained in graph G
   
   Returns
   -------
   colors 
         Returns vertices with their corresponding colors
   """    
   colors = {}
    for u in V(G):
        neighbour_colors = {colors[v] for v in G[u] if v in colors}
        for color in it.count():
            if color not in neighbour_colors:
                break
        colors[u] = color
    return colors
コード例 #10
0
def maximum_clique_set(G):
    n = len(V(G))
    for k in range(n,1,-1):
        for s in combinations(V(G),k):
            if is_clique(G,list(s)) == True:
                return list(s)
コード例 #11
0
def maximum_clique(G):
    for k in range(n(G), 1, -1):
        for S in combinations(V(G), k):
            if is_clique(G, list(S)) == True:
                return list(S)
コード例 #12
0
def minimum_dominating(G):
    for k in range(1, n(G)):
        for S in combinations(V(G), k):
            if is_dominating(G, list(S)) == True:
                return list(S)
コード例 #13
0
def is_spanning(G, H):
    return set(V(G)) == set(V(H))
コード例 #14
0
def maximum_independent_set(G):
    for k in range (n(G), 1, -1):
        for S in combinations(V(G), k):
            if is_independent(G, list(S)) == True:
                return list(S)
コード例 #15
0
def maximum_independent_set(G):
    n = len(V(G))
    for k in range(n - 1, 1, -1):
        for S in combinations(V(G), k):
            if is_independent(G, list(S)):
                return list(S)
コード例 #16
0
def maximum_independent_set(G):
n = len(V(G))
for k in range(n-1,1,-1):
    for S in combinations(V(G),t);:
        if is_independent (6, list(3))==True:
            return list(S)
コード例 #17
0
def independent_set(G, v):
    n = len(V(G))
    for k in range(n - 1, 1, -1):
        for S in combinations(V(G), k):
            if is_independent_spes(G, list(S), v) == True:
                return list(S)