feat: solution for 'Повторный экзамен #2: Граф с рефлексией на код'
This commit is contained in:
@@ -1,60 +1,51 @@
|
||||
# Graph with Reflection on Code – Refactored Implementation
|
||||
# Graph with Reflection
|
||||
|
||||
This repository contains a minimal, self‑contained Python implementation of an undirected graph that uses a single, consistent approach: an adjacency list represented by a dictionary of sets.
|
||||
The original assignment required that the solution use only one approach; this refactor removes any mixed‑strategy code and provides a clean, well‑documented API.
|
||||
This project implements an undirected graph using an adjacency list and provides reflection capabilities that record all operations performed on the graph.
|
||||
|
||||
## Features
|
||||
|
||||
- **Add / remove nodes** – Nodes are any hashable Python objects.
|
||||
- **Add / remove edges** – Undirected edges; self‑loops (reflexive edges) are allowed.
|
||||
- **Query adjacency** – Retrieve neighbors, check for an edge, list all nodes or edges.
|
||||
- **Automatic node creation** – Adding an edge automatically creates missing nodes.
|
||||
- **Readable representation** – `__repr__` and `__str__` give a quick overview of the graph.
|
||||
- **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.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
## Running Tests
|
||||
|
||||
```bash
|
||||
npm test
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
from src.index import Graph
|
||||
```js
|
||||
const { createGraph } = require('./src/index');
|
||||
|
||||
# Create an empty graph
|
||||
g = Graph()
|
||||
const graph = createGraph();
|
||||
|
||||
# Add edges (nodes are created automatically)
|
||||
g.add_edge("A", "B")
|
||||
g.add_edge("B", "C")
|
||||
g.add_edge("C", "A") # triangle
|
||||
g.add_edge("D", "D") # reflexive edge
|
||||
graph.addNode('a');
|
||||
graph.addNode('b');
|
||||
graph.addEdge('a', 'b');
|
||||
|
||||
print(g) # Pretty print
|
||||
|
||||
# Query
|
||||
print("Neighbors of B:", g.neighbors("B"))
|
||||
print("Has edge (A, D)?", g.has_edge("A", "D"))
|
||||
|
||||
# Modify
|
||||
g.remove_edge("A", "B")
|
||||
g.remove_node("C")
|
||||
|
||||
print("After modifications:")
|
||||
print(g)
|
||||
console.log(graph.getNeighbors('a')); // ['b']
|
||||
console.log(graph.getLog());
|
||||
// [
|
||||
// { method: 'addNode', args: ['a'] },
|
||||
// { method: 'addNode', args: ['b'] },
|
||||
// { method: 'addEdge', args: ['a', 'b'] }
|
||||
// ]
|
||||
```
|
||||
|
||||
## Running the Example
|
||||
|
||||
```bash
|
||||
python -m src.index
|
||||
```
|
||||
|
||||
The script will output the graph state after each operation.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
src/
|
||||
└── index.py # Graph implementation
|
||||
README.md # Documentation
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is released under the MIT License.
|
||||
The `getLog()` method returns a snapshot of all recorded operations, allowing you to inspect the history of changes made to the graph.
|
||||
Reference in New Issue
Block a user