-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added Graph module to Math #3404
Open
jyoung4242
wants to merge
13
commits into
main
Choose a base branch
from
Graph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c116fa6
added Graph.ts to Math
jyoung4242 84927b2
added jsdocs
jyoung4242 9f3c482
checked bfs and dfs, working
jyoung4242 93b470d
got BFS,DFS, and Djikstra working
jyoung4242 f0c0ed7
updated djikstra and shortest path tests
jyoung4242 a49528b
fixed Astar and added distance method
jyoung4242 b438fb3
creating Doc page
jyoung4242 f1a922a
cleaned up jsdocs
jyoung4242 5e8c7d2
updated Math.random() to Random
jyoung4242 adbeb6d
removed fdescribe on Graph spec test
jyoung4242 e947515
file naming shenanigans
jyoung4242 6f74747
fixed graph.ts name
jyoung4242 e82555a
removed local dependency
jyoung4242 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,4 +157,5 @@ | |
"webpack": ">=5.68.0" | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
--- | ||
title: Graph | ||
slug: /graph | ||
section: Math | ||
--- | ||
|
||
## Graphs | ||
|
||
A powerful and flexible graph data structure implementation for working with connected data. This module provides a complete set of tools for creating, manipulating, and traversing graph structures with support for both directed and undirected weighted edges. | ||
|
||
## Overview | ||
|
||
The Graph module allows you to: | ||
|
||
- Create and manage nodes/vertices with custom data | ||
- Connect nodes with weighted, directed or undirected edges | ||
- Position nodes in 2D space for spatial algorithms | ||
- Perform common graph traversal operations like BFS and DFS | ||
- Find optimal paths using Dijkstra's algorithm or A* search | ||
|
||
## Basic Usage | ||
|
||
### Creating a Graph and working with Nodes and Edges | ||
|
||
```ts | ||
import { Graph } from 'excalibur'; | ||
|
||
// Create an empty graph of strings | ||
const graph = new Graph<string>(); | ||
|
||
// Add a few nodes with string data | ||
const nodeA = graph.addNode("A"); | ||
const nodeB = graph.addNode("B"); | ||
const nodeC = graph.addNode("C"); | ||
|
||
// Connect nodes with directed edges (default) | ||
graph.addEdge(nodeA, nodeB); | ||
graph.addEdge(nodeB, nodeC); | ||
graph.addEdge(nodeC, nodeD); | ||
graph.addEdge(nodeD, nodeE); | ||
|
||
// Connect nodes with undirected edges | ||
graph.addEdge(nodeA, nodeC, { directed: false }); | ||
|
||
// Connect nodes with weighted edges | ||
graph.addEdge(nodeA, nodeB, { weight: 5 }); | ||
|
||
// Check if nodes are connected | ||
const connected = graph.areNodesConnected(nodeA, nodeB); // true | ||
|
||
// Get neighbors of a node | ||
const neighbors = graph.getNeighbors(nodeA); // [nodeB] | ||
|
||
// Delete a node (and its edges) | ||
graph.deleteNode(nodeC); | ||
|
||
// Delete an edge | ||
graph.deleteEdge(edges[0]); | ||
``` | ||
|
||
## Core Concepts | ||
|
||
### Node Types | ||
|
||
The Graph module supports several node types: | ||
|
||
Node: Basic graph node with data | ||
PositionNode: Node with 2D spatial coordinates, uses Excalibur's Native Vector type for position | ||
Vertex: An alias for Node for more traditional graph terminology | ||
|
||
```ts | ||
// Add positioned nodes, whe Vector positions are attached to nodes, it returns a PositionNode | ||
const nodeA = graph.addNode("A", new Vector(0, 0)); | ||
const nodeB = graph.addNode("B", new Vector(5, 10)); | ||
const nodeC = graph.addNode("C", new Vector(10, 5)); | ||
``` | ||
|
||
### Edge Properties | ||
|
||
Edges connect nodes and can have properties: | ||
|
||
weight: Numeric value representing distance or cost (default: 0) | ||
directed: Whether the edge is one-way or bidirectional (default: true) | ||
|
||
Using a bidrectional edge will create two edges that are mirrored, and connected by a property. | ||
|
||
```ts | ||
// Connect the nodes | ||
spatialGraph.addEdge(nodeA, nodeB, { weight: 11.2, directed: false }); // Euclidean distance | ||
|
||
``` | ||
|
||
### Graph Traversal | ||
|
||
#### Breadth-First Search (BFS) | ||
|
||
Explore the graph layer by layer, visiting all direct neighbors before moving deeper: | ||
|
||
```ts | ||
// Create and populate your graph first | ||
const visitedNodeIds = graph.bfs(startNode); | ||
``` | ||
|
||
#### Depth-First Search (DFS) | ||
|
||
Explore the graph by moving as far as possible along each branch before backtracking: | ||
|
||
```ts | ||
// Create and populate your graph first | ||
const visitedNodeIds = graph.dfs(startNode); | ||
``` | ||
|
||
### Pathfinding Algorithms | ||
|
||
#### Shortest Path and Dijkstra's Algorithm | ||
|
||
Find the shortest path between two nodes in a weighted graph: | ||
|
||
```ts | ||
// Find shortest path from A to C | ||
const { path, distance } = graph.shortestPathDijkstra(nodeA, nodeC); | ||
|
||
// Get full analysis | ||
const dijkstraAnalysis = graph.dijkstra(nodeA); | ||
``` | ||
|
||
#### A* Algorithm | ||
|
||
Find the shortest path using spatial information for better performance: | ||
|
||
## Other Features | ||
|
||
### Building a Graph from Data Arrays | ||
|
||
For convenience, you can create a graph from arrays of node data: | ||
```ts | ||
// Create a graph with string data nodes | ||
const cities = ["New York", "London", "Tokyo", "Sydney", "Paris"]; | ||
const graph = Graph.createGraphFromNodes(cities); | ||
|
||
// Use alias for more traditional graph terminology | ||
const graph2 = Graph.createGraphFromVertices(cities); | ||
``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious if directed should default to false? No strong data, but I generally use bidirectional edges when I'm building nav mesh type situations.
Also is the direction A->B in
.addEdge(nodeA, nodeB, ...)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changing default is easy, and i can do some parameter renaming to accommodate direction