graph_explorer

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

A versatile library for generating and analyzing graphs with support for JSON and ASCIIArtTree.

Usage

import 'dart:convert';
import 'package:graph_explorer/graph_explorer.dart';

void main() async {
  var graph = Graph<String>();

  graph.node('a').getOrAddOutput('b')
    ..addOutput('c')
    ..addOutput('d');
  
  graph.node('c').addOutput('f');
  graph.node('d').getOrAddOutput('e').addOutput('f');
  
  graph.node('f').addOutput('x');
  
  var result = await graph.scanPathsFrom('a', 'f', findAll: true);
  
  print("Paths from `a` to `f`:");
  for (var p in result.paths) {
    var lStr = p.toListOfString();
    print('- $lStr');
  }

  var shortest = result.paths.shortestPaths().toListOfStringPaths();
  print('\nShortest paths:');
  for (var p in shortest) {
    print('- $p');
  }

  var tree = graph.toTree();

  print('\nGraph to JSON Tree:');
  print(_encodeJsonPretty(tree));
}

String _encodeJsonPretty(dynamic json) =>
    JsonEncoder.withIndent('  ').convert(json);

Output:

Paths from `a` to `f`:
- [a, b, c, f]
- [a, b, d, e, f]

Shortest paths:
- [a, b, c, f]

Graph to JSON Tree:
{
  "a": {
    "b": {
      "c": {
        "f": {
          "x": null
        }
      },
      "d": {
        "e": {
          "f": "f"  // This is a reference to the node `f` above.
        }
      }
    }
  }
}

ascii_art_tree

With the package ascii_art_tree you can easly generate an ASCII Art Tree of the Graph object:

import 'package:ascii_art_tree/ascii_art_tree.dart'; // Import extension on `Graph`
import 'package:graph_explorer/graph_explorer.dart';

void main() {
  var graph = Graph<String>();

  // Build the graph as
  // shown in the example above...

  // Build the `ASCIIArtTree`:
  var asciiArtTree = graph.toASCIIArtTree();

  // Generate the ASCII Art text:
  var tree = asciiArtTree.generate();
  print(tree);
}

Output:

a
└─┬─ b
  ├─┬─ d
  │ └─┬─ e
  │   └─┬─ f
  │     └──> x
  └─┬─ c
    └─┬─ f ººº

Features and bugs

Please file feature requests and bugs at the issue tracker.

Author

Graciliano M. Passos: gmpassos@GitHub.

Don't be shy, show some love, and become our GitHub Sponsor. Your support means the world to us, and it keeps the code caffeinated! ☕✨

Thanks a million! 🚀😄

License

Dart free & open-source license.

Libraries

graph_explorer
Graph Explorer library