目录

MoonGraph

MoonGraph is a high-performance, general-purpose graph data structure and algorithm library implemented purely in the MoonBit programming language. It is designed to be the foundational building block for complex network analysis, pathfinding, dependency resolution, and compiler infrastructure.

Features

  • Core Data Structures: Adjacency list based Graph[N, E] supporting both Directed and Undirected graphs with generic node/edge weights.
  • Graph Traversal: Highly efficient Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms.
  • Shortest Path & Pathfinding:
    • Dijkstra’s Algorithm: For finding single-source shortest paths in weighted graphs (non-negative weights).
    • A (A-Star) Pathfinding*: Heuristic-based shortest path search.
    • Bellman-Ford Algorithm: Handles negative edge weights and detects negative cycles.
  • Minimum Spanning Tree (MST): Kruskal’s algorithm backed by a robust Disjoint Set (Union-Find) structure.
  • Advanced Algorithms:
    • Topological Sort: Kahn’s algorithm for directed acyclic graphs (DAGs).
    • Tarjan’s SCC: Finding Strongly Connected Components (SCC) in directed graphs.
    • Cycle Detection: Detects cycles in both directed and undirected graphs.
  • Visualization:
    • Graphviz DOT Exporter: Exports the graph structure to Graphviz DOT string representation.
  • Zero Dependencies: Pure MoonBit implementation with no external package dependencies.

Usage

Basic Graph Creation & Traversal

let g: Graph[String, Int] = Graph::new(directed=true)
let a = g.add_node("A")
let b = g.add_node("B")
let c = g.add_node("C")
g.add_edge(a, b, 10)
g.add_edge(b, c, 15)

// Traversals
let bfs_result = g.bfs(a)
let dfs_result = g.dfs(a)

Dijkstra & A* Pathfinding

// Dijkstra's algorithm
let distances = g.dijkstra(a)

// A* pathfinding (with heuristic)
let heuristic = fn(node) { 0 }
let path = g.a_star(a, c, heuristic)

Tarjan’s SCC & Topological Sort

// Find Strongly Connected Components
let sccs = g.tarjan_scc()

// Get Topological Order
let sorted = g.topological_sort()

Exporter (Graphviz DOT)

let dot_string = g.to_dot(
  fn(node_weight) { node_weight }, 
  fn(edge_weight) { edge_weight.to_string() }
)

Future Roadmap

  • Floyd-Warshall Algorithm (All-pairs shortest paths)
  • Maximum Flow / Minimum Cut (Ford-Fulkerson)
  • Graph isomorphism detection

License

MIT License

关于

MoonGraph 是一个专注于提供全面、高效的图论数据结构与算法的 MoonBit 开源基础库。本项目灵感来源于 Rust 生态的优秀基建项目 petgraph,旨在填补目前 MoonBit 生态在通用复杂网络数据结构领域的空白。 MoonGraph 提供了统一且安全的图数据接口(支持有向图、无向图、泛型带权边),并内置了成熟的图遍历(DFS/BFS)、寻路工具(Dijkstra、A* 算法)以

54.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号