GithubHelp home page GithubHelp logo

vitor-alves / floyd-warshall Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cy94/floyd-warshall

0.0 0.0 0.0 164 KB

Implementation of the Floyd-Warshall all pairs shortest paths algorithm in C++ and Python, with testcases

floyd-warshall's Introduction

floyd-warshall

This README outlines an implementation of the Floyd Warshall algorithm by - Ananth Murthy ([email protected]) Chandan Yeshwanth ([email protected])

Contents

  1. Introduction

  2. Data structures used

  3. Pseudocode

  4. Compiling and Executing

  5. Contribution

  6. References

  7. Introduction


The Floyd Warshall algorithm is used to find shortest paths between all pairs of vertices in a graph. It is a dynamic-programming algorithm; shortest path distances are calculated bottom up, these estimates are refined until the shortest path is obtained. Positive and zero weight cycles in the graph are ignored, and negative weight cycles are detected. The algorithm runs in O(V^3) time, where V is the number of vertices in the graph.

  1. Data structures used

  1. Python - adjacency and parent matrices stored using standard Python list

  2. C++ - adjacency matrix is a matrix of Distance objects, implemented by class Distance. Operator overloading takes care of infinite distance values. Parent matrix stored using regular int matrix

  3. Pseudocode


The Floyd Warshall Algorithm finds all-pairs shortest paths given the adjacency matrix of a graph. The algorithm here is from the textbook mentioned in the references section (page 695).

Given an adjacency matrix W,

FLOYD-WARSHALL( W ):
	n = W.rows
	D(0) = W
	for k = 1 to n
		let D(k) = = [ d[i][j](k) ] be a new n x n matrix
		for i = 1 to n
			for j = 1 to n
				d[i][j](k) = min(d[i][j](k-1), d[i][k](k) + d[k][j](k-1))

	return D(n)
  1. Compiling and Execution

Several testcases are included in the 'testcases' folder, along with the expected output files. <input_file> path must be given as "testcases/inputX" where X = 1 to 5 to use provided inputs.

  1. Python - execution python FloydWarshall.py <input_file>
  2. C++ - compile using the makefile make all - execution ./FloydWarshall <input_file>

Both Python and C++ versions use the same input and output formats.

Input
-----
(number of vertices V - integer)
(number of edges    E - integer)
(u - integer)   (v - integer)   (weight of edge connecting u, v)
(from 1 to V)   (from 1 to V)   (edge  is  considered  directed)
.
.
. (E lines)

Output
------
All Pairs Shortest Paths : 

<groups of 3 lines>

From : u To : v
Path : u <u1 u2 ... if path exists else "no path to"> v
Distance : <shortest distance if it exists else "inf">	

If the graph contains a negative weight cycle, "Negative cycle at : X" is displayed where X is a vertex. 
The program is then terminated.
  1. Contribution

Ananth Murthy

  • implemented Python version
  • implemented the algorithm in C++
  • wrote sections 1-3 of this README
  • wrote testcases and expected output 2, 4 (negative testcases)

Chandan Yeshwanth

  • implemented C++ Distance class
  • implemented display of shortest paths
  • wrote sections 4-6 of this README
  • wrote testcases and expected output 1, 3, 5 (positive testcases)
  1. References

  1. Introduction to Algorithms 3rd edition, Cormen, Leiserson, Rivest, Stein
  2. Wikipedia page on Floyd Warshall algorithm (en.wikipedia.org/wiki/Floyd–Warshall_algorithm)

floyd-warshall's People

Contributors

cy94 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.