简体   繁体   English

线性有向无环图

[英]Linear Directed Acyclic Graph

I have an OCR task, where I over-segment the image. 我有一个OCR任务,在该任务中图像过度分割。 Now I want to build a data-structure (a variety of a directed acyclic graph) to get all possible combinations of images. 现在,我想构建一个数据结构(各种有向无环图)以获取图像的所有可能组合。

Example: 例:
这张照片

I start with splitting it into four parts, a [3], b [left half of 4], c[right half of 4], d [2]. 我首先将其分为四个部分,a [3],b [4的左半部分],c [4的右半部分],d [2]。 Now I will combine them variously. 现在,我将对它们进行各种组合。 To get, say, the following paths. 要获得以下路径。

0) a, b, c, d (The basic config) 0)a,b,c,d(基本配置)

1) a, bc, d (The correct config) 1)a,bc,d(正确的配置)

2) ab, c, d 2)ab,c,d

3) a, b, cd 3)a,b,cd

etc. 等等

I am looking to implement this in Python. 我正在寻找在Python中实现这一点。 Is there an existing package? 是否有现有的软件包? If not, what is the best data structure? 如果不是,最好的数据结构是什么? Is DAG the closest? DAG最近吗? Is there a variety of DAG that works better? 有各种各样的DAG效果更好吗?

Thanks, 谢谢,

OK, I have an answer. 好,我有一个答案。 One can implement it like this. 可以这样实现。

import random

def combine(wt1, wt2):
    return random.random() < .1, (wt1+wt2)//2

class Linetree():
    def __init__(self, wts):
        self.nodes = []
        for i, wt in enumerate(wts):
            self.nodes.append([[i+1, wt]])

        self.nodes.append([])
        self.processed = []
        self.checked = []

    def processnode(self, idx):
        if idx in self.processed:
            return

        ichild = 0
        while ichild < len(self.nodes[idx]):
            chidx, chwt = self.nodes[idx][ichild]
            self.processnode(chidx)

            igrandchild = 0
            while igrandchild < len(self.nodes[chidx]):
                grchidx, grchwt = self.nodes[chidx][igrandchild]
                if (idx, grchidx) in self.checked:
                    igrandchild += 1
                    continue

                tocombine, newwt = combine(chwt, grchwt)
                self.checked.append((idx, grchidx))
                if tocombine:
                    self.nodes[idx].append([grchidx, newwt])

                igrandchild += 1
            ichild += 1
        self.processed.append(idx)

    def build(self):
        self.processnode(0)

    def get_paths(self, n=0):
        if len(self.nodes[n]) == 0:
            yield [n]

        for ch, wt in self.nodes[n]:
            for sub_path in self.get_paths(ch):
                yield [n] + sub_path

    def pathwt(self, path):
        ret = 0
        for i in range(len(path)-1):
            for child, wt in self.nodes[path[i]]:
                if child == path[i+1]:
                    ret += wt
                    break
            else:
                raise ValueError(str(path))

        return ret

    def __str__(self):
        ret = ""
        for i, children in enumerate(self.nodes):
            ret += "\nNode {}: ".format(i)
            for child in children:
                ret += "{}, ".format(child)
        return ret

def main():
    wts = range(10, 80, 10)
    print(list(enumerate(wts)))
    lt = Linetree(wts)
    print(lt.nodes)
    print(lt)
    lt.build()
    print(lt)

    paths = lt.get_paths()
    for path in paths:
        print(path, lt.pathwt(path))

main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM