Coder Social home page Coder Social logo

python-dijkstra's Introduction

python-dijkstra

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.

Contents

How to use dijksta module?

You must show your graph as an adjacency matrix. For example, notice this graph with its adjacency matrix:

Drag Racing Drag Racing

Notice that using python's indexing you get a = 0, b = 1 ... g = 6, z = 7

Download dijkstra.py module and copy this in your workspace

Find all distances and paths

dijkstra.find_all(wmat, start, end=-1):

Returns a tuple with a distances' list and paths' list of all remaining vertices with the same indexing.

    (distances, paths)

For example, distances[x] are the shortest distances from x vertex which shortest path is paths[x]. x is an element of {0, 1, ..., n-1} where n is the number of vertices

Args:
wmat    --  weighted graph's adjacency matrix
start   --  paths' first vertex
end     --  (optional) path's end vertex. Return just the 
            distance and its path

Exceptions:
Index out of range, Be careful with start and end vertices.

Example code

import dijkstra # Import the module

# Weighted adjacency matrix
wmat = [[0, 2, 0, 0, 0, 1, 0, 0],
        [2, 0, 2, 2, 4, 0, 0, 0],
        [0, 2, 0, 0, 3, 0, 0, 1],
        [0, 2, 0, 0, 4, 3, 0, 0],
        [0, 4, 3, 4, 0, 0, 7, 0],
        [1, 0, 0, 3, 0, 0, 5, 0],
        [0, 0, 0, 0, 7, 5, 0, 6],
        [0, 0, 1, 0, 0, 0, 6, 0]]

print(dijkstra.find_all(wmat, 0))

Output:

([0, 2, 4, 4, 6, 1, 6, 5], [[0], [0, 1], [0, 1, 2], [0, 5, 3], [0, 1, 4], [0, 5], [0, 5, 6], [0, 1, 2, 7]])

Find the shortest path

dijkstra.find_shortest_path(wmat, start, end=-1):

Returns paths' list of all remaining vertices.

Args:
wmat    --  weighted graph's adjacency matrix
start   --  paths' first vertex
end     --  (optional) path's end vertex. Return just
            the path

Exceptions:
Index out of range, Be careful with start and end vertices.

Example code with final vertex

import dijkstra # Import the module

# Weighted adjacency matrix
wmat = [[0, 2, 0, 0, 0, 1, 0, 0],
        [2, 0, 2, 2, 4, 0, 0, 0],
        [0, 2, 0, 0, 3, 0, 0, 1],
        [0, 2, 0, 0, 4, 3, 0, 0],
        [0, 4, 3, 4, 0, 0, 7, 0],
        [1, 0, 0, 3, 0, 0, 5, 0],
        [0, 0, 0, 0, 7, 5, 0, 6],
        [0, 0, 1, 0, 0, 0, 6, 0]]

print(dijkstra.find_shortest_path(wmat, 0, 7))

Output:

[0, 1, 2, 7]

Example code without final vertex

import dijkstra # Import the module

# Weighted adjacency matrix
wmat = [[0, 2, 0, 0, 0, 1, 0, 0],
        [2, 0, 2, 2, 4, 0, 0, 0],
        [0, 2, 0, 0, 3, 0, 0, 1],
        [0, 2, 0, 0, 4, 3, 0, 0],
        [0, 4, 3, 4, 0, 0, 7, 0],
        [1, 0, 0, 3, 0, 0, 5, 0],
        [0, 0, 0, 0, 7, 5, 0, 6],
        [0, 0, 1, 0, 0, 0, 6, 0]]

print(dijkstra.find_shortest_path(wmat, 0))

Output:

[[0], [0, 1], [0, 1, 2], [0, 5, 3], [0, 1, 4], [0, 5], [0, 5, 6], [0, 1, 2, 7]]

Find the shortest distance

dijkstra.find_shortest_distance(wmat, start, end=-1):

Returns distances' list of all remaining vertices.

Args:
wmat    --  weighted graph's adjacency matrix
start   --  paths' first vertex
end     --  (optional) path's end vertex. Return just
            the distance

Exceptions:
Index out of range, Be careful with start and end vertices.

Example code with final vertex

import dijkstra # Import the module

# Weighted adjacency matrix
wmat = [[0, 2, 0, 0, 0, 1, 0, 0],
        [2, 0, 2, 2, 4, 0, 0, 0],
        [0, 2, 0, 0, 3, 0, 0, 1],
        [0, 2, 0, 0, 4, 3, 0, 0],
        [0, 4, 3, 4, 0, 0, 7, 0],
        [1, 0, 0, 3, 0, 0, 5, 0],
        [0, 0, 0, 0, 7, 5, 0, 6],
        [0, 0, 1, 0, 0, 0, 6, 0]]

print(dijkstra.find_shortest_distance(wmat, 0, 7))

Output:

5

Example code without final vertex

import dijkstra # Import the module

# Weighted adjacency matrix
wmat = [[0, 2, 0, 0, 0, 1, 0, 0],
        [2, 0, 2, 2, 4, 0, 0, 0],
        [0, 2, 0, 0, 3, 0, 0, 1],
        [0, 2, 0, 0, 4, 3, 0, 0],
        [0, 4, 3, 4, 0, 0, 7, 0],
        [1, 0, 0, 3, 0, 0, 5, 0],
        [0, 0, 0, 0, 7, 5, 0, 6],
        [0, 0, 1, 0, 0, 0, 6, 0]]

print(dijkstra.find_shortest_distance(wmat, 0))

Output:

[0, 2, 4, 4, 6, 1, 6, 5]

Drawing graphs

To get a visual representation using the adjacency matrix, you can use the next module draw_graph.py

draw_graph.undirected_graph(wmat, name="weighted_undirected_graph")

Creates a pdf file with the weigthted graph's visualization. You must have installed graphviz (Python conector and OS compilation)

OS: https://www.graphviz.org/

Python module: https://graphviz.readthedocs.io/en/stable/index.html

Args:
wmat  --  weigthted graph's adjacency matrix
name  --  (optional) graph's filenma

Example code

import draw_graph # Import the module

# Weighted adjacency matrix
wmat = [[0, 2, 0, 0, 0, 1, 0, 0],
        [2, 0, 2, 2, 4, 0, 0, 0],
        [0, 2, 0, 0, 3, 0, 0, 1],
        [0, 2, 0, 0, 4, 3, 0, 0],
        [0, 4, 3, 4, 0, 0, 7, 0],
        [1, 0, 0, 3, 0, 0, 5, 0],
        [0, 0, 0, 0, 7, 5, 0, 6],
        [0, 0, 1, 0, 0, 0, 6, 0]]

print(draw_graph.undirected_graph(wmat))

Output file: wmat.pdf

python-dijkstra's People

Contributors

crixodia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

josevilchez247

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.