Coder Social home page Coder Social logo

drudilorenzo / network-flows Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 2.0 542 KB

Network-flows: a c++ command line tool for network optimization problems

License: MIT License

CMake 0.08% C++ 99.64% Python 0.28%
algorithm cmake cpp cycle-canceling data-structures edmonds-karp graph network-flows network-optimization primal-dual

network-flows's Introduction

Network-flows: a c++ command line tool for network optimization problems

c++ CMake Python License: MIT Linux

  1. Description
  2. Project structure
  3. Algorithms
  4. How to use
  5. Python Tester

Description

Command-line solver of the following network flows optimization problems:

  1. Maximum Flow: it seeks a feasible solution that sends the maximum amount of flow from a specified source note s to node t per unit of time.
  2. Minimum Cost Flow: it is the most fundamental of all the network flow problems. It searches for the cheapest possible way of sending a certain amount of flow through a flow network.
  3. In particular, the solver solves the minimum cost maximum flow problem, seeking the least-cost maximum flow.

Algorithms

Maximum Flow:

Minimum Cost Flow:

Basic algorithms:

(See the implementations here)

Project structure

  • data: example graphs
  • docs: report of the project and results of the algorithms applied to the graphs inside data directory
  • pyTest: python tester which permits to easily solve the network flow problems and to draw a graph using matplotlib
  • src: the command-line tool source files

How to use

The following commands are for a generic Linux system, you may need to adapt them depending on your os

Requirements

CMake: minimum version 3.20, see here how to install.

Use your own graph

Both the c++ command line tool and the Python solver requires an input graph described by a well-formed JSON file.
To run the algorithms on you own graph you have to create a JSON file formatted as following:

{
    "Num_nodes": 5,
    "Edges": [
      {
        "Source": 0,
        "Sink": 1,
        "Capacity": 3,
        "Cost": 1
      },
      {
        "Source": 0,
        "Sink": 2,
        "Capacity": 2,
        "Cost": 1
      },
      {
        "Source": 1,
        "Sink": 2,
        "Capacity": 2,
        "Cost": 1
      },
      {
        "Source": 1,
        "Sink": 3,
        "Capacity": 2,
        "Cost": 1
      },
      {
        "Source": 2,
        "Sink": 3,
        "Capacity": 3,
        "Cost": 1
      },
      {
        "Source": 2,
        "Sink": 4,
        "Capacity": 1,
        "Cost": 1
      },
      {
        "Source": 3,
        "Sink": 4,
        "Capacity": 3,
        "Cost": 1
      }
    ]
 }
  • Num_nodes: number of nodes of the graph;
  • Edges: JSON array containing all the edges;
  • Source: source node of the direct edge;
  • Sink: sink node of the edge;
  • Capacity: maximum capacity of the edge;
  • Cost: cost (or weight) per unit flow of the edge,

Note:

  • The first node (source) has index 0;
  • The last node (sink) has index Num_nodes - 1;
  • Each edge must have positive (> 0) capacity and cost.

See data directory for more examples.

Build

  1. Clone the repo:
    git clone [email protected]:LorenzoDrudi/network-flows.git
  1. Enter the repo:
    cd network-flows
  1. Generate project build system:
    cmake -S . -B build

This command creates a build folder containing all the cmake files and the executable.

  1. Build the project:
    cmake --build build

Run

  1. After doing the build, enter the build folder:
  cd build
  1. Run the project:
  ./network_flows path/filename.json

The argument passed to the executable is the full or relative path of the JSON file of the graph
(e.g. ./network_flows ../data/graph1.json).
The filename argument is optional, you can enter it during the execution.

Python Tester

Inside the pyTest directory there is a simple python solver developed using Networkx library. The solver permits to:

  • draw a graph using matplotlib
  • solve the maximum flow problem
  • solve the minimum cost maximum flow problem

Requirements:

Python3: see here how to install

How to use:

The following commands are for a generic linux system, you may need to adapt them depending on your os

  1. Enter the directory:
  cd pyTest

(You can ignore the next 2 steps (2, 3) if you want to install Python dependencies locally)

  1. Create a python virtual environment where install all the required dependencies:
  python3 -m venv venv
  1. Activate the virtual environment:
  source venv/bin/activate
  1. Install requirements:
  pip install -r requirements.txt
  1. Execute the Python solver:
  python3 graph_solver.py path/filename.json

(e.g. python3 graph_solver.py ../data/graph1.json)

  1. Then, when you don't need anymore the python solver, deactivate the virtual environment:
  deactivate

network-flows's People

Contributors

drudilorenzo avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

vck2004 qmarcusv

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.