feat: solution for 'Повторный экзамен #2: Граф с рефлексией на код'

This commit is contained in:
2026-07-01 11:22:21 +03:00
parent 7832ec4f07
commit 9412dbf851
7 changed files with 240 additions and 101 deletions
+53 -32
View File
@@ -1,51 +1,72 @@
# Graph with Reflection
# Graph with Reflexivity
This project implements an undirected graph using an adjacency list and provides reflection capabilities that record all operations performed on the graph.
A lightweight JavaScript implementation of a directed graph where every node automatically has a selfloop (reflexive edge).
The library is intentionally minimal and does **not** depend on any external graph libraries or Python code.
## Features
- **Adjacency List**: Efficient storage and traversal of graph nodes and edges.
- **Reflection**: Every mutating operation (`addNode`, `addEdge`, `removeNode`, `removeEdge`) is logged.
- **API**:
- `addNode(node)`
- `addEdge(u, v)`
- `removeNode(node)`
- `removeEdge(u, v)`
- `getNeighbors(node)`
- `hasEdge(u, v)`
- `getNodes()`
- `getLog()` returns a copy of the operation log.
- **Automatic reflexivity** when a node is added, an edge from the node to itself is created.
- **Directed edges** you can add edges in any direction.
- **Simple API** add nodes, add edges, query edges, list nodes, list edges.
- **Pure JavaScript** works in Node.js environments.
## Installation
```bash
npm install
npm install graph-reflexivity
```
## Running Tests
> If you want to run the tests or develop locally, clone the repository and run `npm install`.
## Usage
```js
const Graph = require('graph-reflexivity');
const g = new Graph();
// Add nodes
g.addNode('A');
g.addNode('B');
// Add directed edge A → B
g.addEdge('A', 'B');
// Reflexive edges are automatically added
console.log(g.hasEdge('A', 'A')); // true
console.log(g.hasEdge('B', 'B')); // true
// Query
console.log(g.getNeighbors('A')); // ['A', 'B']
console.log(g.nodes()); // ['A', 'B']
console.log(g.edges()); // [['A', 'A'], ['B', 'B'], ['A', 'B']]
```
## API
| Method | Description |
|--------|-------------|
| `addNode(node)` | Adds a node and its reflexive edge. |
| `addEdge(from, to)` | Adds a directed edge; missing nodes are created automatically. |
| `hasEdge(from, to)` | Returns `true` if an edge exists. |
| `getNeighbors(node)` | Returns an array of all neighbors of `node`. |
| `nodes()` | Returns an array of all nodes. |
| `edges()` | Returns an array of `[from, to]` pairs for all edges. |
## Testing
The project uses Jest for unit tests.
```bash
npm test
```
## Usage
All tests are located in the `tests/` directory.
```js
const { createGraph } = require('./src/index');
## License
const graph = createGraph();
MIT © Your Name
graph.addNode('a');
graph.addNode('b');
graph.addEdge('a', 'b');
---
console.log(graph.getNeighbors('a')); // ['b']
console.log(graph.getLog());
// [
// { method: 'addNode', args: ['a'] },
// { method: 'addNode', args: ['b'] },
// { method: 'addEdge', args: ['a', 'b'] }
// ]
```
The `getLog()` method returns a snapshot of all recorded operations, allowing you to inspect the history of changes made to the graph.
Feel free to open issues or pull requests if you find bugs or want to add features.