Skip to main content

A small example package

Project description

HDoubleH's Graphs Algorithm Library

This package implements two graph algorithms in python, including Dijkstra's Shortest Path Algorithm and Tarjan's Algorithm for Strongly Connected Components.

Graphs are data structures which connect nodes identified by labels.

  • Dijkstra's Shortest Path Algorithm: This computes the shortest path from a single source node to all the other nodes in a graph. It will find the shortest path from a single source node to all other nodes in a weighted graph with non-negative edge weights. It operates by iteratively expanding the node with the smallest known distance and updating the distances to its neighbors.

  • Tarjan's Algorithm for Strongly Connected Components: This algorithm detects all the strongly connected components (SCCs) in a directed graph using a depth-first search. Each SCC is a maximal subgraph where every node is reachable from every other node in the same subgraph.

Usage

Dijkstra's Shortest Path Algorithm- Create a file with the following named filename.py

```
from graphs_HDoubleH import sp
import sys

if __name__ == '__main__':
    
    if len(sys.argv) != 2:
        print(f'Use: {sys.argv[0]} graph_file')
        sys.exit(1)

    graph = {}
    with open(sys.argv[1], 'rt') as f:
        f.readline() # skip first line
        for line in f:
            line = line.strip()
            s, d, w = line.split()
            s = int(s)
            d = int(d)
            w = int(w)
            if s not in graph:
                graph[s] = {}
            graph[s][d] = w
    
    s = 0
    dist, path = sp.dijkstra(graph, s)
    print(f'Shortest distances from {s}:')
    print(dist)
    for d in path: 
        print(f'spf to {d}: {path[d]}')
```

To run this enter the following in the terminal

python src/filename graph.txt

Tarjan's Algorithm for Strongly Connected Components-

Create a file with the following named tarjan_filename.py

```

import sys
from graphs_HDoubleH import tarjan  # Import Tarjan's Algorithm

if __name__ == '__main__':
    
    if len(sys.argv) != 2:
        print(f'Use: {sys.argv[0]} graph_file')
        sys.exit(1)

    # Load graph from the file
    graph = {}
    with open(sys.argv[1], 'rt') as f:
        f.readline()  # skip the first line
        for line in f:
            line = line.strip()
            s, d, w = line.split()
            s = int(s)
            d = int(d)
            w = int(w)
            if s not in graph:
                graph[s] = {}
            graph[s][d] = w

    # Run Tarjan's Algorithm to find SCCs
    sccs = tarjan.tarjan_scc(graph)
    
    # Output the results
    print(f'Strongly Connected Components (SCCs): {sccs}')

```

To run this enter the following in the terminal


python src/tarjan_filename.py graph.txt

Installation

Package can be installed with - pip3 install graphs_HDoubleH2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphs_hdoubleh2-0.0.1.tar.gz (5.7 MB view details)

Uploaded Source

Built Distribution

graphs_hdoubleh2-0.0.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file graphs_hdoubleh2-0.0.1.tar.gz.

File metadata

  • Download URL: graphs_hdoubleh2-0.0.1.tar.gz
  • Upload date:
  • Size: 5.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for graphs_hdoubleh2-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3fe9089d1ee2d3d9744667c214a56c55acea6bf41d2fde0fb3f3e4f6cc65d552
MD5 2ba922443bdfa5b8638cbf39a7dfdbbb
BLAKE2b-256 9293682873b02194be5cfc894ffa7a20757ce2971f1f0bdda7adc0aa47bc22fa

See more details on using hashes here.

File details

Details for the file graphs_hdoubleh2-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for graphs_hdoubleh2-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d8e5e18d253f4eb63f91fef5df155674ad538f69916a67dc7d945ff718c36bbd
MD5 2c5bc961563a0ff75c9e715cd2ce8c93
BLAKE2b-256 1ccc6e2e2688f19cbbb35ec5d94599861d15e5292bcd0757ab0767e375ad71ad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page