Example #1
0
 def setUp(self):
     self.G = Graph(5)
     self.G.add_edge(1, 2)
     self.G.add_edge(1, 3)
     self.G.add_edge(1, 4)
     self.G.add_edge(2, 4)
     self.G.add_edge(3, 4)
Example #2
0
def random_weighted_graph_resolver(data, _):
    def random_matrix(size, p):
        adj_matrix = [['.'] * size for i in range(size)]

        for i in range(size):
            for j in range(i, size):
                if i != j and random.random() < p:
                    # 0.4 - probability that edge between i and j exists
                    adj_matrix[i][j] = adj_matrix[j][i] = 1
        return adj_matrix

    size = int(data.split(' ')[0])
    p = float(data.split(' ')[1])
    if size < 0:
        return "Size cannot be a negative number"
    matrix = random_matrix(size, p)
    graph = Graph.from_cost_matrix(matrix)
    components = Algorithms.max_connected_comp(graph)
    while len(components) > 1:
        matrix = random_matrix(size, p)
        graph = Graph.from_cost_matrix(matrix)
        components = Algorithms.max_connected_comp(graph)

    for i in range(size):
        for j in range(i, size):
            if matrix[i][j] == 1:
                matrix[i][j] = matrix[j][i] = random.randrange(1, 11)
    s = ""
    for row in matrix:
        s += f"{'  '.join([f' {cell}' if len(str(cell)) == 1 else f'{cell}' for cell in row])}\n"

    return s
Example #3
0
def randomization_resolver(graph: Graph, args):
    permutations = int(args[0])
    try:
        graph.randomize(permutations)
        return AdjacencyMatrix(graph)
    except Exception:
        return 'Graph cannot be randomized'
Example #4
0
def bellman_ford_resolver(graph: Graph, args):
    try:
        dist, pred = graph.BellmanFord()
    except Exception as e:
        return str(e)

    ret = "Tablica odległości: \n"
    ret += str(dist) + "\n\n"

    ret += "Ścieżki: \n"
    for i in range(len(pred)):
        if pred[i] is None:
            pass
        current = i
        path = []
        while current != None:
            path.append(current)
            current = pred[current]
        ret += f'{i}: '
        path = path[::-1]
        for v in path:
            ret += f'({v}) '
        ret += f'\n'

    return ret
Example #5
0
def random_strongly_connected_component_resolver(data, _):
    def random_matrix(n, p):
        adj_matrix = [['.'] * n for i in range(n)]

        for i in range(n):
            for j in range(n):
                if i != j and random.random() < p:
                    adj_matrix[i][j] = 1
        return adj_matrix

    n = int(data.split(' ')[0])
    p = float(data.split(' ')[1])

    if n < 0:
        return "Size cannot be a negative number"
    if p < 0 or p > 1:
        return "Probability not in range [0, 1]"
    graph = Graph()
    adj_matrix = AdjacencyMatrix(graph)
    adj_matrix.matrix = random_matrix(n, p)
    components = adj_matrix.Kosaraju().split('\n')
    while len(components) > 1:
        adj_matrix.matrix = random_matrix(n, p)
        components = adj_matrix.Kosaraju().split('\n')

    numbers = list(range(-5, 11))
    for i in range(n):
        for j in range(n):
            if adj_matrix.matrix[i][j] == 1:
                adj_matrix.matrix[i][j] = random.choice(numbers)
    return adj_matrix
Example #6
0
    def __init__(self, node_radius, rows=None, columns=None):
        self.rows, self.cols = rows, columns
        self.node_radius = node_radius

        if rows is not None:
            self.gameWidth, self.gameHeight = 2 * node_radius * columns, 2 * node_radius * rows
        else:
            self.gameWidth, self.gameHeight = 500, 500

        self.graph = Graph()
        self.currentNode = None
        self.shortestPath = []
        self.findingPath = False

        self.choosingStart = True
        self.choosingGoal = False
        self.creatingWalls = False

        pygame.init()

        self.gameDisplay = pygame.display.set_mode(
            (self.gameWidth, self.gameHeight))
        self.clock = pygame.time.Clock()
        self.running = True
        self.dragging = False

        if rows is not None:
            self.create_nodes()
Example #7
0
def create_random_eulerian_resolver(data, _):
    nodes = int(data)

    eulerian_graph = Algorithms.create_random_eulerian(nodes)
    adjacency_list = AdjacencyList(eulerian_graph)
    print(Graph.from_adjacency_list(adjacency_list))
    euler_cycle = AdjacencyList.find_euler_path(adjacency_list, Node(0))
    return euler_cycle
Example #8
0
def display_loaded_page(search, dark_theme):
    uid = search[6:]
    try:
        oid = ObjectId(uid)
    except InvalidId:
        return html.Div([
            error_layout(
                "Analysis Not Found",
                "the url you entered is either incorrect or the data has expired",
            ),
            dcc.Link("Start another analysis", href="/"),
        ])
    processed_data = get_processed_data(oid)
    if processed_data:
        p = Parser()
        dfs, participants = p.reload_data(processed_data[URI])

        # Update the Graph class with the data and set the default graph template
        g = Graph()

        g.df = p.parsed_df
        g.dfs = dfs
        g.participants = participants
        g.color_map = processed_data[COLOR_MAP]
        g.media_counter = processed_data[MEDIA_COUNTER]

        # Create the final layout with Dash Graphs
        return graph_layout(g, dark_theme)
    else:
        return error_layout
Example #9
0
def random_digraph_resolver(data, _):
    n = int(data.split(' ')[0])
    p = float(data.split(' ')[1])

    graph = Graph()
    am = AdjacencyMatrix(graph)
    am.random_digraph(n, p)

    return am
Example #10
0
def k_regular_graph_resolver(args, _):

    [nodes, degree] = [int(num) for num in args.split(' ')]

    result = Algorithms.generate_random_regular_graph(degree, nodes)
    graph = Graph()
    adj_matrix = AdjacencyMatrix(graph)
    adj_matrix.matrix = result

    return adj_matrix
Example #11
0
class TestGraph(unittest.TestCase):
    def setUp(self):
        self.G = Graph(5)
        self.G.add_edge(1, 2)
        self.G.add_edge(1, 3)
        self.G.add_edge(1, 4)
        self.G.add_edge(2, 4)
        self.G.add_edge(3, 4)

    def testing_len(self):
        self.assertEqual(self.G.len(), 5)

    def testing_nodes_adjacents(self):
        self.assertEqual(self.G[4], [1, 2, 3])
        self.assertEqual(self.G[2], [1, 4])
        self.assertEqual(self.G[0], [])

    def testing_degree(self):
        self.assertEqual(self.G.degree(4), 3)
        self.assertEqual(self.G.max_degree(), 3)
Example #12
0
class TestCC(unittest.TestCase):
    def setUp(self):
        self.G = Graph(6)
        self.G.add_edge(1, 2)
        self.G.add_edge(1, 3)
        self.G.add_edge(1, 4)
        self.G.add_edge(2, 4)
        self.G.add_edge(3, 4)
        self.G.add_edge(0, 5)

    def testing_path_to(self):
        cc = ConnectedComponent(self.G)
        self.assertEqual(cc.len(), 2)
Example #13
0
class TestBFS(unittest.TestCase):
    def setUp(self):
        self.G = Graph(6)
        self.G.add_edge(1, 2)
        self.G.add_edge(1, 3)
        self.G.add_edge(1, 4)
        self.G.add_edge(2, 4)
        self.G.add_edge(3, 4)
        self.G.add_edge(0, 5)

    def testing_path_to(self):
        bfs = BFS(self.G, 4)
        self.assertEqual(bfs.path_to(self.G, 1), [1, 4])
        self.assertEqual(bfs.path_to(self.G, 2), [2, 4])
        self.assertEqual(bfs.path_to(self.G, 0), None)

    def testing_has_path_to(self):
        bfs = BFS(self.G, 4)
        self.assertEqual(bfs.has_path_to(self.G, 1), True)
        self.assertEqual(bfs.has_path_to(self.G, 2), True)
        self.assertEqual(bfs.has_path_to(self.G, 0), False)
Example #14
0
def directed_cost_matrix(filename):
    with open(filename) as f:
        data = f.read()

        dir_cost_mat = parse_matrix(data)
        return Graph.from_cost_matrix(dir_cost_mat, is_directed=True)
Example #15
0
def cost_matrix(filename):
    with open(filename) as f:
        data = f.read()

        cost_mat = parse_matrix(data)
        return Graph.from_cost_matrix(cost_mat)
Example #16
0
def directed_adjacency_matrix(filename):
    with open(filename, 'r') as file:
        data = file.read()

        adj_matrix = parse_matrix(data)
        return Graph.from_adjacency_matrix(adj_matrix, is_directed=True)
Example #17
0
def adjacency_matrix(filename):
    with open(filename, 'r') as file:
        data = file.read()

        adj_matrix = parse_matrix(data)
        return Graph.from_adjacency_matrix(adj_matrix)
Example #18
0
from datautils.processor import process_data
from graphs.Graph import Graph
from layouts.graph_layout import graph_layout
from layouts.error_layout import error_layout
from services.counter_service import get_chat_count, get_message_count

CIRCLE_LOADING = "circle-loading"
CONTENTS = "contents"
GRAPH = "graph-container"
SHARE_URL = "share-url"
SHARE_BUTTON = "share-button"
UPLOAD = "upload-data"

# This graph and parser objects needs to be initialized here as their data
# will be shared by most of the methods
g = Graph()
parser = Parser()

layout = html.Div(
    [
        dcc.Upload(
            id=UPLOAD,
            children=html.Div(
                [
                    "Drag and drop or ",
                    html.A("select a file"),
                    " - note that processing may take as long as 2 minutes, or even longer",
                ]
            ),
            style={
                "width": "100%",