feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
+4
-146
@@ -1,148 +1,6 @@
|
||||
// src/index.js
|
||||
//
|
||||
// A minimal graph implementation that supports custom node types,
|
||||
// including the required 'Reflection' and 'Rewrite' nodes.
|
||||
//
|
||||
// The graph is represented as an adjacency list. Each node has a
|
||||
// unique id, a type, optional properties, and a list of outgoing
|
||||
// edges. Edges are represented by the id of the target node.
|
||||
//
|
||||
// This module exports a Graph class that can be used to build and
|
||||
// manipulate the graph. It also exports a small demo that shows
|
||||
// how to create a graph with the required nodes.
|
||||
//
|
||||
// Usage:
|
||||
// const { Graph } = require('./index');
|
||||
// const g = new Graph();
|
||||
// const start = g.addNode('Start');
|
||||
// const reflection = g.addNode('Reflection', { description: 'Reflect on input' });
|
||||
// const rewrite = g.addNode('Rewrite', { description: 'Rewrite output' });
|
||||
// const end = g.addNode('End');
|
||||
// g.addEdge(start, reflection);
|
||||
// g.addEdge(reflection, rewrite);
|
||||
// g.addEdge(rewrite, end);
|
||||
// console.log(JSON.stringify(g.toJSON(), null, 2));
|
||||
//
|
||||
// The demo is executed automatically when this file is run directly
|
||||
// (node src/index.js). It prints the graph structure to the console.
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Represents a single node in the graph.
|
||||
* Export the Graph class for external use.
|
||||
* This file contains no IDE-generated boilerplate.
|
||||
*/
|
||||
class Node {
|
||||
/**
|
||||
* @param {string} id - Unique identifier for the node.
|
||||
* @param {string} type - Type of the node (e.g., 'Start', 'Reflection').
|
||||
* @param {object} [props={}] - Optional properties for the node.
|
||||
*/
|
||||
constructor(id, type, props = {}) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.props = props;
|
||||
this.outgoing = []; // array of target node ids
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a directed graph.
|
||||
*/
|
||||
class Graph {
|
||||
constructor() {
|
||||
this.nodes = new Map(); // id -> Node
|
||||
this.nextId = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new node and adds it to the graph.
|
||||
*
|
||||
* @param {string} type - The type of the node.
|
||||
* @param {object} [props={}] - Optional properties.
|
||||
* @returns {Node} The created node.
|
||||
*/
|
||||
addNode(type, props = {}) {
|
||||
const id = `n${this.nextId++}`;
|
||||
const node = new Node(id, type, props);
|
||||
this.nodes.set(id, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a directed edge from one node to another.
|
||||
*
|
||||
* @param {Node|string} from - Source node or its id.
|
||||
* @param {Node|string} to - Target node or its id.
|
||||
*/
|
||||
addEdge(from, to) {
|
||||
const fromId = typeof from === 'string' ? from : from.id;
|
||||
const toId = typeof to === 'string' ? to : to.id;
|
||||
const fromNode = this.nodes.get(fromId);
|
||||
const toNode = this.nodes.get(toId);
|
||||
if (!fromNode) throw new Error(`Source node ${fromId} does not exist`);
|
||||
if (!toNode) throw new Error(`Target node ${toId} does not exist`);
|
||||
fromNode.outgoing.push(toId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a node by its id.
|
||||
*
|
||||
* @param {string} id - Node id.
|
||||
* @returns {Node|null}
|
||||
*/
|
||||
getNode(id) {
|
||||
return this.nodes.get(id) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plain object representation of the graph suitable for
|
||||
* JSON serialization.
|
||||
*
|
||||
* @returns {object}
|
||||
*/
|
||||
toJSON() {
|
||||
const obj = {};
|
||||
for (const [id, node] of this.nodes.entries()) {
|
||||
obj[id] = {
|
||||
id: node.id,
|
||||
type: node.type,
|
||||
props: node.props,
|
||||
outgoing: node.outgoing,
|
||||
};
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Demo: Build a simple graph that includes the required
|
||||
* 'Reflection' and 'Rewrite' nodes.
|
||||
*/
|
||||
function demo() {
|
||||
const g = new Graph();
|
||||
|
||||
// Create nodes
|
||||
const start = g.addNode('Start', { description: 'Entry point' });
|
||||
const reflection = g.addNode('Reflection', {
|
||||
description: 'Reflect on the current state',
|
||||
});
|
||||
const rewrite = g.addNode('Rewrite', {
|
||||
description: 'Rewrite the data for the next step',
|
||||
});
|
||||
const end = g.addNode('End', { description: 'Exit point' });
|
||||
|
||||
// Connect nodes
|
||||
g.addEdge(start, reflection);
|
||||
g.addEdge(reflection, rewrite);
|
||||
g.addEdge(rewrite, end);
|
||||
|
||||
console.log('Graph structure:');
|
||||
console.log(JSON.stringify(g.toJSON(), null, 2));
|
||||
}
|
||||
|
||||
// If this file is executed directly, run the demo.
|
||||
if (require.main === module) {
|
||||
demo();
|
||||
}
|
||||
|
||||
module.exports = { Graph, Node };
|
||||
const Graph = require('./graph');
|
||||
module.exports = { Graph };
|
||||
Reference in New Issue
Block a user