Exemplo n.º 1
0
class GreddyConstruction(object):
    def __init__(self, adj_matrix, size):
        self.size = size
        self.adj_list = AdjacencyList(size)
        for i in range(len(adj_matrix)):
            for j in range(len(adj_matrix)):
                aux = adj_matrix[i][j]
                self.adj_list.insert(i, j, aux)
                self.adj_list.insert(j, i, aux)
        self.adj_list = self.adj_list.p_list()

    def build(self, alfa, solution, origin=0):
        processed = [False for i in range(self.size)]
        pq = [(0, origin)]

        while pq:
            index = randint(0, int(alfa * len(pq))) - 1
            u = pq[0 if index < 0 else index][1]
            solution.append(u)
            processed[u] = True
            pq.clear()

            for i in range(0, len(self.adj_list[u])):
                v = self.adj_list[u][i][0]
                if not processed[v]:
                    cost = self.adj_list[u][i][1]
                    pq.append((cost, v))

            pq.sort()

        solution.append(0)
Exemplo n.º 2
0
 def __init__(self, adj_matrix, size):
     self.size = size
     self.adj_list = AdjacencyList(size)
     for i in range(len(adj_matrix)):
         for j in range(len(adj_matrix)):
             aux = adj_matrix[i][j]
             self.adj_list.insert(i, j, aux)
             self.adj_list.insert(j, i, aux)
     self.adj_list = self.adj_list.p_list()
Exemplo n.º 3
0
def dfs_tree(graph, start):
	"""
	Return the the traversel tree of a DFS run from the node `start`
	in the graph `graph`
	"""
	tree = AdjacencyList()
	visited = {start}
	def dfs_tree_rec(node):
		for succ in graph[node]:
			if succ not in visited:
				visited.add(succ)
				tree.add_edge(node, succ)
				dfs_tree_rec(succ)
	dfs_tree_rec(start)
	return tree
Exemplo n.º 4
0
    for i in range(graph.get_number()):
        if not vertex_visited_list[i]:
            depth_first_search_list(graph, i)


a = VertexNode('A')
b = VertexNode('B')
c = VertexNode('C')
d = VertexNode('D')
e = VertexNode('E')
f = VertexNode('F')
g = VertexNode('G')
h = VertexNode('H')
i = VertexNode('I')

test_list = AdjacencyList(False, a, b, c, d, e, f, g, h, i)
test_list.add_arc(a, b, 1)
test_list.add_arc(a, f, 1)
test_list.add_arc(b, g, 1)
test_list.add_arc(f, g, 1)
test_list.add_arc(e, f, 1)
test_list.add_arc(e, h, 1)
test_list.add_arc(d, e, 1)
test_list.add_arc(d, h, 1)
test_list.add_arc(g, h, 1)
test_list.add_arc(d, g, 1)
test_list.add_arc(d, i, 1)
test_list.add_arc(c, d, 1)
test_list.add_arc(c, i, 1)
test_list.add_arc(b, c, 1)
test_list.add_arc(b, i, 1)
Exemplo n.º 5
0
'''
    Generator 9x9 sudoku matrix.
    hide some cell value from generated sudoku matrix
    check if provided matrix is same as the generated
'''
import random as random
import numpy as np
from adjacency_list import AdjacencyList

ADJACENT_LIST = AdjacencyList()
class Generator:
    '''
        generate 9x9 sudoku matrix using graph coloring method
    '''
    def __init__(self):
        self.list = ADJACENT_LIST.generate()
        self.colors = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.sudoku = np.zeros((9, 9))
        self.result = np.zeros((9, 9))
    def __assign_color__(self, vertex, colors, r_sudoku):
        '''
            assign a value to vertex depending on the adjacency list
        '''
        # create a local value so that on backtrack previous matrix can be used
        sudoku = np.empty_like(r_sudoku)
        sudoku[:] = r_sudoku
        # get the position in 9x9 matrix
        i = int(vertex/9)
        j = vertex % 9
        # tail case
        if vertex >= 81:
Exemplo n.º 6
0
from adjacency_list import AdjacencyList

def dfs_tree(graph, start):
	"""
	Return the the traversel tree of a DFS run from the node `start`
	in the graph `graph`
	"""
	tree = AdjacencyList()
	visited = {start}
	def dfs_tree_rec(node):
		for succ in graph[node]:
			if succ not in visited:
				visited.add(succ)
				tree.add_edge(node, succ)
				dfs_tree_rec(succ)
	dfs_tree_rec(start)
	return tree

if __name__ == '__main__':
	graph = AdjacencyList()
	graph.add_edges([(0, 1), (1, 2), (0, 2)])
	tree = dfs_tree(graph, 0)
	print(tree.adj)
Exemplo n.º 7
0
import math


def dijkstra_distances(graph, start):
    """
	Return the minimum distance from `start` to each node
	in the positively weighted graph `graph`
	"""
    distances = {node: math.inf for node in graph.nodes()}
    distances[start] = 0
    visited = set()
    heap = [(0, start)]
    while heap:
        cost, node = heapq.heappop(heap)
        if node in visited:
            continue
        visited.add(node)
        for succ, weight in graph.data(node, 'weight'):
            new_dist = cost + weight
            if new_dist < distances[succ]:
                distances[succ] = new_dist
                heapq.heappush(heap, (new_dist, succ))
    return distances


if __name__ == '__main__':
    graph = AdjacencyList()
    graph.add_edge(0, 1, weight=1)
    graph.add_edge(1, 2, weight=2)
    graph.add_edge(0, 2, weight=4)
    print(dijkstra_distances(graph, 0))