当前位置: 首页>>代码示例>>Python>>正文


Python Graph.cliques方法代码示例

本文整理汇总了Python中igraph.Graph.cliques方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.cliques方法的具体用法?Python Graph.cliques怎么用?Python Graph.cliques使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在igraph.Graph的用法示例。


在下文中一共展示了Graph.cliques方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _cliques

# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import cliques [as 别名]
    def _cliques(self, images, prefix='c'):
        g = Graph()
        vertices = {}
        image_to_vert = {}
        cmd_to_vert = {}
        edges = []

        # All commands that are used in these images.
        commands = set()
        for cmds in images.values():
            commands.update(cmds)

        # Vertices are all images and all commands.
        idx = 0
        for i in images:
            vertices[idx] = 'image-%s' % i
            image_to_vert[i] = idx
            idx += 1
        for c in commands:
            vertices[idx] = 'cmd-%s' % c
            cmd_to_vert[c] = idx
            idx += 1

        g.add_vertices(vertices)

        # Add nodes for all images and connect them.
        for i1, i2 in combinations(images, 2):
            edges.append((image_to_vert[i1], image_to_vert[i2]))

        # Add nodes for all commands and connect them.
        for c1, c2 in combinations(commands, 2):
            edges.append((cmd_to_vert[c1], cmd_to_vert[c2]))

        # Connect images and commands that are related.
        for i, cmds in images.items():
            for c in cmds:
                edges.append((image_to_vert[i], cmd_to_vert[c]))

        g.add_edges(edges)

        # Find all maximal cliques. Filter out those with < 2 images.
        # Fix the image and command names.
        cliques = []
        num = 1
        for c in g.cliques(3):  # At least 2 images and 1 command
            imgs = set([
                vertices[x].replace('image-', '') for x in c
                if vertices[x].startswith('image-')
            ])
            cmds = set([
                vertices[x].replace('cmd-', '') for x in c
                if vertices[x].startswith('cmd-')
            ])
            if len(imgs) > 1 and cmds:
                total_time = sum(self.commands[c] for c in cmds)

                name = '%s%d' % (prefix, num)

                # Find all the child cliques of this clique.
                children = []
                if len(imgs) > 2:
                    new_images = {}
                    for img in imgs:
                        new_cmds = set(images[img]) - cmds
                        if new_cmds:
                            new_images[img] = new_cmds

                    if len(new_images) > 1:
                        children.append(self._cliques(new_images, prefix='%s_' % name))

                cliques.append({
                    'name':     '%s%d' % (prefix, num),
                    'time':     total_time,
                    'images':   imgs,
                    'commands': cmds,
                    'children': children
                })

                num += 1

        # Find intersections among them.
        by_img = defaultdict(set)
        for cl in cliques:
            for c in cl['images']:
                by_img[c].add(cl['name'])

        intersections = set()
        for x in by_img.values():
            if len(x) > 1:
                intersections.add(tuple(sorted(x)))

        return {'cliques': cliques, 'intersections': intersections}
开发者ID:ryanjoneil,项目名称:docker-image-construction,代码行数:94,代码来源:problem.py


注:本文中的igraph.Graph.cliques方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。