Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision 48e26abf0067fa41b92e8f82533fd00121acaec6 authored by Mikhail Kolmogorov on 05 May 2015, 23:21:22 UTC, committed by Mikhail Kolmogorov on 05 May 2015, 23:21:22 UTC
some refactoring
1 parent 8f07fce
  • Files
  • Changes
  • 61fab56
  • /
  • ragout
  • /
  • breakpoint_graph
  • /
  • adjacency_graph.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:48e26abf0067fa41b92e8f82533fd00121acaec6
directory badge
swh:1:dir:6fa6fda2502f03aed997127e3750f8604fd6cb48
content badge
swh:1:cnt:6ad7f6bd71ce622c704d768758b04fe21b8eec39

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
adjacency_graph.py
#(c) 2013-2014 by Authors
#This file is a part of Ragout program.
#Released under the BSD license (see LICENSE file)

"""
Adjacency graph -- weighted graph with black edges.
Provides some useful methods for analysis
"""

from collections import defaultdict

import networkx as nx

from ragout.shared.priority_queue import PriorityQueue

class AdjacencyGraph(object):
    """
    A wrapper for convenient use
    """
    def __init__(self, bp_graph, phylogeny):
        self.graph = self._from_bp_graph(bp_graph, phylogeny)
        self.orphan_weight = 0

    def _from_bp_graph(self, bp_graph, phylogeny):
        """
        Constructs adjacency graph (weighted with black edges)
        """
        weighted_graph = bp_graph.to_weighted_graph(phylogeny)
        adj_graph = nx.MultiGraph()
        adj_graph.add_edges_from(weighted_graph.edges(data=True))
        black_nodes = set()
        for node_1, node_2 in bp_graph.contig_ends:
            adj_graph.add_edge(node_1, node_2, black=True)
            black_nodes.add(node_1)
            black_nodes.add(node_2)

        for node in adj_graph.nodes():
            if node not in black_nodes:
                adj_graph.remove_node(node)
        return adj_graph

    def _neighbors(self, node, colored):
        neighbors = []
        for neighbor in self.graph[node]:
            for edge in self.graph[node][neighbor].values():
                if ("black" not in edge) == colored:
                    neighbors.append(neighbor)
        return neighbors

    def _edge_weight(self, node_1, node_2, colored):
        if colored:
            if self.graph.has_edge(node_1, node_2):
                for value in self.graph[node_1][node_2].values():
                    if "weight" in value:
                        return value["weight"]
            return self.orphan_weight
        else:
            return 0

    def shortest_path(self, src, dst, prohibited_nodes, orphaned_nodes):
        """
        Finds shortest path wrt to prohibited and orphaned nodes
        """
        #logger.debug("Finding path from {0} to {1}".format(src, dst))
        dist = defaultdict(lambda: float("inf"))
        dist[src] = 0
        parent = {}
        queue = PriorityQueue()
        queue.insert((src, True), 0)

        found = False
        while queue.get_length():
            cur_dist, (cur_node, colored) = queue.pop()
            if cur_node == dst:
                if not colored:
                    found = True
                    break
                else:
                    continue

            if cur_node != src and cur_node in prohibited_nodes:
                continue

            neighbors = self._neighbors(cur_node, colored)
            if colored and cur_node in orphaned_nodes:
                extra_neighbors = orphaned_nodes - set([cur_node])
                neighbors.extend(list(extra_neighbors))

            for other_node in neighbors:
                weight = self._edge_weight(cur_node, other_node, colored)

                if dist[other_node] > dist[cur_node] + weight:
                    dist[other_node] = dist[cur_node] + weight
                    parent[other_node] = cur_node
                    queue.insert((other_node, not colored), dist[other_node])

        if not found:
            return None

        path = [dst]
        cur_node = dst
        while cur_node != src:
            path.append(parent[cur_node])
            cur_node = parent[cur_node]
        return path[::-1]
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API