def Kruskal(graph): """Algorithme de Kruskal Retourne la liste des arretes du graphe de coût minimum """ forest = DisjointSet() chemin = [] for n in graph['Nodes']: forest.add(n) nbrNodes = len(graph['Nodes']) - 1 for arc in sorted(graph['Edges'], key=itemgetter(2)): #O( --- ) n1, n2, poids = arc t1 = forest.find(n1) t2 = forest.find(n2) if t2 != t1: chemin.append(arc) nbrNodes -= 1 if nbrNodes == 0: return chemin forest.union(t1, t2)
def getSurprise(graph, paredges, num_nodes, num_edges): n = num_nodes p = n * (n - 1) / 2 m = num_edges mi, pi = 0, 0 dse = DS() for e in paredges: dse.add(e[0], e[1]) for x in dse.group.values(): ni = len(x) pi += ni * (ni - 1) / 2 mi += graph.subgraph(x).number_of_edges() return compute_surprise(p, pi, m, mi), p, pi, m, mi
def Prim(graph): # A revoir totalement avec tas binaire minimun ou fibonacci """Algorithme de Prim + Union-Find Retourne la liste des arretes du graphe de coût minimum """ forest = DisjointSet() chemin = [] tempEdges = [] usedNodes = [] for n in graph['Nodes']: forest.add(n) nbrNodes = len(graph['Nodes']) origin = graph['Nodes'][0] usedNodes.append(origin) while(len(chemin) != nbrNodes-1): tempNodes = [] tempEdges = [] for node in forest: if forest[node] in usedNodes: # On cherche tous les sommets dont le père fait parti des sommet déjà parcouru tempNodes.append(node) for node in tempNodes: # A AMELIORER !! SOURCE DE PERTE DE TEMPS !!!! for edge in graph['Edges']: if node in edge and edge not in chemin: tempEdges.append(edge) tempEdges = sorted(tempEdges, key=itemgetter(2)) chemin.append(tempEdges[0]) # On prend l'arrete avec le plus petit poids if graph['Type'].upper() == "UNDIRECTED GRAPH": if chemin[-1][0] in usedNodes: forest.union(chemin[-1][0], chemin[-1][1]) usedNodes.append(chemin[-1][0]) else: forest.union(chemin[-1][1], chemin[-1][0]) usedNodes.append(chemin[-1][1]) else: forest.union(chemin[-1][1], chemin[-1][0]) usedNodes.append(chemin[-1][1]) return chemin
def find_blobs(raw_img, args): '''function performing two dimensional connected component analysis on an image. Args: img (ndarray): original image to be analyzed args (Arguments instance): defined the threshold value to binarze the image Returns: an instance of the Components class, a stencil containing the final labels of components, and a stencil containing the labels before eliminating equivalences ''' # dimensions height = raw_img.shape[0] width = raw_img.shape[1] img = processing.threshold(raw_img, args) # adding column of zeros to prevent left and right most blob # form being mistaken as one zeros = np.zeros((height, 1)) img = np.concatenate((img, zeros), axis=1) width += 1 size = height * width img = img.reshape(size) stencil = np.zeros(size, dtype=int) labels = DisjointSet(n_labels=1) # first pass for i in range(size): if img[i] != 0: # if a neighboring pixel is labeled the investigated pixel is given the same label # Note: when iterating from top left to bottom right indices to the right bottom of investigated # pixel cannot be labeled before this pixel for j in [i - 1, i - width, i - width - 1, i - width + 1]: if j < 0 or j >= size: continue if stencil[j] != 0 and stencil[i] == 0: # connection stencil[i] = stencil[j] elif stencil[j] != 0 and stencil[j] != stencil[i]: # conflict labels.unite(stencil[i], stencil[j]) else: # no connection nor conflict continue # if no neighboring pixel is labeled the investigated pixel is give a new label if stencil[i] == 0: new_label = labels.next() stencil[i] = new_label labels.add(new_label) # uncomment to print show labels after first pass # first_pass = deepcopy(stencil.reshape((height, width))) # second pass to eliminate equivalences eq = labels.get_equivalents() for label in eq.keys(): stencil[stencil == label] = eq[label] # reshaping stencil stencil = stencil.reshape((height, width)) # SCIPY VARIANT #stencil = measure.label(img, background=0) # count pixels in blobs, calculate median to filter blobs final_labels = np.arange(1, np.max(stencil) + 1) pixel_counts = [] for label in final_labels: pixel_counts.append(np.sum(stencil == label)) pixel_counts = np.array(pixel_counts) min_allowed_pixels = np.median( pixel_counts[pixel_counts > 0]) / 5 # arbitrary; seems to work well # filter final lables and stencil final_labels = np.array(final_labels)[pixel_counts >= min_allowed_pixels] new_stencil = np.zeros_like(stencil) for i, label in enumerate(final_labels): new_stencil[stencil == label] = i + 1 stencil = new_stencil # construct boxes around letters bounding_boxes = get_bboxes(stencil) # chars = get_chars_from_boxes(raw, bounding_boxes) # extract characters from image in correct order #chars = [] #bounding_boxes = [] #while boxes: # box = heappop(boxes) # chars.append(raw[box[2]:box[3], box[0]:box[1]]) # bounding_boxes.append(box) return Components(boxes=bounding_boxes, img=raw_img, stencil=stencil)