예제 #1
0
def GenerateGraph(a: [], n: int):
    if CheckSeries(a[:], n):
        a.sort(reverse=True)
        b = list(range(0, n))
        g = Graph(n, 0.0, 1)
        while True:
            empty = True
            negative = False
            for x in a:
                if x != 0:
                    empty = False
                if x < 0:
                    negative = True
            if empty:
                return g
            elif a[0] < 0 or a[0] >= n or negative:
                return False
            else:
                i = 1
                while i <= a[0]:
                    a[i] -= 1
                    g.addEdge(b[0], b[i])
                    i += 1
                a[0] = 0
                for i in range(n - 1):
                    for j in range(n - 1):
                        if a[j] < a[j + 1]:
                            a[j], a[j + 1] = a[j + 1], a[j]
                            b[j], b[j + 1] = b[j + 1], b[j]
    else:
        return False
예제 #2
0
def randomize(graph: Graph, n: int = 1):
    edges = []

    # check if edge
    for i in range(len(graph.adjMatrix)):
        for j in range(i):
            if graph.adjMatrix[i][j] == 1:
                edge = Edge(i, j)
                edges.append(edge)

    # ab, cd -> ad, cb
    i = 0
    while i < n:
        x, y = random.randint(0,
                              len(edges) - 1), random.randint(
                                  0,
                                  len(edges) - 1)
        nodes = [edges[x].start, edges[x].end, edges[y].start, edges[y].end]
        edge_1 = Edge(edges[x].start, edges[y].end)
        edge_2 = Edge(edges[y].start, edges[x].end)
        if edge_1 not in edges and edge_2 not in edges and len(nodes) == len(
                set(nodes)):
            edges[x].end, edges[y].end = edges[y].end, edges[x].end
            i += 1

    # change adjMatrix
    for i in range(len(graph.adjMatrix)):
        for j in range(len(graph.adjMatrix)):
            graph.adjMatrix[i][j] = 0

    for val in edges:
        start = val.start
        end = val.end
        graph.adjMatrix[start][end] = 1
        graph.adjMatrix[end][start] = 1
예제 #3
0
def get_coherent_graph(n: int = 10, p: float = 0.5):
    i = 0
    while True:
        example_graph = Graph(n, p, 1)
        i += 1
        # if there's only one coherent component graph is coherent
        if is_one_coherent_component(Components(example_graph)):
            break
        if i > 10:
            print("Change graph parameters")
            break

    example_graph.add_weight_to_edges()
    return example_graph
def get_minimum_spanning_tree(start_graph: Graph):

    start = random.randint(0, start_graph.size)
    vertexes = list()
    vertexes.append(start)
    out = Graph(start_graph.size, 0, 1)
    potential_edges = list()
    while len(vertexes) < out.size:
        for edge in start_graph.edges:
            if len(out.edges) == 0 and (edge.start == start
                                        or edge.end == start):
                potential_edges.append(edge)
            for tree_edge in out.edges:
                if edge.start == tree_edge.start or edge.start == tree_edge.end or edge.end == tree_edge.start \
                        or edge.end == tree_edge.end:
                    potential_edges.append(edge)

        potential_edges = list(set(potential_edges))
        potential_edges.sort(key=lambda item: item.weight)

        out.edges.append(
            Edge(potential_edges[0].start, potential_edges[0].end,
                 potential_edges[0].weight))
        out.values[potential_edges[0].start][
            potential_edges[0].end] = potential_edges[0].weight
        out.values[potential_edges[0].end][
            potential_edges[0].start] = potential_edges[0].weight
        out.addEdge(potential_edges[0].start, potential_edges[0].end)

        vertexes.append(potential_edges[0].start)
        vertexes.append(potential_edges[0].end)

        vertexes = list(set(vertexes))
        for edge in start_graph.edges:
            if edge.start in vertexes and edge.end in vertexes:
                start_graph.edges.remove(edge)
        potential_edges.clear()
    return out
예제 #5
0
from Shared.Graph import Graph
from Shared.Canvas import draw_graph

opening_graph = Graph(8, 15, 0)
draw_graph(opening_graph)
예제 #6
0
from Shared.Graph import Graph
from Shared.EdgeRandomization import randomize
from Shared.Generator import GenerateGraph

example_graph = Graph(5, 0.5, 1)
series = [7, 5, 5, 5, 3, 3, 2, 1, 1, 0]
output = GenerateGraph(series, len(series))
example_graph.change_adj_matrix(output.adjMatrix)

example_graph.printAdjacencyMatrix()
print("\n")

randomize(example_graph, 10)
example_graph.printAdjacencyMatrix()
예제 #7
0
from Shared.Graph import Graph

# Zadanie 3.
print('Adjacency matrix - given number of vertexes and number of edges:')
g = Graph(4, 2, 0)
# last variable defines if we want to use second argument as probability(1) or as number of edges(0)
print(
    'Adjacency matrix - given number of vertexes and probability of existing an edge between vertexes:'
)
f = Graph(4, 0.5, 8)
예제 #8
0
from Shared.Graph import Graph
from Shared.Canvas import draw_graph
from Shared.HamiltonianCycle import hamiltonian_cycle

example_graph = Graph(10, 0.5, 1)
hamiltonian_cycle(example_graph)
draw_graph(example_graph)