Git Product home page Git Product logo

dg's Introduction

DG

Elixir wrapper of :digraph with a pinch of protocols and sigils

Installation

The package can be installed by adding dg to your list of dependencies in mix.exs:

def deps do
  [
    {:dg, "~> 0.4"},
    ...
  ]
end

Highlights

Inspect

dg = DG.new()
DG.add_vertex(dg, "v1")
DG.add_vertex(dg, "v2", "label 2")
DG.add_vertex(dg, "v3")
DG.add_edge(dg, "v1", "v2")
DG.add_edge(dg, "v1", "v3", "1 to 3")

IO.inspect(dg)

Outputs mermaid.js format flowchart

graph LR
    v1-->v2[label 2]
    v1--1 to 3-->v3

which can be put into livebook mermaid block and will display as

graph LR
    v1-->v2[label 2]
    v1--1 to 3-->v3

Collectable

Easily add

  • vertices [{:vertex, 1}, {:vertex, 2, "label 2"}, ...]
  • or edges [{:edge, 1, 2}, {:edge, "a", "b", "label ab"}, ...]

to the graph via Enum.into/2

dg = DG.new()

~w(a b c d e) |> Enum.map(&{:vertex, &1}) |> Enum.into(dg)

~w(a b c d e)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [f, t] -> {:edge, f, t} end)
|> Enum.into(dg)

IO.inspect(dg)

outputs

graph LR
    b-->c
    c-->d
    a-->b
    d-->e

Functions

  • Most functions from :digraph and :digraph_utils are mirrored under DG.
  • Some functions have their parameters re-arranged so that the graph is always the first parameter. (namely reachable/2, reachable_neighbours/2, reaching/2 and reaching_neighbours/2)
  • new/{0,1,2,3} returns %DG{} instead of an Erlang digraph
  • new/2 and new/3 are shortcuts that can add vertices and edges on creation, which don't exist on :digraph

Sigils

~G and ~g are provided so you can copy mermaid.js flowchart and paste into Elixir to get a %DG{}

import DG.Sigil

dg = ~G"""
graph LR
  a[some label]
  b[other label]
  1-->2
  3[three] -- three to four --> 4[four]
  a --> b
"""
# With interpolation

label = "1 2 3"
dg = ~g"""
graph LR
  a --#{label}--> b
"""

caveat: :digraph is stateful (using ets), don't use the sigil at compile time, e.g. as a module attribute, it won't carry over to runtime. Only use it in runtime code, e.g. function body, and remember to clean up properly when it's no longer used, with delete/1.

Load from libgraph

dg = DG.new()
DG.from({:libgraph, graph})

dg's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

rotwang jdewar

dg's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/elixir.yml
.github/workflows/publish-docs.yml
.github/workflows/publish.yml
mix
mix.exs
  • abnf_parsec ~> 1.2
  • libgraph >= 0.0.0
  • ex_doc >= 0.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

DG.subgraph does not output %DG{}

I had a DG that was too large to print together so I did some edge snipping to make several components to display. I found that I had to manually shove the output of DG.subgraph into a DG struct.

components =
  dg
  |> DG.components()
  |> Enum.map(fn vertices ->
    new_dg = DG.subgraph(dg, vertices, keep_labels: true)
    %DG{dg: new_dg, opts: []}
  end)

components |> Enum.at(0) |> inspect |> Kino.Mermaid.new()

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.