Git Product home page Git Product logo

bellmanford's Introduction

bellmanford

This package provides a few small extensions of the Bellman-Ford routines in NetworkX, primarily for convenience.

Installation

bellmanford is available on PyPI:

pip install bellmanford

Usage

bellman_ford

length, nodes, negative_cycle = bellman_ford(G, source, target, weight='weight')

Compute shortest path and shortest path lengths between a source node and target node in weighted graphs using the Bellman-Ford algorithm.

Parameters

  • G : NetworkX graph
  • pred : dict - Keyed by node to predecessor in the path
  • dist : dict - Keyed by node to the distance from the source
  • source: node label - Source node
  • target: node label - Target node
  • weight : string - Edge data key corresponding to the edge weight

Returns

  • length : numeric - Length of a negative cycle if one exists. Otherwise, length of a shortest path. Length is inf if source and target are not connected.
  • nodes : list - Nodes in a negative edge cycle (in order) if one exists. Otherwise nodes in a shortest path. List is empty if source and target are not connected.
  • negative_cycle : bool - True if a negative edge cycle exists, otherwise False.

Examples

>>> import networkx as nx
>>> G = nx.path_graph(5, create_using = nx.DiGraph())
>>> bf.bellman_ford(G, source=0, target=4)
(3, [1, 2, 3, 4], False)

negative_edge_cycle

length, nodes, negative_cycle = negative_edge_cycle(G, weight='weight')

If there is a negative edge cycle anywhere in G, returns True. Also returns the total weight of the cycle and the nodes in the cycle.

Parameters

  • G : NetworkX graph
  • weight : string, optional (default = 'weight') - Edge data key corresponding to the edge weight

Returns

  • length : numeric - Length of a negative edge cycle if one exists, otherwise None.
  • nodes : list - Nodes in a negative edge cycle (in order) if one exists, otherwise None.
  • negative_cycle : bool - True if a negative edge cycle exists, otherwise False.

Examples

>>> import networkx as nx
>>> import bellmanford as bf
>>> G = nx.cycle_graph(5, create_using = nx.DiGraph())
>>> print(bf.negative_edge_cycle(G))
(None, [], False)
>>> G[1][2]['weight'] = -7
>>> print(bf.negative_edge_cycle(G))
(-3, [4, 0, 1, 2, 3, 4], True)

bellmanford's People

Contributors

nelsonuhan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.